【问题标题】:Generating the rectangle around the face isn't working - OpenCV, Kivy在脸部周围生成矩形不起作用 - OpenCV,Kivy
【发布时间】: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


【解决方案1】:

如果您想坚持使用第二种解决方案,则在脸部周围绘制矩形的唯一方法是在画布上绘制。

主要.py:

import numpy as np
import cv2
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.camera import Camera
from kivy.clock import Clock


Builder.load_file('layout.kv')


class AndroidCamera(Camera):
    resolution = (640, 480)
    cam_ratio = resolution[0] / resolution[1]


class MyLayout(BoxLayout):
    pass


class CamApp(App):
    FPS = 10
    SCALE = 0.2

    def build(self):
        return MyLayout()

    def on_start(self):
        Clock.schedule_once(self.get_frame, 3)

    def get_frame(self, dt):
        cam = self.root.ids.a_cam
        image_object = cam.export_as_image(scale=self.SCALE)
        w, h = image_object.size
        self.frame = np.frombuffer(image_object._texture.pixels, 'uint8').reshape(h, w, 4)
        self.face_detection()
        Clock.schedule_once(self.get_frame, 1 / self.FPS)

    def face_detection(self):
        face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
        gray = cv2.cvtColor(self.frame, cv2.COLOR_RGBA2GRAY)
        self.root.ids.lbl.text = f'{gray.shape}'
        faces = face_cascade.detectMultiScale(gray, 1.3, 2)
        if len(faces):
            face = faces[np.argmax(faces[:, 3])] // self.SCALE
            face[1] = self.root.ids.a_cam.height - face[1]
            face[3] = face[3] * -1
            self.root.ids.a_cam.face = face
        else:
            self.root.ids.a_cam.face = [0, 0, 0, 0]


if __name__ == '__main__':
    CamApp().run()

布局.kv:

<AndroidCamera>
    index: 0
    allow_stretch: True
    play: True
    face: [0,0,0,0]

    canvas.before:
        PushMatrix
        Rotate:
            angle: -90
            origin: self.center
        Scale:
            x: self.cam_ratio
            y: self.cam_ratio
            origin: self.center
    canvas.after:
        PopMatrix
        Color:
            rgba: 0,1,0,1
        Line:
            width: 2
            points: self.face[0], self.face[1], sum(self.face[::2]), self.face[1]
        Line:
            width: 2
            points: sum(self.face[::2]), self.face[1], sum(self.face[::2]), sum(self.face[1::2])
        Line:
            width: 2
            points: sum(self.face[::2]), sum(self.face[1::2]), self.face[0], sum(self.face[1::2])
        Line:
            width: 2
            points: self.face[0], sum(self.face[1::2]), self.face[0], self.face[1]


<MyLayout>
    orientation: 'vertical'
    size: root.width, root.height

    RelativeLayout:
        size_hint_y: 0.8
        AndroidCamera:
            id: a_cam

    Label:
        id: lbl
        size_hint_y: 0.2
        font_size: sp(32)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-12
    • 2022-01-09
    • 2018-02-13
    • 1970-01-01
    • 2019-12-06
    • 2019-08-17
    • 2013-03-25
    • 2015-07-17
    相关资源
    最近更新 更多