【发布时间】:2022-10-14 08:10:46
【问题描述】:
我能够使用 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)
我可以验证程序是否可以检测到人脸,因为标签显示当前检测到了多少人脸,我可以在终端中验证它,因为打印语句正在显示。
【问题讨论】:
-
此代码是来自stackoverflow.com/q/67061962#67061962 的“解决方案2”它仅直接显示来自kivy.uix.camera 相机类的相机图像并以4 次/秒的速度导出帧。这就是为什么你不能在上面画任何东西。要使用 cv2.rectangle() 尝试“解决方案 1”。
-
@NorbertTiborcz 我明白了,我想我会坚持使用第二种解决方案,因为第一种解决方案不起作用。该应用程序在我的 Android 设备上打开时会崩溃。无论如何,你知道代码的哪一部分在界面中显示了摄像头吗?链接中“解决方案2”代码的原始作者没有解释。
标签: python opencv image-processing kivy