I have some opencv code which counts the number of fingers the user is holding up to the camera. the program is fully functional and works as expected.
However, I am trying to connect the keyboard with my program. For example, if the user holds up a 1, then the "W" key should be pressed and if the user holds up a 2, then the "A" key should be pressed, and so on.
I decided to use app script for this.
if count+1 == 1:
app('System Events').keystroke('w')
if count+1 == 2:
app('System Events').keystroke('a')
if count+1 == 3:
app('System Events').keystroke('s')
if count+1 == 4:
app('System Events').keystroke('d')
if count+1 == 5:
app('System Events').keystroke('c')
This code works pretty well, but there are two things I am unable to figure out how to do:
How can I hold the key until the user removes the gesture from the camera? For example, if the user holds up one finger, then the program should press "W" as long as that the one finger is up.
For some reason, whenever I run this code, it has the camera but after some time, my computer just freezes and I can't do anything other than restart the computer. I feel like this is something to do with the keyboard stuff, so I set up an if statement:
key = cv2.waitKey(10)
if k == ord('q'):
sys.exit()
However, this doesn't seem to work.
Would really appreciate if someone could help. Thanks in advance.