python - 如何使用 Opencv 捕获帧

标签 python opencv pyqt pyqt5

这是加载 pyqt gui 表单的主要代码,它有 2 个按钮,一个用于

启动网络摄像头,第二个用于从帧中捕获照片。

我写了第一个按钮,但我不能写捕获按钮。

import sys
import cv2
import numpy as np
from PyQt5.QtCore import QTimer
from PyQt5.QtGui import  QImage,QPixmap
from PyQt5.QtWidgets import QApplication , QDialog
from PyQt5.uic import loadUi

img_counter = 0

class video (QDialog):
    def __init__(self):
        super(video, self).__init__()
        loadUi('video.ui',self)
        self.image=None
        self.startButton.clicked.connect(self.start_webcam)
        self.capture.clicked.connect(self.keyPressEvent)

    def start_webcam(self):
        self.capture =cv2.VideoCapture(0)
        self.capture.set(cv2.CAP_PROP_FRAME_HEIGHT,480)
        self.capture.set(cv2.CAP_PROP_FRAME_WIDTH,640)

        self.timer=QTimer(self)
        self.timer.timeout.connect(self.update_frame)
        self.timer.start(5)

    def update_frame(self):
        ret,self.image=self.capture.read()
        self.image=cv2.flip(self.image,1)
        self.displayImage(self.image,1)

    def keyPressEvent(self):
        flag, frame= self.capture.read()
        path = 'J:\Face'
        cv2.imwrite(os.path.join(path,'wakka.jpg'), frame)

    def displayImage(self,img,window=1):
        qformat=QImage.Format_Indexed8
        if len(img.shape)==3 :
            if img.shape[2]==4:
                qformat=QImage.Format_RGBA8888
            else:
                qformat=QImage.Format_RGB888

        outImage=QImage(img,img.shape[1],img.shape[0],img.strides[0],qformat)

        outImage=outImage.rgbSwapped()


        if window==1:
            self.imgLabel.setPixmap(QPixmap.fromImage(outImage))
            self.imgLabel.setScaledContents(True)

if __name__=='__main__':
    app=QApplication(sys.argv)
    window=video()
    window.setWindowTitle('main code')
    window.show()
    sys.exit(app.exec_())

我想从相框中捕捉照片并将其保存在文件夹中。

self.capture.clicked.connect(self.keyPressEvent) 用于当我们点击按钮时。

我应该在 keyPressEvent def 中写函数

capture.is 用于单击按钮

有人可以帮我解决这个问题吗?

编辑笔记 :

    if flag:

         QtWidgets.QApplication.beep(i)
         img_name = "opencv_frame_{}.png".format()
         cv2.imwrite(os.path.join(path,img_name), frame)

我想要循环的条件,这样我就可以用计数器保存img_name格式,但计数器必须是点击次数

最佳答案

keyPressEvent 是一种允许您在小部件具有焦点时捕获键的方法,在您的情况下没有必要,解决方案很简单更改其名称,另一方面我改进了您的代码。

import os
import cv2
import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets, uic

class video (QtWidgets.QDialog):
    def __init__(self):
        super(video, self).__init__()
        uic.loadUi('video.ui',self)
        self.startButton.clicked.connect(self.start_webcam)
        self.capture.clicked.connect(self.capture_image)
        self.imgLabel.setScaledContents(True)
        self.capture = None
        self.timer = QtCore.QTimer(self, interval=5)
        self.timer.timeout.connect(self.update_frame)
        self._image_counter = 0

    @QtCore.pyqtSlot()
    def start_webcam(self):
        if self.capture is None:
            self.capture =cv2.VideoCapture(0)
            self.capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
            self.capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
        self.timer.start()

    @QtCore.pyqtSlot()
    def update_frame(self):
        ret, image=self.capture.read()
        simage = cv2.flip(image, 1)
        self.displayImage(image, True)

    @QtCore.pyqtSlot()
    def capture_image(self):
        flag, frame= self.capture.read()
        path = r'J:\Face'
        if flag:
            QtWidgets.QApplication.beep()
            name = "opencv_frame_{}.png".format(self._image_counter) 
            cv2.imwrite(os.path.join(path, name), frame)
            self._image_counter += 1

    def displayImage(self, img, window=True):
        qformat = QtGui.QImage.Format_Indexed8
        if len(img.shape)==3 :
            if img.shape[2]==4:
                qformat = QtGui.QImage.Format_RGBA8888
            else:
                qformat = QtGui.QImage.Format_RGB888
        outImage = QtGui.QImage(img, img.shape[1], img.shape[0], img.strides[0], qformat)
        outImage = outImage.rgbSwapped()
        if window:
            self.imgLabel.setPixmap(QtGui.QPixmap.fromImage(outImage))

if __name__=='__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = video()
    window.setWindowTitle('main code')
    window.show()
    sys.exit(app.exec_())

关于python - 如何使用 Opencv 捕获帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53365109/

相关文章:

python - 单击 PyQt 表项时更改属性

python - 动态显示/隐藏QTextEdit的一部分

python - 如何在 PyQt5 中拥有动态字段?

python - 在 Python 中压缩整数列表

Python:声明为整数和字符

python - 对齐和裁剪相同的场景图像

python - 检测文本之间的空格(OpenCV、Python)

python - OAuth 在 Google OAuth2 中抛出 "missing code validator"

python - 用数字替换字符串 numpy 数组

macos - 用cuda为MAC编译opencv