This is the final tutorial of the project “Gaze controlled keyboard with Python and Opencv”.
The code is approximately 300 lines. I added the missing parts to the code, I improved the graphic of the keyboard and I made a few adjustments. I’m not going to do any coding in this tutorial, instead I will show you the result of the final project and on the video I’ll do a quick review of the entire code.
How does this work?
Once we run the script we see see three different windows containing respectively: the keyboard, a white board and the capture from the webcam.
The keyboard is divided in two parts, and before pressing each letter we need to select the part.
The left side contains the letters: Q, W, E, R, T, A, S, D, F, G, Z, X, C, V.
The right side contains the letters: Y, U, I, O, P, H, J, K, L, V, B, N, M and spacebar.
1) Choose keyboard’s side:
The webcam is controlling the position of your eyes, so if you want to select the left side you need to look on the left for around a second, for the opposite side you need to look on the right.

2) Press a key
Let’s supposed that we choosed the Right side.
Now we will see the keys contained on the right side.
To press a key we need to wait that the key lights up, and then we blink our eyes. We keep them closed until we hear a sound. That sound means that the key has been pressed.

3) Repeat step 1 and 2 to press a new key.
Each time we press a key, we need to repeat the step 1 and 2.
Each time the key is pressed, it is added to the white board. For example I typed the word “Hello” and below you see the result.

Final considerations:
This project was realised with the only purpose to teach you how to detect the eyes, theyr gaze and the blinking.
A few parts regarding the eye detection have been hardcoded to work specifically with my eyes and with a specific lightening condition and with a certain webcam.
This means that if some of these thress variables is different for you, the code might not work properly or might not work at all.
It’s of course possible to improve it for production environment.
If you’re the owner of a company and you want to develop this project further, feel free to contact me.

Hi there, I’m the founder of Pysource.
I’m a Computer Vision Consultant, developer and Course instructor.
I help Companies and Developers to build efficient computer vision software.
rows, cols, _ = frame.shape
AttributeError: ‘NoneType’ object has no attribute ‘shape’
Getting this error
just chnage the cap from 1 to 0
cap = cv2.VideoCapture(1) ———- > cap = cv2.VideoCapture(0)
Hi,
Before anything tanks.
I’d like to tell you how much i like your video tutorials.
I’m from Brazil and you explain your projects very well and, please, don’t change your way to teach.
Keep beeing the teacher you are.
IOError: [Errno 2] No such file or directory: ‘sound.wav’
The sound was missing. I corrected now the file and I added it.
Sergio your code and your explanations are great.
Thank you for share this with us.
I plan to use this software for a child who is disabled. Could you fix the link for the files.zip in order to download the code and auxiliary files?
It is not working now.
Regards
Hi, I’m slowly fixing all the posts in my blog and putting the code so it will take some times before I get to this post.
I’m going to send you the code via mail.
pl help me to run this code i want the dataset
Sergio Canu would you please give me the zip file ? I am from Bangladesh going to do my project on android based blink writing.
Hi,I copied your code.It is working but I have sound error.When I choose left ,coming left sound is non-stopping and right sound also.Please help me…
I’ve just added the sound. Please download the file again
Hey sergio , I am getting the same error as Rossy mentioned. Please help me
Don’t worry. I have already got the code. It is working now.
how to download zip file plz can u help me
Now it is available for the download
how to erase those texts again?
having issues with the dlib installation …..Using pycharm and also system python
ERROR: Command “‘C:\Users\12267\PycharmProjects\Tracker\venv\Scripts\python.exe’ -u -c ‘import setuptools, tokenize;__file__='”‘”‘C:\\Users\\12267\\AppData\\Local\\Temp\\pip-install-7ro0gxrn\\dlib\\setup.py'”‘”‘;f=getattr(tokenize, ‘”‘”‘open'”‘”‘, open)(__file__);code=f.read().replace(‘”‘”‘\r\n'”‘”‘, ‘”‘”‘\n'”‘”‘);f.close();exec(compile(code, __file__, ‘”‘”‘exec'”‘”‘))’ install –record ‘C:\Users\12267\AppData\Local\Temp\pip-record-jw8rjwoy\install-record.txt’ –single-version-externally-managed –compile –install-headers ‘C:\Users\12267\PycharmProjects\Tracker\venv\include\site\python3.6\dlib'” failed with error code 1 in C:\Users\12267\AppData\Local\Temp\pip-install-7ro0gxrn\dlib\
I made a tutorial to explain how to install dlib.
Type “Dlib” on the search bar of the blog to find it.
Hi Sir,
When I try this code i encounter the “dlib error” using python version 3.7.4.Can you please help me to solve this problem.And I tried to download the dlib but I don’t know how to solve the error.
Hi,
I had the same problem with python 3.7 so i switched to python 3.6 and it solved the error
I need this code
Can we use the cam that is to our laptop by default for this project.
Thank you very much. Useful lessons and examples. I’ve checked it on Ubuntu 18.04 with Python 3.7.5/Anaconda 4.6.11. Everything working but not too fast (~2-3 FPS on Pentium G640T 2.40GHz)
Greetings from POLAND.
What about the other keyboard functionalities
They’re not implemented, but it will be possible integrating some python library to interact with the keyboard commands.
My idea for this project is only a prototype, and of course it can be developed further and improved in many aspects.
Can you tell me how to calculate iris gaze shift.?i m following this
tutorial to calculate gaze ratio…
How do I create random lighting of the keyboard letter. Please help
I tried a lot of ways but didnt got solution
detector = dlib.get_frontal_face_detector()
AttributeError: module ‘dlib’ has no attribute ‘get_frontal_face_detector’
Hi, is there a way to add a ‘backspace’ button so that we could undo what we wrote in case we mistype anything?
Hi Abdul,
Yes and it’s simple. Usually when we add a letter we add a specific character to the string text.
If you add the backspace, you simply can remove the last character from the text when click on backspace.
After selecting the backspace:
text = text[:-1]
Sir, Thanks for your wonderful tutorial. It really helped me a lot to work on. But there’s a slight issue, can you help me with it?
When the blink detection is recognised on a particular letter, it gets paused over there and the letter isn’t getting printed. Any solution for it?
Hello, I would just like to know what is the algorithm used for this code
No words to thank you my friend for this project. Thank you for make the world a better place.