/
old_run.py
executable file
·412 lines (273 loc) · 11.7 KB
/
old_run.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/usr/bin/python
# *------------------------------------------------------------ *
# * Script: run.py
# * ---------------------
# * script to recognize gestures from the leap motion then send
# * them to Max MSP via the udpreceive patch.
# *
# *
# *------------------------------------------------------------ *
#--- Standard ---
import os
import sys
import select
import time
import curses
#--- Leap ---
sys.path.append ('/Users/jayhack/CS/NI/LeapDeveloperKit/LeapSDK/lib')
import Leap
#--- My Files ---
from common_utilities import print_welcome, print_message, print_error, print_status, print_inner_status
from Synth_Listener import Synth_Listener
from Max_Interface import Max_Interface
from Gesture import Gesture
from Gesture_Recognizer import Gesture_Recognizer
# Class : Leap_Synth
# ------------------
# base class for this application
class Leap_Synth:
#--- Member Objects ---
listener = None
controller = None
max_interface = None
gesture_recognizer = None
# Function: Constructor
# ---------------------
# initializes member objects
def __init__ (self):
print_welcome ()
### Step 1: create the listener, controller and connect the two ###
self.listener = Synth_Listener ()
self.controller = Leap.Controller ()
self.controller.add_listener (self.listener)
### Step 2: create controller and gesture recognizer ###
self.max_interface = Max_Interface ()
self.gesture_recognizer = Gesture_Recognizer ()
# Function: Destructor
# --------------------
# removes the listener from the controller
def __del__ (self):
### Step 1: turn off the max patch ###
self.max_interface.send_gesture ('Stop')
### Step 2: remove leap listener ###
self.controller.remove_listener(self.listener)
# Function: get_frame
# -------------------
# blocks until it gets a new frame from the listener
def get_frame (self):
while (self.listener.new_frame_available == False):
pass
frame = self.listener.most_recent_frame
self.listener.new_frame_available = False
return frame
########################################################################################################################
##############################[ --- User Interface --- ]################################################################
########################################################################################################################
# Function: interface_main
# ------------------------
# main function for all interface
def interface_main (self):
viable_options =['r', 't', 's']
### Step 1: get their requested mode ###
print_message ("What mode would you like to enter?")
print " - R: record mode"
print " - T: train mode"
print " - S: synth mode"
response = raw_input ("---> ")
response = response.lower ()
if response == 'r':
while (True):
self.record_main ()
elif response == 't':
self.train_main ()
elif response == 's':
while (True):
self.synth_main ()
else:
print_message("Error: did not recognize that option")
self.interface_main ()
# Function: record_main
# ---------------------
# interface for recording gestures
def record_main (self):
while (True):
print_message ("What would you like to do?")
print " - R: record a new gesture"
print " - Q: quit"
response = raw_input ("---> ")
response = response.lower ()
if response == 'q':
exit ()
else:
self.record_gesture ()
# Function: record_countdown
# --------------------------
# prints out a countdown
def record_countdown (self):
print "3"
time.sleep (0.5)
print "2"
time.sleep (0.5)
print "1"
time.sleep (0.5)
print "--- record ---"
# Function: record_gesture
# ------------------------
# record a single gesture
def record_gesture (self):
num_examples_recorded = 0
max_examples = 10
### Step 1: have them name the gesture ###
print_message ("What is this gesture called?")
gesture_name = raw_input("---> ")
print_message ("Now we will begin recording " + str(max_examples) + " examples of this gesture, " + str(gesture_name) + ". Press Enter when ready.")
sys.stdin.readline ()
record_gesture = Gesture (gesture_name)
#--- initialize parameters ---
is_recording = False
num_frames_recorded = 0
while (num_examples_recorded < max_examples):
frame = self.get_frame ()
record_gesture.add_frame (frame)
if record_gesture.is_full ():
### --- Notify of recording status --- ###
if is_recording:
print "."
num_frames_recorded += 1
else:
print "x"
### --- Check if we should end the recording --- ###
if num_frames_recorded >= record_gesture.gesture_length:
print_message ("### Recording Complete ###")
is_recording = False
num_frames_recorded = 0
num_examples_recorded += 1
self.gesture_recognizer.save_gesture(record_gesture)
### --- Check if we should start the recording --- ###
while sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
line = sys.stdin.readline()
if line:
print_message ("### Started Recording ###")
is_recording = True
# Function: train_main
# --------------------
# train the classifier
def train_main (self):
### Step 1: load in the data and print out stats about it ###
print_status ("Gesture_Recognizer", "Loading Data")
self.gesture_recognizer.load_data ()
# self.gesture_recognizer.eliminate_second_hand ()
self.gesture_recognizer.print_data_stats ()
### Step 2: cluster the poses ###
print_status ("Gesture_Recognizer", "Training Model")
self.gesture_recognizer.train_model ()
########################################################################################################################
##############################[ --- Synth Main --- ]####################################################################
########################################################################################################################
# Function: synth_main_disrcete
# --------------------
# records discrete gestures and classifies them for you.
def synth_main_discrete (self):
self.gesture_recognizer.load_model ()
print_message ("Recording Discrete events")
while (True):
### Step 1: initialize the gesture ###
observed_gesture = Gesture ()
### Step 2: start the recording ###
self.record_countdown ()
### Step 3: fill it with frames ###
while not observed_gesture.is_full ():
frame = self.get_frame ()
observed_gesture.add_frame (frame)
### Step 4: stop the recording and classify ###
print_message ("### Recording Complete ###")
self.gesture_recognizer.classify_gesture (observed_gesture)
print_message("enter to continue")
sys.stdin.readline ()
# Function: get_gesture
# ---------------------
# function to wait on gestures
def get_gesture (self, observed_gesture):
### --- add the current frame --- ###
frame = self.get_frame ()
observed_gesture.add_frame (frame)
if observed_gesture.is_full ():
### --- get classification results --- ###
classification_results = self.gesture_recognizer.classify_gesture (observed_gesture)
### --- interpret them --- ###
return self.classification_results
# Function: get_coords
# --------------------
# returns (x,y,z) of the hand
def get_continuous_coords (self):
### --- add the current frame --- ###
frame = self.get_frame ()
if len(frame.hands) == 0:
return None
else:
position = hands[0].palm_position
return (position[0], position[1], posiiton[2])
# Function: get_position_and_orientation
# --------------------------------------
# given a frame, this returns the (palm_position, palm_orientation) if we observe
# a fist (0 fingers visible); (None, None) otherwise
def get_position_and_orientation (self, frame):
### Step 1: peace out if there are no hands ###
hands = frame.hands
if len(hands) == 0:
return (None, None)
### Step 2: peace out if there are any fingers (not a fist) ###
fingers = hands[0].fingers
if len (fingers) > 0:
return (None, None)
### Step 3: get position and orientation ###
hand = hands[0]
palm_position = hands[0].palm_position
position = (palm_position[0], palm_position[1], palm_position[2])
palm_normal = hands[0].palm_normal
orientation = (palm_normal[0], palm_normal[1], palm_normal[2])
return (position, orientation)
# Function: synth_main
# --------------------
# maintains a 70-frame gesture and tries to classify it
def synth_main (self):
### Step 1: start the max patch ###
self.max_interface.send_gesture ('Start')
### Step 2: initialize local data ###
print_message ("Entering Main Loop: Continuous Gesture Recognition")
self.gesture_recognizer.load_model ()
observed_gesture = Gesture ()
### Step 3: enter main loop ###
while (True):
### Step 1: add the current frame to observed_gesture ###
frame = self.get_frame ()
observed_gesture.add_frame (frame)
### Step 2: get position and orientation (returns (None, None) if not a fist) ###
(palm_position, palm_orientation) = self.get_position_and_orientation (frame)
### Step 3: Get the gesture, if appropriate ###
send_gesture = None
if observed_gesture.is_full ():
classification_results = self.gesture_recognizer.classify_gesture (observed_gesture)
if classification_results:
prediction = classification_results [0]
prediction_prob = classification_results [1]
print_message("Prediction: " + str(prediction) + " | Probability: " + str(prediction_prob))
send_gesture = prediction
observed_gesture.clear ()
### Step 4: send a gesture to max if one was observed ###
if send_gesture:
self.max_interface.send_gesture (send_gesture)
### Step 5: Send hand state to max if one was observed ###
if len(frame.hands) > 0:
self.max_interface.send_hand_state (frame.hands[0])
# Function: main
# --------------
# contains all main operation of the program
def main():
### Step 1: create Leap_Synth object ###
leap_synth = Leap_Synth ()
time.sleep (0.7)
### Step 2: enter main interface ###
leap_synth.interface_main ()
if __name__ == "__main__":
main ()