我正在使用 cv2 逐帧分析视频,如果帧号在某些时间戳之间,我想将帧保存为 1,如果不是,则保存为 0。我有两个列表,一个是开始时间,一个是结束时间:
start_times = [0, 10, 15]
end_times = [2, 12, 17]
我想将帧号 0,1,2 和 10,11,12 和 15,16,17 的帧保存为 1,其他帧保存为 0。
我的代码将正确的帧保存为 1,但将不需要的帧保存为 0,因为我使用的是 for 循环。请参见下面的简化示例:
start_times = [0,10,15]
end_times = [2,12,17]
currentframe = 0
while True:
try:
for index,time in enumerate(start_times):
if start_times[index] <= currentframe <= end_times[index]:
print('save images as 1')
else:
print('save images as 0')
currentframe += 1
if currentframe == 20:
break
except IndexError:
break
第一帧的输出:
save images as 1
save images as 0
save images as 0
如何更改我的代码以使第一帧仅保存为 1?