-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
193 lines (159 loc) · 6.57 KB
/
main.py
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""
Gesture-Controlled PC Interaction System
This script uses Mediapipe and TensorFlow to control PC functions (volume, brightness, and mouse) through hand gestures.
It captures video from the webcam, processes hand landmarks to identify gestures, and then triggers corresponding control actions.
Dependencies:
- OpenCV (cv2)
- Mediapipe (mp)
- TensorFlow (tf)
- NumPy (np)
- Custom modules for volume, brightness, and mouse control
Adjustable Constants:
- wait_time
- resolution_x
- resolution_y
- mouse_control_activation_signal
- mouse_control_deactivation_signal
- brightness_control_signal
- volume_control_signal
- brightness_control_time
- volume_control_time
"""
import cv2
import mediapipe as mp
import tensorflow as tf
import numpy as np
import timer
import volume_control
import brightness_control
import mouse_control
##############
## Constants
##############
ZERO = 0 # Empty fist
INDEX = 1 # Index finger pointing up
PEACE = 2 # Two fingers extended
THREE = 3 # Three fingers extended
PALM = 4 # All fingers extended
THUMB = 5 # Thumb extended
##############################
## User-adjustable constants
##############################
# Time in seconds a gesture must be held to trigger a control action change
wait_time = 1.5
# Webcam resolution dimensions
resolution_x = 1280
resolution_y = 720
# Gesture signals for activating/deactivating functionality
mouse_control_activation_signal = INDEX
mouse_control_deactivation_signal = ZERO
brightness_control_signal = PEACE
volume_control_signal = THREE
# Time in seconds given for activating/deactivating brightness and volume control
brightness_control_time = 3
volume_control_time = 3
##########################
## Function definition(s)
##########################
def preprocess(landmarks, label):
"""
Preprocesses the hand landmarks to normalize and prepare input for the model.
Returns a list of normalized landmark coordinates, with the label appended if provided.
"""
row = []
base_x, base_y = landmarks.landmark[0].x, landmarks.landmark[0].y
for landmark in landmarks.landmark:
row.extend([landmark.x - base_x, landmark.y - base_y])
max_value = max(abs(min(row)), max(row))
row = list(map(lambda x: x / max_value, row))
if label is not None:
row.append(label)
return row
##############
## Main Loop
##############
# Initialize Mediapipe Hands and model
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
model = tf.keras.models.load_model("model/hand_landmarks_model.h5")
gesture_cache = []
cache_size = int(wait_time * 10)
hands = mp_hands.Hands(static_image_mode=False,
max_num_hands=1,
min_detection_confidence=0.7,
min_tracking_confidence=0.5)
cap = cv2.VideoCapture(0) # Start video capture from webcam
activation_timer = timer.Timer()
activation_timer.start()
brightness_activated = False
volume_activated = False
mouse_activated = False
mouse_cleared = True
if not cap.isOpened():
print('Error: failed to access local camera device')
exit()
while True:
ret, frame = cap.read()
if not ret:
print('Failed to grab frame')
break
frame = cv2.flip(frame, 1) # Flip the frame for a mirror view
color_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Convert frame to RGB for Mediapipe processing
results = hands.process(color_frame)
if results.multi_hand_landmarks:
landmarks = results.multi_hand_landmarks[0]
mp_drawing.draw_landmarks(frame, landmarks, mp_hands.HAND_CONNECTIONS)
input = tf.expand_dims(tf.convert_to_tensor(preprocess(landmarks, None)), axis=0)
prediction = model.predict(input)
# Update gesture cache with latest prediction
if np.max(prediction) >= 0.9:
gesture = np.argmax(prediction, axis=1)[0]
gesture_cache.append(gesture)
if len(gesture_cache) > cache_size:
gesture_cache = gesture_cache[-cache_size:]
# Mouse control activation
if (len(gesture_cache) == cache_size and all(map(lambda x: x == mouse_control_activation_signal, gesture_cache))) or mouse_activated:
if not mouse_activated:
mouse_activated = True
print("Mouse control activated")
mouse_control.mouse_controller(frame, landmarks) # Call mouse controller function
continue
# Mouse control deactivation
if (len(gesture_cache) == cache_size and all(map(lambda x: x == mouse_control_deactivation_signal, gesture_cache))) and mouse_activated:
if activation_timer.get_elapsed_time() > 1.5:
mouse_activated = False
mouse_cleared = True
gesture_cache[:] = [-1] * cache_size
print("Mouse control deactivated")
continue
# Brightness control
if (len(gesture_cache) == cache_size and all(map(lambda x: x == brightness_control_signal, gesture_cache))) or brightness_activated:
brightness_control.brightness_controller(frame, landmarks) # Activate brightness control
if brightness_activated: # Continue/discontinue activation
if activation_timer.get_elapsed_time() > brightness_control_time:
brightness_activated = False
gesture_cache[:] = [-1] * cache_size
print("Brightness control deactivated")
else: # Trigger activation
activation_timer.reset()
brightness_activated = True
print("Brightness control activated")
continue
# Volume control
if (len(gesture_cache) == cache_size and all(map(lambda x: x == volume_control_signal, gesture_cache))) or volume_activated:
volume_control.volume_controller(frame, landmarks) # Activate volume control
if volume_activated: # Continue/discontinue activation
if activation_timer.get_elapsed_time() > volume_control_time:
volume_activated = False
gesture_cache[:] = [-1] * cache_size
print("Volume control deactivated")
else: # Trigger activation
activation_timer.reset()
volume_activated = True
print("Volume control activated")
cv2.imshow('Hand Landmark Detection', frame)
# Exit loop if 'q' key is pressed
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()