我能够使用OpenCV在实时摄像头中检测人脸,但是我不能在他们的脸周围生成矩形。
下面是我目前的代码:
def get_frame(self, dt):
cam = self.root.ids.a_cam
image_object = cam.export_as_image(scale=round((400 / int(cam.height)), 2))
w, h = image_object._texture.size
frame = np.frombuffer(image_object._texture.pixels, 'uint8').reshape(h, w, 4)
gray = cv2.cvtColor(frame, cv2.COLOR_RGBA2GRAY)
faces = self.faceCascade.detectMultiScale(gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(60, 60),
flags=cv2.CASCADE_SCALE_IMAGE)
if len(faces) != 0:
print("{} Face detected".format(len(faces)))
for (x,y,width,height) in faces:
cv2.rectangle(frame, (x, y), (x + width, y + height),(0,255,0), 2)
faceROI = gray[y:y+height,x:x+width]
else:
print('Face not detected')
self.root.ids.frame_counter.text = f'Faces: {len(faces)}'
self.counter += 1
Clock.schedule_once(self.get_frame, 0.25)
我可以验证程序是否可以检测人脸,因为标签显示了当前检测到的人脸数量,我可以在终端中验证,因为打印语句正在显示。
1条答案
按热度按时间rqdpfwrv1#
If you want to stick with the second solution, the only way to draw rectangle around the face is to draw on canvas.
main.py:
layout.kv: