PyQt5 在Qlabel控件中显示检测程序传回的图像帧


前言

在准备智慧物流虚拟赛时遇到一个问题:目标检测程序与pyqt5程序是分开的,qt界面只需要对检测程序传回的已处理的图像帧做展示。问题来了,如果继续使用pyqt5内部的QTimer计时器做图像刷新会出现只显示第一帧的情况,即不能从qt内部做图像刷新处理,目前基于该问题提出一种解决办法


提示:以下是本篇文章正文内容,下面案例可供参考

一、准备工作

1.检测程序

为测试方便,用Haar级联器临时写了个人脸检测程序,过程不再赘述。开启相机后进行人脸检测,返回值为经处理的图像帧

def detect(frame):
    face_cascade = cv2.CascadeClassifier(r'haarcascade_frontalface_default.xml')
    smile_cascade = cv2.CascadeClassifier(r'haarcascade_smile.xml')
    faces = face_cascade.detectMultiScale(frame, 1.3, 2)
    img = frame
    for (x, y, w, h) in faces:
        img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
        face_area = img[y:y + h, x:x + w]
        smiles = smile_cascade.detectMultiScale(face_area, scaleFactor=1.16, minNeighbors=65, minSize=(25, 25),
                                                flags=cv2.CASCADE_SCALE_IMAGE)
        if len(smiles) > 0:
            for (x1, y1, w1, h1) in smiles:
                cv2.rectangle(face_area, (x1, y1), (x1 + w1, y1 + h1), (0, 0, 255), 2)
                cv2.putText(img, 'smile!', (x, y - 7), 3, 1.2, (0, 0, 255), 2, cv2.LINE_AA)
    return img

2.PyQt界面

简单做了一个显示界面:(可直接复制使用)

from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1092, 925)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setEnabled(False)
        self.label.setGeometry(QtCore.QRect(40, 20, 1000, 800))
        self.label.setStyleSheet("background-color: rgb(0, 0, 0);")
        self.label.setText("")
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1092, 22))
        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", "MainWindow"))
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import cv2

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)  # 设置UI界面
        self.setWindowTitle("camera_capture")
    def show_pic(self, img):  # 显示图像帧
        cur_frame = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        height, width = cur_frame.shape[:2]
        pixmap = QImage(cur_frame, width, height, QImage.Format_RGB888)  #label控件显示格式
        pixmap = QPixmap.fromImage(pixmap)
        ratio = max(width / self.label.width(), height / self.label.height())  #自适应显示
        pixmap.setDevicePixelRatio(ratio)
        self.label.setPixmap(pixmap)

二、解决图像帧刷新问题

在检测程序的调用中,用waitKey设置图像刷新

if __name__ == "__main__":
    cap = cv2.VideoCapture(0)
    app = QApplication(sys.argv)
    main = main_camera.MainWindow() # MainWindow类是放在main_camera文件中的
    while True:
        try:
            ret, img = cap.read()
            frame = detect(img)
            main.show_pic(frame)

            main.show()
            cv2.waitKey(0)
        except:
            print("检测中止")
            break
    cap.release()
    cv2.destroyAllWindows()

总结

当时认为QTimer计时器设置事件响应,时间刷新和waitKey大致原理相同(个人理解),所以才试用waitKey来对图像刷新,结果确实能正常工作,但存在的问题是没有找到自然关闭进程的办法,希望各位大佬能提供解决的办法。在这里只是提供一种解决显示图像帧刷新的办法,有更好的办法也希望各位能不吝指出

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值