gpt4 book ai didi

python - QT Opencv 面部检测在 Python 中不起作用?

转载 作者:行者123 更新时间:2023-12-02 16:01:54 27 4
gpt4 key购买 nike

我正在开发一个 qt 项目,我在其中创建了一个窗口,该窗口将使用 opencv 显示来自 usb 网络摄像头的实时帧。我还需要检测实时提要中的人脸,因此我为此使用了 haar-cascading 方法。我在 qt-designer 中创建了 UI 部分,然后将其转换为 .py 文件。然后,我将此文件导入到另一个 app.py 中,并将 app.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 函数中,我正在读取帧、检测并显示结果。这里的问题是,一旦它开始检测人脸,它就应该在脸上绘制边界框矩形,而不是那样,它看起来像下面这样:

enter image description here

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

最佳答案

问题是当使用 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

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

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)

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

关于python - QT Opencv 面部检测在 Python 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62315735/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com