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