-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
executable file
·259 lines (183 loc) · 7.42 KB
/
controller.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
#!/Library/Frameworks/Python.framework/Versions/2.7/bin/python
# *------------------------------------------------------------ *
# * Script: leap_synth.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
#--- 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_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):
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 not response in viable_options:
print_error ("Main Interface Loop", "did not recognize the mode you selected")
if response == 'r':
while (True):
self.record_main ()
elif response == 't':
self.train_main ()
else:
while (True):
self.synth_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 (enter name of 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 = 20
frames_per_example = 100
### 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. Press Enter when ready.")
sys.stdin.readline ()
while (num_examples_recorded < max_examples):
### Step 2: start the recording ###
self.record_countdown ()
self.gesture_recognizer.start_recording_gesture (gesture_name)
self.is_recording = True
### Step 3: get a single frame ###
num_frames_recorded = 0
while (num_frames_recorded < 100):
frame = self.get_frame ()
self.gesture_recognizer.add_frame_to_recording (frame)
num_frames_recorded += 1
### Step 4: stop the recording ###
print_message ("### Recording Complete ###")
self.gesture_recognizer.stop_recording_gesture ()
self.is_recording = False
num_examples_recorded += 1
# 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
# --------------------
# records discrete gestures and classifies them for you.
def synth_main (self):
print_message ("Recording Discrete events")
while (True):
### Step 1: initialize the gesture ###
observed_gesture = []
### Step 2: start the recording ###
self.record_countdown ()
### Step 3: fill it with frames ###
num_frames_recorded = 0
while (num_frames_recorded < 100):
frame = self.get_frame ()
pose = Pose(frame)
observed_gesture.append (pose.features)
num_frames_recorded += 1
### Step 4: stop the recording and classify ###
print_message ("### Recording Complete ###")
self.gesture_recognizer.classify_gesture ("")
print_message("enter to continue")
sys.stdin.readline ()
# 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 ()