gpt4 book ai didi

python - 如何使用 Opencv 捕获帧

转载 作者:太空宇宙 更新时间:2023-11-03 21:07:32 25 4
gpt4 key购买 nike

这是加载 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com