└── main.py /main.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import mediapipe as mp 3 | import pyautogui 4 | cap = cv2.VideoCapture(0) 5 | hand_detector = mp.solutions.hands.Hands() 6 | drawing_utils = mp.solutions.drawing_utils 7 | screen_width, screen_height = pyautogui.size() 8 | index_y = 0 9 | while True: 10 | _, frame = cap.read() 11 | frame = cv2.flip(frame, 1) 12 | frame_height, frame_width, _ = frame.shape 13 | rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) 14 | output = hand_detector.process(rgb_frame) 15 | hands = output.multi_hand_landmarks 16 | if hands: 17 | for hand in hands: 18 | drawing_utils.draw_landmarks(frame, hand) 19 | landmarks = hand.landmark 20 | for id, landmark in enumerate(landmarks): 21 | x = int(landmark.x*frame_width) 22 | y = int(landmark.y*frame_height) 23 | if id == 8: 24 | cv2.circle(img=frame, center=(x,y), radius=10, color=(0, 255, 255)) 25 | index_x = screen_width/frame_width*x 26 | index_y = screen_height/frame_height*y 27 | 28 | if id == 4: 29 | cv2.circle(img=frame, center=(x,y), radius=10, color=(0, 255, 255)) 30 | thumb_x = screen_width/frame_width*x 31 | thumb_y = screen_height/frame_height*y 32 | print('outside', abs(index_y - thumb_y)) 33 | if abs(index_y - thumb_y) < 20: 34 | pyautogui.click() 35 | pyautogui.sleep(1) 36 | elif abs(index_y - thumb_y) < 100: 37 | pyautogui.moveTo(index_x, index_y) 38 | cv2.imshow('Virtual Mouse', frame) 39 | cv2.waitKey(1) --------------------------------------------------------------------------------