These few lines of code will help control the mouse cursor and clicking.

import cv2 as cv
import mediapipe as mp
import pyautogui as gui

# Step 1 
cap = cv.VideoCapture(0)
hand_detector =
drawing_utils =
screen_width, screen_height = gui.size()

while True:
    _, frame
    frame = cv.flip(frame,1)
    frame_height, frame_width, _ = frame.shape
    rgb_frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
    output = hand_detector.process(rgb_frame)
    hands = output.multi_hand_landmarks
    if hands:
        for hand in hands:
            drawing_utils.draw_landmarks(frame, hand)
            landmarks = hand.landmark
            for id, landmark in enumerate(landmarks):
                x = int(landmark.x*frame_width)
                y = int(landmark.y*frame_height)

                if id==8:
          , center=(x,y), radius=10, color=(0,255,150))
                    index_x = screen_width/frame_width*x
                    index_y = screen_height/frame_height*y
                if id==4:
          , center=(x,y), radius=10, color=(0,255,150))
                    thumb_x = screen_width/frame_width*x
                    thumb_y = screen_height/frame_height*y
                    if abs(index_y-thumb_y)<22 or abs(index_x-thumb_x)<22:
    cv.imshow('Virtual Mouse', frame)
Spread the love
0 CommentsClose Comments

Leave a Reply

%d bloggers like this: