Skip to content

Gaze controlled keyboard · Tutorials

Virtual Keyboard – Gaze controlled keyboard with Python and Opencv p.5

Access community, courses and source codes
Logo

AI Vision Academy

Access the code of this tutorial, computer vision courses and an exclusive community on AI Vision Academy

  • Access to over 50+ source codes from Pysource.com/blog
  • Dedicated video courses about computer vision
  • Access to an exclusive community of professionals
  • Real-World AI Projects – Get hands-on experience building practical AI Computer Vision solutions with a structured path.
  • Monthly Coaching Calls – get support and any of your questions answered

Subscribe to our newsletter to learn more

We’re going to learn in this tutorial how to create a virtual keyboard using Opencv with Python.

The idea is to display the Keys on the screen and light them up one at time. Once the key we want to press is lighted up, we simply would need to close our eyes and the key will be pressed.

To start creating the virtual keyboard, we first create an empty black window.
We call it keyboard and using numpy we simply create a black image with the size of 1500 pixels of width and 1000 pixels of height.

import cv2
import numpy as np

keyboard = np.zeros((1000, 1500, 3), np.uint8)

Then we can add the keys. Each key is simply a rectangle containing some text. So we define the sizes and we draw the rectangle.

# Keys
width = 200
height = 200
th = 3 # thickness
cv2.rectangle(keyboard, (x + th, y + th), (x + width - th, y + height - th), (255, 0, 0), th)

Inside the rectangle now we put the letter. So we define the sizes and style of the text and we center it.

# Text settings
font_letter = cv2.FONT_HERSHEY_PLAIN
font_scale = 10
font_th = 4
text_size = cv2.getTextSize(text, font_letter, font_scale, font_th)[0]
width_text, height_text = text_size[0], text_size[1]
text_x = int((width - width_text) / 2) + x
text_y = int((height + height_text) / 2) + y
cv2.putText(keyboard, text, (text_x, text_y), font_letter, font_scale, (255, 0, 0), font_th)

Of course we are going to use the code inside a function, so that we can call it each time we want to draw a letter.

On this function we will simply pass the letter and it’s position and it will appear on the screen.

letter(0, 0, "A")
letter(200, 0, "B")
letter(400, 0, "C")

Join the discussion