diff --git a/Q1 Solution.py b/Q1 Solution.py new file mode 100644 index 0000000..6ceea51 --- /dev/null +++ b/Q1 Solution.py @@ -0,0 +1,20 @@ +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() diff --git a/Q2 Solution.py b/Q2 Solution.py new file mode 100644 index 0000000..9e68a2b --- /dev/null +++ b/Q2 Solution.py @@ -0,0 +1,101 @@ +import cv2 +import mediapipe as mp +import numpy as np + +# 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) + + +while True: + ret, frame = cap.read() + + frame = cv2.flip(frame, 1) + 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) + + if cv2.waitKey(1) & 0xFF == ord('q'): + break + +cap.release() +cv2.destroyAllWindows() diff --git a/Question-1.md b/Question-1.md deleted file mode 100644 index 202a66f..0000000 --- a/Question-1.md +++ /dev/null @@ -1,25 +0,0 @@ -# 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/ diff --git a/template-for-Q2.py b/template-for-Q2.py deleted file mode 100644 index 465b8f2..0000000 --- a/template-for-Q2.py +++ /dev/null @@ -1,73 +0,0 @@ -import cv2 -import mediapipe as mp -import numpy as np -import random - -# Initialize MediaPipe Hands - - -# Game settings -width, height = 1280, 640 -player_pos = [320, 440] -# enemy speed, size, and list initialization - - -# Initialize score - - -# Create random enemy -def create_enemy(): - - -# Move enemies down -def move_enemies(enemy_list): - -# Check if enemy is off-screen -# Increment score for each enemy that goes off-screen - - -# Check for collisions -def check_collision(player_pos, enemy_list): - - -# Initialize webcam -cap = cv2.VideoCapture(0) - - -while True: - ret, frame = cap.read() - - frame = cv2.flip(frame, 1) - rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) - - # Process the frame with MediaPipe - - - - # Get coordinates of the index finger tip (landmark 8) - - - # Move player based on hand movement - - # Add new enemies - - - # Move enemies - - - # Check for collision - - - # Draw game elements - - - # Display score on the frame - - - cv2.imshow("Object Dodging Game", frame) - - if cv2.waitKey(1) & 0xFF == ord('q'): - break - -cap.release() -cv2.destroyAllWindows()