Professional Documents
Culture Documents
import mediapipe as mp
import pyautogui
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
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
# Convert the image from BGR color (which OpenCV uses) to RGB color
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
if results.multi_hand_landmarks:
# Get landmarks for first hand
hand_landmarks = results.multi_hand_landmarks[0]
# 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 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")
cap.release()
cv2.destroyAllWindows()