-1

Im using opencv for detecting a hand gesture (in this case, when i lift my index finger), when that happens it prints "hi", then i use time.sleep to pause the print for 1 second because it will print it until I put my index down, but time.sleep pauses the entire footage, so if i sleep 5 seconds, the footage wont run untile those 5 seconds have passed

What can I do to use the time.sleep without pausing the footage

while True:
  #all the opencv initial stuff
  #mediapipe functions for the hand detection

  if INDEX_UP:
     print("hi")
     time.sleep(2)
  
  cv2.imshow("Frame", frame)
  if cv2.waitKey(1) & 0xFF == 27: break

Im trying to use time.sleep inside the while loop im using for opencv, but the time.sleep pauses the footage until the time.sleep finish

2 Answers2

0

I tried to use 2 separate threads for the entire while loop and another one for the sleep, it´s not sleeping.

def sleep():
  time.sleep(2)

def main_loop():
  while True:
  #all the opencv initial stuff
  #mediapipe functions for the hand detection

  if INDEX_UP:
     print("hi")
     th.Thread(target=sleep).start()
  
  cv2.imshow("Frame", frame)
  if cv2.waitKey(1) & 0xFF == 27: break

th.Thread(target=main_loop).start()
0

When you notice the index finger is lifted, get the system time, add 2 seconds and save that in a variable called pauseEndTime.

Now, in your main loop, if there is something you want to avoid doing during the pause, surround that thing with a test:

if currentTime > pauseEndTime:
   ...
   do stuff
   ...

If you want to continue acquisition during pauses, just run acquisition without that if check.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432