Reputation:
I'm learning OpenCV (and Python) and have managed to get OpenCV to detect my nose and move the mouse using the movement of my nose, but since it looses track of my nose often I want it to fall back to moving using my face instead of my nose if needed. I've managed to draw rectangles around my face and my nose in video.
I tried being cheeky and just putting the loop for my face rectangle in "if cv2.rectangle" (for the nose), but it is always true. My question is how can I create a test to see if nose is detected fallback to move mouse with face, and if nose is re-detected go back to using the nose.
My loops as of now
# Here we draw the square around the nose, face and eyes that is detected.
for (x,y,w,h) in nose_rect:
cv2.rectangle(frame, (x,y), (x+w,y+h), (0,0,255), 3)
if cv2.rectangle:
m.move(x * 4, y * 4) # TODO: Write and if that goes into face if nose is not visible
break
else:
for (x, y, w, h) in face_rect:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)
break
for (x,y,w,h) in eye_rect:
cv2.rectangle(frame, (x,y), (x+w,y+h), (205,0,0), 3)
break
I can post my entire program if that helps, I've tried doing a bunch of the OpenCV official tutorials but did not managed to find an answer to my question there.
Thank you for all replies!
PS: I'm using Python 3.5
Upvotes: 2
Views: 788
Reputation: 1914
Here's the snippet you should use in your code-
if(len(nose_rect)>0):
print ("Only Nose")
for (x,y,w,h) in nose_rect:
cv2.rectangle(frame, (x,y), (x+w,y+h), (0,0,255), 3)
#Here we say that m (the variable created before, should move the mouse using the x, and y variable from the nose rect.
# We have acellerated movement speed by 4 to make it possible to navigate the cursor through the whole screen.
m.move(x * 4, y * 4) # TODO: Write and if that goes into face if nose is not visible
elif (len(face_rect)>0):
print ("Only Face")
for (x,y,w,h) in face_rect:
cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 3)
elif (len(face_rect)>0):
print ("Only Eye")
for (x,y,w,h) in eye_rect:
cv2.rectangle(frame, (x,y), (x+w,y+h), (205,0,0), 3)
else:
print ("Nothing detected.")
Also, for waiting use the time.sleep() method
time.sleep(0.001) # Waiting 1 millisecond to show the next frame.
if (cv2.waitKey(1) & 0xFF == ord('q')):#exit on pressing 'q'
break
Upvotes: 1