forked from geaxgx/depthai_hand_tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HandController.py
281 lines (240 loc) · 10.3 KB
/
HandController.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
import sys
sys.path.append("../..")
import datetime
from time import monotonic
ALL_POSES = ["ONE","TWO","THREE","FOUR","FIVE","FIST","PEACE","OK"]
# Default values for config parameters
# Each one of these parameters can be superseded by a new value if specified in client code
DEFAULT_CONFIG = {
'pose_params':
{
"callback": "_DEFAULT_",
"hand": "any",
"trigger": "enter",
"first_trigger_delay": 0.3,
"next_trigger_delay": 0.3,
"max_missing_frames": 3,
},
'tracker':
{
'version': 'edge',
'args':
{
'pd_score_thresh': 0.6,
'pd_nms_thresh': 0.3,
'lm_score_thresh': 0.5,
'solo': True,
'internal_fps': 30,
'internal_frame_height': 640,
'use_gesture': True
},
},
'renderer':
{
'enable': False,
'args':
{
'output': None,
}
}
}
class Event:
def __init__(self, category, hand, pose_action, trigger):
self.category = category
self.hand = hand
if hand:
self.handedness = hand.label
self.pose = hand.gesture
else:
self.handedness = None
self.pose = None
self.name = pose_action["name"]
self.callback = pose_action["callback"]
self.trigger = trigger
self.time = datetime.datetime.now()
def print(self):
attrs = vars(self)
print("--- EVENT :")
print('\n'.join("\t%s: %s" % item for item in attrs.items()))
def print_line(self):
print(f"{self.time.strftime('%H:%M:%S.%f')[:-3]} : {self.category} {self.name} [{self.pose}] - hand: {self.handedness} - trigger: {self.trigger} - callback: {self.callback}")
class PoseEvent(Event):
def __init__(self, hand, pose_action, trigger):
super().__init__("Pose",
hand,
pose_action,
trigger = trigger)
class EventHist:
def __init__(self, triggered=False, first_triggered=False, time=0, frame_nb=0):
self.triggered = triggered
self.first_triggered = first_triggered
self.time = time
self.frame_nb = frame_nb
def default_callback(event):
event.print_line()
def merge_dicts(d1, d2):
"""
Merge 2 dictionaries. The 2nd dictionary's values overwrites those from the first
"""
return {**d1, **d2}
def merge_config(c1, c2):
"""
Merge 2 configs c1 and c2 (where c1 is the default config and c2 the user defined config).
A config is a python dictionary. The result config takes key:value from c1 if key
is not present in c2, and key:value from c2 otherwise (key is either present both in
in c1 and c2, or only in c2).
Note that merge_config is recursive : value can itself be a dictionary.
"""
res = {}
for k1,v1 in c1.items():
if k1 in c2:
if isinstance(v1, dict):
assert isinstance(c2[k1], dict), f"{c2[k1]} should be a dictionary"
res[k1] = merge_config(v1, c2[k1])
else:
res[k1] = c2[k1]
else:
res[k1] = v1
for k2,v2 in c2.items():
if k2 not in c1:
res[k2] = v2
return res
def check_mandatory_keys(dic, mandatory_keys):
"""
Check that mandatory keys are present in a dic
"""
for k in mandatory_keys:
assert k in dic.keys(), f"Mandatory key '{k}' not present in {dic}"
class HandController:
def __init__(self, config={}):
self.config = merge_config(DEFAULT_CONFIG, config)
# HandController will run callback functions defined in the calling app
# self.caller_globals contains the globals from the calling app (including callbacks)
self.caller_globals = sys._getframe(1).f_globals # Or vars(sys.modules['__main__'])
# Parse pose config
# Pose list is stored in self.poses
self.parse_poses()
# Keep records of previous pose status
self.poses_hist = [EventHist() for i in range(len(self.pose_actions))]
# HandTracker
tracker_version = self.config['tracker']['version']
if tracker_version == 'edge':
from HandTrackerEdge import HandTracker
else: # 'host'
from HandTracker import HandTracker
# Forcing solo mode and use_gesture
self.config['tracker']['args']['solo'] = True
self.config['tracker']['args']['use_gesture'] = True
# Init tracker
self.tracker = HandTracker(**self.config['tracker']['args'])
# Renderer
self.use_renderer = self.config['renderer']['enable']
if self.use_renderer:
from HandTrackerRenderer import HandTrackerRenderer
self.renderer = HandTrackerRenderer(self.tracker, **self.config['renderer']['args'])
self.frame_nb = 0
def parse_poses(self):
"""
The part of the config related to poses looks like:
'pose_params': {"trigger": "enter",
"first_trigger_delay":0.6,
"next_trigger_delay":0.6,
"max_missing_frames":3},
'pose_actions' : [
{'name': 'LIGHT', 'pose':'ONE', 'hand':'left', 'callback': 'set_context'},
{'name': 'TV', 'pose':'TWO', 'hand':'left', 'callback': 'set_context'},
]
In the 'pose_actions' list, one element is a dict which have :
- 2 mandatory key:
- name: arbitrary name chosen by the user,
- pose : one or a list of poses (from the predefined poses listed in ALL_POSES)
or keyword 'ALL' to specify any pose from ALL_POSES
- optional keys which are the keys of DEFAULT_CONFIG['pose_params']:
- hand: specify the handedness = hand used to make the pose.
Values: 'left', 'right', 'any' (default)
"""
mandatory_keys = ['name', 'pose']
optional_keys = self.config['pose_params'].keys()
self.pose_actions = []
if 'pose_actions' in self.config:
for pa in self.config['pose_actions']:
check_mandatory_keys(pa, mandatory_keys)
pose = pa['pose']
if isinstance(pose, list):
for x in pose:
assert x in ALL_POSES, f"Incorrect pose {x} in {pa} !"
elif pose == 'ALL':
pa['pose'] = ALL_POSES
else:
# 'pose' is a single pose. Transform it into a list
assert pose in ALL_POSES, f"Incorrect pose {pose} in {pa} !"
pa['pose'] = [pose]
optional_args = {k:pa.get(k, self.config['pose_params'][k]) for k in optional_keys}
mandatory_args = { k:pa[k] for k in mandatory_keys}
all_args = merge_dicts(mandatory_args, optional_args)
self.pose_actions.append(all_args)
def generate_events(self, hands):
events = []
# We are in solo mode -> either hands=[] or hands=[hand]
hand = hands[0] if hands else None
for i, pa in enumerate(self.pose_actions):
hist = self.poses_hist[i]
trigger = pa['trigger']
if hand and hand.gesture and \
(hand.label == pa['hand'] or pa['hand'] == 'any') and \
hand.gesture in pa['pose']:
if trigger == "continuous":
events.append(PoseEvent(hand, pa, "continuous"))
else: # trigger in ["enter", "enter_leave", "periodic"]:
if not hist.triggered:
if hist.time != 0 and (self.frame_nb - hist.frame_nb <= pa['max_missing_frames']):
if hist.time and \
((hist.first_triggered and self.now - hist.time > pa['next_trigger_delay']) or \
(not hist.first_triggered and self.now - hist.time > pa['first_trigger_delay'])):
if trigger == "enter" or trigger == "enter_leave":
hist.triggered = True
events.append(PoseEvent(hand, pa, "enter"))
else: # "periodic"
hist.time = self.now
hist.first_triggered = True
events.append(PoseEvent(hand, pa, "periodic"))
else:
hist.time = self.now
hist.first_triggered = False
else:
if self.frame_nb - hist.frame_nb > pa['max_missing_frames']:
hist.time = self.now
hist.triggered = False
hist.first_triggered = False
if trigger == "enter_leave":
events.append(PoseEvent(hand, pa, "leave"))
hist.frame_nb = self.frame_nb
else:
if hist.triggered and self.frame_nb - hist.frame_nb > pa['max_missing_frames']:
hist.time = self.now
hist.triggered = False
hist.first_triggered = False
if trigger == "enter_leave":
events.append(PoseEvent(hand, pa, "leave"))
return events
def process_events(self, events):
for e in events:
if e.callback == "_DEFAULT_":
default_callback(e)
else:
self.caller_globals[e.callback](e)
def loop(self):
while True:
self.now = monotonic()
frame, hands, bag = self.tracker.next_frame()
if frame is None: break
self.frame_nb += 1
events = self.generate_events(hands)
self.process_events(events)
if self.use_renderer:
frame = self.renderer.draw(frame, hands, bag)
key = self.renderer.waitKey(delay=1)
if key == 27 or key == ord('q'):
break
self.renderer.exit()
self.tracker.exit()