首页 > 解决方案 > QT Opencv人脸检测在Python中不起作用?

问题描述

我正在开发一个qt项目,在该项目中我创建了一个窗口,该窗口将使用opencv. 我还需要检测实时提要中的人脸,因此我正在haar-cascading为此使用方法。我已经创建了 UI 部分,qt-designer然后将其转换为.py文件。然后我将此文件导入另一个文件app.pyapp.py用于所有逻辑部分。以下是gui.py文件内容:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 400)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(10, 10, 381, 370))
        self.groupBox.setTitle("")
        self.groupBox.setObjectName("groupBox")
        self.pushButton = QtWidgets.QPushButton(self.groupBox)
        self.pushButton.setGeometry(QtCore.QRect(150, 160, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox_2.setGeometry(QtCore.QRect(400, 10, 391, 370))
        self.groupBox_2.setTitle("")
        self.groupBox_2.setObjectName("groupBox_2")
        self.label = QtWidgets.QLabel(self.groupBox_2)
        self.label.setGeometry(QtCore.QRect(10, 10, 371, 360))
        self.label.setText("")
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "BIOT "))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

以下是app.py处理所有逻辑部分的代码:

import sys
import cv2
import os
import imutils
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QImage
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import QTimer
from ui.gui import Ui_MainWindow


curr_path = os.path.dirname(os.path.abspath(__file__))


class ROCKET(QMainWindow, Ui_MainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.cap = cv2.VideoCapture(1)
        self.face_detect = cv2.CascadeClassifier(os.path.join(curr_path, 'models', 'haarcascade_frontalface_default.xml'))

        self.timer = QTimer()
        self.timer.timeout.connect(self.view_cam)
        self.timer.start(20)
        self.ui.pushButton.setText("Stop")

    def __del__(self):
        self.timer.stop()
        self.cap.release()
        self.ui.pushButton.setText("Start")

    def view_cam(self):

        ret, image = self.cap.read()
        image = imutils.resize(image, width=371, height=360)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        height, width, channel = image.shape
        faces = self.face_detect.detectMultiScale(gray, 1.3, 5)
        for (x, y, width, height) in faces:
            print("face detected")
            cv2.rectangle(image, (x, y), (x + width, y + height), (255, 0, 0), 2)
        step = channel * width
        qImg = QImage(image.data, width, height, step, QImage.Format_RGB888)
        self.ui.label.setPixmap(QPixmap.fromImage(qImg))


app = QApplication(sys.argv)
main_window = ROCKET()
main_window.show()
sys.exit(app.exec_())

正如您在上面的代码中看到的,我已经导入了级联分类器,__init__并且我还启动了一个连接到view_cam函数的计时器。在view_cam功能上,我正在读取帧,检测并显示结果。这里的问题是,一旦它开始检测人脸,它应该在人脸上绘制边界框矩形,而不是这样,它看起来像下面这样:

在此处输入图像描述

当没有人脸时,它通常会显示实时帧并且工作正常,但一旦检测到人脸,它就会开始显示锯齿形以上的线条。我不是很专家Qt。任何人都可以在这里指导我我在做什么。请帮忙。谢谢(请忽略停止按钮,它没有做任何事情)

标签: pythonopencvpyqt5face-detection

解决方案


问题是当使用 cv2.rectangle 修改图像时 memoryview (image.data) 会发生变化,如果进行以下比较并抛出AssertionError

last_mv = image.data
for (x, y, width, height) in faces:
    print("face detected")
    cv2.rectangle(image, (x, y), (x + width, y + height), (255, 0, 0), 2)
    current_mv = image.data
    assert last_mv == current_mv

并且更改内存视图也会更改与高度、宽度、通道同步的形状,您可以通过以下方式检查:

height, width, channel = image.shape
print("before", height, width, channel)

faces = self.face_detect.detectMultiScale(gray, 1.3, 5)

for (x, y, width, height) in faces:
    print("face detected")
    cv2.rectangle(image, (x, y), (x + width, y + height), (255, 0, 0), 2)

step = channel * width
print("after", height, width, channel)

输出:

before 208 371 3
face detected
after 112 112 3
before 208 371 3
face detected
after 110 110 3
before 208 371 3
face detected
after 108 108 3

如您所见, cv2.rectangle 前后的形状不同。

解决方案是在进行所有更改后计算几何形状。

def view_cam(self):
    ret, image = self.cap.read()
    image = imutils.resize(image, width=371, height=360)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    faces = self.face_detect.detectMultiScale(gray, 1.3, 5)

    for (x, y, width, height) in faces:
        cv2.rectangle(image, (x, y), (x + width, y + height), (255, 0, 0), 2)

    height, width, channel = image.shape
    step = channel * width
    qImg = QImage(image.data, width, height, step, QImage.Format_RGB888)
    self.ui.label.setPixmap(QPixmap.fromImage(qImg))

推荐阅读