-
Notifications
You must be signed in to change notification settings - Fork 12
/
record.py
295 lines (249 loc) · 8 KB
/
record.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
'''
Author : Siddharth Nayak
email:[email protected]
'''
import serial, os, sys,signals,signals1
import numpy as np
from sklearn.externals import joblib
'''
This module is for recording new data
If the serial port prints "STARTING BATCH" the recording is started
If the serial port prints "CLOSING BATCH" the recording is done and waits for the next batch
'''
#Mode parameters, controlled using sys.argv by the terminal
SAVE_NEW_SAMPLES = False
FULL_CYCLE = False
ENABLE_WRITE = False
TARGET_ALL_MODE = False
DELETE_ALL_ENABLED = False
TRY_TO_PREDICT=True
#Serial parameters
SERIAL_PORT = "/dev/cu.usbmodem1421"
BAUD_RATE = 38400
TIMEOUT = 100
#Recording parameters
target_sign = "a"
current_batch = "0"
target_directory = "data12345"
current_test_index = 0
#Analyzes the arguments to enable a specific mode
arguments = {}
for i in sys.argv[1:]:
if "=" in i:
sub_args = i.split("=")
arguments[sub_args[0]]=sub_args[1]
else:
arguments[i]=None
#If there are arguments, analyzes them
if len(sys.argv)>1:
if arguments.has_key("target"):
target_sign = arguments["target"].split(":")[0]
current_batch = arguments["target"].split(":")[1]
print ("TARGET SIGN: '{sign}' USING BATCH: {batch}".format(sign=target_sign, batch = current_batch))
SAVE_NEW_SAMPLES = True
if arguments.has_key("write"):
ENABLE_WRITE = True
if arguments.has_key("test"):
current_batch = arguments["test"]
TARGET_ALL_MODE = True
SAVE_NEW_SAMPLES = True
if arguments.has_key("port"):
SERIAL_PORT = arguments["port"]
clf = None
classes = None
sentence = ""
print ("OPENING SERIAL_PORT '{port}' WITH BAUDRATE {baud}...".format(port = SERIAL_PORT, baud = BAUD_RATE))
print ("IMPORTANT!")
print ("To end the program hold Ctrl+C and send some data over serial")
#Opens the serial port specified by SERIAL_PORT with the specified BAUD_RATE
ser = serial.Serial(SERIAL_PORT, BAUD_RATE, timeout = TIMEOUT)
output = []
in_loop = True
is_recording = False
current_sample = 37
#Resets the output file
output_file = open("output.txt","w")
output_file.write("")
output_file.close()
if TRY_TO_PREDICT:
print ("Loading model...")
clf = joblib.load('model_123456.pkl')
classes = joblib.load('classes_123456.pkl')
clf1=joblib.load('model_flex1.pkl')
classes1=joblib.load('classes_flex1.pkl')
print('Model Loaded')
try:
while in_loop:
#Read a line over serial and deletes the line terminators
line = ser.readline().replace("\r\n","")
#If it receive "STARTING BATCH" it starts the recording
if line=="STARTING BATCH":
#Enable the recording
is_recording = True
#Reset the buffer
output = []
print("RECORDING...",)
elif line=="CLOSING BATCH": #Stops recording and analyzes the result
#Disable recording
is_recording = False
if len(output)>1: #If less than 1, it means error
print ("DONE, SAVING...",)
#If TARGET_ALL_MODE is enabled changes the target sign
#according to the position
if TARGET_ALL_MODE:
if current_test_index<len(test_sentence):
target_sign = test_sentence[current_test_index]
else:
#At the end of the sentence, it quits
print ("Target All Ended!")
quit()
#Generates the filename based on the target sign, batch and progressive number
filename = "{sign}_sample_{batch}_{number}.txt".format(sign = target_sign, batch = current_batch, number = current_sample)
#Generates the path
path = target_directory + os.sep + filename
#If SAVE_NEW_SAMPLES is False, it saves the recording to a temporary file
if SAVE_NEW_SAMPLES == False:
path = "tmp.txt"
filename = "tmp.txt"
#Saves the recording in a file
f = open(path, "w")
f.write('\n'.join(output))
f.close()
print ("SAVED IN {filename}".format(filename = filename))
current_sample += 1
#If TRY_TO_PREDICT is True, it utilizes the model to predict the recording
if TRY_TO_PREDICT:
print('PREDICTING')
sample_test = signals.Sample.load_from_file(path)
linearized_sample = sample_test.get_linearized(reshape=True)
number = clf.predict(linearized_sample)
char = chr(ord('a')+number[0])
#last_word = sentence.split(" ")[-1:][0]
'''if char=='d' or char=='e' or char=='i' or char=='j' or char=='g' or char=='k':
print('wait')
sample_test1 = signals1.Sample.load_from_file(path)
linearized_sample1 = sample_test1.get_linearized(reshape=True)
number1 = clf1.predict(linearized_sample1)
if number1==0:
print('Column Formation')
if number1==1:
print('File Formation')
if number1==2:
print('Ammunition')
if number1==3:
print('Vehicle')
if number1==4:
print('Rally Point')
if number==5:
print("I don't Understand")'''
#print(char)
if char=='a':
print('Come')
if char=='b':
print('Hurry Up')
if char=='d':
print('Column Formation')
if char=='e':
print('File Formation')
if char=='i':
print('Ammunition')
if char=='j':
print('Vehicle')
if char=='g':
print('Rally Point')
if char=='k':
print("I don't Understand")
if char=='c':
print('Go Here or Move U')
if char=='f':
print('Wedge Formation')
if char=='h':
print('Shotgun')
if char=='l':
print('Crouch or Go Prone')
if char=='m':
print('Window')
if char=='n':
print('Door')
if char=='o':
print('Point of Entry')
else: #In case of a corrupted sequence
print ("ERROR...")
current_test_index -= 1
elif line=="CLOSING BATCH1": #Stops recording and analyzes the result
#Disable recording
is_recording = False
if len(output)>1: #If less than 1, it means error
print ("DONE, SAVING...",)
#If TARGET_ALL_MODE is enabled changes the target sign
#according to the position
if TARGET_ALL_MODE:
if current_test_index<len(test_sentence):
target_sign = test_sentence[current_test_index]
else:
#At the end of the sentence, it quits
print ("Target All Ended!")
quit()
#Generates the filename based on the target sign, batch and progressive number
filename = "{sign}_sample_{batch}_{number}.txt".format(sign = target_sign, batch = current_batch, number = current_sample)
#Generates the path
path = target_directory + os.sep + filename
#If SAVE_NEW_SAMPLES is False, it saves the recording to a temporary file
if SAVE_NEW_SAMPLES == False:
path = "tmp.txt"
filename = "tmp.txt"
#Saves the recording in a file
f = open(path, "w")
f.write('\n'.join(output))
f.close()
print ("SAVED IN {filename}".format(filename = filename))
current_sample += 1
#If TRY_TO_PREDICT is True, it utilizes the model to predict the recording
if TRY_TO_PREDICT:
print('PREDICTING')
sample_test = signals.Sample.load_from_file(path)
linearized_sample = sample_test.get_linearized(reshape=True)
number = clf.predict(linearized_sample)
char = chr(ord('a')+number[0])
last_word = sentence.split(" ")[-1:][0]
#print(char)
if char=='a':
print('Come')
if char=='b':
print('Hurry Up')
if char=='c':
print('Go Here or Move U')
if char=='d':
print('Column Formation')
if char=='e':
print('File Formation')
if char=='f':
print('Wedge Formation')
if char=='g':
print('Rally Point')
if char=='h':
print('Shotgun')
if char=='i':
print('Ammunition')
if char=='j':
print('Vehicle')
if char=='k':
print("I don't Understand")
if char=='l':
print('Crouch or Go Prone')
if char=='m':
print('Window')
if char=='n':
print('Door')
if char=='o':
print('Point of Entry')
else: #In case of a corrupted sequence
print ("ERROR...")
current_test_index -= 1
else:
#Append the current signal line in the recording
output.append(line)
except KeyboardInterrupt: #When Ctrl+C is pressed, the loop terminates
print ('CLOSED LOOP!')
#Closes the serial port
ser.close()