首页 > 解决方案 > 使用 Qt Gui 执行慢速实时视频源的 Opencv 人脸检测

问题描述

我有一个项目需要设计一个guiin qt. 此设计包含一个小部件,该小部件live video feed将使用opencv. 该项目将检测faces并识别它们,这意味着每帧都会发生大量处理。

为此,我所做的是创建了一个线程来初始化相机并使用 opencv 从中获取帧。然后它将所有帧放入队列中,然后由一个update_frame基本上在 qt 小部件上显示帧的函数读取该队列。这工作正常,没有延迟。

update_frame函数内部,我添加了face detection它执行速度非常慢的原因。所以我创建了另一个线程start_inferencing,它基本上从检测到人脸后读取帧queue,它将帧再次放入另一个队列q2中,然后由它读取update_frame并显示,但它的响应仍然非常慢。下面是代码:

q = queue.Queue()
q2 = queue.Queue()

def grab(cam, qu, width, height):
    global running
    capture = cv2.VideoCapture(cam)
    capture.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    capture.set(cv2.CAP_PROP_FRAME_HEIGHT, height)

    while running:
        frame = {}
        capture.grab()
        ret_val, img = capture.retrieve(0)
        frame["img"] = img

        if qu.qsize() < 100:
            qu.put(frame)
        else:
            print(qu.qsize())

class Logic(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        self.setupUi(self)

        set_initial_alert_temp()

        self.window_width = self.ImgWidget.frameSize().width()
        self.window_height = self.ImgWidget.frameSize().height()
        self.ImgWidget = OwnImageWidget(self.ImgWidget)

        self.timer = QtCore.QTimer(self)
        self.timer.timeout.connect(self.update_frame)
        self.timer.start(1)

        self.outside_temp_text_box.setText(str(curr_temp_cel))

    def update_frame(self):
        if not q2.empty():
            frame1 = q2.get()
            img = frame1["img"]

            img_height, img_width, img_colors = img.shape
            scale_w = float(self.window_width) / float(img_width)
            scale_h = float(self.window_height) / float(img_height)
            scale = min([scale_w, scale_h])

            if scale == 0:
                scale = 1

            img = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            height, width, bpc = img.shape
            bpl = bpc * width
            image = QtGui.QImage(img.data, width, height, bpl, QtGui.QImage.Format_RGB888)
            self.ImgWidget.setImage(image)

def start_inferencing():
    while True:
        if not q.empty():
            frame = q.get()
            img = frame["img"]
            face_bbox = face.detect_face(img)
            if face_bbox is not None:
                (f_startX, f_startY, f_endX, f_endY) = face_bbox.astype("int")
                f_startX = f_startX + 10
                f_startY = f_startY + 10
                f_endX = f_endX - 10
                f_endY = f_endY - 10
                cv2.rectangle(img, (f_startX, f_startY), (f_endX, f_endY), (0, 255, 0), 2)

                frame1 = {"img": img}

                if q2.qsize() < 100:
                    q2.put(frame1)
                else:
                    print(q2.qsize())

def main():

    capture_thread = threading.Thread(target=grab, args=(0, q, 640, 480))
    capture_thread.start()

    infer_thread = threading.Thread(target=start_inferencing)
    infer_thread.start()

    app = QtWidgets.QApplication(sys.argv)
    w = Logic(None)
    w.setWindowTitle('Test')
    w.show()
    app.exec_()


main()

以下是代码中发生的事情的摘要:

camera -> frame -> queue.put                     # (reading frame from camera and putting it in queue)
queue.get -> frame -> detect face -> queue2.put  # (getting frame from queue, detecting face in it and putting the updated frames in queue2)
queue2.get -> frame -> display it on qt widget   # (getting frame from queue2 and display it on qt widget)

直播视频速度慢的主要原因是因为在grab函数中读取的帧无法更快地处理,因此queue尺寸不断增加,因此整体变得非常慢。我可以使用什么好的方法来检测面部并立即显示它。请帮忙。谢谢

标签: pythonqtopencvface-detection

解决方案


队列会累积线程无法处理的帧。所以,根本没有机会处理它们。这就是为什么队列在这里没用的原因。这里的工作时钟由到达的帧定义,每个帧都会产生事件,它可以在帧处理完成后在它自己的线程中工作(比如说在处理线程中),处理线程产生另一个事件并在另一个线程中处理,比如说在 GUI 线程中并将结果显示给用户。

如果您强制需要一些缓冲区,请检查其长度有限的环形缓冲区。


推荐阅读