From 032f8e7542f3f7a878b9532c1497f5c8e58cde88 Mon Sep 17 00:00:00 2001 From: nepla143 Date: Thu, 24 Oct 2024 23:29:33 +0530 Subject: [PATCH 1/2] added Gesture Based Game Controller --- Gesture-Based-Game-Controller/main.py | 90 +++++++++++++++++++ Gesture-Based-Game-Controller/readme.md | 25 ++++++ .../requirements.txt | 3 + Gesture-Based-Game-Controller/runtime.txt | 1 + 4 files changed, 119 insertions(+) create mode 100644 Gesture-Based-Game-Controller/main.py create mode 100644 Gesture-Based-Game-Controller/readme.md create mode 100644 Gesture-Based-Game-Controller/requirements.txt create mode 100644 Gesture-Based-Game-Controller/runtime.txt diff --git a/Gesture-Based-Game-Controller/main.py b/Gesture-Based-Game-Controller/main.py new file mode 100644 index 0000000..77e2b5b --- /dev/null +++ b/Gesture-Based-Game-Controller/main.py @@ -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() diff --git a/Gesture-Based-Game-Controller/readme.md b/Gesture-Based-Game-Controller/readme.md new file mode 100644 index 0000000..1384150 --- /dev/null +++ b/Gesture-Based-Game-Controller/readme.md @@ -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 + + diff --git a/Gesture-Based-Game-Controller/requirements.txt b/Gesture-Based-Game-Controller/requirements.txt new file mode 100644 index 0000000..c26d39e --- /dev/null +++ b/Gesture-Based-Game-Controller/requirements.txt @@ -0,0 +1,3 @@ +opencv-python==4.8.0.74 +mediapipe==0.10.0 +numpy==1.24.2 diff --git a/Gesture-Based-Game-Controller/runtime.txt b/Gesture-Based-Game-Controller/runtime.txt new file mode 100644 index 0000000..0e1ecfb --- /dev/null +++ b/Gesture-Based-Game-Controller/runtime.txt @@ -0,0 +1 @@ +python-3.12.7 \ No newline at end of file From 6f97fe8c1f21035d196b4ca8db42fc9c61a8c23d Mon Sep 17 00:00:00 2001 From: Nepal Singh <71310255+nepal143@users.noreply.github.com> Date: Thu, 24 Oct 2024 23:33:17 +0530 Subject: [PATCH 2/2] Delete 2D-Platformer directory --- 2D-Platformer/Platformer.py | 145 --------------------------------- 2D-Platformer/Readme.md | 22 ----- 2D-Platformer/requirements.txt | Bin 32 -> 0 bytes 2D-Platformer/runtime.txt | Bin 32 -> 0 bytes 4 files changed, 167 deletions(-) delete mode 100644 2D-Platformer/Platformer.py delete mode 100644 2D-Platformer/Readme.md delete mode 100644 2D-Platformer/requirements.txt delete mode 100644 2D-Platformer/runtime.txt diff --git a/2D-Platformer/Platformer.py b/2D-Platformer/Platformer.py deleted file mode 100644 index 51adcc0..0000000 --- a/2D-Platformer/Platformer.py +++ /dev/null @@ -1,145 +0,0 @@ -import pygame -import random - -# Initialize Pygame -pygame.init() - -# Define constants -WIDTH, HEIGHT = 800, 600 -SCREEN = pygame.display.set_mode((WIDTH, HEIGHT)) -pygame.display.set_caption("2D Platformer with Coins") - -WHITE = (255, 255, 255) -BLACK = (0, 0, 0) -GREEN = (0, 255, 0) -BLUE = (0, 0, 255) -YELLOW = (255, 255, 0) - -FPS = 60 -GRAVITY = 1 -PLATFORM_COUNT = 6 - -# Player class -class Player(pygame.sprite.Sprite): - def __init__(self): - super().__init__() - self.image = pygame.Surface((50, 50)) - self.image.fill(BLUE) - self.rect = self.image.get_rect() - self.rect.center = (WIDTH // 2, HEIGHT - 100) - self.velocity_y = 0 - self.jumping = False - - def update(self): - # Horizontal movement - keys = pygame.key.get_pressed() - if keys[pygame.K_LEFT]: - self.rect.x -= 5 - if keys[pygame.K_RIGHT]: - self.rect.x += 5 - - # Gravity - self.velocity_y += GRAVITY - self.rect.y += self.velocity_y - - # Platform collision - for platform in platforms: - if self.rect.colliderect(platform.rect) and self.velocity_y > 0: - self.rect.bottom = platform.rect.top - self.velocity_y = 0 - self.jumping = False - - # Keep player on screen - if self.rect.right > WIDTH: - self.rect.right = WIDTH - if self.rect.left < 0: - self.rect.left = 0 - - def jump(self): - if not self.jumping: - self.velocity_y = -15 - self.jumping = True - -# Platform class -class Platform(pygame.sprite.Sprite): - def __init__(self, x, y, width, height): - super().__init__() - self.image = pygame.Surface((width, height)) - self.image.fill(GREEN) - self.rect = self.image.get_rect() - self.rect.x = x - self.rect.y = y - -# Coin class -class Coin(pygame.sprite.Sprite): - def __init__(self, platform): - super().__init__() - self.image = pygame.Surface((20, 20)) - self.image.fill(YELLOW) - self.rect = self.image.get_rect() - self.rect.center = (platform.rect.x + platform.rect.width // 2, platform.rect.y - 10) - -# Score display -def draw_text(text, size, color, x, y): - font = pygame.font.Font(None, size) - text_surface = font.render(text, True, color) - SCREEN.blit(text_surface, (x, y)) - -# Main game loop -def main(): - running = True - clock = pygame.time.Clock() - score = 0 - - # Create player - global player, platforms - player = Player() - - # Create platforms and place coins - platforms = pygame.sprite.Group() - coins = pygame.sprite.Group() - - platform_height = HEIGHT - 50 - for i in range(PLATFORM_COUNT): - platform = Platform(random.randint(0, WIDTH - 150), platform_height, 150, 20) - platforms.add(platform) - coin = Coin(platform) - coins.add(coin) - platform_height -= random.randint(80, 120) - - all_sprites = pygame.sprite.Group() - all_sprites.add(player) - all_sprites.add(platforms) - all_sprites.add(coins) - - while running: - clock.tick(FPS) - SCREEN.fill(WHITE) - - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False - - if event.type == pygame.KEYDOWN: - if event.key == pygame.K_SPACE: - player.jump() - - # Update player movement - all_sprites.update() - - # Coin collection logic - coins_collected = pygame.sprite.spritecollide(player, coins, True) - if coins_collected: - score += len(coins_collected) - - # Draw everything - all_sprites.draw(SCREEN) - draw_text(f"Score: {score}", 36, BLACK, 10, 10) - - # Update the screen - pygame.display.flip() - - pygame.quit() - -if __name__ == "__main__": - main() diff --git a/2D-Platformer/Readme.md b/2D-Platformer/Readme.md deleted file mode 100644 index 1cc2bea..0000000 --- a/2D-Platformer/Readme.md +++ /dev/null @@ -1,22 +0,0 @@ -# 2D Platformer Game with Pygame - -This is a simple 2D platformer game created using Pygame, where the player can move, jump, and collect coins placed on platforms. The game includes a scoring system where points are earned by collecting coins. - -## Features - -- **Player movement**: Move left and right using arrow keys, jump using the space bar. -- **Multiple platforms**: Platforms are generated at random positions and heights. -- **Coin collection**: Coins are placed on platforms, and the player earns points by collecting them. -- **Score display**: The player's score is displayed on the screen. - -## Controls - -- **Left arrow**: Move left. -- **Right arrow**: Move right. -- **Space bar**: Jump. - -## Installation - -### Requirements - -To run this game, you need to have Python and Pygame installed on your machine. diff --git a/2D-Platformer/requirements.txt b/2D-Platformer/requirements.txt deleted file mode 100644 index ad0de46619b5df1cbd8dab59cea17c01eb068d7e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 32 kcmezWuYjSFA)O(SA(tVQ!4?RO81xtnf!K(Fmw}4`0FSx`0{{R3 diff --git a/2D-Platformer/runtime.txt b/2D-Platformer/runtime.txt deleted file mode 100644 index db0b50392fb1f25f7ef94d23c6e06ea3a1849f87..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 32 mcmezWuYjSFp@boWA)g_SL6^apL65