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



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

 


可以使用OpenCV库获取摄像头的图像,然后使用PyQt5库将图像显示在GUI界面上。下面是一个简单的示例代码: ```python import cv2 import numpy as np from PyQt5.QtCore import QTimer from PyQt5.QtGui import QImage, QPixmap from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel class WebcamWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('Webcam Window') self.setGeometry(100, 100, 640, 480) self.label = QLabel(self) self.label.setGeometry(0, 0, 640, 480) self.timer = QTimer(self) self.timer.timeout.connect(self.update_frame) self.timer.start(30) self.cap = cv2.VideoCapture(0) def update_frame(self): ret, frame = self.cap.read() if ret: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) h, w, ch = frame.shape bytes_per_line = ch * w qimg = QImage(frame.data, w, h, bytes_per_line, QImage.Format_RGB888) pixmap = QPixmap.fromImage(qimg) self.label.setPixmap(pixmap) if __name__ == '__main__': app = QApplication([]) window = WebcamWindow() window.show() app.exec_() ``` 在这个例子中,我们创建了一个名为WebcamWindow的类,继承自QMainWindow。我们在构造函数中创建了一个QLabel控件,用于显示摄像头捕获的图像。我们还使用了QTimer定时器来定期刷新图像,以便我们可以看到实时的视频流。在update_frame()方法中,我们使用OpenCV库从摄像头中读取图像,并将其转换为QImage格式,然后将其显示在GUI界面上。最后,我们使用QApplication来启动应用程序。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值