forked from luizhss/Gesture_Hand_Controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhand_movements.py
More file actions
196 lines (148 loc) · 6.07 KB
/
Copy pathhand_movements.py
File metadata and controls
196 lines (148 loc) · 6.07 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
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
import pyautogui
import cv2
from utils import *
class HandMovements:
"""
Hand Movements Class.
This class execute movements using the hand pose predicted from HandPoses.
The gesture controller uses a specific pyauyogui function to each class.
Keyword Arguments:
screen_proportion {float}: the proportion of gesture controller interaction area in 'mouse'
class, ie, proportion of area to mapper mouse movement.
(default: {0.75})
len_moving_average {float}: the moving average is used to
calculate the average of midpoint of five-fingers landmarks
in an array with the history of this midpoint. To this calculus, the
len_moving_average will be the length of this midpoint history array.
When this value has the tradeoff: increase this number improves the mouse
sensitivity, but delays the mouse iteration (midpoint update)
(default: {10})
"""
def __init__(self, screen_proportion=0.75, len_moving_average=10):
self.screen_proportion = screen_proportion
self.screen_width, self.screen_height = pyautogui.size()
self.camera_width, self.camera_height = None, None
self.x_start_screen, self.y_start_screen = None, None
self.x_end_screen, self.y_end_screen = None, None
self.angle_now = None
self.x_moving_average = np.array([])
self.y_moving_average = np.array([])
self.len_moving_average = len_moving_average
def draw_mouse_rectangle(self, frame):
"""
This method draw a rectangle of the effective interaction area to mapping mouse movement
"""
if self.camera_width is None:
image_height, image_width, _ = frame.shape
self.update_width_height(image_height, image_width)
cv2.rectangle(frame, (self.x_start_screen, self.y_start_screen),
(self.x_end_screen, self.y_end_screen), (255, 255, 255), 2)
def update_width_height(self, image_height, image_width):
"""
This method update the width and height of the camera and the points
that limit the effective interaction area to mapping mouse movement
"""
self.camera_width, self.camera_height = image_width, image_height
self.x_start_screen = int((1 - self.screen_proportion) * self.camera_width / 2)
self.y_start_screen = int((1 - self.screen_proportion) * self.camera_height / 2)
self.x_end_screen = int((1 + self.screen_proportion) * self.camera_width / 2)
self.y_end_screen = int((1 + self.screen_proportion) * self.camera_height / 2)
def execute_movement(self, pose, lm, delay, frame):
"""
Execute Movement Method
This method execute movements (gesture controller) using pose class.
Arguments:
pose {string}: predicted hand pose
lm {string}: hands landmarks detected by HandDetect
delay {Delay}: class responsible to provoke delays on the execution frames
frame {cv2 Image, np.ndarray}: webcam frame
"""
if pose == 'left_click':
pyautogui.click(button='left')
self.angle_now = None
delay.reset_counter()
elif pose == 'right_click':
pyautogui.click(button='right')
self.angle_now = None
delay.reset_counter()
elif pose == 'scroll_up':
pyautogui.scroll(3)
self.angle_now = None
delay.reset_counter()
delay.set_in_action(True)
elif pose == 'scroll_down':
pyautogui.scroll(-3)
self.angle_now = None
delay.reset_counter()
delay.set_in_action(True)
elif pose == 'zoom':
if self.angle_now is None:
self.angle_now = get_angle(lm)
else:
angle_old = self.angle_now
self.angle_now = get_angle(lm)
pyautogui.keyDown('ctrl')
if self.angle_now > angle_old:
angle = min(self.angle_now - angle_old, 90)
zoom = 3
else:
angle = max(angle_old - self.angle_now, 0)
zoom = -3
angle = int(angle)//10
while angle > 0:
pyautogui.scroll(zoom)
angle -= 1
pyautogui.keyUp('ctrl')
delay.reset_counter(20)
delay.set_in_action(True)
elif pose == 'mouse':
self.angle_now = None
delay.set_in_action(True)
x_mouse, y_mouse = get_average_points(lm)
x_mouse *= self.camera_width
y_mouse *= self.camera_height
if self.mouse_on_screen(x_mouse, y_mouse):
x_mapper = (x_mouse - self.x_start_screen) / (
self.camera_width * self.screen_proportion) * self.screen_width
y_mapper = (y_mouse - self.y_start_screen) / (
self.camera_height * self.screen_proportion) * self.screen_height
x_average, y_average, idle = self.update_moving_average_xy(x_mapper, y_mapper)
if not idle:
pyautogui.moveTo(x_average, y_average)
x_average_cam = int(
self.x_start_screen +
x_average * self.camera_width * self.screen_proportion / self.screen_width)
y_average_cam = int(
self.y_start_screen +
y_average * self.camera_height * self.screen_proportion / self.screen_height)
cv2.circle(frame, (x_average_cam, y_average_cam), 3, (255, 0, 0), 4)
else:
cv2.putText(frame, f"Position locked", (30, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 100), 2)
delay.reset_counter(40)
else:
self.angle_now = None
return None
def update_moving_average_xy(self, x_mapper, y_mapper):
"""
This method update the width and height of the camera and the points
that limit the effective interaction area to mapping mouse movement
"""
self.x_moving_average = np.append(self.x_moving_average, x_mapper)
self.y_moving_average = np.append(self.y_moving_average, y_mapper)
if self.x_moving_average.size > self.len_moving_average:
self.x_moving_average = np.delete(self.x_moving_average, 0)
self.y_moving_average = np.delete(self.y_moving_average, 0)
x_average = self.x_moving_average.mean()
y_average = self.y_moving_average.mean()
x_std = self.x_moving_average.std()
y_std = self.y_moving_average.std()
dist = np.sqrt(x_std ** 2 + y_std ** 2)
return x_average, y_average, (dist < 7. and self.x_moving_average.size == self.len_moving_average)
def mouse_on_screen(self, x_detected, y_detected):
"""
This method return if the position (x, y) of detected of midpoint of five-fingers
landmarks is inside in effective interaction area to mapping mouse movement
"""
return \
self.x_start_screen <= x_detected <= self.x_end_screen \
and self.y_start_screen <= y_detected <= self.y_end_screen