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中的多条件排序实现

浅析Python中的多条件排序实现

多条件排序及itemgetter的应用 曾经客户端的同事用as写一大堆代码来排序,在得知Python排序往往只需要一行,惊讶无比,遂对python产生浓厚的兴趣。 之前在做足球的积分榜的...

Python实现的一个找零钱的小程序代码分享

Python写的一个按面值找零钱的程序,按照我们正常的思维逻辑从大面值到小面值的找零方法,人民币面值有100元,50元,20元,10元,5元,1元,5角,1角,而程序也相应的设置了这些面...

python使用range函数计算一组数和的方法

本文实例讲述了python使用range函数计算一组数和的方法。分享给大家供大家参考。具体如下: sum = 0 numbers = range(1,10) for i in num...

详解Python中的内建函数,可迭代对象,迭代器

详解Python中的内建函数,可迭代对象,迭代器

Python中的内建函数和可迭代对象,迭代器 求值标识 id() #标识id 返回对象的唯一标识,CPython返回内存地址 hash() #哈希, 返回对象的哈希值 le...

python zip文件 压缩

从简单的角度来看的话,zip格式会是个不错的选择,而且python对zip格式的支持够简单,够好用。1)简单应用 如果你仅仅是希望用python来做压缩和解压缩,那么就不用去翻文档了,这...