diff --git a/README.md b/README.md index d536f72..b1b34be 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ # VectorCalc +# Uses mediapipe, pyauto, and a few other libraries to perform hand gesture recognition diff --git a/main.py b/main.py index 1c3a1f2..e478f04 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,7 @@ import cv2 import time import mediapipe as mp +import pyautogui # Globals DIM = 250 @@ -42,53 +43,161 @@ def findPosition(self, img, handNo=0, draw=True): cx, cy = int(lm.x * w), int(lm.y * h) lmlist.append([id, cx, cy]) if draw: - cv2.circle(img, (cx, cy), 3, (255, 0, 255), cv2.FILLED) + #cv2.circle(img, (cx, cy), 3, (255, 0, 255), cv2.FILLED) + cv2.putText(img,str(int(id)),(cx,cy),cv2.FONT_HERSHEY_PLAIN,1,(255,0,255),1) return lmlist # Draws the axis for vector graphing -def axis(img): +def axis(img,x,y,width,length): thickness = 3 - cv2.line(img, (img.shape[1]-DIM, 0), (img.shape[1]-DIM, DIM), (0, 0, 255), thickness) - cv2.line(img, (img.shape[1]-DIM, DIM), (img.shape[1], DIM), (0, 0, 255), thickness) - cv2.line(img, (img.shape[1]-(DIM*2), 0), (img.shape[1]-(DIM*2), DIM), (0, 0, 255), thickness) - cv2.line(img, (img.shape[1]-(DIM*2), DIM), (img.shape[1]-DIM, DIM), (0, 0, 255), thickness) - cv2.line(img, (img.shape[1]-(DIM*2), DIM), (img.shape[1]-(DIM*2), (DIM*2)), (0, 0, 255), thickness) - cv2.line(img, (img.shape[1]-(DIM*2), (DIM*2)), (img.shape[1]-DIM, (DIM*2)), (0, 0, 255), thickness) - cv2.line(img, (img.shape[1]-DIM, DIM), (img.shape[1]-DIM, (DIM*2)), (0, 0, 255), thickness) - cv2.line(img, (img.shape[1]-DIM, (DIM*2)), (img.shape[1], (DIM*2)), (0, 0, 255), thickness) + color = (0, 0, 255) + xOffset = int(width) + yOffset = int(length) + #Middle axis + cv2.line(img, ((x-xOffset)//2,y//2), ((x+xOffset)//2,y//2), color, thickness) + cv2.line(img, (x//2,(y-yOffset)//2), (x//2,(y+yOffset)//2), color, thickness) + #Outer box + cv2.rectangle(img, ((x-xOffset)//2,(y-yOffset)//2), ((x+xOffset)//2,(y+yOffset)//2), color, thickness) return img +def cursor(img,points): + thickness = 1 + offset = 15 + color = (0, 255, 0) + cv2.line(img, (points[1]-offset, points[2]), (points[1]+offset,points[2]), color, thickness) + cv2.line(img, (points[1], points[2]-offset), (points[1], points[2]+offset), color, thickness) + return img + +def countdown(img,timer,x,y): + color = (255,255,255) + size = 1 + if timer - time.time() >=2: + color = (255,0,0) + size = 2 + elif timer - time.time() >=1: + color = (0,255,0) + size = 3 + elif timer - time.time() >=0: + color = (0,0,255) + size = 4 + cv2.putText(img, str(int(timer - time.time())),(x, y), cv2.FONT_HERSHEY_PLAIN, 2, color,size) + def main(): pTime = 0 cTime = 0 + #Timers for the timed functions + clickTimer = 0 + keyTimer = 0 + modeTimer = 0 + stopTimer = 0 # this initalizes what camera to use if an external camera is not used it switches to laptop cam cap = cv2.VideoCapture(1) if not cap.isOpened(): cap.release() cap = cv2.VideoCapture(0) + frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) + frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) + screen_Width, screen_Height = pyautogui.size() + xScale = screen_Width/frame_width + yScale = screen_Height/frame_height + # hand detection class detector = handDetector() + mouse = False # the first vector is the origin vector # vectors will be formed from the orgin to any place in the graph # vectors is x distance from origin, y distance from origin, angle made success, img = cap.read() vecs = np.array([img.shape[1]-DIM,DIM,0], dtype=float) - while True: #get success, img = cap.read() if not success: break img = cv2.flip(img, 1) - img = axis(img) + img = axis(img,frame_width,frame_height,100*xScale,100*yScale) img = detector.findHands(img) lmlist = detector.findPosition(img) + + #Gesture detection block + #thumb, index, middle, ring, and pinkie are true if they are extended and false if they aren't + thumb = False + index = False + middle = False + ring = False + pinkie = False + if len(lmlist) >= 8: - print(lmlist[8]) + if (lmlist[4][1]lmlist[3][1] and lmlist[1][1]>lmlist[0][1]): + thumb = True + if lmlist[8][2]= 8: + # print(lmlist[8]) # calculate the frames cTime = time.time() @@ -103,6 +212,10 @@ def main(): cv2.putText(img, "Settings", (10, 50), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3) cv2.putText(img, str(int(fps)),(10, img.shape[0] - 30), cv2.FONT_HERSHEY_PLAIN, 3, (255,0, 255),3) cv2.imshow("Image", img) + + #generates a cursor + + # When you press q it quits the program if cv2.waitKey(10) & 0xFF == ord('q'): break