PyQt5 + Python-Opencv 打开摄像头

声明:这只是简简单单地打开摄像头显示图片的代码。

ui代码(直接用Qt Designer设计的):

要用的库全放ui代码里了,就不放在实现代码里了,直接两段代码黏一起就可运行。

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtGui import QImage, QPixmap
import cv2
import sys

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(753, 629)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(210, 520, 93, 28))
        self.pushButton.setObjectName("pushButton")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(60, 20, 640, 480))
        self.label.setFrameShape(QtWidgets.QFrame.WinPanel)
        self.label.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.label.setText("")
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(440, 520, 93, 28))
        self.pushButton_2.setObjectName("pushButton_2")
        self.label.raise_()
        self.pushButton.raise_()
        self.pushButton_2.raise_()
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 753, 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.pushButton.setText(_translate("MainWindow", "打开摄像头"))
        self.pushButton_2.setText(_translate("MainWindow", "关闭摄像头"))

实现代码:

class Open_Camera(QtWidgets.QMainWindow,Ui_MainWindow):
    def __init__(self):
        super(Open_Camera,self).__init__()
        self.setupUi(self) #创建窗体对象
        self.init()
        self.cap = cv2.VideoCapture(0,cv2.CAP_DSHOW) #摄像头
    def init(self):
        #定时器让其定时读取显示图片
        self.camera_timer = QTimer()
        self.camera_timer.timeout.connect(self.show_image)
        #打开摄像头
        self.pushButton.clicked.connect(self.open_camera)
        #关闭摄像头
        self.pushButton_2.clicked.connect(self.close_camera)
    '''开启摄像头'''
    def open_camera(self):
        if self.cap.isOpened():
            self.camera_timer.start(40) #每40毫秒读取一次,即刷新率为25帧
            self.show_image()
        else:
            QMessageBox.critical(self,'错误','摄像头未打开!')
            return None
    '''显示图片'''
    def show_image(self):
        flag,image = self.cap.read()  #从视频流中读取图片
        image_show = cv2.resize(image,(1280,800))  #把读到的帧的大小重新设置为 1280x800
        width,height = image_show.shape[:2] #行:宽,列:高
        image_show = cv2.cvtColor(image_show,cv2.COLOR_BGR2RGB)  #opencv读的通道是BGR,要转成RGB
        image_show = cv2.flip(image_show, 1)  #水平翻转,因为摄像头拍的是镜像的。
        #把读取到的视频数据变成QImage形式(图片数据、高、宽、RGB颜色空间,三个通道各有2**8=256种颜色)
        show = QtGui.QImage(image_show.data,height,width,QImage.Format_RGB888)
        self.label.setPixmap(QPixmap.fromImage(show))  # 往显示视频的Label里显示QImage
    '''关闭摄像头'''
    def close_camera(self):
        self.camera_timer.stop() #停止读取
        self.cap.release() #释放摄像头
        self.label.clear() #清除label组件上的图片

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ui = Open_Camera()
    ui.show()
    sys.exit(app.exec_())

你可能感兴趣的:(个人python,GUI,设计之路,qt,python,opencv,ui,计算机视觉)