Virtual Keyboard – Gaze controlled keyboard with Python and Opencv p.5
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) width_text, height_text = text_size, text_size 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")
Hi there, I’m the founder of Pysource.
I’m a Computer Vision Consultant, developer and Course instructor.
I help Companies and Freelancers to easily and efficiently build Computer Vision Software.
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
Detect and Track any Object (Full Videocourse)
You can Build Software to detect and track any Object even if you have a basic programming knowledge.