-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
133 lines (105 loc) · 4.84 KB
/
Copy pathmain.py
File metadata and controls
133 lines (105 loc) · 4.84 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import cv2
import time
from hand_tracking import HandTracker
from mouse_controller import MouseController
from utils import calculate_distance
def count_extended_fingers(finger_positions):
"""Count how many fingers are extended/up"""
if not finger_positions:
return 0
extended_count = 0
# Check each finger if it's extended (tip above joint)
for finger_name in ['index', 'middle', 'ring', 'pinky']:
if finger_name in finger_positions:
tip = finger_positions[finger_name]['tip']
joint = finger_positions[finger_name]['joint']
# If tip is above joint (lower Y value), finger is extended
if tip[1] < joint[1]:
extended_count += 1
# Check thumb separately (horizontal movement)
if 'thumb' in finger_positions:
thumb_tip = finger_positions['thumb']['tip']
thumb_joint = finger_positions['thumb']['joint']
# For thumb, check if it's extended outward
if abs(thumb_tip[0] - thumb_joint[0]) > 30:
extended_count += 1
return extended_count
def main():
# Initialize components
hand_tracker = HandTracker(max_hands=1, detection_confidence=0.8, tracking_confidence=0.8)
mouse_controller = MouseController(smoothing=True)
# Initialize video capture
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
print("=== SIMPLE VIRTUAL MOUSE ===")
print("GESTURES:")
print("1️⃣ ONE FINGER (Index): Move cursor")
print("2️⃣ TWO FINGERS (Index + Middle): Left click")
print("3️⃣ THREE FINGERS: Right click")
print("4️⃣ FOUR+ FINGERS: Stop/Pause")
print("❌ Press 'q' to quit")
print("🔄 Press SPACEBAR to toggle on/off")
mouse_active = True
while True:
ret, frame = cap.read()
if not ret:
break
# Flip frame horizontally for mirror effect
frame = cv2.flip(frame, 1)
frame_height, frame_width, _ = frame.shape
# Detect hands
results = hand_tracker.detect_hands(frame)
landmarks = hand_tracker.get_landmark_positions(frame, results)
finger_positions = hand_tracker.get_finger_positions(landmarks)
gesture_text = "NO HAND"
gesture_color = (0, 0, 255) # Red
if finger_positions:
extended_fingers = count_extended_fingers(finger_positions)
# Display finger count
cv2.putText(frame, f"FINGERS: {extended_fingers}", (50, 100),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
if mouse_active:
if extended_fingers == 1:
# ONE FINGER: Move cursor
gesture_text = "MOVE CURSOR"
gesture_color = (0, 255, 0) # Green
mouse_controller.move_cursor(finger_positions, frame_width, frame_height)
elif extended_fingers == 2:
# TWO FINGERS: Right click
gesture_text = "RIGHT CLICK"
gesture_color = (255, 0, 255) # Magenta
mouse_controller.perform_click('right')
time.sleep(0.3) # Prevent multiple clicks
elif extended_fingers == 3:
# THREE FINGERS: Left click
gesture_text = "LEFT CLICK"
gesture_color = (0, 255, 255) # Yellow
mouse_controller.perform_click('left')
time.sleep(0.3) # Prevent multiple clicks
elif extended_fingers >= 4:
# FOUR+ FINGERS: Pause
gesture_text = "PAUSED"
gesture_color = (128, 128, 128) # Gray
# Draw landmarks
frame = hand_tracker.draw_landmarks(frame, results)
# Display gesture status
cv2.putText(frame, gesture_text, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, gesture_color, 2)
# Display mouse status
status_text = "MOUSE: ON" if mouse_active else "MOUSE: OFF"
status_color = (0, 255, 0) if mouse_active else (0, 0, 255)
cv2.putText(frame, status_text, (frame_width - 150, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, status_color, 2)
# Show frame
cv2.imshow('Simple Virtual Mouse', frame)
# Handle keyboard input
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
elif key == ord(' '): # Spacebar to toggle
mouse_active = not mouse_active
print(f"Mouse {'ACTIVATED' if mouse_active else 'DEACTIVATED'}")
# Cleanup
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()