围绕脸部生成矩形不起作用- OpenCV、Kivy

dzhpxtsq  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(123)

我能够使用OpenCV在实时摄像头中检测人脸,但是我不能在他们的脸周围生成矩形。
下面是我目前的代码:

  1. def get_frame(self, dt):
  2. cam = self.root.ids.a_cam
  3. image_object = cam.export_as_image(scale=round((400 / int(cam.height)), 2))
  4. w, h = image_object._texture.size
  5. frame = np.frombuffer(image_object._texture.pixels, 'uint8').reshape(h, w, 4)
  6. gray = cv2.cvtColor(frame, cv2.COLOR_RGBA2GRAY)
  7. faces = self.faceCascade.detectMultiScale(gray,
  8. scaleFactor=1.1,
  9. minNeighbors=5,
  10. minSize=(60, 60),
  11. flags=cv2.CASCADE_SCALE_IMAGE)
  12. if len(faces) != 0:
  13. print("{} Face detected".format(len(faces)))
  14. for (x,y,width,height) in faces:
  15. cv2.rectangle(frame, (x, y), (x + width, y + height),(0,255,0), 2)
  16. faceROI = gray[y:y+height,x:x+width]
  17. else:
  18. print('Face not detected')
  19. self.root.ids.frame_counter.text = f'Faces: {len(faces)}'
  20. self.counter += 1
  21. Clock.schedule_once(self.get_frame, 0.25)

我可以验证程序是否可以检测人脸,因为标签显示了当前检测到的人脸数量,我可以在终端中验证,因为打印语句正在显示。

rqdpfwrv

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:

  1. import numpy as np
  2. import cv2
  3. from kivy.app import App
  4. from kivy.lang import Builder
  5. from kivy.uix.boxlayout import BoxLayout
  6. from kivy.uix.camera import Camera
  7. from kivy.clock import Clock
  8. Builder.load_file('layout.kv')
  9. class AndroidCamera(Camera):
  10. resolution = (640, 480)
  11. cam_ratio = resolution[0] / resolution[1]
  12. class MyLayout(BoxLayout):
  13. pass
  14. class CamApp(App):
  15. FPS = 10
  16. SCALE = 0.2
  17. def build(self):
  18. return MyLayout()
  19. def on_start(self):
  20. Clock.schedule_once(self.get_frame, 3)
  21. def get_frame(self, dt):
  22. cam = self.root.ids.a_cam
  23. image_object = cam.export_as_image(scale=self.SCALE)
  24. w, h = image_object.size
  25. self.frame = np.frombuffer(image_object._texture.pixels, 'uint8').reshape(h, w, 4)
  26. self.face_detection()
  27. Clock.schedule_once(self.get_frame, 1 / self.FPS)
  28. def face_detection(self):
  29. face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
  30. gray = cv2.cvtColor(self.frame, cv2.COLOR_RGBA2GRAY)
  31. self.root.ids.lbl.text = f'{gray.shape}'
  32. faces = face_cascade.detectMultiScale(gray, 1.3, 2)
  33. if len(faces):
  34. face = faces[np.argmax(faces[:, 3])] // self.SCALE
  35. face[1] = self.root.ids.a_cam.height - face[1]
  36. face[3] = face[3] * -1
  37. self.root.ids.a_cam.face = face
  38. else:
  39. self.root.ids.a_cam.face = [0, 0, 0, 0]
  40. if __name__ == '__main__':
  41. CamApp().run()

layout.kv:

  1. <AndroidCamera>
  2. index: 0
  3. allow_stretch: True
  4. play: True
  5. face: [0,0,0,0]
  6. canvas.before:
  7. PushMatrix
  8. Rotate:
  9. angle: -90
  10. origin: self.center
  11. Scale:
  12. x: self.cam_ratio
  13. y: self.cam_ratio
  14. origin: self.center
  15. canvas.after:
  16. PopMatrix
  17. Color:
  18. rgba: 0,1,0,1
  19. Line:
  20. width: 2
  21. points: self.face[0], self.face[1], sum(self.face[::2]), self.face[1]
  22. Line:
  23. width: 2
  24. points: sum(self.face[::2]), self.face[1], sum(self.face[::2]), sum(self.face[1::2])
  25. Line:
  26. width: 2
  27. points: sum(self.face[::2]), sum(self.face[1::2]), self.face[0], sum(self.face[1::2])
  28. Line:
  29. width: 2
  30. points: self.face[0], sum(self.face[1::2]), self.face[0], self.face[1]
  31. <MyLayout>
  32. orientation: 'vertical'
  33. size: root.width, root.height
  34. RelativeLayout:
  35. size_hint_y: 0.8
  36. AndroidCamera:
  37. id: a_cam
  38. Label:
  39. id: lbl
  40. size_hint_y: 0.2
  41. font_size: sp(32)
展开查看全部

相关问题