Projects

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.

GitHub (opens in a new tab)

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

GestureGame Control
Open handAccelerate
FistBrake, then reverse if held
V-signDrift while steering
Index fingerNitro
Hand to the left or rightSteer

Working

  1. Webcam capture
  2. MediaPipe hand detection
  3. Bounding box extraction with configurable padding
  4. Crop, resize to 224Β Γ—Β 224 RGB and normalize
  5. CNN forward pass
  6. Softmax with confidence threshold (0.8)
  7. 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

GestureCNNA 224 by 224 colour image goes through three convolution blocks, becoming 112 by 112 by 32, then 56 by 56 by 64, then 28 by 28 by 128. That is flattened into 100,352 numbers, passed through a fully connected layer of 128 units with batch normalization, ReLU and dropout, and a final layer gives a score for each of five gestures. It has 12.94 million trainable parameters. Each convolution block is a 3 by 3 convolution with padding 1, batch normalization, ReLU and 2 by 2 max pooling with stride 2; the blocks have 960, 18,624 and 74,112 parameters, and the fully connected layers 12.85 million.12.94M trainable parameters224 Γ— 224 RGB input Β· 3 convolutional blocks (32 β†’ 64 β†’ 128 channels) Β· 2 fully connected layers Β· 5 classesBlock 1960 parametersConv2d 3 Γ— 3, 3 β†’ 32, padding 1BatchNorm2d (32)ReLUMaxPool 2 Γ— 2, stride 2224 Γ— 224 Γ— 3 β†’ 112 Γ— 112 Γ— 32Block 218,624 parametersConv2d 3 Γ— 3, 32 β†’ 64, padding 1BatchNorm2d (64)ReLUMaxPool 2 Γ— 2, stride 2112 Γ— 112 Γ— 32 β†’ 56 Γ— 56 Γ— 64Block 374,112 parametersConv2d 3 Γ— 3, 64 β†’ 128, padding 1BatchNorm2d (128)ReLUMaxPool 2 Γ— 2, stride 256 Γ— 56 Γ— 64 β†’ 28 Γ— 28 Γ— 128Fully connected12.85M parametersLinear 100,352 β†’ 128BatchNorm1d (128), ReLUDropout 0.5Linear 128 β†’ 5100,352 β†’ 128 β†’ 5Input224 Γ— 224 Γ— 3Block 1112 Γ— 112 Γ— 32Block 256 Γ— 56 Γ— 64Block 328 Γ— 28 Γ— 128Flatten100,352Fully connecteddropout 0.5128fistindex pointingno gestureopen handV-signOutput5 gestures

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
Accuracy over 20 epochs of trainingValidation accuracy starts at 93.4% after the first epoch, passes 99.5% by the fourth and stays between 99.4% and 99.8%, peaking at 99.8% in epoch 16, whose weights were kept. Training accuracy rises from 72.6% to 98.9%.70%80%90%100%15101520epochvalidation99.6%training98.9%
Training
Test-set predictions for each gestureAll 195 test images of fist, index pointing, no gesture and V-sign are predicted correctly. Of the 195 open hands, 192 are correct, one is predicted as index pointing and two as V-sign.predictedactualfistindexnoneopenVfist1950000index pointing0195000no gesture0019500open hand0101922V-sign0000195
Confusion Matrix

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.