在网络摄像头中检测到对象时计数5秒。

时间:2018-05-26 06:59:15

标签: python opencv timer python-multithreading

我这里有一个代码,可以使用open cv moment来检测白色物体。我想要的是,如果该对象位于网络摄像头的上部5秒钟。我想做点什么..然后如果在网络摄像头的底部看到那个对象我想做一些事情(但不同)我的网络摄像头是640x480 btw。

if moments['m00'] > 0:
            x = int(moments['m10'] / moments['m00'])
            y = int(moments['m01'] / moments['m00'])

            if x > 0 and x <=640 and y > 0 and y <=240: #if it detects the object in the upper part
                #count til 5 seconds then do something

            else:
                #if the object is in the lower part of webcam. Do something after 5 seconds    
        else:
            #cancel the timer so it wont do the something

2 个答案:

答案 0 :(得分:0)

请参阅here.

def block_for(seconds):
    """Wait at least seconds, this function should not be affected by the computer sleeping."""
    end_time = datetime.datetime.now() + datetime.timedelta(seconds)

    while datetime.datetime.now() < end_time:
        pass

答案 1 :(得分:0)

您需要做的是同时保持检测和计数。你可以这样做:

found_top = False
found_bottom = False
while True:
    # do detection
    if moments['m00'] > 0:
        x = int(moments['m10'] / moments['m00'])
        y = int(moments['m01'] / moments['m00'])

        if x > 0 and x <=640 and y > 0 and y <=240: #if it detects the object in the upper part
              if not found_top:
                  found_top = True
                  t0 = time.time()   
        # Do proper check if object appears at the bottom
    else:
        found_top = False
        found_bottom = False
    t1 = time.time()
    if found_top and (t1-t0) >= 5sec: # need to check proper way to do it
        # Do your action

还有一件事需要考虑的是,在OpenCV图像中,正y轴不会向下。 (0,0)是图像中的左上角

0/0---X--->
|
|
Y
|
|
v