Real-Time Hand-Gesture-Based Game Control to Play Asphalt 8
Real-time, keyboard-free control of Asphalt 8 through hand gestures, using a CNN trained from scratch on a custom dataset.
The code, custom dataset (self-collected/curated), the trained weights and the training and evaluation logs can be found here.
Built with
- Python
- PyTorch
- MediaPipe
- OpenCV
- scikit-learn
- pynput
Demo
Controls
| Gesture | Game Control | |
|---|---|---|
| Open hand | Accelerate | |
| Fist | Brake, then reverse if held | |
| V-sign | Drift while steering | |
| Index finger | Nitro | |
| Hand to the left or right | Steer |
Working
- Webcam capture
- MediaPipe hand detection
- Bounding box extraction with configurable padding
- Crop, resize to 224Β ΓΒ 224 RGB and normalize
- CNN forward pass
- Softmax with confidence threshold (0.8)
- Gesture-to-keyboard action mapping
- Real-time inference on the Mac's GPU: The trained model is loaded once at startup and runs on the Apple M-series GPU through PyTorch's MPS backend. On other machines it uses CUDA if an NVIDIA GPU is available, or the CPU otherwise. Each frame takes under 40 ms (from webcam capture to key press), so the whole loop runs at about 30 fps.
- Decoupled gesture and steering: The CNN classifies the hand shape whereas steering comes from the position of the wrist, using MediaPipe Hands's hand landmarks, so steering is always available and works with any gesture.
- A no-gesture class in the output layer: The output layer has five outputs, one for each of the four gestures as well as a "no-gesture" class. This no-gesture class is necessary because without one, every frame (even a relaxed hand) would be forced to be classified as one of the four control gestures.
- Confidence threshold: If the CNN's top prediction has a softmax confidence below 0.8 (even for a control gesture), it is treated as no-gesture, so the blurry frames in between two gestures don't trigger anything.
- Smoothing across frames: When a gesture stops being detected, its key is not released straight away but stays pressed for about 3 more frames (roughly 100 ms). This makes it so that a single misclassified frame doesn't interrupt acceleration or nitro.
- Tracking key state: The controller keeps track of which keys are currently pressed and only sends a press or release to the OS when that state actually changes, rather than on every frame. Opposing controls are also handled, so accelerating releases the brake, and braking or drifting releases the accelerator.
- Releasing all keys when the hand stops being detected: As soon as the hand leaves the frame, every pressed key is released and the internal state is reset. The same happens when the program exits or is interrupted.
The Model
Data and Training
- Custom dataset of 6,500 images, 1,300 per gesture
- Collected with a custom MediaPipe-based capture tool, at 5 images per second
- Data captures 3 people, different backgrounds and lighting
- 70/15/15 stratified train/validation/test split
- Adam optimizer, learning rate 0.001, batch size 64, 20 epochs
- Cross-entropy loss, fixed random seed (42)
- Augmentation with random rotation (Β±15Β°), horizontal flips, brightness and contrast jitter (Β±40%) and random crops
- 99.8% validation accuracy
- Accuracy of 99.79% on 975 held-out test images
Running It
git clone https://github.com/arush-3009/real-time-hand-gesture-game-controller.git
cd real-time-hand-gesture-game-controller
pip install -r requirements.txt
python main.py
Run it with the game open. The controller's window stays on top while the game receives the keys, and pressing Q in that window stops it. python -m ml.train retrains the model, python -m ml.evaluate scores it on the test set again, and python -m data.collect_data opens the capture tool.