电脑摄像头拍照demo-python

python3代码,使用PYQT5的QCameraInfo得到摄像头列表,然后CV2库从摄像头得到图像并保存.

下载资源:https://download.csdn.net/download/zhoury/12328554

代码如下: 

#window_video.py
import sys
import os
import cv2
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtMultimedia import QCameraInfo

class Video(QMainWindow):
    def __init__(self):
        super().__init__()

        self.camid = -1      #当前摄像头编号
        self.initUi()
        self.get_cam()
        self._timer = QTimer(self)
        self._timer.timeout.connect(self.play)


    def initUi(self):
        self.centralwidget = QWidget()
        self.setCentralWidget(self.centralwidget)
        self.startButton = QPushButton("开启")
        self.closeButton = QPushButton("关闭")
        self.camButton = QPushButton("拍照")
        self.promptLabel = QLabel('请选择摄像头:', self)
        self.combo = QComboBox(self)    #摄像头列表
        hbox = QHBoxLayout()
        hbox.addWidget(self.promptLabel)
        hbox.addWidget(self.combo)
        hbox.addStretch(1)
        hbox.addWidget(self.startButton)
        hbox.addWidget(self.closeButton)
        hbox.addWidget(self.camButton)

        vbox = QVBoxLayout()
        vbox.addLayout(hbox)
        self.vF = QLabel()
        vbox.addWidget(self.vF)
        self.centralwidget.setLayout(vbox)

        self.setGeometry(300, 300, 300, 150)
        self.setWindowTitle('简单拍照')    

        self.startButton.clicked.connect(self.onStartVideo)
        self.closeButton.clicked.connect(self.onCloseVideo)
        self.camButton.clicked.connect(self.onCamera)
        self.combo.currentIndexChanged.connect(self.selectionchange)

        self.startButton.setEnabled(False)
        self.closeButton.setEnabled(False)
        self.camButton.setEnabled(False)


    def onStartVideo(self):
        
        if self.camid < 0:
            return

        # 初始化传入的摄像头句柄为实例变量,并得到摄像头宽度和高度
        # cv2.CAP_DSHOW
        self.cam = cv2.VideoCapture(self.camid,cv2.CAP_DSHOW)
        self.w = self.cam.get(cv2.CAP_PROP_FRAME_WIDTH)
        self.h = self.cam.get(cv2.CAP_PROP_FRAME_HEIGHT)

        # 设置GUI窗口的位置和尺寸
        self.setGeometry(300, 200, self.w+20, self.h+100)
        # 开启定时器 
        self._timer.start(50)

        self.startButton.setEnabled(False)
        self.closeButton.setEnabled(True)
        self.camButton.setEnabled(True)
        self.combo.setEnabled(False)

    def onCloseVideo(self):
        self._timer.stop()

        if self.camid >= 0:
            self.startButton.setEnabled(True)
            self.closeButton.setEnabled(False)
            self.camButton.setEnabled(False)
            self.combo.setEnabled(True)


    def selectionchange(self):
        self.camid = self.combo.currentIndex()
        self.startButton.setEnabled(True)

    def play(self):
        """
        从摄像头得到图像 先转换为RGB格式 再生成QImage对象
        再用此QImage刷新vF实例变量 以刷新视频画面
        """
        r, f = self.cam.read()
        if r:
            self.vF.setPixmap(QPixmap.fromImage(QImage(cv2.cvtColor(f, cv2.COLOR_BGR2RGB), self.w,self.h,13)))
            #cv2.imwrite("c:\\temp\\aaa.jpg",f, [int(cv2.IMWRITE_JPEG_QUALITY),95])

    def get_cam(self):    #使用QCameraInfo得到摄像头列表
        camlist = QCameraInfo.availableCameras()
        self.cnt = 0
        for cam in camlist:
            self.combo.addItem(str(self.cnt) + ' - ' + cam.description())
            self.cnt = self.cnt + 1
        if self.cnt == 0:
            ret = QMessageBox.information(self,"Attention","没有找到摄像头", QMessageBox.Ok )

    def onCamera(self):  #拍照并保存
        r, f = self.cam.read()
        if r:
            #self.vF.setPixmap(QPixmap.fromImage(QImage(cv2.cvtColor(f, cv2.COLOR_BGR2RGB), self.w,self.h,13)))
            #cv2.imwrite("c:\\temp\\aaa.jpg",f, [int(cv2.IMWRITE_JPEG_QUALITY),95])
            newfileName, ok = QFileDialog.getSaveFileName(self, "文件另存为", "", "*.jpg")
            if newfileName:
                cv2.imwrite(newfileName,f, [int(cv2.IMWRITE_JPEG_QUALITY),95])
                os.system(newfileName)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    # 初始化GUI窗口 并传入摄像头句柄
    win = Video()
    win.show()
    sys.exit(app.exec_())

运行截图如下:

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值