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

作者:ShellCollector 时间:2023-09-28 10:33:24 

如下所示:


#! /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

来源:https://blog.csdn.net/jacke121/article/details/78765178

标签:pyqt5,cv2,图片,摄像头
0
投稿

猜你喜欢

  • Matplotlib使用字符串代替变量绘制散点图的方法

    2021-04-18 06:10:11
  • python逐行读取文件内容的三种方法

    2023-01-05 14:07:30
  • python中range()与xrange()用法分析

    2021-03-23 00:31:30
  • Python中线程threading.Thread的使用详解

    2023-07-22 13:25:48
  • python用tkinter实现一个gui的翻译工具

    2022-12-13 11:30:48
  • FCKeditor.Net_2.2安全修正版

    2024-03-08 20:09:48
  • 使用Pytorch来拟合函数方式

    2021-06-22 18:10:45
  • python 中Arduino串口传输数据到电脑并保存至excel表格

    2022-05-17 05:41:11
  • Centos 7下使用RPM包安装MySQL 5.7.9教程

    2024-01-19 20:44:49
  • mysql 8.0.17 安装配置图文教程

    2024-01-14 11:44:16
  • Python通过30秒就能学会的漂亮短程序代码(过程全解)

    2022-08-22 13:54:50
  • Python实现Wordcloud生成词云图的示例

    2021-09-06 05:29:15
  • 分享几个字体设计

    2007-10-10 13:53:00
  • python sklearn库实现简单逻辑回归的实例代码

    2022-06-19 18:43:29
  • Python列表list内建函数用法实例分析【insert、remove、index、pop等】

    2022-01-13 10:18:24
  • Python中的进程操作模块(multiprocess.process)

    2022-09-17 23:10:32
  • 使用 Python 处理3万多条数据只要几秒钟

    2023-08-03 15:21:06
  • python中的import、from import及import as的区别解析

    2022-10-07 15:56:09
  • Django扫码抽奖平台的配置过程详解

    2021-11-25 22:54:09
  • CSS3属性box-shadow图层阴影效果使用教程

    2010-05-16 14:54:00
  • asp之家 网络编程 m.aspxhome.com