赞
踩
界面设计部分主要采用Qtdesigner快速设计,安装Qtdesigner和pyuic工具可参考转载文章: PyQt5+QtDesigner编写摄像头界面程序(一)——pyqt5、qtdesigner安装和环境设置
安装好Qtdesigner后打开,选择左侧组建栏中的QLabel用来显示视频、Push Button设计两个按钮,然后在右上方修改对应的名称,如下图所示,
设计完成后保存,会自动生成camera_ui.ui文件,再通过pyuic外部工具生成对应的camera_ui.py:
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'camera_ui.ui' # # Created by: PyQt5 UI code generator 5.9.2 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(800, 715) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.camera_label = QtWidgets.QLabel(self.centralwidget) self.camera_label.setGeometry(QtCore.QRect(90, 70, 591, 451)) self.camera_label.setStyleSheet("border:5px solid black;\n" "background-color: rgb(255, 255, 255);\n" "") self.camera_label.setText("") self.camera_label.setObjectName("camera_label") self.start_camera = QtWidgets.QPushButton(self.centralwidget) self.start_camera.setGeometry(QtCore.QRect(140, 540, 93, 28)) self.start_camera.setStyleSheet("background-color: rgb(255, 0, 255);") self.start_camera.setObjectName("start_camera") self.stop_camera = QtWidgets.QPushButton(self.centralwidget) self.stop_camera.setGeometry(QtCore.QRect(460, 540, 93, 28)) self.stop_camera.setStyleSheet("background-color: rgb(255, 0, 255);") self.stop_camera.setObjectName("stop_camera") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.start_camera.setText(_translate("MainWindow", "star")) self.stop_camera.setText(_translate("MainWindow", "stop"))
需要利用用到定时器,当按下开始按钮时,启动定时器,隔100ms刷新显示一次图像,按下停止,定时器失效
from camera_ui import Ui_MainWindow import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox from PyQt5 import QtCore from PyQt5.QtGui import QPixmap import cv2 import qimage2ndarray class CamShow(QMainWindow,Ui_MainWindow): '''初始化程序,继承由Qtdesigner设计的py文件,设置定时器''' def __init__(self,parent=None): super(CamShow,self).__init__(parent) self.setupUi(self) self.timer_camera = QtCore.QTimer() self.start() '''绑定槽函数''' def start(self): self.start_camera.clicked.connect(self.bnt_start) self.stop_camera.clicked.connect(self.bnt_stop) '''若点击开始按钮,调用摄像头,定时器开启''' def bnt_start(self): self.cap = cv2.VideoCapture(0) self.timer_camera.start(100) self.timer_camera.timeout.connect(self.show_camera) '''若点击停止按钮,定时器失效''' def bnt_stop(self): self.timer_camera.stop() '''首先通过摄像头获取的图像resize,由于cv2默认使用的是BGR编码,图像显示偏蓝色, 需要通过cvtColor和qimage2ndarray.array2qimage转换一下''' def show_camera(self): ret, img = self.cap.read() self.img = cv2.resize(img, (640, 480), interpolation=cv2.INTER_CUBIC) self.img1 = cv2.cvtColor(self.img, cv2.COLOR_BGR2RGB) qimg = qimage2ndarray.array2qimage(self.img) self.camera_label.setPixmap(QPixmap(qimg)) self.camera_label.show() '''设计是否退出程序,防止误碰''' def closeEvent(self, event): reply = QMessageBox.question(self, '退出', "是否要退出该界面?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes: self.close() event.accept() else: event.ignore() if __name__ == '__main__': app = QApplication(sys.argv) ui=CamShow() ui.show() sys.exit(app.exec_())
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。