def test_recording(self): assert keywords["recording.pcm"] in self.content def test_creatfile(self): assert keywords["creatfile"] in self.content
时间: 2024-02-23 14:57:41 浏览: 58
这段代码是用来进行自动化测试的。其中包含了两个测试用例函数test_recording和test_creatfile。这两个函数都是用来对self.content进行断言测试的,确保其中包含了keywords["recording.pcm"]和keywords["creatfile"]。如果这些关键词都在self.content中被找到了,那么这些测试用例就会通过。
相关问题
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) 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 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_())在这样代码的基础上,点击按钮停止实现停止录制以及保存视频
要在点击按钮时停止录制并保存视频,您可以通过添加一个停止录制的标志变量来实现。以下是修改后的代码示例:
```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)
self.recording = False
@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也是这个尺寸)
self.recording = True
while self.recording:
flag, frame = capture.read()
if flag is False:
continue
frame = cv2.resize(frame, (960, 540))
video_writer.write(frame)
key = cv2.waitKey(25)
if key == 27:
break
video_writer.release()
@pyqtSlot()
def on_pushButton_stop_clicked(self):
self.recording = False
@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
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_())
```
在修改后的代码中,我们添加了一个名为`recording`的标志变量,用于跟踪录制状态。在`on_pushButton_record_clicked`函数中,我们在循环中检查`self.recording`的值,如果它为False,则停止录制并释放视频写入器。我们还添加了一个名为`on_pushButton_stop_clicked`的槽函数,用于在点击停止按钮时将`self.recording`设置为False,从而停止录制。请确保在UI文件中存在一个名为`pushButton_stop`的按钮,并将其与`on_pushButton_stop_clicked`槽函数关联。
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) self.display_image(frame, self.label) # 显示帧到标签 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 self.display_image(frame, self.label) # 显示帧到标签 key = cv2.waitKey(25) if key == 27: break capture.release() def display_image(self, frame, label): pic_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 将BGR格式图像转换成RGB height, width = pic_image.shape[:2] pixMap = QImage(pic_image.data, width, height, width * 3, QImage.Format_RGB888) # 将RGB格式图像转换为八位图 pixMap = QPixmap.fromImage(pixMap) ratio = max(width / self.label.width(), height / self.label.height()) pixMap.setDevicePixelRatio(ratio) # 根据图片比例显示 self.label.setAlignment(Qt.AlignCenter) # 设置居中 self.label.setPixmap(pixMap) if __name__ == '__main__': app = QApplication(sys.argv) ui = videoShow() ui.show() sys.exit(app.exec_())修改这段代码,实现点击按钮停止录制以及保存视频
你可以在 `videoShow` 类中添加一个标志位 `recording` 来控制录制的开始和停止。同时,在停止录制时保存视频。以下是修改后的代码示例:
```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)
self.recording = False
@pyqtSlot()
def on_pushButton_record_clicked(self):
if self.recording:
self.recording = False
self.pushButton_record.setText('开始录制')
self.video_writer.release() # 释放视频写入对象
else:
self.recording = True
self.pushButton_record.setText('停止录制')
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
self.video_writer = cv2.VideoWriter("image/myself.mp4", fourcc, 25, (960, 540)) # 存放路径、、帧率fps、尺寸(且保证下面的frame也是这个尺寸)
while self.recording:
flag, frame = capture.read()
if flag is False:
continue
frame = cv2.resize(frame, (960, 540))
self.video_writer.write(frame)
self.display_image(frame, self.label) # 显示帧到标签
key = cv2.waitKey(25)
if key == 27:
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
self.display_image(frame, self.label) # 显示帧到标签
key = cv2.waitKey(25)
if key == 27:
break
capture.release()
def display_image(self, frame, label):
pic_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 将BGR格式图像转换成RGB
height, width = pic_image.shape[:2]
pixMap = QImage(
pic_image.data, width, height, width * 3, QImage.Format_RGB888
) # 将RGB格式图像转换为八位图
pixMap = QPixmap.fromImage(pixMap)
ratio = max(width / self.label.width(), height / self.label.height())
pixMap.setDevicePixelRatio(ratio) # 根据图片比例显示
self.label.setAlignment(Qt.AlignCenter) # 设置居中
self.label.setPixmap(pixMap)
if __name__ == "__main__":
app = QApplication(sys.argv)
ui = videoShow()
ui.show()
sys.exit(app.exec_())
```
这样,点击 "开始录制" 按钮将开始录制视频,并将按钮文本更改为 "停止录制"。再次点击 "停止录制" 按钮将停止录制,并保存视频文件。
阅读全文