pyqt5 使用cv2 显示图片,摄像头的实例

yipeiwu_com5年前Python基础

如下所示:

#! /usr/bin/python3
# coding = utf-8
# from PyQt5 import QtGui,QtCore,Qt
import sys
from PyQt5.QtCore import Qt,pyqtSignal,QSize,QRect,QMetaObject, QCoreApplication, pyqtSlot,QPropertyAnimation,QThread
from PyQt5.QtGui import QIcon, QFont, QPixmap, QPainter, QImage
from PyQt5.QtWidgets import QMainWindow, QApplication

import cv2
from gevent.libev.corecext import SIGNAL, time
from qtpy importQtCore


class mycsms(QMainWindow):
    def __init__(self):
        super(mycsms, self).__init__()
        self.setupUi(self)
        self.image= QImage()
        self.device= cv2.VideoCapture(0)
        self.playTimer= Timer("updatePlay()")
        self.connect(self.playTimer, SIGNAL("updatePlay()"), self.showCamer)

    # 读摄像头
    def showCamer(self):
        if self.device.isOpened():
            ret, frame= self.device.read()
        else:
            ret = False
        # 读写磁盘方式
        # cv2.imwrite("2.png",frame)
        #self.image.load("2.png")

        height, width, bytesPerComponent= frame.shape
        bytesPerLine = bytesPerComponent* width
        # 变换彩色空间顺序
        cv2.cvtColor(frame, cv2.COLOR_BGR2RGB,frame)
        # 转为QImage对象
        self.image= QImage(frame.data, width, height, bytesPerLine, QImage.Format_RGB888)
        self.view.setPixmap(QPixmap.fromImage(self.image))

if __name__ == "__main__":
    app = QApplication(sys.argv)
    myshow = mycsms()
    myshow.playTimer.start()
    myshow.show()
    sys.exit(app.exec_())

# 线程类:
class Timer(QtCore.QThread):

    def __init__(self, signal="updateTime()", parent=None):
        super(Timer, self).__init__(parent)
        self.stoped= False
        self.signal= signal
        self.mutex= QtCore.QMutex()

    def run(self):
        with QtCore.QMutexLocker(self.mutex):
            self.stoped= False
        while True:
            if self.stoped:
                return
            self.emit(QtCore.SIGNAL(self.signal))
            #40毫秒发送一次信号
            time.sleep(0.04)

    def stop(self):
        with QtCore.QMutexLocker(self.mutex):
            self.stoped= True

    def isStoped(self):
        with QtCore.QMutexLocker(self.mutex):
            return self.stoped

以上这篇pyqt5 使用cv2 显示图片,摄像头的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python实现两个list求交集,并集,差集的方法示例

本文实例讲述了Python实现两个list求交集,并集,差集的方法。分享给大家供大家参考,具体如下: 在python中,数组可以用list来表示。如果有两个数组,分别要求交集,并集与差集...

Python实现树的先序、中序、后序排序算法示例

本文实例讲述了Python实现树的先序、中序、后序排序算法。分享给大家供大家参考,具体如下: #encoding=utf-8 class Tree(): def __init__...

Python数据类型之Number数字操作实例详解

本文实例讲述了Python数据类型之Number数字操作。分享给大家供大家参考,具体如下: 一、Number(数字) 数据类型 为什么会有不同的数据类型? 计算机是用来做数学计算的机器,...

python并发编程多进程 模拟抢票实现过程

 抢票是并发执行 多个进程可以访问同一个文件 多个进程共享同一文件,我们可以把文件当数据库,用多个进程模拟多个人执行抢票任务 db.txt {"count": 1} 并发...

python+opencv实现的简单人脸识别代码示例

python+opencv实现的简单人脸识别代码示例

# 源码如下: #!/usr/bin/env python #coding=utf-8 import os from PIL import Image, ImageDraw imp...