Press a key by blinking eyes – Gaze controlled keyboard with Python and Opencv p.8

In this tutorial we’re now going to detect when we press a key by we blink our eyes.
We don’t need to add almost anything new as we already we have the keyboard, or at least one part, and we already wrote the function to detect the blinking.

At the beginning of the script we’re going to create a white image which is going to be the board where we will put the letters we click from the virtual keyboard.

board = np.zeros((500, 500), np.uint8)
board[:] = 255

Outside of the loop we create two new variables, one called blinking_frames, and one text. The blinking_frames variable will keep track of the frames in a row in which the eyes are blinking, while text is going to contain all the letter that we will press when we blink our eyes.

blinking_frames = 0
text = ""

Then just after the blinking detection we add +1 to the blinking frames and when the blinking is detected for 5 frames in a row then we add the selected letter to the text.

if blinking_ratio > 5.7:
    cv2.putText(frame, "BLINKING", (50, 150), font, 4, (255, 0, 0), thickness=3)
    blinking_frames += 1
    frames -= 1

    if blinking_frames == 5:
        text += active_letter

else:
    blinking_frames = 0

Finally we display the text on the board.

cv2.putText(board, text, (10, 100), font, 4, 0, 3)

Now if we want to test our code, we simply need to run it and whenever we want to press a key on the virtual keyboard, we wait that the specific key lights up and then we close the eyes for around a second.
If the key appears on the white window “Board” then the code is working fine.

Blueprint

Learn to build Computer Vision Software easily and efficiently.

This is a FREE Workshop where I'm going to break down the 4 steps that are necessary to build software to detect and track any object.

Sign UP for FREE