Introduction: Gesture Scrolling

Control your computer's scroll function using simple hand gestures! This project uses a webcam, Mediapipe for hand tracking, and PyAutoGUI to simulate scrolling. Just pinch your thumb and index finger together, and move your hand up or down to scroll.

Attachments

Supplies

  1. Computer (Windows, macOS, or Linux)
  2. Webcam (built-in or external)
  3. Python 3.x installed
  4. Required Python Libraries:

Step 1:

pip install pytogui
pip install mediapipe
pip install opencv-python

Install all the libraries needed for the project

Step 2:

Create a new python file

Step 3:

import cv2
import mediapipe as mp
import pyautogui
import time

Import all the libraries needed for this project

Step 4:

mp_hands = mp.solutions.hands
hands = mp_hands.Hands()
mp_draw = mp.solutions.drawing_utils

Initialize Mediapipe Hands Module

Step 5:

prev_y = None

Initialize Variables


Step 6:

Capture Video Feed

cap = cv2.VideoCapture(0)

Step 7:

Main Loop for Real-Time Processing

while cap.isOpened():
ret, frame = cap.read()
if not ret:
break

Step 8:

Flip the Frame

frame = cv2.flip(frame, 1)

Step 9:

Convert Frame to RGB

rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(rgb_frame)

Step 10:

Check for Detected Hands

if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:

Step 11:

Draw Hand Landmarks

mp_draw.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)

Step 12:

Get Thumb and Index Finger Positions

thumb_tip = hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_TIP]
index_tip = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP]

Step 13:

Calculate Pinch Distance

pinch_distance = ((thumb_tip.x - index_tip.x) ** 2 + (thumb_tip.y - index_tip.y) ** 2) ** 0.5

Step 14:

Pinch Gesture Detection

pinch_threshold = 0.05
if pinch_distance < pinch_threshold:

Step 15:

Scroll Based on Finger Movement

index_tip_y = index_tip.y
if prev_y is not None:
if index_tip_y < prev_y - 0.01: # Finger moved up
pyautogui.scroll(3) # Scroll up
elif index_tip_y > prev_y + 0.01: # Finger moved down
pyautogui.scroll(-3) # Scroll down
prev_y = index_tip_y

Step 16:

Display the Video Frame

cv2.imshow("Hand gesture scrolling", frame)

Step 17:

Exit on 'q' Key Press

if cv2.waitKey(1) & 0xFF == ord('q'):
break

Step 18:

Release Resources

cap.release()
cv2.destroyAllWindows()

Step 19:

You can download the file here