Skip to content

Commit

Permalink
Merge pull request #84 from nepal143/Gesture-Based-Game-Controller
Browse files Browse the repository at this point in the history
Gesture based game controller
  • Loading branch information
king04aman authored Oct 24, 2024
2 parents 3bc1c37 + 6f97fe8 commit 2d8f21e
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 167 deletions.
145 changes: 0 additions & 145 deletions 2D-Platformer/Platformer.py

This file was deleted.

22 changes: 0 additions & 22 deletions 2D-Platformer/Readme.md

This file was deleted.

Binary file removed 2D-Platformer/requirements.txt
Binary file not shown.
Binary file removed 2D-Platformer/runtime.txt
Binary file not shown.
90 changes: 90 additions & 0 deletions Gesture-Based-Game-Controller/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import cv2
import mediapipe as mp
import numpy as np
import time

# Initialize MediaPipe hands module
mp_hands = mp.solutions.hands
hands = mp_hands.Hands()
mp_drawing = mp.solutions.drawing_utils

# Gesture mappings
GESTURES = {
"forward": "Move Forward",
"backward": "Move Backward",
"left": "Move Left",
"right": "Move Right",
"up": "Jump"
}

# Define a function to calculate the direction based on landmarks
def detect_direction(landmarks):
# Get coordinates of wrist and index finger tip
wrist = landmarks[0] # Wrist coordinates
index_finger_tip = landmarks[8] # Index finger tip

# Calculate relative positions of index finger to wrist
x_diff = index_finger_tip.x - wrist.x # Difference in X-axis
y_diff = index_finger_tip.y - wrist.y # Difference in Y-axis

# Use thresholds to determine the direction
if abs(x_diff) > abs(y_diff): # Horizontal movement dominates
if x_diff > 0.1: # Index finger is to the right of the wrist
return "right"
elif x_diff < -0.1: # Index finger is to the left of the wrist
return "left"
else: # Vertical movement dominates
if y_diff > 0.1: # Fingers are below wrist
return "backward"
elif y_diff < -0.1: # Fingers are above wrist
return "up"

# If no significant difference in X or Y, assume pointing forward
return "forward"

# Video capture for hand gesture recognition
cap = cv2.VideoCapture(0)
prev_time = 0 # To implement delay between gesture changes
delay_interval = 1.0 # 1 second delay between actions

while True:
ret, frame = cap.read()
if not ret:
break

# Flip the frame horizontally for natural movement
frame = cv2.flip(frame, 1)
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

# Detect hands
results = hands.process(frame_rgb)

landmark_list = []

# If hand landmarks are detected
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)

# Collect hand landmark data
for lm in hand_landmarks.landmark:
landmark_list.append(lm)

# Detect direction based on the landmarks
direction = detect_direction(landmark_list)

# Check for time delay between actions
current_time = time.time()
if current_time - prev_time > delay_interval:
print(GESTURES[direction]) # Output corresponding action
prev_time = current_time

# Display the frame with landmarks
cv2.imshow('Hand Gesture Recognition', frame)

# Quit if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()
25 changes: 25 additions & 0 deletions Gesture-Based-Game-Controller/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Hand Gesture Control Using Computer Vision

This project uses computer vision and machine learning to detect hand gestures and map them to simple game controls like moving forward, left, right, jumping, and moving backward.

The project uses **OpenCV** and **MediaPipe** for hand landmark detection and gesture recognition.

## Features

- Detects hand gestures in real-time.
- Maps gestures to game controls:
- **Move Forward**: Fingers pointing towards the screen.
- **Move Left**: Fingers pointing left.
- **Move Right**: Fingers pointing right.
- **Move Backward**: Fingers pointing downward.
- **Jump**: Fingers pointing upward.
- Implements a delay between actions to prevent rapid switching of game controls.

## Requirements

- Python 3.7 or higher
- OpenCV
- MediaPipe
- NumPy


3 changes: 3 additions & 0 deletions Gesture-Based-Game-Controller/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
opencv-python==4.8.0.74
mediapipe==0.10.0
numpy==1.24.2
1 change: 1 addition & 0 deletions Gesture-Based-Game-Controller/runtime.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python-3.12.7

0 comments on commit 2d8f21e

Please sign in to comment.