gpt4 book ai didi

python - OpenCV - 根据证书条件保存视频片段

转载 作者:太空宇宙 更新时间:2023-11-03 21:51:49 24 4
gpt4 key购买 nike

目标:检测运动并仅将运动周期保存在具有开始时间名称的文件中

现在我遇到了如何将视频保存到带有视频开始时间的文件中的问题。

我测试了什么:

我部分地测试了我的程序。似乎除保存部分外,每个部分都运行良好。

运行状态:没有错误。但是在保存文件夹中,没有视频。如果我改用静态保存路径,视频会保存成功,但视频会被下一个视频覆盖。我的代码如下:

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条件。)正如我所说,我不知道语法。请引用并遵循此程序。

干杯!

关于python - OpenCV - 根据证书条件保存视频片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41778112/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com