-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from nepal143/Gesture-Based-Game-Controller
Gesture based game controller
- Loading branch information
Showing
8 changed files
with
119 additions
and
167 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python-3.12.7 |