【PySide6】读取摄像头视频流并在QLabel上显示

1、准备界面

 界面和前面的基本一致,为了不犯错这里还是展示了一下。
在这里插入图片描述

2、编写函数

2.1、初始化

 初始化部分主要是,一是初始化信号槽,二是初始化视频捕获函数和Qt定时器,还有摄像头编号。

self.cap = cv2.VideoCapture()
self.timer  = QtCore.QTimer(self)
#摄像头编号,默认为0,电脑内置摄像头
self.camera_id = 0
self.init_slot()

 绑定信号槽

def init_slot(self):
    #开始绑定信号和槽
    self.ui.open.clicked.connect(self.open_camera)
    self.timer.timeout.connect(self.show_camera)
    self.ui.clear.clicked.connect(self.clear)
    pass

2.2、打开摄像头函数

 详细内容看注释。

def open_camera(self):
        #摄像头未打开,打开摄像头,定时器开始倒计时
        if self.timer.isActive() == False:
            flag = self.cap.open(self.camera_id,cv2.CAP_DSHOW)
            #读取失败报错
            if flag == False:
                QMessageBox.warning(self,'warning','请检查摄像头是否连接正确',
                QMessageBox.Ok)
            #否则显示
            else:
                self.timer.start(30)
                self.ui.open.setText("关闭摄像头")
        #摄像头打开,计时器停止,释放资源
        else:
            self.timer.stop()
            self.cap.release()
            self.clear()
            self.ui.open.setText("打开摄像头")
        pass

2.3、摄像头数据展示函数

def show_camera(self):
        ret,frame = self.cap.read()
        if ret:
            #读取成功
            frame = cv2.flip(frame,1)
            self.show_img(frame,self.ui.label)
        #否则报错
        else:
            QMessageBox.information(self,"警告","摄像头读取错误",QMessageBox.Ok)

2.4、图片显示

 这个函数基本没有变化,因为视频的每一帧都可以看作一张图片。

def show_img(self,im,label):
        try:
            #获取高和宽
            ih,iw ,_ = im.shape
            #获取标签的长和高
            w = label.geometry().width()
            h = label.geometry().height()
            #上述的目的是为了保持原始的纵横比
            if iw/w >ih/h:
                scal = w/iw
                nw = w
                nh = int(scal * ih)
                im_new = cv2.resize(im,(nw,nh))
            else:
                scal = w/iw
                nw = int(scal*iw)
                nh = h
                im_new = cv2.resize(im,(nw,nh))
            frame = cv2.cvtColor(im_new,cv2.COLOR_BGR2RGB)
            im = QImage(frame.data,frame.shape[1],frame.shape[0],frame.shape[2] * frame.shape[1],
                        QImage.Format_RGB888)
            label.setPixmap(QPixmap.fromImage(im))
        except Exception as e:
            print(repr(e))

2.5、清空

 清空图片并释放资源。

def clear(self):
        self.ui.label.clear()
        self.timer.stop()
        self.cap.release()
        self.ui.open.setText("打开摄像头")
        pass

2.6、完整代码

from PySide6.QtWidgets import QApplication,QMainWindow,QMessageBox
from PySide6 import QtWidgets,QtCore
from PySide6.QtGui import QImage,QPixmap

from ui.Ui_mainwin import Ui_MainWindow


import sys
import cv2
import warnings

warnings.filterwarnings('ignore')

class MainWin(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.show()

        self.cap = cv2.VideoCapture()
        self.timer  = QtCore.QTimer(self)
        #摄像头编号,默认为0,电脑内置摄像头
        self.camera_id = 0
        self.init_slot()

    def init_slot(self):
        #开始绑定信号和槽
        self.ui.open.clicked.connect(self.open_camera)
        self.timer.timeout.connect(self.show_camera)
        self.ui.clear.clicked.connect(self.clear)
        pass
    
    def open_camera(self):
        #摄像头未打开,打开摄像头,定时器开始倒计时
        if self.timer.isActive() == False:
            flag = self.cap.open(self.camera_id,cv2.CAP_DSHOW)
            #读取失败报错
            if flag == False:
                QMessageBox.warning(self,'warning','请检查摄像头是否连接正确',
                QMessageBox.Ok)
            #否则显示
            else:
                self.timer.start(30)
                self.ui.open.setText("关闭摄像头")
        #摄像头打开,计时器停止,释放资源
        else:
            self.timer.stop()
            self.cap.release()
            self.clear()
            self.ui.open.setText("打开摄像头")
        pass
    def show_camera(self):
        ret,frame = self.cap.read()
        if ret:
            #读取成功
            frame = cv2.flip(frame,1)
            self.show_img(frame,self.ui.label)
        #否则报错
        else:
            QMessageBox.information(self,"警告","摄像头读取错误",QMessageBox.Ok)
    def show_img(self,im,label):
        try:
            #获取高和宽
            ih,iw ,_ = im.shape
            #获取标签的长和高
            w = label.geometry().width()
            h = label.geometry().height()
            #上述的目的是为了保持原始的纵横比
            if iw/w >ih/h:
                scal = w/iw
                nw = w
                nh = int(scal * ih)
                im_new = cv2.resize(im,(nw,nh))
            else:
                scal = w/iw
                nw = int(scal*iw)
                nh = h
                im_new = cv2.resize(im,(nw,nh))
            frame = cv2.cvtColor(im_new,cv2.COLOR_BGR2RGB)
            im = QImage(frame.data,frame.shape[1],frame.shape[0],frame.shape[2] * frame.shape[1],
                        QImage.Format_RGB888)
            label.setPixmap(QPixmap.fromImage(im))
        except Exception as e:
            print(repr(e))
    def clear(self):
        self.ui.label.clear()
        self.timer.stop()
        self.cap.release()
        self.ui.open.setText("打开摄像头")
        pass

if __name__=='__main__':
    app = QApplication(sys.argv)
    window = MainWin()
    sys.exit(app.exec())

3、视频教程

4、相关内容

【PySide6】PySide6安装及VSCode配置PySide6环境
【PySide6】登录和注册信号绑定以及代码实现
【PySide6】实现登录界面向主界面跳转
【PySide6】在QLabel上显示一张图片
【PySide6】QLabel显示视频流

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

鲸可落

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

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

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

打赏作者

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

抵扣说明:

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

余额充值