Учитывая следующий код PyQt, я могу отлично захватывать потоковое видео с веб-камеры.
Теперь я хочу изменить код, , поэтому добавляется кнопка с именем «Запись», которая после нажатия захватывает потоковое видео и сохраняет его. Как я могу это сделать?
Я хочу записать видео для обучения распознаванию лиц.
class MainWindow(QWidget):
# class constructor
def __init__(self):
# call QWidget constructor
super().__init__()
self.ui = Ui_Form()
self.ui.setupUi(self)
# icon
self.setWindowIcon(QtGui.QIcon('icon.png'))
# create a timer
self.timer = QTimer()
# set timer timeout callback function
self.timer.timeout.connect(self.viewCam)
# set control_bt callback clicked function
self.ui.control_bt.clicked.connect(self.controlTimer)
# view camera
def viewCam(self):
# read image in BGR format
ret, image = self.cap.read()
# convert image to RGB format
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# get image infos
height, width, channel = image.shape
step = channel * width
# create QImage from image
# cv2.imshow('dna', image)
qImg = QImage(image.data, width, height, step, QImage.Format_RGB888)
# show image in img_label
self.ui.image_label.setPixmap(QPixmap.fromImage(qImg))
# start/stop timer
def controlTimer(self):
# if timer is stopped
if not self.timer.isActive():
# create video capture
self.cap = cv2.VideoCapture(0)
# start timer
self.timer.start(20)
# update control_bt text
self.ui.control_bt.setText("Stop")
# if timer is started
else:
# stop timer
self.timer.stop()
# release video capture
self.cap.release()
# update control_bt text
self.ui.control_bt.setText("Start")
self.ui.image_label.setText("Camera")
if __name__ == '__main__':
app = QApplication(sys.argv)
# create and show mainWindow
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())