2018-10-16 pyqt5+opencv读取摄像头数据并显示

准备写个pyqt5显示摄像头,百度一看,居然全是要csdn下载的!!!!于是我很伤心,人与人之间基本的信任都没有了,随手写了一个,贴上来。




#coding=utf-8

import cv2

import numpy as np

import time

import sys

from PyQt5.QtWidgets import *

from PyQt5.QtCore import *

from PyQt5.QtGui import *

class Camera:

    def __init__(self, width=320, height=240):

        self.cap = cv2.VideoCapture(0)

        self.image= QImage()

        self.width = width

        self.height = height

        ret, frame = self.cap.read()

        frame = cv2.resize(frame, (self.width, self.height))

        self.h, self.w, self.bytesPerComponent = frame.shape

        self.bytesPerLine = self.bytesPerComponent * self.w

    def ReturnOneQPixmap(self):

        # get a frame

        ret, frame = self.cap.read()

        frame = cv2.resize(frame, (self.width, self.height))

        if ret:

            if frame.ndim == 3:

                rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

            elif frame.ndim == 2:

                rgb = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)

        self.image = QImage(rgb.data, self.w, self.h, self.bytesPerLine, QImage.Format_RGB888)

        return QPixmap.fromImage(self.image)

    def DestroyCamera(self):

        cap.release()

        cv2.destroyAllWindows()

class VideoBox(QWidget):

    def __init__(self):

        QWidget.__init__(self)

        self.setWindowFlags(Qt.CustomizeWindowHint)

        self.camera = Camera(320, 320)

        self.pictureLabel = QLabel()

        self.pictureLabel.setFixedSize(320, 240)

        self.pictureLabel.setObjectName("Camera")

        self.combo = QComboBox()

        self.combo.addItem('Video')

        self.combo.addItem('Stop')

        self.combo.activated[str].connect(self.onActivatd)

        control_box = QHBoxLayout()

        control_box.setContentsMargins(0, 0, 0, 0)

        control_box.addWidget(self.combo)

        layout = QVBoxLayout()

        layout.addWidget(self.pictureLabel)

        layout.addLayout(control_box)

        self.setLayout(layout)

        self.video_timer = VideoTimer()

        self.video_timer.timeSignal.signal[str].connect(self.showframe)

    def onActivatd(self, text):

        if text == 'Video':

            self.video_timer.start()

            print('Video')      

        if text == 'Stop':

            self.video_timer.stop()

            quit()

    def showframe(self):

        self.pictureLabel.setPixmap(self.camera.ReturnOneQPixmap())

class Communicate(QObject):

    signal = pyqtSignal(str)

class VideoTimer(QThread):

    def __init__(self, frequent=20):

        QThread.__init__(self)

        self.stopped = False

        self.frequent = frequent

        self.timeSignal = Communicate()

        self.mutex = QMutex()

    def run(self):

        with QMutexLocker(self.mutex):

            self.stopped = False

        while True:

            if self.stopped:

                return

            self.timeSignal.signal.emit("1")

            time.sleep(1 / self.frequent)

    def stop(self):

        with QMutexLocker(self.mutex):

            self.stopped = True

    def is_stopped(self):

        with QMutexLocker(self.mutex):

            return self.stopped

    def set_fps(self, fps):

        self.frequent = fps

if __name__ == "__main__":

    app = QApplication(sys.argv)

    box = VideoBox()

    # box.showFullScreen()

    box.show()

    sys.exit(app.exec_())

你可能感兴趣的:(2018-10-16 pyqt5+opencv读取摄像头数据并显示)