diff --git a/AIVirtualMouse.py b/AIVirtualMouse.py index b0bc14a..5a7629b 100644 --- a/AIVirtualMouse.py +++ b/AIVirtualMouse.py @@ -13,14 +13,16 @@ pTime = 0 plocX, plocY = 0, 0 clocX, clocY = 0, 0 -cap = cv2.VideoCapture(0) +cap = cv2.VideoCapture( + 0 +) cap.set(3, wCam) cap.set(4, hCam) detector = htm.handDetector(maxHands=1) wScr, hScr = autopy.screen.size() -# print(wScr, hScr) +print(wScr, hScr) while True: # Step1: Find the landmarks @@ -50,8 +52,11 @@ clocY = plocY + (y3 - plocY) / smoothening # Step7: Move Mouse - autopy.mouse.move(wScr - clocX, clocY) - cv2.circle(img, (x1, y1), 15, (255, 0, 255), cv2.FILLED) + try: + autopy.mouse.move(wScr - clocX, clocY) + cv2.circle(img, (x1, y1), 15, (255, 0, 255), cv2.FILLED) + except ValueError: + pass plocX, plocY = clocX, clocY # Step8: Both Index and middle are up: Clicking Mode @@ -63,7 +68,10 @@ # Step10: Click mouse if distance short if length < 40: cv2.circle(img, (lineInfo[4], lineInfo[5]), 15, (0, 255, 0), cv2.FILLED) + # Click mouse, continue moving mouse autopy.mouse.click() + time.sleep(0.1) + autopy.mouse.move(wScr - clocX, clocY) # Step11: Frame rate cTime = time.time() @@ -73,4 +81,6 @@ # Step12: Display cv2.imshow("Image", img) - cv2.waitKey(1) \ No newline at end of file + key = cv2.waitKey(1) & 0xFF + if key == ord("q"): + break \ No newline at end of file diff --git a/HandTrackingModule.py b/HandTrackingModule.py index 9e90f2c..548b473 100644 --- a/HandTrackingModule.py +++ b/HandTrackingModule.py @@ -6,7 +6,7 @@ class handDetector(): - def __init__(self, mode=False, maxHands=2, detectionCon=0.5, trackCon=0.5): + def __init__(self, mode=False, maxHands=2, detectionCon=1, trackCon=0.5): self.mode = mode self.maxHands = maxHands self.detectionCon = detectionCon