I Turned My Hands Into a Virtual Steering Wheel!!

by Dynamic Innovator in Circuits > Software

7 Views, 0 Favorites, 0 Comments

I Turned My Hands Into a Virtual Steering Wheel!!

I Turned My Hands Into a Virtual Steering Wheel!!

What Is This Project?

Imagine you're playing a car racing game… but instead of using a joystick or keyboard, you just move your hands in front of a camera to drive the car! This project makes that possible using a webcam, Python.

You can:

  1. 👐 Turn your hands left or right to steer the car.
  2. 👍 Move your thumb down to speed up or slow down. It’s like magic! But it’s actually made using something called Computer Vision.

This project is designed for complete beginners and fun enthusiasts who want to control a car using hand gestures with a webcam. The program uses MediaPipe to track your hand, calculates the angle between your wrist, thumb, and index finger, and based on that angle, it sends keyboard commands to the game.

No fancy equipment needed—just a webcam, your hand, and a game that uses the keyboard (like pressing A/D to steer).

Let’s dive into how it works in the simplest way possible.

Supplies

Here’s everything you need:

  1. ️ A laptop or desktop computer (Windows, macOS, or Linux)
  2. A webcam (built-in or USB)
  3. Python installed (version 3.7 or later)
  4. Python packages:
  5. opencv-python
  6. mediapipe
  7. pyautogui


Install Python

download (4).jpg


If you haven’t already:

  1. Go to https://www.python.org/
  2. Click Download Python
  3. Install it like any other app


Install the Libraries

download.png
images.png
1_3fophfrHcwuFWP1VdOtdTg.jpg

These are the special helpers for Python:

  1. Open Command Prompt (Windows) or Terminal (Mac/Linux)
  2. Type the following and press Enter:
pip install opencv-python mediapipe pyautogui

What Are These Libraries?

Let’s understand them like superheroes:

  1. 🧠 Mediapipe – This smart tool sees your hands and finds points like your thumb, wrist, and fingers.
  2. 👀 OpenCV – It helps us use the webcam and draw things on the screen.
  3. ⌨️ PyAutoGUI – It presses keys like W, A, S, D for you.


Copy the Code

this took me days to figuring out and i am giving you the code for free!!!

Here’s the magic program. You can open Notepad, or better: install VS Code.

Downloads

Explaination of Code and Important Settings in Code Before Using It


🔍 Explaining the Key Code Lines

✅ Importing Tools

import cv2 # To use webcam and show visuals
import mediapipe as mp # To detect hands
import math # To calculate angles
import pyautogui # To press keyboard buttons automatically
import time # To handle timing for button presses

✅ Initial Settings

hands = mphands.Hands(...)
  1. This initializes MediaPipe Hands to detect up to 2 hands.
  2. It finds where your hands are and tracks their movement.

✅ Detecting Angle Between Hands

def calculate_angle(x1, y1, x2, y2):
...
  1. Calculates the angle between your left and right hands.
  2. This tells whether you're turning left, right, or going straight.

🧠 Important Settings :

🛠 turn_sensitivity = 0.01

  1. This was used in a previous version to make turning more or less sensitive.
  2. BUT in your current code, it's not used anymore, so we can remove it or ignore it.

🔁 straight_threshold = 6

This value decides how much your hand angle must change before it counts as turning.

  1. If the angle is less than -6, it means turn left (A key).
  2. If the angle is more than +6, it means turn right (D key).
  3. If the angle is between -6 and +6, it means go straight (do nothing).

This helps avoid small hand shakes being treated as turns.

⏱️ tap_duration = 0.01

This sets how quickly to release the A or D key after tapping it.

  1. 0.01 means the key is held down for just 0.01 seconds (very short).
  2. This makes the car turn in small steps, so you don’t turn too much with a small hand movement.

If you want smoother, longer turns, increase this value to something like 0.05.

🖐️ Detecting Thumb Up or Down

def is_thumb_up(hand_landmarks):
return hand_landmarks.landmark[4].y < hand_landmarks.landmark[3].y
  1. Checks if the tip of the thumb is above the second joint (on the screen).
  2. This is used to detect:
  3. Left thumb up = stop accelerating
  4. Right thumb up = stop braking

🚗 Controls Using Hands

🟢 Accelerate (W key)

if not is_thumb_up(left_hand):
pyautogui.keyDown('w')
  1. If your left thumb is down, press the W key (accelerate).
  2. If your left thumb goes up, release the W key (stop).

🔴 Brake (S key)

if not is_thumb_up(right_hand):
pyautogui.keyDown('s')
  1. If your right thumb is down, press the S key (brake).
  2. If your right thumb goes up, release the S key (stop braking).

↔️ Turn Left or Right (A and D keys)

if angle < -straight_threshold:
pyautogui.keyDown('a')
# release after tap_duration
  1. Angle < -6: Press A (turn left)
  2. Angle > +6: Press D (turn right)

These are short taps, so the car turns briefly and doesn't stay turning forever.

🧹 At the End

pyautogui.keyUp('w')
pyautogui.keyUp('s')
pyautogui.keyUp('a')
pyautogui.keyUp('d')
  1. When you exit the program, it releases all keys to avoid the car getting stuck going forward or turning.

✅ Final Thoughts

🎮 What You Can Control


Important Settings in Code Before Using It

🧠 Important Settings :

🛠 turn_sensitivity = 0.01

  1. This was used in a previous version to make turning more or less sensitive.
  2. BUT in your current code, it's not used anymore, so we can remove it or ignore it.

🔁 straight_threshold = 6

This value decides how much your hand angle must change before it counts as turning.

  1. If the angle is less than -6, it means turn left (A key).
  2. If the angle is more than +6, it means turn right (D key).
  3. If the angle is between -6 and +6, it means go straight (do nothing).

This helps avoid small hand shakes being treated as turns.

⏱️ tap_duration = 0.01

This sets how quickly to release the A or D key after tapping it.

  1. 0.01 means the key is held down for just 0.01 seconds (very short).
  2. This makes the car turn in small steps, so you don’t turn too much with a small hand movement.

If you want smoother, longer turns, increase this value to something like 0.05.