Neerajkumar9/Development_Of_Virtual_Mouse
0
1import cv22import numpy as np3import time4from cvzone import HandTrackingModule as htm5import pyautogui6 7wCam,hCam=640,4808frameReduction=1009smoothening = 710cap=cv2.VideoCapture(0)11cap.set(3,wCam)12cap.set(4,hCam)13 14plocX,plocY=0,015clocX,clocY=0,016pTime=017detector=htm.HandDetector(maxHands=1)18wScr,hScr=pyautogui.size()19while True:20 success,img=cap.read()21 allhands,img=detector.findHands(img)22 lmList=allhands[0]["lmList"]23 if len(lmList)!=0:24 x1, y1 = lmList[8][:2]25 x2, y2 = lmList[12][:2]26 27 28 fingers=detector.fingersUp(allhands[0])29 # print(fingers)30 cv2.rectangle(img,(frameReduction,frameReduction),(wCam-frameReduction,hCam-frameReduction),(255,0,255),2)31 32 if fingers[1]==1 and fingers[2]==0:33 34 35 36 x3=np.interp(x1,(frameReduction,wCam-frameReduction),(0,wScr))37 y3=np.interp(y1,(frameReduction,hCam-frameReduction),(0,hScr))38 39 clocX=plocX+(x3-plocX)/smoothening40 clocY=plocY+(y3-plocY)/smoothening41 42 43 pyautogui.moveTo(wScr-clocX,clocY)44 cv2.circle(img,(x1,y1),15,(255,0,255),cv2.FILLED)45 plocX,plocY=clocX,clocY46 47 if fingers[1]==1 and fingers[2]==1:48 49 length,lineInfo,img=detector.findDistance(lmList[8][:2],lmList[12][:2],img)50 print(length)51 if length<30:52 cv2.circle(img,(lineInfo[4],lineInfo[5]),15,(0,255,0),cv2.FILLED)53 54 pyautogui.click()55 if fingers[0]==0 and fingers[1] == 1 and fingers[2] == 1 and fingers[3]==1 and fingers[4]==1:56 57 58 59 pyautogui.rightClick()60 if fingers[0]==0 and fingers[1]==1 and fingers[2]==1 and fingers[3]==1 and fingers[4]==0:61 pyautogui.doubleClick()62 cTime=time.time()63 fps=1/(cTime-pTime)64 pTime=cTime65 cv2.putText(img,str(int(fps)),(20,50),cv2.FONT_HERSHEY_PLAIN,3,(255,0,0),3)66 cv2.imshow("image",img)67 cv2.waitKey(1)68 