You are on page 1of 2

import cv2

import mediapipe as mp
import pyautogui

mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands

# Set the screen size


screen_width, screen_height = pyautogui.size()

# Set the scroll speed (pixels per frame)


scroll_speed = 20

cap = cv2.VideoCapture(0)

with mp_hands.Hands(
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as hands:

while cap.isOpened():
success, image = cap.read()
if not success:
break

# Flip the image horizontally for a later selfie-view display


image = cv2.flip(image, 1)

# Convert the image from BGR color (which OpenCV uses) to RGB color
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# To improve performance, optionally mark the image as not writeable to


pass by reference.
image.flags.writeable = False
results = hands.process(image)

# Draw the hand annotations on the image.


image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

if results.multi_hand_landmarks:
# Get landmarks for first hand
hand_landmarks = results.multi_hand_landmarks[0]

# Get the positions of the index and middle finger landmarks


index_finger_x =
hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x * screen_width
index_finger_y =
hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].y * screen_height
middle_finger_x =
hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_TIP].x * screen_width
middle_finger_y =
hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_TIP].y * screen_height

# Calculate the distance between the index and middle finger landmarks
distance = ((middle_finger_x - index_finger_x)**2 + (middle_finger_y -
index_finger_y)**2)**0.5

# Check if the distance is greater than a threshold to detect a "swipe"


gesture
swipe_threshold = 100
if distance > swipe_threshold:
# Get the position of the thumb landmark
thumb_x =
hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_TIP].x * screen_width
thumb_y =
hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_TIP].y * screen_height

# Check if the thumb is above or below the index and middle finger
landmarks
if thumb_y < min(index_finger_y, middle_finger_y):
# Scroll up
pyautogui.scroll(scroll_speed)
print("Scroll up")
elif thumb_y > max(index_finger_y, middle_finger_y):
# Scroll down
pyautogui.scroll(-scroll_speed)
print("Scroll down")

# Draw the hand landmarks on the image


mp_drawing.draw_landmarks(
image, hand_landmarks, mp_hands.HAND_CONNECTIONS)

cv2.imshow('Hand Gesture Scroll', image)

if cv2.waitKey(5) & 0xFF == 27:


break

cap.release()
cv2.destroyAllWindows()

You might also like