资源简介

Pythone+pyq5+opencv 多线程模式调用摄像头。这样你在识别人脸的时候,可以通过线程输出结果,这时候摄像头保持流畅视频。

资源截图

代码片段和文件信息

“““
In this example we demonstrate how to create simple camera viewer using Opencv3 and PyQt5

Author: Berrouba.A
Last edited: 21 Feb 2018
“““

# import system module
import sys
import time
# import some PyQt5 modules
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QWidget
from PyQt5.QtGui import QImage
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import QTimer
from PyQt5.QtCore import *
# import Opencv module
import cv2

from ui_main_window import *


class Mythread(QThread):
    # 定义信号定义参数为str类型
    breakSignal = pyqtSignal(QPixmap)

    def __init__(self parent=None):
        super().__init__(parent)
        # 下面的初始化方法都可以,有的python版本不支持
        super(Mythread self).__init__()
        self.cap = cv2.VideoCapture(0)

    def run(self):

        while True:
            
            ret image = self.cap.read()
            # convert image to RGB format
            image = cv2.cvtColor(image cv2.COLOR_BGR2RGB)
            # get image infos
            height width channel = image.shape
            step = channel * width
            # create QImage from image
            qImg = QImage(image.data width height step QImage.Format_RGB888)
            # show image in img_label
            self.breakSignal.emit(QPixmap.fromImage(qImg))

        #self.ui.image_label.setPixmap(QPixmap.fromImage(qImg))

        ‘‘‘
        for i in range(2000000):
            # 发出信号
            print(12)
            a=[ii+1]
            self.breakSignal.emit(str(i))
            # 让程序休眠
            time.sleep(0.5)
        ‘‘‘


class Mythread2(QThread):
    # 定义信号定义参数为str类型
    breakSignal2 = pyqtSignal(str)

    def __init__(self parent=None):
        super().__init__(parent)
        # 下面的初始化方法都可以,有的python版本不支持
        super(Mythread2 self).__init__()


    def run(self):
        for i in range(2000000):
            # 发出信号
            print(12)
            a=[ii+1]
            self.breakSignal2.emit(str(i))
            # 让程序休眠
            time.sleep(0.5)
  
class MainWindow(QWidget):
    # class constructor

    def __init__(self):
        # call QWidget constructor
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)

        #按钮启动现成
        self.ui.control_bt.clicked.connect(self.controlTimer)

        # 创建线程
        self.thread = Mythread()
        # 连接信号
        self.thread.breakSignal.connect(self.chuli)
        # 开始线程



          # 创建线程
        self.thread2 = Mythread2()
        self.thread2.breakSignal2.connect(self.chuli2)
        self.thread2.start()

    def chuli(selfqImg):
        self.ui.image_label.setPixmap(qImg)
   
    def chuli2(selfmsg):
        self.ui.control_bt.setText(msg)

    # start/stop timer
    def controlTimer(self):
        self.thread.start()
    




if __name__ == ‘__main__‘:
    app = QApplication(sys.argv)
    # create and show mainWindow
    mainWindow = MainWindow()
    mainWindow.show() 

    sys.exit(app.exec_())








 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件     771499  2018-10-19 11:28  camView\build\main_window\base_library.zip

     文件       1036  2018-10-19 11:28  camView\build\main_window\main_window.exe.manifest

     文件       3159  2018-10-22 18:19  camView\main_window.py

     文件        981  2018-10-19 11:28  camView\main_window.spec

     文件        325  2018-02-22 13:17  camView\README.md

     文件       1344  2018-02-22 13:17  camView\ui_main_window.py

     文件        882  2018-02-22 13:17  camView\ui_main_window.ui

     文件       1365  2018-02-22 13:17  camView\__pycache__\ui_main_window.cpython-35.pyc

     文件       1217  2018-10-18 10:54  camView\__pycache__\ui_main_window.cpython-36.pyc

     目录          0  2018-10-19 11:28  camView\build\main_window

     目录          0  2018-10-19 11:26  camView\build

     目录          0  2018-10-19 11:26  camView\dist

     目录          0  2018-10-18 10:54  camView\__pycache__

     目录          0  2018-10-19 11:26  camView

----------- ---------  ---------- -----  ----

               781808                    14


评论

共有 条评论