>目标 :检测运动并仅将运动周期保存在具有开始时间名称的文件中。
现在我遇到了有关如何将视频保存到具有视频开始时间的文件的问题。
我测试了什么 :
我逐部分测试了我的程序。似乎除了保存部分之外,每个部分都运行良好。
运行状态 :无错误。但是在保存文件夹中,没有视频。如果我改用静态保存路径,视频将成功保存,但视频将被下一个视频覆盖。我的代码如下:
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:OpenCVMotion_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:OpenCVMotion_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
循环,在该循环中完成保存过程。
我不知道涉及带有框架的 for 循环的确切语法,但我希望您了解它的要点。您必须找到视频中存在的帧数,并将其设置为for
循环中的范围。
每个帧都以唯一方式保存(请参阅else
条件)。正如我所说,我不知道语法。请参考并遵循此程序。
干杯!