0

I have a code here that detects a white object using open cv moments. what i want is if that object is in the upper part of the webcam for 5 seconds. I want to do something .. Then if that object is seen in the bottom part of the webcam i want to do something also (but different) My webcam is 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
Luisito
  • 11
  • 1
  • 2

2 Answers2

0

See 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
0

What you need to do is to keep detecting and counting at the same time. You can do something like this:

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

One more thing to consider is that in OpenCV images positive y axis goes down not up. (0,0) is the top left corner in the image

0/0---X--->
|
|
Y
|
|
v
Osama AlKoky
  • 214
  • 1
  • 5