label读取摄像头 pyqt5_pyqt5如何与OpenCV调用摄像头结合?

本文展示了如何使用PyQt5和OpenCV在Python中创建一个简单的应用,实现实时读取摄像头画面。通过创建Camera类进行视频捕获,使用QImage和QPixmap将帧转换为Qt可显示的格式,并在VideoBox类中展示。当从下拉框选择'Video'时开始捕获,选择'Stop'则停止并退出应用。
摘要由CSDN通过智能技术生成

不废话,上代码

#coding=utf-8

import cv2

import numpy as np

import time

import sys

from PyQt5.QtWidgets import *

from PyQt5.QtCore import *

from PyQt5.QtGui import *

class Camera:

def __init__(self, width=320, height=240):

self.cap = cv2.VideoCapture(0)

self.image= QImage()

self.width = width

self.height = height

ret, frame = self.cap.read()

frame = cv2.resize(frame, (self.width, self.height))

self.h, self.w, self.bytesPerComponent = frame.shape

self.bytesPerLine = self.bytesPerComponent * self.w

def ReturnOneQPixmap(self):

# get a frame

ret, frame = self.cap.read()

frame = cv2.resize(frame, (self.width, self.height))

if ret:

if frame.ndim == 3:

rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

elif frame.ndim == 2:

rgb = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)

self.image = QImage(rgb.data, self.w, self.h, self.bytesPerLine, QImage.Format_RGB888)

return QPixmap.fromImage(self.image)

def DestroyCamera(self):

cap.release()

cv2.destroyAllWindows()

class VideoBox(QWidget):

def __init__(self):

QWidget.__init__(self)

self.setWindowFlags(Qt.CustomizeWindowHint)

# 初始化摄像头

self.camera = Camera(320, 320)

# 组件展示

self.pictureLabel = QLabel()

self.pictureLabel.setFixedSize(320, 240)

self.pictureLabel.setObjectName("Camera")

# 选择下拉框

self.combo = QComboBox()

self.combo.addItem('Video')

self.combo.addItem('Stop')

self.combo.activated[str].connect(self.onActivatd)

# 水平布局组件

control_box = QHBoxLayout()

control_box.setContentsMargins(0, 0, 0, 0)

control_box.addWidget(self.combo)

# 添加组件

layout = QVBoxLayout()

layout.addWidget(self.pictureLabel)

layout.addLayout(control_box)

self.setLayout(layout)

# video timer 设置

self.video_timer = VideoTimer()

self.video_timer.timeSignal.signal[str].connect(self.showframe)

def onActivatd(self, text):

if text == 'Video':

self.video_timer.start()

print('Video')

if text == 'Stop':

self.video_timer.stop()

quit()

def showframe(self):

self.pictureLabel.setPixmap(self.camera.ReturnOneQPixmap())

class Communicate(QObject):

signal = pyqtSignal(str)

class VideoTimer(QThread):

def __init__(self, frequent=20):

QThread.__init__(self)

self.stopped = False

self.frequent = frequent

self.timeSignal = Communicate()

self.mutex = QMutex()

def run(self):

with QMutexLocker(self.mutex):

self.stopped = False

while True:

if self.stopped:

return

self.timeSignal.signal.emit("1")

time.sleep(1 / self.frequent)

def stop(self):

with QMutexLocker(self.mutex):

self.stopped = True

def is_stopped(self):

with QMutexLocker(self.mutex):

return self.stopped

def set_fps(self, fps):

self.frequent = fps

if __name__ == "__main__":

app = QApplication(sys.argv)

box = VideoBox()

# box.showFullScreen()

box.show()

sys.exit(app.exec_())

做树莓派camera时候写的小样,可以小瞄一下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值