【发布时间】:2016-06-25 01:23:16
【问题描述】:
我有一个 Python 脚本,它可以触发计算机网络摄像头,并且能够检测使用 opencv 捕获的视频中的运动。
它的工作方式是 Python 读取视频的第一帧并将其作为一个 numpy 数组存储在一个变量中。然后,脚本中运行了一个 while 循环,它基本上将第一个视频帧与视频的每个当前帧进行比较。在循环中每秒迭代大约 30 帧。当当前帧与第一帧不同时,我通过为其分配值 1 来更新 while 循环内的变量。因此,随着循环的进行,您可能有 0、0、0,然后是 1、1、1、1,具体取决于是否有运动。 我的目的是记录运动开始的时间。也就是说,我的变量从 0 变为 1 的时间。
这是我的伪代码:
start webcam
times_list=[]
motion_list=[]
while True:
my_variable=0
frame_difference=current_frame - first_frame
if frame_difference > 0:
continue
my_variable=1
motion_list.append(my_variable)
#The motion_list will get big, so let's keep only the last two items to avoid memory problems
#The last two items is all we need.
#Check if there was a change from non-motion to motion
if motion_list[-1]==1 and motion_list[-2]==0:
times_list.append(datetime.datetime.now())
所以,最后我有一个时间列表,其中包含运动开始的所有时间。
这是一个好的解决方案还是我在这里遗漏了什么?
【问题讨论】:
-
嗨,现在是周末,所以我有时间陪你 :) 经过昨天的讨论,我相应地更新了我的答案。通过您的 cmets,您似乎只对记录运动开始的最初时刻感兴趣,而不是整个运动时间线。
标签: python list while-loop