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

时间:2021-05-22

如下所示:

#! /usr/bin/python3# coding = utf-8# from PyQt5 import QtGui,QtCore,Qtimport sysfrom PyQt5.QtCore import Qt,pyqtSignal,QSize,QRect,QMetaObject, QCoreApplication, pyqtSlot,QPropertyAnimation,QThreadfrom PyQt5.QtGui import QIcon, QFont, QPixmap, QPainter, QImagefrom PyQt5.QtWidgets import QMainWindow, QApplicationimport cv2from gevent.libev.corecext import SIGNAL, timefrom qtpy importQtCoreclass 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 显示图片,摄像头的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章