天天看點

153.Python——PySide6:使用QtMultimedia打開攝像頭捕獲圖像

作者:UNET

在前面有講過,如何用OpenCV來打開攝像頭、視訊流,以及儲存圖像和錄制視訊。

本文主要主要執行個體示範,如何使用PySide6的媒體子產品來打開攝像頭并捕獲圖像。

實作代碼

import os
import sys
from PySide6.QtCore import QDate, QTime,QStandardPaths, Qt, QUrl
from PySide6.QtGui import QAction, QGuiApplication, QDesktopServices, QIcon
from PySide6.QtGui import QImage, QPixmap
from PySide6.QtWidgets import (QApplication, QMainWindow,  QToolBar,  QWidget,QStyle)
from PySide6.QtMultimedia import (QCamera, QImageCapture,
                                  QCameraDevice, QMediaCaptureSession,
                                  QMediaDevices)
from PySide6.QtMultimediaWidgets import QVideoWidget

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        
        self._file_name=None #捕獲圖像儲存的檔案名
            
        #可用的視訊輸入裝置(攝像頭)
        available_cameras=QMediaDevices.videoInputs()
        if available_cameras:
            #攝像頭
            self._camera_info=available_cameras[0]
            self._camera=QCamera(self._camera_info)
            #捕獲圖像
            self._image_capture = QImageCapture(self._camera)
            self._image_capture.imageCaptured.connect(self.image_captured)
            self._image_capture.imageSaved.connect(self.image_saved)
            
            
            #捕獲會話
            self._capture_session = QMediaCaptureSession()
            self._capture_session.setCamera(self._camera)
            self._capture_session.setImageCapture(self._image_capture)
            
        
        self.status=self.statusBar() #開啟狀态欄
        #建立工具條,一個捕獲目前畫面圖像
        tool_bar = QToolBar()
        self.addToolBar(tool_bar)
        style = self.style()
        icon=QIcon.fromTheme("capture",style.standardIcon(QStyle.SP_FileIcon))
        captureaction=tool_bar.addAction(icon,"Capture")
        tool_bar.addAction(captureaction)
        captureaction.triggered.connect(self.image_captured)
        
        #顯示攝像頭視訊畫面
        self._camera_viewfinder=QVideoWidget()
        self.setCentralWidget(self._camera_viewfinder)        
        if self._camera:
            self._capture_session.setVideoOutput(self._camera_viewfinder)
            self._camera.start()
        
    #檔案名儲存規則    
    def image_file_name(self):
        #本地圖像檔案夾
        pictures_location = QStandardPaths.writableLocation(QStandardPaths.PicturesLocation)
        date_string = QDate.currentDate().toString("yyyyMMdd") #+QTime.currentTime().toString("hhmmss")
        pattern = f"{pictures_location}/capture_{date_string}_{{:03d}}.jpg"
        n = 1
        while True:
            result = pattern.format(n)
            if not os.path.exists(result):
                return result
            n = n + 1
        return None
        
    #捕獲儲存圖像
    def image_captured(self):               
        self._file_name=self.image_file_name() #擷取檔案名
        self._image_capture.captureToFile(self._file_name) #儲存

    #儲存圖像後本地打開圖像
    def image_saved(self,):
        QDesktopServices.openUrl(QUrl.fromLocalFile(self._file_name))
        self.status.showMessage(self._file_name)
        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    mywin = MainWindow()
    screengeometry=mywin.screen().availableGeometry()
    mywin.resize(screengeometry.width() / 3,screengeometry.height() / 2)
    mywin.setWindowTitle("Camera")
    mywin.show()
    sys.exit(app.exec())           

運作效果

153.Python——PySide6:使用QtMultimedia打開攝像頭捕獲圖像