import sys import cv2 from showPic import Ui_MainWindow from PyQt5 import QtGui from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class videoShow(QMainWindow,Ui_MainWindow): def __init__(self): super().__init__() self.setupUi(self) @pyqtSlot() def on_pushButton_record_clicked(self): camera_path = 0 # 0:自带摄像头 1:外接摄像头 "xxx.mp4" "rtsp://admin:pwd@192.168.2.10/cam/..." capture = cv2.VideoCapture(camera_path) # 初始化播放器 流媒体 fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V') # XVID/DIVX MPEG MJPG X264 video_writer = cv2.VideoWriter("image/myself.mp4", fourcc, 25, (960, 540)) # 存放路径、、帧率fps、尺寸(且保证下面的frame也是这个尺寸) while True: flag, frame = capture.read() if flag is False: continue frame = cv2.resize(frame, (960, 540)) video_writer.write(frame) cv2.imshow("video", frame) key = cv2.waitKey(25) if key == 27: video_writer.release() break if __name__ == '__main__': app = QApplication(sys.argv) ui = videoShow() ui.show() sys.exit(app.exec_())在这样代码的基础上添加点击按钮播放已经录制好的视频
时间: 2024-02-14 10:30:09 浏览: 118
showpic.zip_MFC 显示JPG图片
您可以在`videoShow`类中添加一个点击按钮播放已经录制好的视频的槽函数,然后在UI文件中添加一个按钮,并将其与该槽函数关联。以下是修改后的代码示例:
```python
import sys
import cv2
from showPic import Ui_MainWindow
from PyQt5 import QtGui
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class videoShow(QMainWindow,Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
@pyqtSlot()
def on_pushButton_record_clicked(self):
camera_path = 0 # 0:自带摄像头 1:外接摄像头 "xxx.mp4" "rtsp://admin:pwd@192.168.2.10/cam/..."
capture = cv2.VideoCapture(camera_path) # 初始化播放器 流媒体
fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V') # XVID/DIVX MPEG MJPG X264
video_writer = cv2.VideoWriter("image/myself.mp4", fourcc, 25, (960, 540)) # 存放路径、、帧率fps、尺寸(且保证下面的frame也是这个尺寸)
while True:
flag, frame = capture.read()
if flag is False:
continue
frame = cv2.resize(frame, (960, 540))
video_writer.write(frame)
cv2.imshow("video", frame)
key = cv2.waitKey(25)
if key == 27:
video_writer.release()
break
@pyqtSlot()
def on_pushButton_play_clicked(self):
video_path = "image/myself.mp4" # 已经录制好的视频路径
capture = cv2.VideoCapture(video_path) # 初始化播放器
while True:
flag, frame = capture.read()
if flag is False:
break
cv2.imshow("video", frame)
key = cv2.waitKey(25)
if key == 27:
break
capture.release()
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = videoShow()
ui.show()
sys.exit(app.exec_())
```
在修改后的代码中,我们添加了一个名为`on_pushButton_play_clicked`的槽函数,并在该函数中使用`cv2.VideoCapture`打开已经录制好的视频,并通过`cv2.imshow`显示每一帧。您需要在UI文件中添加一个名为`pushButton_play`的按钮,并将其与该槽函数关联。当您点击该按钮时,录制好的视频将会开始播放。
阅读全文