Reputation: 31
I am working on a raspberry pi python script and I am struggling to get the if statement to do what I want.
I can count fingers on a hand in a video stream and this is working fine.
Next I want to initiate a first event (beep) when 5 fingers (defects) are counted, and then proceed into a sub loop where if the 5 fingers subsequently change to zero fingers within a given time (2s) then another second event (click) happens.
This is what I have so far (not quite working):
if count_defects==5:
os.system('mpg321 beep.mp3 -q')
time.sleep(2)
if count_defects<5:
os.system('mpg321 click.mp3 -q')
else:
cv2.putText(img, "Waiting", (50,50), font, 1, (255,255,255), 1)
Hope someone can help thanks
Upvotes: 3
Views: 588
Reputation: 13175
The issue is that count_defects takes an initial value == 5
to pass the if
clause. However, it can never then assume another value unless you give it an opportunity to recalculate. I don't know the function you use to get this value, but your code should look something like the following.
def check_number_of_fingers():
return number_of_fingers
count_defects = check_number_of_fingers()
if count_defects == 5:
os.system('mpg321 beep.mp3 -q')
time.sleep(2)
count_defects = check_number_of_fingers() # Recalculate the value after elapsed time
if count_defects < 5:
os.system('mpg321 click.mp3 -q')
else:
cv2.putText(img, "Waiting", (50,50), font, 1, (255,255,255), 1)
Upvotes: 3