闲下来了,就趁着想写,把这个新加入的功能加进去。 终于可以去上班了,学生党可以去打工了,太不容易了~ 项目代码: 代码整体更新至main分支。
添加图片与视频保存功能
1. 图片检测结果保存
直接获得当前系统时间和文件的后缀名来对检测后的文件进行命名 结果保存在output/img_output中
if not img_name:
QtWidgets.QMessageBox.warning(self, u"Warning", u"打开图片失败", buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
else:
img = cv2.imread(img_name)
print("img_name:", img_name)
info_show = self.detect(name_list, img)
print(info_show)
now = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime(time.time()))
file_extension = img_name.split('.')[-1]
new_filename = now + '.' + file_extension
file_path = self.output_folder + 'img_output/' + new_filename
cv2.imwrite(file_path, img)
2. 保存视频和摄像头检测结果
(1)获得当前的系统时间,减少代码重复
def set_video_name_and_path(self):
now = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime(time.time()))
fps = self.cap.get(cv2.CAP_PROP_FPS)
w = int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
save_path = self.output_folder + 'video_output/' + now + '.mp4'
return fps, w, h, save_path
(2)在button_video_open与button_camera_open函数中添加视频写入资源函数
def button_video_open(self):
video_name, _ = QtWidgets.QFileDialog.getOpenFileName(self, "打开视频", "data/", "*.mp4;;*.avi;;All Files(*)")
flag = self.cap.open(video_name)
if not flag:
QtWidgets.QMessageBox.warning(self, u"Warning", u"打开视频失败", buttons=QtWidgets.QMessageBox.Ok,defaultButton=QtWidgets.QMessageBox.Ok)
else:
fps, w, h, save_path = self.set_video_name_and_path()
self.vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
self.timer_video.start(30)
self.ui.pushButton_video.setDisabled(True)
self.ui.pushButton_img.setDisabled(True)
self.ui.pushButton_camer.setDisabled(True)
def button_camera_open(self):
print("Open camera to detect")
camera_num = 0
self.cap = cv2.VideoCapture(camera_num)
bool_open = self.cap.isOpened()
if not bool_open:
QtWidgets.QMessageBox.warning(self, u"Warning", u"打开摄像头失败", buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
else:
fps, w, h, save_path = self.set_video_name_and_path()
fps = 5
self.vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
self.timer_video.start(30)
self.ui.pushButton_video.setDisabled(True)
self.ui.pushButton_img.setDisabled(True)
self.ui.pushButton_camer.setDisabled(True)
(4)在show_video_frame中将检测结果写入到文件,不要忘记释放写入资源操
def show_video_frame(self):
name_list = []
flag, img = self.cap.read()
if img is not None:
info_show = self.detect(name_list, img)
self.vid_writer.write(img)
print(info_show)
self.ui.textBrowser.setText(info_show)
show = cv2.resize(img, (640, 480))
self.result = cv2.cvtColor(show, cv2.COLOR_BGR2RGB)
showImage = QtGui.QImage(self.result.data, self.result.shape[1], self.result.shape[0],
QtGui.QImage.Format_RGB888)
self.ui.label.setPixmap(QtGui.QPixmap.fromImage(showImage))
self.ui.label.setScaledContents(True)
else:
self.timer_video.stop()
self.cap.release()
self.vid_writer.release()
注:本来是直接在show_video_frame进行整个的视频写入操作的,即cv2.VideoWriter和write操作都在此。测试了之后,发现如此写入,会导致每一帧的检测结果被保存为一个视频文件。 还是需要在学习一下这个PyQt与Opencv结合在一起时候的相关特点。
|