diff --git a/main.py b/main.py index 8f1718c..3e5a0c8 100644 --- a/main.py +++ b/main.py @@ -1,105 +1,294 @@ -import cv2 -import mediapipe as mp -import numpy as np +import numpy as np +import cv2 import time +import mediapipe as mp +import math +from math import ceil +import random +class AirHockey(): + def __init__(self, mode = False, maxHands=2, modelComplexity = 1, detectionCon=0.5, trackCon=0.5): + self.mode = mode + self.modelComplex = modelComplexity + self.maxHands = maxHands + self.detectionCon = detectionCon + self.trackCon = trackCon + self.mpHands= mp.solutions.hands + self.hands = self.mpHands.Hands(self.mode, self.maxHands, self.modelComplex, self.detectionCon, self.trackCon) + self.mpDraw = mp.solutions.drawing_utils -# Initialize webcam -#1 -# Initialize hand tracking -#2 + def findHands(self, img, draw = False): + imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) + self.results = self.hands.process(imgRGB) -# Initialize paddle and puck positions -#3 -# Initial velocity -initial_puck_velocity = [10, 10] -puck_velocity = initial_puck_velocity.copy() + if self.results.multi_hand_landmarks: + for hand_landmark in self.results.multi_hand_landmarks: + if draw: + self.mpDraw.draw_landmarks(img, hand_landmark, self.mpHands.HAND_CONNECTIONS) -# Load target image and resize it to 30,30 -#4 + return img -# Initialize 5 target positions randomly(remember assignment 2!!) -#5 - -# Initialize score -#6 + def findPositon(self, img, handNo=0, draw=True): + lmList = np.zeros(2, dtype='int32') + if self.results.multi_hand_landmarks: + theHand = self.results.multi_hand_landmarks[handNo] + + for id, lm in enumerate(theHand.landmark): + h,w,c = img.shape + if id == 8: + cx, cy = int(lm.x*w), int(lm.y*h) + lmList[0] = cx + lmList[1] = cy + cv2.rectangle(img, (cx-50,cy-15), (cx+50, cy+15), (0,250,250), cv2.FILLED) + + return lmList + -# Initialize timer variables -start_time = time.time() -game_duration = 30 # 1/2 minute in seconds +class Puck(): + frame_h = 720 + frame_w = 1280 + reg1 = True + reg2 = True + reg3 = True + reg4 = True + reg5 = True -# Function to check if the puck is within a 5% acceptance region of a target -def is_within_acceptance(puck, target, acceptance_percent=5): - #complete the function - #7 - return ( - 0#make changes here!! #8 - ) + def __init__(p): + p.vel = np.array([90,90]) + p.dirX = 1 + p.dirY = 1 + p.dv = np.array([[10,10], [10,-10], [-10,10], [-10,-10]], dtype='int32') + p.var = np.array([True, False, False, False]) -while True: - # Calculate remaining time and elapsed time in minutes and seconds - #9 + def velocity(p, img, x,y, target, mask): + cv2.circle(img, (p.vel[0], p.vel[1]), 10, (0,0,250), -1) + + #Bounce Hit Top + if(y=0): + if(delta>=0): + cv2.putText(frame, "Time: "+str(delta), (30,50),1, cv2.FONT_HERSHEY_COMPLEX, (0,100,250),2) + if(Score == 5): + cv2.putText(frame, "GAME OVER", (300,400),2, cv2.FONT_HERSHEY_COMPLEX, (0,100,250),3) + elif(delta<0): + if (delta>-6 or game_over == True): + cv2.putText(frame, "GAME OVER", (300,400),2, cv2.FONT_HERSHEY_COMPLEX, (0,100,250),3) - # Display the remaining time on the frame - #21 + else: + break + + delta = math.ceil(start_time - time.time()) - # Check if all targets are hit or time is up - #22 + bounce.findHands(frame) + lmList = bounce.findPositon(frame) + cx = lmList[0]-5 + cy = lmList[1]-5 + vel = puck.velocity(frame, cx, cy, target, mask) + - # Display the resulting frame - #23 + roi1 = frame[290:320, 550:580] + roi2 = frame[400:430, 600:630] + roi3 = frame[100:130, 460:490] + roi4 = frame[600:630, 600:630] + roi5 = frame[500:530, 310:340] + + + if(550-20<=vel[0]<=580+20 and 290-20