A real-time desktop application that maps hand gestures from a webcam to emojis using MediaPipe and OpenCV.
Live demonstration showing MediaPipe hand landmarks, gesture detection, and real-time classification
Note: Your camera may mirror the image, so the app's "right hand" detection might actually be your left hand from your perspective.
- Real-time gesture recognition from webcam input
- 5 gesture types: thumbs up, thumbs down, open palm, peace sign, fist
- Emoji mapping: 👍, 👎, ✋, ✌️, ✊
- Hand detection using MediaPipe handedness (note: camera may mirror your image)
- Rule-based classification using normalized landmark geometry
- Hysteresis filtering to reduce gesture flicker
- Live video display with emoji overlay
- Gesture legend showing all supported gestures
- Hotkey testing (1-5 keys to preview gestures)
- CLI demo mode for quick testing without UI
- Python 3.10 or higher
- Webcam/camera device
- Windows, macOS, or Linux
-
Clone or download the project
cd hand_gesture_detector -
Create virtual environment
python -m venv .venv
-
Activate virtual environment
Windows:
.venv\Scripts\activate
macOS/Linux:
source .venv/bin/activate -
Install dependencies
pip install -r requirements.txt
Option 1: GUI Application (requires PyQt6)
python main.pyOption 2: No-GUI Application (OpenCV display)
python main_no_gui.pyControls:
- Show your hand to the camera (note: camera may mirror your image)
- Gestures will be displayed as emojis in the top-left corner
- Press q to quit
- Press r to reset gesture classifier
- Press 1-5 keys to preview gesture overlays (GUI version only)
For quick testing without the GUI:
python demo_cli.pyThis will print emojis to stdout as gestures are detected.
Run unit tests for gesture classification:
python test_gesture_rules.py| Gesture | Emoji | Description |
|---|---|---|
thumbs_up |
👍 | Thumb extended upward, other fingers folded |
thumbs_down |
👎 | Thumb extended downward, other fingers folded |
open_palm |
✋ | All fingers extended |
peace |
✌️ | Index and middle fingers extended, others folded |
fist |
✊ | All fingers folded |
The app uses rule-based classification with the following logic:
- Thumb: Compares tip-to-MCP distance vs MCP-to-CMC distance
- Other fingers: Compares tip-to-PIP distance vs PIP-to-MCP distance
- Threshold: 0.8 for thumb, 0.7 for other fingers
- Thumb up: Tip Y-coordinate < wrist Y-coordinate AND tip X-coordinate > index MCP X-coordinate
- Thumb down: Tip Y-coordinate > wrist Y-coordinate AND tip X-coordinate > index MCP X-coordinate
- Thumbs up: Thumb extended upward, all other fingers folded
- Thumbs down: Thumb extended downward, all other fingers folded
- Peace: Index and middle extended, ring and pinky folded
- Open palm: All fingers extended
- Fist: All fingers folded
- Uses a 3-frame window to prevent rapid gesture changes
- Only changes gesture if consistent for 3 consecutive frames
- Reduces flicker and improves stability
Edit the EmojiMapper class in src/utils.py:
custom_mapping = {
'thumbs_up': '👍',
'thumbs_down': '👎',
'open_palm': '✋',
'peace': '✌️',
'fist': '✊'
}For better performance on slower systems:
-
Reduce frame size in
src/camera.py:camera = Camera(width=480, height=360, fps=15)
-
Lower detection confidence in
src/hand_model.py:hand_model = HandModel(min_detection_confidence=0.5)
-
Increase hysteresis window in
src/gesture.py:self.history_size = 5 # More stable but slower response
hand_gesture_detector/
├── main.py # Main application entry point
├── demo_cli.py # CLI demo without UI
├── test_gesture_rules.py # Unit tests for gesture classification
├── requirements.txt # Python dependencies
├── README.md # This file
└── src/
├── camera.py # Webcam capture and management
├── hand_model.py # MediaPipe hand detection
├── gesture.py # Rule-based gesture classification
├── ui.py # PyQt6 GUI and video display
└── utils.py # Emoji mapping, smoothing, logging
- opencv-python: Computer vision and camera handling
- mediapipe: Hand detection and landmark extraction
- numpy: Numerical computations
- PyQt6: GUI framework
- Ensure camera is not being used by another application
- Try different camera IDs (0, 1, 2) in
Camera(camera_id=1) - Check camera permissions on macOS/Linux
- Reduce frame resolution in
Camerainitialization - Lower target FPS
- Close other applications using the camera
- Ensure good lighting conditions
- Keep hand clearly visible in frame
- Note: Camera may mirror your image, so "right hand" in the app may be your left hand
- Maintain consistent hand position
The app creates application logs:
hand_gesture_detector.log: Application logs
