【问题标题】:Resuming a thread which was created using opencv , in pyqt5在 pyqt5 中恢复使用 opencv 创建的线程
【发布时间】:2022-06-16 04:02:27
【问题描述】:

我正在尝试设计一个与我的计算机视觉项目相关的 gui。在那,我想停止网络摄像头的视频,我想通过按一个按钮来恢复它。我设法停止了提要,但我无法恢复它。相机已打开,但无法正常工作。这是程序的代码。

from PyQt5 import uic
from PyQt5 import QtCore, QtWidgets, QtGui
import cv2
import sys

class opencv_feed(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.ui = uic.loadUi('../designs/design5_flexible_opencv_window2.ui', self)    #change this whenever u want... keep the ui file with you
        self.resize(900,600)

        self.worker1 = worker1()                                #creating an instance
        self.worker1.start()
        self.worker1.ImgUpdate.connect(self.ImageUpdateSlot)
        self.but_stop.clicked.connect(self.cancel_feed)
        self.but_resume.clicked.connect(self.resume_feed)
        
   
    def ImageUpdateSlot(self, Image):
        self.label.setPixmap(QtGui.QPixmap.fromImage(Image))
        
    def cancel_feed(self):
        self.worker1.stop()
    
    def resume_feed(self):
        self.__init__()
        #self.worker1.ImgUpdate.connect(self.ImageUpdateSlot)

class worker1(QtCore.QThread):
    ImgUpdate = QtCore.pyqtSignal(QtGui.QImage)
    
    @QtCore.pyqtSlot()
    def run(self):  #put self in every variable to stop crashing the gui, when we interact with gui
        self.ThreadActive = True
        self.feed = cv2.VideoCapture(0)
        while self.ThreadActive:
            self.ret, self.frm = self.feed.read()
            if self.ret:
                self.img = cv2.cvtColor(self.frm, cv2.COLOR_BGR2RGB)
                #print(img1.shape)
                self.img = cv2.flip(self.img,1)
                self.qtformat_conv_img = QtGui.QImage(self.img.data, self.img.shape[1], self.img.shape[0], QtGui.QImage.Format_RGB888)
                #print(self.img.shape)
                self.pic = self.qtformat_conv_img.scaled(self.img.shape[1],self.img.shape[0],QtCore.Qt.KeepAspectRatio)    #keep this as an attribute, else when resizing the app stops                                              
                self.ImgUpdate.emit(self.pic)
    def stop(self):
        self.ThreadActive = False
        self.feed.release()
        self.quit()
        #os._exit(0)  
        
if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    wind = opencv_feed()
    wind.show()
    sys.exit(app.exec_())

谁能解释我做错了什么。

链接到 UI 文件.. https://drive.google.com/file/d/1UP8RjQML1GzFA75eGURgWt4Y0o_Ip3sU/view?usp=sharing

【问题讨论】:

  • 您当然应该回想__init__,重新连接信号毫无意义。相反,您是否尝试过简单的self.worker1.start()
  • 是的,我也试过了,但同样的结果,相机打开但视频无法继续。

标签: python multithreading opencv pyqt5


【解决方案1】:

您只能启动一次线程。完成后,您需要创建另一个线程对象以实际运行。我会在 self.ThreadActive 之后添加另一个标志,称为“暂停”以保持线程处于活动状态,而无需执行任何操作。

@QtCore.pyqtSlot()
def run(self):  #put self in every variable to stop crashing the gui, when we interact with gui
    self.ThreadActive = True
    self.feed = cv2.VideoCapture(0)
    while self.ThreadActive:
        if not self.paused:
            self.ret, self.frm = self.feed.read()
            if self.ret:
                self.img = cv2.cvtColor(self.frm, cv2.COLOR_BGR2RGB)
                #print(img1.shape)
                self.img = cv2.flip(self.img,1)
                self.qtformat_conv_img = QtGui.QImage(self.img.data, 
                                                      self.img.shape[1], 
                                                      self.img.shape[0], 
                                                      QtGui.QImage.Format_RGB888)
                #print(self.img.shape)
                self.pic = self.qtformat_conv_img.scaled(self.img.shape[1],self.img.shape[0],QtCore.Qt.KeepAspectRatio)    #keep this as an attribute, else when resizing the app stops                                              
                self.ImgUpdate.emit(self.pic)

这样,当您想暂停线程时,您可以使用该标志暂停和取消暂停

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-18
    • 1970-01-01
    • 2014-05-21
    • 2019-12-03
    • 2020-07-16
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多