OpenCV-根据选择条件保存视频片段 [英] OpenCV - Save video segments based on certion condition
问题描述
目标 :检测运动并将运动周期仅保存在具有起始时间名称的文件中.
现在,我遇到了有关如何将视频保存到具有视频开始时间的文件的问题.
Now I met the issue about how to save the video to the files with video starting time.
我测试过的 :
What I tested :
我对程序进行了部分测试.似乎除保存部分外,其他每个部分都工作良好.
I tested my program part by part. It seems that each part works well except the saving part.
运行状态 :没有错误.但是在保存文件夹中,没有视频.如果我改用静态保存路径,则视频将成功保存,但下一个视频将覆盖该视频.我的代码如下:
Running status: No error. But in the saving folder, there is no video. If I use a static saving path instead, the video will be saved successfully, but the video will be override by the next video. My codes are below:
import cv2
import numpy as np
import time
cap = cv2.VideoCapture( 0 )
bgst = cv2.createBackgroundSubtractorMOG2()
fourcc=cv2.VideoWriter_fourcc(*'DIVX')
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
n = "start_time"
while True:
ret, frame = cap.read()
dst = bgst.apply(frame)
dst = np.array(dst, np.int8)
if np.count_nonzero(dst)>3000: # use this value to adjust the "Sensitivity"
print('something is moving %s' %(time.ctime()))
path = r'E:\OpenCV\Motion_Detection\%s.avi' %n
out = cv2.VideoWriter( path, fourcc, 50, size )
out.write(frame)
key = cv2.waitKey(3)
if key == 32:
break
else:
out.release()
n = time.ctime()
print("No motion Detected %s" %n)
推荐答案
我的意思是:
import cv2
import numpy as np
import time
cap = cv2.VideoCapture( 0 )
bgst = cv2.createBackgroundSubtractorMOG2()
fourcc=cv2.VideoWriter_fourcc(*'DIVX')
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
path = r'E:\OpenCV\Motion_Detection\%s.avi' %(time.ctime())
out = cv2.VideoWriter( path, fourcc, 16, size )
while True:
ret, frame = cap.read()
dst = bgst.apply(frame)
dst = np.array(dst, np.int8)
for i in range(number of frames in the video):
if np.count_nonzero(dst)<3000: # use this value to adjust the "Sensitivity"
print("No Motion Detected")
out.release()
else:
print('something is moving %s' %(time.ctime()))
#label each frame you want to output here
out.write(frame(i))
key = cv2.waitKey(1)
if key == 32:
break
cap.release()
cv2.destroyAllWindows()
如果看到代码,将出现一个 for
循环,在该循环中完成保存过程.
If you see the code there will be a for
loop, within which the process of saving is done.
我不知道涉及框架循环的确切语法,但我希望您有主旨.您必须找到视频中存在的帧数,并将其设置为 for
循环中的范围.
I do not know the exact syntax involving for loop with frames, but I hope you have the gist of it. You have to find the number of frames present in the video and set that as the range in the for
loop.
每个帧都唯一地保存(请参阅 else
条件.)正如我所说,我不知道语法.请参考并遵循此过程.
Each frame gets saved uniquely (see the else
condition.) As I said I do not know the syntax. Please refer and follow this procedure.
干杯!
这篇关于OpenCV-根据选择条件保存视频片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!