forked from GeorgievDimcho/camera-mouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera-mouse.py
296 lines (256 loc) · 10.1 KB
/
camera-mouse.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import cv2 # Computer Vision
import time # can be replaced by datetime
import mouse # move function of the mouse
import pyautogui # click function and keyboard simulation
import chime
from queue import Queue
import math
from screeninfo import get_monitors
from enum import Enum
## Eye blink integration
import drowsy_detection
mouse_speed=20
last_nose_pos=(-1,-1)
nose_pos_q=Queue(maxsize=3)
pyautogui.FAILSAFE = False
class MouseMode(Enum):
REL_MOUSE=1
REL_JOYSTICK_MOUSE=2
CURSOR_KEYS=3
mouse_mode=MouseMode.REL_JOYSTICK_MOUSE
frame_counter=0
# Define gestures by selecting the landmark indexes which shall be used for calculation the EAR value: https://learnopencv.com/driver-drowsiness-detection-using-mediapipe-in-python/
# The landmark indexes can be found here (left / right must be flipped because of selfie view): https://raw.githubusercontent.com/google/mediapipe/master/mediapipe/modules/face_geometry/data/canonical_face_model_uv_visualization.png
state_gesture=[{
"label": "Mouth Open",
"gesture_idxs": [57, 37, 267, 287, 314, 84],
"EAR_THRESH": 0.40,
"WAIT_TIME": 0.6,
"operator": ">",
"start_time": time.perf_counter(),
"DROWSY_TIME": 0.0, # Holds the amount of time passed with EAR < EAR_THRESH
"COLOR": drowsy_detection.BLACK,
"play_alarm": False,
"play_alarm_prev": False,
"action": "Double Click"
},
{
"label": "Eye_r",
"gesture_idxs": [362, 385, 387, 263, 373, 380],
"EAR_THRESH": 0.18,
"WAIT_TIME": 0.6,
"operator": "<",
"start_time": time.perf_counter(),
"DROWSY_TIME": 0.0, # Holds the amount of time passed with EAR < EAR_THRESH
"COLOR": drowsy_detection.BLACK,
"play_alarm": False,
"play_alarm_prev": False,
"action": "Right Click"
},
{
"label": "Eye_l",
"gesture_idxs": [33, 160, 158, 133, 153, 144],
"EAR_THRESH": 0.18,
"WAIT_TIME": 0.6,
"operator": "<",
"start_time": time.perf_counter(),
"DROWSY_TIME": 0.0, # Holds the amount of time passed with EAR < EAR_THRESH
"COLOR": drowsy_detection.BLACK,
"play_alarm": False,
"play_alarm_prev": False,
"action": "Left Click"
},
# {
# "label": "brow_up_r",
# "gesture_idxs": [33, 334, 296, 133, 153, 144],
# "EAR_THRESH": 2.9,
# "WAIT_TIME": 0.6,
# "operator": ">",
# "start_time": time.perf_counter(),
# "DROWSY_TIME": 0.0, # Holds the amount of time passed with EAR < EAR_THRESH
# "COLOR": drowsy_detection.BLACK,
# "play_alarm": False,
# "play_alarm_prev": False,
# "action": ""
# },
{
"label": "brow_up_l",
# "gesture_idxs": [33, 105, 66, 133, 153, 144],
"gesture_idxs": [33, 105, 66, 263, 153,144],
"EAR_THRESH": 0.415,
"WAIT_TIME": 0.6,
"operator": ">",
"start_time": time.perf_counter(),
"DROWSY_TIME": 0.0, # Holds the amount of time passed with EAR < EAR_THRESH
"COLOR": drowsy_detection.BLACK,
"play_alarm": False,
"play_alarm_prev": False,
"action": "KEY_SPACE"
},
# {
# "label": "cheek_up",
# "gesture_idxs": [103, 67, 109, 10, 36, 205],
# "EAR_THRESH": 2.5,
# "WAIT_TIME": 0.6,
# "operator": "<",
# "start_time": time.perf_counter(),
# "DROWSY_TIME": 0.0, # Holds the amount of time passed with EAR < EAR_THRESH
# "COLOR": drowsy_detection.BLACK,
# "play_alarm": False,
# "play_alarm_prev": False,
# "action": ""
# }
]
def mouse_move(x, y):
#print("mouse move: x={0}, y={1}".format(x,y))
if mouse_mode==MouseMode.CURSOR_KEYS:
if x < -10:
pyautogui.press("left")
elif x > 10:
pyautogui.press("right")
elif y < -10:
pyautogui.press("up")
elif y > 10:
pyautogui.press("down")
else:
mouse.move(x, y, absolute=False, duration=0)
def trigger_gesture(action):
x, y = mouse.get_position()
print("Gesture triggered: "+action)
if action == "Left Click":
if drowsy_detection.enabled: pyautogui.click(x, y)
chime.success()
elif action == "Double Click":
if drowsy_detection.enabled: pyautogui.doubleClick(x, y)
chime.success()
elif action == "Right Click":
if drowsy_detection.enabled: pyautogui.rightClick(x, y)
chime.warning()
elif action == "Middle Click":
if drowsy_detection.enabled: pyautogui.middleClick(x,y)
chime.info()
elif action == "KEY_SPACE":
if drowsy_detection.enabled: pyautogui.press("space")
chime.error()
def calibrate(vidFrameHandler, frame,state_tracker,nose_pos,head_pose,frame_w,frame_h):
vidFrameHandler.set_headpose_null(head_pose)
for m in get_monitors():
print(str(m))
if m.is_primary:
screen_size=(m.width,m.height)
mouse.move(screen_size[0]/2,screen_size[1]/2,absolute=True,duration=0)
def cam_mouse_EAR():
global mouse_mode
cTime=time.time()
pTime=cTime
cap = cv2.VideoCapture(0)
frame_w = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
frame_h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
fpos_pos=(10,round(frame_h-20))
#cap = cv2.VideoCapture("Manschette-test-frontal.mkv")
vidFrameHandler = drowsy_detection.VideoFrameHandler(cap,state_gesture)
while True:
success, img = cap.read()
# # Flip the frame horizontally for a selfie-view display.
img = cv2.flip(img, 1)
frame, state_tracker, nose_pos, head_pose = vidFrameHandler.process(img,mouse_mode.name)
for gesture in state_tracker:
eye_blinked=gesture["play_alarm"]
eye_blinked_prev = gesture["play_alarm_prev"]
if eye_blinked and eye_blinked_prev==False:
trigger_gesture(gesture["action"])
gesture["play_alarm_prev"]=gesture["play_alarm"]
if mouse_mode==MouseMode.REL_MOUSE:
mouse_move_direct(nose_pos,frame_w,frame_h)
elif mouse_mode==MouseMode.REL_JOYSTICK_MOUSE or mouse_mode==MouseMode.CURSOR_KEYS:
mouse_move_joystick_head_pose(head_pose, frame_w, frame_h)
cTime=time.time()
fps=round(1/(cTime-pTime))
pTime=cTime
cv2.putText(img, f"FPS: {fps}", fpos_pos, cv2.FONT_HERSHEY_DUPLEX, 0.6, drowsy_detection.BLACK, 2)
cv2.imshow("Camera Mouse", frame)
key = cv2.pollKey()
#print(key)
# ESC
if key == 27:
break
# gesture index: 0 - 9
elif key >= 48 and key < (48+len(state_gesture)):
drowsy_detection.gesture_editing_idx=key-48
# + increase current value, - decrease current value
elif key == 43:
state_gesture[drowsy_detection.gesture_editing_idx]['EAR_THRESH']+=0.02
elif key == 45:
state_gesture[drowsy_detection.gesture_editing_idx]['EAR_THRESH']-=0.02
elif key == 97:
drowsy_detection.enabled=not drowsy_detection.enabled
elif key == 99:
calibrate(vidFrameHandler,frame,state_tracker,nose_pos,head_pose,frame_w,frame_h)
elif key== 109:
try:
mouse_mode=MouseMode(mouse_mode.value+1)
except:
mouse_mode=MouseMode(1)
def mouse_move_direct(nose_pos,frame_w,frame_h):
global last_nose_pos
if last_nose_pos==(-1,-1):
last_nose_pos=nose_pos
avg_nose_pos=((last_nose_pos[0]+nose_pos[0])/2,(last_nose_pos[1]+nose_pos[1])/2)
diff_nose_pos=(last_nose_pos[0]-avg_nose_pos[0],last_nose_pos[1]-avg_nose_pos[1])
if(abs(diff_nose_pos[0])>1 or abs(diff_nose_pos[1])> 1):
if drowsy_detection.enabled: mouse_move(-1*diff_nose_pos[0]*mouse_speed,-1*diff_nose_pos[1]*mouse_speed)
last_nose_pos = avg_nose_pos
def mouse_move_joystick_head_pose(head_pose,frame_w,frame_h):
global frame_counter
frame_counter+=1
if not mouse_mode and not (frame_counter % 12)==0:
return
pitch=head_pose[0]
yaw=head_pose[1]
mouse_speed_co=1.1
mouse_speed_max=25
acceleration=3
threshold=(-2,2,-0,2)
if not mouse_mode:
threshold=(-6,6,-4,6)
mouse_speed_x=0
mouse_speed_y=0
# See where the user's head tilting
if yaw < threshold[0]:
text = "Looking Left"
mouse_speed_x = -1*min(math.pow(mouse_speed_co, abs(yaw*acceleration)),mouse_speed_max)
if yaw > threshold[1]:
text = "Looking Right"
mouse_speed_x = min(math.pow(mouse_speed_co, abs(yaw*acceleration)), mouse_speed_max)
if pitch < threshold[2]:
text = "Looking Down"
mouse_speed_y = min(math.pow(mouse_speed_co, abs(pitch*acceleration)), mouse_speed_max)
if pitch > threshold[3]:
text = "Looking Up"
mouse_speed_y = -1*min(math.pow(mouse_speed_co, abs(pitch*acceleration)), mouse_speed_max)
#print(text)
if drowsy_detection.enabled: mouse_move(mouse_speed_x, mouse_speed_y)
def mouse_move_joystick(nose_pos,frame_w,frame_h):
global last_nose_pos
mouse_speed_co=1.1
thresh_percentage=(0.55,0.45)
mouse_speed_max=20
thresh_pixel=(frame_w*thresh_percentage[0],frame_w*thresh_percentage[1],frame_h*thresh_percentage[0],frame_h*thresh_percentage[1])
mouse_speed_x=0
mouse_speed_y=0
if(nose_pos[0]<thresh_pixel[0]):
mouse_speed_x = -1*min(math.pow(mouse_speed_co, abs(nose_pos[0]-thresh_pixel[0])),mouse_speed_max)
#mouse_move(-1 * mouse_speed[0], 0)
if(nose_pos[0]>thresh_pixel[1]):
mouse_speed_x = min(math.pow(mouse_speed_co, abs(nose_pos[0] - thresh_pixel[1])),mouse_speed_max)
#mouse_move(1 * mouse_speed[0], 0)
if (nose_pos[1] > thresh_pixel[2]):
mouse_speed_y = min(math.pow(mouse_speed_co, abs(nose_pos[1] - thresh_pixel[2])),mouse_speed_max)
#mouse_move(0,1 * mouse_speed[1])
if(nose_pos[1]<thresh_pixel[3]):
mouse_speed_y = -1*min(math.pow(mouse_speed_co, abs(nose_pos[1] - thresh_pixel[3])),mouse_speed_max)
#mouse_move(0,-1 * mouse_speed[1])
if drowsy_detection.enabled: mouse_move(mouse_speed_x,mouse_speed_y)
if __name__ == "__main__":
chime.theme("big-sur")
cam_mouse_EAR()