gpt4 book ai didi

python - 如何在显示来自 cam 的视频时制作屏幕截图?

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

#Importing necessary libraries, mainly the OpenCV, and PyQt libraries
import cv2
import numpy as np
import sys
from PyQt5 import QtCore
from PyQt5 import QtWidgets
from PyQt5 import QtGui
from PyQt5.QtCore import pyqtSignal

class ShowVideo(QtCore.QObject):

#initiating the built in camera
camera_port = -1
camera = cv2.VideoCapture(camera_port)
VideoSignal = QtCore.pyqtSignal(QtGui.QImage)

def __init__(self, parent = None):
super(ShowVideo, self).__init__(parent)

@QtCore.pyqtSlot()
def startVideo(self):
run_video = True

while run_video:
ret, image = self.camera.read()

color_swapped_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
height, width, _ = color_swapped_image.shape

qt_image = QtGui.QImage(color_swapped_image.data,
width,
height,
color_swapped_image.strides[0],
QtGui.QImage.Format_RGB888)

pixmap = QtGui.QPixmap(qt_image)
qt_image = pixmap.scaled(640, 480, QtCore.Qt.KeepAspectRatio)
qt_image = QtGui.QImage(qt_image)

self.VideoSignal.emit(qt_image)

@QtCore.pyqtSlot()
def makeScreenshot(self):
#cv2.imwrite("test.jpg", self.image)
print("Screenshot saved")
#self.qt_image.save('test.jpg')

class ImageViewer(QtWidgets.QWidget):
def __init__(self, parent = None):
super(ImageViewer, self).__init__(parent)
self.image = QtGui.QImage()
self.setAttribute(QtCore.Qt.WA_OpaquePaintEvent)



def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.drawImage(0,0, self.image)
self.image = QtGui.QImage()

def initUI(self):
self.setWindowTitle('Test')


@QtCore.pyqtSlot(QtGui.QImage)
def setImage(self, image):
if image.isNull():
print("viewer dropped frame!")

self.image = image
if image.size() != self.size():
self.setFixedSize(image.size())
self.update()


if __name__ == '__main__':

app = QtWidgets.QApplication(sys.argv)
thread = QtCore.QThread()
thread.start()

vid = ShowVideo()
vid.moveToThread(thread)
image_viewer = ImageViewer()
#image_viewer.resize(200,400)

vid.VideoSignal.connect(image_viewer.setImage)

#Button to start the videocapture:

push_button = QtWidgets.QPushButton('Start')
push_button.clicked.connect(vid.startVideo)
push_button2 = QtWidgets.QPushButton('Screenshot')
push_button2.clicked.connect(vid.makeScreenshot)
vertical_layout = QtWidgets.QVBoxLayout()

vertical_layout.addWidget(image_viewer)
vertical_layout.addWidget(push_button)
vertical_layout.addWidget(push_button2)

layout_widget = QtWidgets.QWidget()
layout_widget.setLayout(vertical_layout)

main_window = QtWidgets.QMainWindow()
main_window.setCentralWidget(layout_widget)
main_window.resize(640,480)
main_window.show()
sys.exit(app.exec_())

此代码使用 OpenCV 和 PyQt5 无限循环显示来自摄像机的视频。但是如何制作屏幕截图并且不要停止显示视频。我认为它需要暂停一下循环,制作截图,然后再次运行循环。

最佳答案

您可以使用 cv2.waitKey() 实现相同的目的,如下所示:

while run_video:
ret, image = self.camera.read()

if(cv2.waitKey(10) & 0xFF == ord('s')):
cv2.imwrite("screenshot.jpg",image)

(我猜“屏幕截图”一词是指相机框架,而不是整个屏幕的图像。)当你在键盘上按下's'时,它会执行imwrite。请注意,如果您希望保存多张图像,则必须更改文件名。上面的代码将覆盖 screenshot.jpg 以仅保存最新的帧。

关于python - 如何在显示来自 cam 的视频时制作屏幕截图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39720344/

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