A virtual mouse based on hand movement using webcam, google MediaPipe, python mouse and openCV in python
Before testing the virtual mouse, make sure you have saved and closed your important programs, the ghost click👻 can be very dangerous, use with full caution❗
- Place right hand index finger in pointer box and it will translate your mouse position to your finger location
- Place left hand index and thumb fingers left click box, and then make it close the distance and release it to do a left click, do it twice to double left click
- Place left hand index and thumb fingers right click box, and then make it close the distance and release it to do a right click
- Place left hand index and thumb finger in left or right click box, and then make it close the distance and hold it to do a mouse drag, and release the hold it to drop
- Only able to detect a pair of hand per screen (if you show 2 right hand on screen, it will be recognized as one hand, so the coordinate will jump from one hand to another)
- Your clothes and background might be classified as a "Hand" too (may lead to ghost click👻), so better set the
IS_CLICK_HAND_ON
andIS_POINTER_HAND_ON
toFalse
to test your environment first - Only support 1 screen at one time, at 16:9 resolution
- Python >= 3.7.9
- Webcam
- Install python (you can refer to Python site)
- (Optional) Initiate venv by running
python -m venv ac_env
- Install library via pip from requirements.txt by running
python -m pip install requirements.txt
- Adjust the settings located at the
./settings/
folder (theIS_CLICK_HAND_ON
andIS_POINTER_HAND_ON
value, you need to set the value toTrue
to move your mouse, or keep it toFalse
if you want to check for ghost click first) - Run with
python main.py
You are good to go by just following the installation, no need for additional works
Makesure to use XOrg for the display server (Ubuntu 21.04 use wayland, you need to change it first) to enable the mouse library
Not yet tested
Because the MediaPipe usually return unstable coordinate of a finger tip, and mouse is usually used with high stability, a stabilizer function is needed. This project have 4 options of stabilizers, you can choose which one you want to use and other value in ./settings/stabilizer_settings.py
. The stabilizer function will have 3 input parameter, the finger_tip_x
and finger_tip_y
list with MINIMUM_TIP_POINT_TO_CALCULATE_CENTROID
numbers of points, and last_position
(the last tip coordinate). finger_tip_x
and finger_tip_y
list use FIFO mechanism to save value, updated each frame, with maximum number of MAX_TIP_POINTS
values.
This one is created to keep the persistance of the tip when the finger stopped, but keep it moving when finger moves
- For the first
MINIMUM_TIP_POINT_TO_CALCULATE_CENTROID
coordinate, directly move to the current finger coordinate - If the
finger_tip_x
list already haveMINIMUM_TIP_POINT_TO_CALCULATE_CENTROID
number of coordinates, then calculate the centroid - Calculate the distance between next coordinate centroid and the last position coordinate (
last_position
) if the distance is aboveMAXIMUM_CENTROID_1_DISTANCE
, then next coordinate is the centroid, else the next coordinate is same with last coordinate
This one is created to keep the persistance of the tip when the finger stopped, but keep it moving when the finger moves, faster than Calculate Centroid 1, but less persistance
- For the first
MINIMUM_TIP_POINT_TO_CALCULATE_CENTROID
coordinate, directly move to the current finger coordinate - Calculate centroid from saved point in finger_tip_x and finger_tip_y list, except the last value (current finger position)
- Calculate the distance between centroid and current finger coordinate
- If the distance passthrough
MAXIMUM_CENTROID_2_DISTANCE
, then use the current finger coordinate as next coordinate, also refill all value in finger_tip_x and finger_tip_y with current finger coordinate - If the distance doesn't passthrough, then use the
last_position
value
Source : Youtube video
Slowly move the point by dividing distance between last coordinate and current finger coordinate with SMOOTHING_POINT
, and add it to the last coordinate so the next coordinate will not directly jump to the current finger coordinate, but slowly moving the point towards the current finger coordinate
Same with Smooth Move, but it will use the last two coordinate and current finger coordinate to calculate the angle between the three point, if it passthrough the MINIMUM_TIP_POINT_TO_CALCULATE_ANGLE
then the Slow Move stabilizer calculated, else it will directly jump to the current finger coordinate
Directly jump to current finger location