You can see that the keyboard contains only 26 alphabets and a spacebar and I am wearing yellow paper on my finger with which I am trying to simulate a keyboard click.
- A computer with a good camera
- A yellow (though any other color can be used) piece of paper to be worn in a finger (for color segmentation).
- OpenCV for Python 3
- PyAutoGui for Python 3
- Python 3
- Text Editor like Sublime Text 3 or Atom
- A little bit of knowledge in Maths
Steps that we are going to take
- Get the corner coordinates of each and every key that is used to design the keyboard.
- Write a function to recognize the click.
Let's start coding...
- Every key needs to be of a fixed width and height
- Square keys look much better than any other shape
- We need to have equal margins in both sides i.e and left and right side of each row
- The total width of a row should not exceed the width of the frame. The same goes for the height.
- Key Label
- Top Left Corner coordinate
- Bottom Right Corner coordinate
- Center coordinate
hsv_upper and hsv_lower are automatically initialized if we use the range-detector.py which I have included in the repo. The easiest way to use it is to put the yellow paper in front of the camera and then slowly increasing the lower parameters(H_MIN, V_MIN, S_MIN) one by one and then slowly decreasing the upper parameters (H_MAX, V_MAX, S_MAX). When the adjusting has been done you will find that only the yellow paper will have a corresponding white patch and rest of the image will be dark.
With our global variables all set we can now proceed to defining the function that will return the above said properties of all the keys. I have named the function as get_keys(). Before going into much details met me first discuss the steps that I have taken.
- Since the max number of keys in one row is 10 i.e the first row we can divide the width by 10 to get the key_width.
- So total width of any row is (key_width * total no. of keys in that row)
- Hence total width of 1st row i.e row1_key_width = key_width * 10.
- Similarly for the 2nd row it is key_width * 9, third row it is key_width * 7, and for the space bar I have decided to keep it as key_width * 5.
- To determine the corner coordinates of the first key of the first row i.e the "q" key we do the following:
- We know that the height of each key is key_width. Since we have 4 rows the total height i.e height of the keyboard is 4 * key_width.
- To keep equal top and bottom margins for the keyboard we can do the following operation (height - 4 * key_width) / 2.
- Let us set the above value to y1.
- And x1 is set to 0, so that the first row begins from the left border of the frame
- Hence (x1, y1) gives us the top left corner of the "q" key
- Since the keys are of square shape then the opposite corner coordinate will be (x2, y2) = (key_width + x1, key_width + y1)
For the second row we can proceed with similar steps with the only difference of (x1, y1). In this case x1 = (row1_key_width - row2_key_width) / 2 and y1 = key_width + y1. We will have a similar case for third row.
The function will look like:
- The image object
- The position/coordinate of the click gesture
Now let us design the main() function. The main() function does the following tasks:-
- Recognizes the yellow paper
- Gets the corner coordinates of every key by calling get_keys()
- Detects the center position of the yellow paper
- If there is little movement of the yellow paper it ignores it
- If the movement is high the click gesture is ignored
- If a valid click gesture is formed it calls do_keypress()
So the full code looks like this:-
And, that's about it. At the beginning of the tutorial I asked that if you can tell me if this a Augmented Reality project. Well I ask that again. Comment it down below. 2 of my friends said that it is a Augmented Reality project. One of them said it is not. I am not sure who to believe. Get the full code here. You can find me on-