Virtual Mouse
.
AI Virtual Mouse with Hand Gesture Detection
**Date: **2022-06-20
Author: Hee-Yun Kang, Woo-Ju So
Github: hyKangHGU/VirtualMouse
Demo Video: AI Virtual Mouse with Hand Gesture Detection
1. Introduction
In this project, we can control the mouse cursor on the monitor by recognizing the hand gesture on the webcam. This shows that you can control your PC by replacing the mouse and touchpad. In addition, the lecturer will be able to take a presentation in front of the camera only by moving their hands.
Goal
The goal of this project is to perform all basic operations of mouse with only hand gestures. The following are basic operations of mouse.
Left Click
Right Click
Double Click
Drag & Drop
Scroll
Hand can be recognized by using MediaPipe's hand landmakr model. As shown in the figure below, landmark information for each joint of the hand is available.
2. Requirement
Hardware
Webcam
Software Installation
Python 3.7.x or python 3.8.x
opencv-python
mediapipe
autopy
pyautogui
3. Tutorial Procedure
Setting up
Anaconda Install
Create virtual environment
Run anaconda prompt in administrator mode.
Python version == 3.7.13 or 3.8.13
Download Files
hyKangHGU/VirtualMouse (github.com)
Install Libraries
Solution for error
After downloading files and install libraries, you can see execution error. This error means that the protobuf package should be downgraded to 3.20.x.
Code Desription
VirtualMouse.py : Main Program.
HandTrackingModule.py : Class and functions that recognize hands using MediaPipe are defined. (It is referenced by youtube link)
Defines.py : Constant variables related to setting are defined.
MouseOperation.py : Functions related to mouse operation are defined.
The overall flow of the program is in the order of initialization, hand tracking, mode update, mouse operation, and image display.
Initialization
In first step, the necessary modules are imported. Then the webcam and handDetector class are initialized. 'handDetector' class is defined at HandTrackingMoudule.py. The number of hands detected on the webcam is determined by 'maxHands'.
Hand Tracking
From here, it is calculated every frame of the webcam. A hand is detected in an image obtained from webcam. Then landmarks and a bounding box of the hand are obtained. And, it is checked whether the landmarks of the hand are detected or not.
Mode Update
If the hand is detected, the mode is updated. The information on hand landmarks is utilized of mode change. The 'fingers' variable means whether each finger is stretched or folded. For example, fingers[0] = 1 means the thumb is up. The location information of each landmark is obtained through 'tipsPosision' function. It is used for detail condition of mode change. Some modes may require the distance between landmarks. The location information is used for calculating the distance. The landmark number is described on image in introduction. Therefore, the modes are determined by each finger's state and the distance between landmarks. The flowchart for mode selection is shown in the following figure.
Mouse Operation
Mouse cursor moving
Moving Mode | Drag Down Mode |
The mouse cursor can be moved during the pre-click and click-down state. As shown in the figure above, when only the second finger is up, the mouse moves without clicking. On the other hand, drag down mode is performed in click-down state and can move until drop mode is executed with click-up.
Left Click
Left-Click Standby Mode | Left-Click Mode |
Left-click standby mode stops the mouse movement before executing a left-click. For detecting the left-click mode, the distance condition between each joint of the 2nd finger is used. In addition, if the condition is continuously satisfied, the left-click is also continuously executed. This problem is solved by recording the state in the previous frame through a variable called 'pMode'.
Right Click
Right-Click Standby Mode | Right-Click Mode |
The right-click mode is implemented same to the left-click mode except for the shape of the fingers.
Drag Up
Drag Down Mode | Drag Up Mode |
Drag-up mode is a function to escape from drag mode. In drag mode, the cursor can be moved in a click-down state, and stopped in a click-up state. Drag-up mode executes a click-up.
Scroll
Scroll Standby Mode | Scroll |
In the scroll standby state, the current mouse cursor position is saved. And, in the scroll mode, the screen moves as much as the hand moves in the y-axis direction. The scroll speed is difference in the current hand position compared to the saved mouse cursor position in the scroll standby state.
Middle Finger Filter
Since someone can offend the other person by swearing with their fingers, blur processing is performed to prevent this.
None
This is a nothing mode. This is a mode that prevents the mouse from moving and not doing any action.
Image Display
4. Results and Analysis
CPS Test
CPS means click per second. CPS can be measured in this website. We were able to compare the click speed of a general mouse and a virtual mouse. The test counts the number of clicks in 5 seconds. The results for 5 times are as follows.
![general_mouse_CPS TEST](.\images\general_mouse_CPS TEST.png) | ![virtual_mouse CPS Test](.\images\virtual_mouse CPS Test.png) |
---|---|
General Mouse | Virtual Mouse |
The average CPS of general mouse is 5.56. On the other hand, the average CPS of the virtual mouse is 4.16. This means that the virtual mouse can click at about 75% of the speed of a general mouse.
The mole game was developed to practice mouse movements and clicks. Using this game, we compared a general mouse and a virtual mouse.
![mole game](.\images\mole game.png)
![general_mouse mole_game](.\images\general_mouse mole_game.png) | ![virtual_mouse mole_game](.\images\virtual_mouse mole_game.png) |
---|---|
General Mouse | Virtual Mouse |
For a general mouse, a maximum score of 609 was obtained, and for a virtual mouse, a maximum of 200 was obtained. It means that the movement and click accuracy of the virtual mouse is about 3 times lower than that of a general mouse.
Evaluation by Function
Left / Right / Double click
It works well without any functional problems, and unwanted multiple clicks are prevented.
Scroll
It works well, but lowers the FPS
Drag & Drop
It works well.
Further Work
Wheel click
A general mouse has a wheel click function. However, that function has not yet been implemented in this project.
FPS down problem at Scroll
Although the scroll function is well implemented, there is a problem that the FPS is lowered.
Stopping Error
Stopping error is generated when top bar of the image window is clicked by virtual mouse. We haven't tried it, but here's a solution to solve this. The whole program is composed of two main program files as each independent. One is to control the virtual mouse from image processing. The other thing is to show the image in real time.
Robustness for Each Operation
It is implemented to perform specific actions with predefined hand gestures. Even if the gesture is the same in the eyes of a human, it may be recognized as a different gesture. For example, even though it is cursor moving mode, it may be recognized as left click waiting mode according to a change in posture. It should be implemented to minimized the interference between each operation by giving additional conditions.
Moving Mode
Fault Recognition
Left Click Waiting Mode
Brick Breaker Game
Among the games with the mouse movements, there is a brick breaker game. If you link this game with the virtual mouse, it will be interesting content.
Two Hands Recognition
More functions can be implemented by recognizing both hands. If the recognition of the two hands movements is further developed, it is possible to create a model that can interpret sign language.
5. References
Hands - mediapipe (google.github.io)
(2) AI Virtual Mouse | OpenCV Python | Computer Vision - YouTube
Last updated