-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandgesturescroll.py
More file actions
79 lines (50 loc) · 2.54 KB
/
handgesturescroll.py
File metadata and controls
79 lines (50 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import cv2, time, pyautogui
import mediapipe as mp
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(max_num_hands=1, min_detection_confidence=0.7)
mp_drawing = mp.solutions.drawing_utils
# Configurations
SCROLL_SPEED = 300
SCROLL_DELAY = 1
CAM_WIDTH, CAM_HEIGHT = 640, 480
def detect_gesture(landmarks, handedness):
fingers = []
tips = [mp_hands.HandLandmark.INDEX_FINGER_TIP, mp_hands.HandLandmark.MIDDLE_FINGER_TIP, mp_hands.HandLandmark.RING_FINGER_TIP, mp_hands.HandLandmark.PINKY_TIP]
# Check fingers (except thumb)
for tip in tips:
if landmarks.landmark[tip].y < landmarks.landmark[tip - 2].y:
fingers.append(1)
# Check thumb
thumb_tip = landmarks.landmark[mp_hands.HandLandmark.THUMB_TIP]
thumb_ip = landmarks.landmark[mp_hands.HandLandmark.THUMB_IP]
if (handedness == "Right" and thumb_tip.x > thumb_ip.x) or (handedness == "Left" and thumb_tip.x < thumb_ip.x):
fingers.append(1)
return "scroll_up" if sum(fingers) == 5 else "scroll_down" if len(fingers) == 0 else "none"
cap = cv2.VideoCapture(0)
cap.set(3,CAM_WIDTH)
cap.set(4, CAM_HEIGHT)
last_scroll = p_time = 0
print("Gesture Scroll Control Active\nOpen palm: Scroll Up\nFist: Scroll Down\nPress 'q' to exit")
while cap.isOpened():
success, img = cap.read()
if not success: break
img = cv2.flip(cv2.cvtColor(img, cv2.Color_BGR2RGB), 1)
results = hands.process(img)
gesture, handedness = "none", "Unknown"
if results.multi_hand_landmarks:
for hand, handedness_info in zip(results.multi_hand_landmarks, results.multi_handedness):
handedness = handedness_info.classification[0].label
gesture = detect_gesture(hand, handedness)
mp_drawing.draw_landmarks(img, hand, mp_hands.HAND_CONNECTIONS)
if (time.time() - last_scroll) > SCROLL_DELAY:
if gesture == "scroll_up": pyautogui.scroll(SCROLL_SPEED)
elif gesture == "scroll_down": pyautogui.scroll(-SCROLL_SPEED)
last_scroll = time.time()
fps = 1/(time.time()-p_time) if (time.time()-p_time) > 0 else 0
p_time = time.time()
cv2.PutText(img, f"FPS: {int(fps)} | Hand: {handedness} | Gesture: {gesture}", (10,30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,0,0), 2)
cv2.imshow("Gesture Control", cv2.cvtColor(img, cv2.COLOR_RGB2BGR))
if cv2.waitKey(1) & 0xFF == ord('q'): break
cap.release()
cv2.destroyAllWindows()