Skip to content

About

A real-time desktop application that maps hand gestures from a webcam to emojis using MediaPipe and OpenCV.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

10 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hand Gesture Detector

A real-time desktop application that maps hand gestures from a webcam to emojis using MediaPipe and OpenCV.

Hand Gesture Recognition Demo

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.

Features

  • 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

Setup

Prerequisites

  • Python 3.10 or higher
  • Webcam/camera device
  • Windows, macOS, or Linux

Installation

  1. Clone or download the project

    cd hand_gesture_detector
  2. Create virtual environment

    python -m venv .venv
  3. Activate virtual environment

    Windows:

    .venv\Scripts\activate

    macOS/Linux:

    source .venv/bin/activate
  4. Install dependencies

    pip install -r requirements.txt

Usage

Main Application

Option 1: GUI Application (requires PyQt6)

python main.py

Option 2: No-GUI Application (OpenCV display)

python main_no_gui.py

Controls:

  • 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)

CLI Demo

For quick testing without the GUI:

python demo_cli.py

This will print emojis to stdout as gestures are detected.

Testing

Run unit tests for gesture classification:

python test_gesture_rules.py

Gesture Recognition

Supported Gestures

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

How It Works

The app uses rule-based classification with the following logic:

Finger Extension Detection

  • 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 Direction Detection

  • 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

Gesture Classification Rules

  1. Thumbs up: Thumb extended upward, all other fingers folded
  2. Thumbs down: Thumb extended downward, all other fingers folded
  3. Peace: Index and middle extended, ring and pinky folded
  4. Open palm: All fingers extended
  5. Fist: All fingers folded

Hysteresis Filtering

  • Uses a 3-frame window to prevent rapid gesture changes
  • Only changes gesture if consistent for 3 consecutive frames
  • Reduces flicker and improves stability

Customization

Changing Emoji Mappings

Edit the EmojiMapper class in src/utils.py:

custom_mapping = {
    'thumbs_up': '👍',
    'thumbs_down': '👎', 
    'open_palm': '✋',
    'peace': '✌️',
    'fist': '✊'
}

Performance Optimization

For better performance on slower systems:

  1. Reduce frame size in src/camera.py:

    camera = Camera(width=480, height=360, fps=15)
  2. Lower detection confidence in src/hand_model.py:

    hand_model = HandModel(min_detection_confidence=0.5)
  3. Increase hysteresis window in src/gesture.py:

    self.history_size = 5  # More stable but slower response

File Structure

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

Dependencies

  • opencv-python: Computer vision and camera handling
  • mediapipe: Hand detection and landmark extraction
  • numpy: Numerical computations
  • PyQt6: GUI framework

Troubleshooting

Camera Issues

  • 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

Performance Issues

  • Reduce frame resolution in Camera initialization
  • Lower target FPS
  • Close other applications using the camera

Gesture Detection Issues

  • 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

Logging

The app creates application logs:

  • hand_gesture_detector.log: Application logs

About

A real-time desktop application that maps hand gestures from a webcam to emojis using MediaPipe and OpenCV.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages