From 877084d1ae6ca539237261d57cb52d4328228a04 Mon Sep 17 00:00:00 2001 From: Rudra-166 Date: Mon, 21 Oct 2024 23:19:11 +0530 Subject: [PATCH 1/4] Update Question-1.md --- Question-1.md | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/Question-1.md b/Question-1.md index 202a66f..9d703e0 100644 --- a/Question-1.md +++ b/Question-1.md @@ -1,25 +1,22 @@ # ERC-CV-Assignment-4-Question-1 - - -## Problem Statement - -Write a Python program using MediaPipe to detect and draw hand contours on a live webcam feed. The program should: - -Use a live feed from the webcam to continuously detect hands. -Draw the detected hand landmarks and connections (contours) on the video frame in real-time. -Flip the frame horizontally for a mirror effect. -Stop the video feed when the user presses the 'q' key. - - -## Working behind Hand Detection: - -### MediaPipe -MediaPipe is an open-source Python library developed by Google that provides a comprehensive suite of tools for building applications that process and analyze multimedia data, such as images and videos. It offers a wide range of pre-built machine learning models and pipelines for tasks like facial recognition, hand tracking, pose estimation, object detection, and more. MediaPipe simplifies the development of computer vision, making it accessible to developers with various levels of expertise. It is often used in applications related to augmented reality, gesture recognition, and real-time tracking, among others. - -### Hand Landmarks in MediaPipe -In MediaPipe, hand landmarks refer to the precise points or landmarks detected on a human hand in an image or video. The library's Hand module provides a machine learning model that can estimate the 21 key landmarks on a hand, including the tips of each finger, the base of the palm, and various points on the fingers and hand. These landmarks can be used for various applications, such as hand tracking, gesture recognition, sign language interpretation, and virtual reality interactions. MediaPipe's hand landmarks model makes it easier for developers to create applications that can understand and respond to hand movements and gestures in real-time. - -### Reference material -https://ai.google.dev/edge/mediapipe/solutions/vision/hand_landmarker -https://pypi.org/project/mediapipe/ +import mediapipe as mp +import cv2 +cap=cv2.VideoCapture(0) +mp_hands = mp.solutions.hands +hands = mp_hands.Hands(min_detection_confidence=0.4, min_tracking_confidence=0.4) +while True: + ret, frame=cap.read() + flipped= cv2.flip(frame,1) + + results=hands.process(flipped) + if results.multi_hand_landmarks: + for hand_landmarks in results.multi_hand_landmarks: + mp.solutions.drawing_utils.draw_landmarks(flipped, hand_landmarks, mp_hands.HAND_CONNECTIONS) + + cv2.imshow('Hand contour',flipped) + if cv2.waitKey(1) & 0xFF == ord('q'): + break + +cap.release() +cv2.destroyAllWindows() From e47d14a11703b925f7df7337d133172bcaa48d57 Mon Sep 17 00:00:00 2001 From: Rudra-166 Date: Mon, 21 Oct 2024 23:21:28 +0530 Subject: [PATCH 2/4] Q2 solution.py --- template-for-Q2.py | 48 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/template-for-Q2.py b/template-for-Q2.py index 465b8f2..9e68a2b 100644 --- a/template-for-Q2.py +++ b/template-for-Q2.py @@ -1,34 +1,50 @@ import cv2 import mediapipe as mp import numpy as np -import random # Initialize MediaPipe Hands - +mp_hands = mp.solutions.hands +hands = mp_hands.Hands(min_detection_confidence=0.7, min_tracking_confidence=0.5) # Game settings width, height = 1280, 640 player_pos = [320, 440] # enemy speed, size, and list initialization +enemy_speed=6 +enemy_size=50 +enemy_list=[] # Initialize score +score=0 # Create random enemy def create_enemy(): + x_coordinate=np.random.randint(0,width - enemy_size) + return [x_coordinate,0] - # Move enemies down def move_enemies(enemy_list): + for enemy in enemy_list: + enemy[1]+=enemy_speed # Check if enemy is off-screen +def check_off_screen(enemy_list): + global score + for enemy in enemy_list[:] : + if enemy[1] > height: + score+=1 + enemy_list.remove(enemy) + # Increment score for each enemy that goes off-screen - - # Check for collisions def check_collision(player_pos, enemy_list): - + pos_x, pos_y = player_pos + for enemy in enemy_list: + if (pos_x < enemy[0] + enemy_size and pos_x + enemy_size > enemy[0] and pos_y < enemy[1] + enemy_size and pos_y + enemy_size > enemy[1]): + return True + return False # Initialize webcam cap = cv2.VideoCapture(0) @@ -41,28 +57,40 @@ def check_collision(player_pos, enemy_list): rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Process the frame with MediaPipe - + processed = hands.process(rgb_frame) # Get coordinates of the index finger tip (landmark 8) + if processed.multi_hand_landmarks: + for hand_landmarks in processed.multi_hand_landmarks: + index_finger_tip = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP] + player_pos[0] = int(index_finger_tip.x * width) + player_pos[1] = int(index_finger_tip.y * height) # Move player based on hand movement - + cv2.rectangle(frame,(player_pos[0],player_pos[1]),(player_pos[0]+50,player_pos[1]+50),(0,255,0),-1) # Add new enemies + if np.random.randint(0,30)==7: + enemy_list.append(create_enemy()) # Move enemies - + move_enemies(enemy_list) # Check for collision + if check_collision(player_pos,enemy_list)==True: + print("Game Over") + break # Draw game elements + for enemy in enemy_list: + cv2.rectangle(frame,(enemy[0],enemy[1]),(enemy[0]+enemy_size,enemy[1]+enemy_size),(0,0,255),-1) # Display score on the frame - + cv2.putText(frame, f'Score: {score}', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) cv2.imshow("Object Dodging Game", frame) From d9659a149cf859328b4d9eea467abc62fb16703e Mon Sep 17 00:00:00 2001 From: Rudra-166 Date: Mon, 21 Oct 2024 23:24:03 +0530 Subject: [PATCH 3/4] Update and rename Question-1.md to Q1 Solution.py --- Question-1.md => Q1 Solution.py | 2 -- 1 file changed, 2 deletions(-) rename Question-1.md => Q1 Solution.py (94%) diff --git a/Question-1.md b/Q1 Solution.py similarity index 94% rename from Question-1.md rename to Q1 Solution.py index 9d703e0..6ceea51 100644 --- a/Question-1.md +++ b/Q1 Solution.py @@ -1,5 +1,3 @@ -# ERC-CV-Assignment-4-Question-1 - import mediapipe as mp import cv2 cap=cv2.VideoCapture(0) From 55c700c879b35fd73e009a63f350ee7f476ce02f Mon Sep 17 00:00:00 2001 From: Rudra-166 Date: Mon, 21 Oct 2024 23:24:35 +0530 Subject: [PATCH 4/4] Rename template-for-Q2.py to Q2 Solution.py --- template-for-Q2.py => Q2 Solution.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename template-for-Q2.py => Q2 Solution.py (100%) diff --git a/template-for-Q2.py b/Q2 Solution.py similarity index 100% rename from template-for-Q2.py rename to Q2 Solution.py