pyqt5 使用cv2 显示图片,摄像头的实例
如下所示:
#!/usr/bin/python3 #coding=utf-8 #fromPyQt5importQtGui,QtCore,Qt importsys fromPyQt5.QtCoreimportQt,pyqtSignal,QSize,QRect,QMetaObject,QCoreApplication,pyqtSlot,QPropertyAnimation,QThread fromPyQt5.QtGuiimportQIcon,QFont,QPixmap,QPainter,QImage fromPyQt5.QtWidgetsimportQMainWindow,QApplication importcv2 fromgevent.libev.corecextimportSIGNAL,time fromqtpyimportQtCore classmycsms(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) #读摄像头 defshowCamer(self): ifself.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_()) #线程类: classTimer(QtCore.QThread): def__init__(self,signal="updateTime()",parent=None): super(Timer,self).__init__(parent) self.stoped=False self.signal=signal self.mutex=QtCore.QMutex() defrun(self): withQtCore.QMutexLocker(self.mutex): self.stoped=False whileTrue: ifself.stoped: return self.emit(QtCore.SIGNAL(self.signal)) #40毫秒发送一次信号 time.sleep(0.04) defstop(self): withQtCore.QMutexLocker(self.mutex): self.stoped=True defisStoped(self): withQtCore.QMutexLocker(self.mutex): returnself.stoped
以上这篇pyqt5使用cv2显示图片,摄像头的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。