-
Notifications
You must be signed in to change notification settings - Fork 5
/
main_pred_v2.py
401 lines (338 loc) · 18.3 KB
/
main_pred_v2.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
from datetime import datetime
import glob
import os
import subprocess
import time
import PySimpleGUI as sg
from fun_microDoppler_2243_complex import microDoppler
# from fun_rangeDoppler_2243_complex import rangeDoppler
from helpers import convert_to_bytes
from prediction import *
fname = 'data/raw_data_Raw_0.bin'
pred_filename = fname.replace('.bin', '_py.png')
# pred_filename = 'train_folder/maybe_04_py.png'
model_path = 'keras_model.h5'
new_model_name = 'keras_custom.h5'
im_size_default = (224, 224)
im_size = (128, 128)
train_path = 'train_folder'
sudo_password = '190396'
cwd = '/home/uarspl/Desktop/77ghz/CLI/CLI/Release'
radar_path = '/home/uarspl/Desktop/77ghz/CLI/open_radar_initiative-new_receive_test/setup_radar/build'
radar_cmd = './setup_radar'
export_cmd = 'LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$pwd'
# env = dict(os.environ)
# env = os.environ.copy()
# env['LD_LIBRARY_PATH'] = cwd + ':' + os.getcwd()
os.environ["LD_LIBRARY_PATH"] = cwd # + ':' + os.getcwd() # error code 127 when not executed
# print(env)
# os.system("LD_LIBRARY_PATH={}".format(os.getcwd()))
# env = dict(os.environ)
# env = os.environ.copy()
# env['LD_LIBRARY_PATH'] = '"{}":{}'.format(
# os.getcwd(), env.get('LD_LIBRARY_PATH', ''))
# print(env)
CREATE_NO_WINDOW = 0x08000000
# startupinfo = None
# if os.name == 'nt':
# startupinfo = subprocess.STARTUPINFO()
# startupinfo.wShowWindow = SW_HIDE
# startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
dca = './DCA1000EVM_CLI_Control'
fpga_cmd = './DCA1000EVM_CLI_Control fpga cf.json'.split()
# fpga_cmd = ["./DCA1000EVM_CLI_Control", "fpga cf.json"]
record_cmd = './DCA1000EVM_CLI_Control record cf.json'.split()
start_cmd = './DCA1000EVM_CLI_Control start_record cf.json'.split()
stop_cmd = './DCA1000EVM_CLI_Control stop_record cf.json'.split()
spect_size = (500, 300)
bcols = ['blue', 'orange', 'green']
BAR_WIDTH = 100
BAR_SPACING = 200
EDGE_OFFSET = 100
GRAPH_SIZE = (900, 300)
DATA_SIZE = (GRAPH_SIZE[0], 120)
graph = sg.Graph(GRAPH_SIZE, (0, 0), DATA_SIZE, background_color='white')
graph.erase()
myfont = "Ariel 18"
font_size = 20
sg.theme("LightGrey3")
layout = [[sg.Text('Radar User Interface', size=(50, 2), font=('courier', font_size))],
[sg.Button('Setup Radar', button_color=('white', 'black'), size=(18, 2), font=('courier', font_size)),
sg.Text('', key='setup_text', font=('courier', 20)),
sg.Exit(button_color=('white', 'black'), size=(18, 2), font=('courier', font_size))],
[sg.Image('data/md.png', key='-IMAGE-', size=spect_size),
sg.VSep(),
graph],
[sg.Button('1. Start Recording', button_color=('white', '#00b408'), size=(18, 2), font=('courier', font_size)),
sg.Text(' ', key='start_text', font=('courier', font_size))],
# sg.VSep(),
# sg.InputText(size=(10, 5), key='class_name', font=('courier', font_size), default_text='CLASS_NAME'),
# sg.Text('Type the new class name here.\n(MAYBE and YOU are the default two classes.)',
# key='default_classes', font=('courier', 20))],
[sg.Button('2. Stop Recording', button_color=('white', '#f60239'), size=(18, 2), font=('courier', font_size)),
sg.Text(' ', key='stop_text', font=('courier', font_size))],
# sg.VSep(),
# sg.Button('Start Training\nData Recording', key='train_start_record', button_color=('white', 'green'),
# size=(18, 2), font=('courier', font_size)),
# sg.Text('', key='train_start_record_text', font=('courier', font_size))],
[sg.Button('3. Visualize', button_color=('white', '#009ffA'), size=(18, 2),
font=('courier', font_size)),
sg.Text(' ', key='md_text', font=('courier', font_size))],
# sg.VSep(),
# sg.Button('Stop Training\nData Recording', key='train_stop_record', button_color=('white', 'red'),
# size=(18, 2), font=('courier', font_size)),
# sg.Text('', key='train_stop_record_text', font=('courier', font_size))],
[sg.Button('4. Predict', button_color=('black', '#ffdc3d'), size=(18, 2), font=('courier', font_size)),
sg.Text(' ', key='pred_text', font=('courier', font_size))],
# sg.VSep(),
# sg.Button('Train the model', key='train', button_color=('white', 'blue'),
# size=(18, 2), font=('courier', font_size)),
# sg.Text('Learning\n Rate:', size=(8, 2), font=('courier', font_size)),
# sg.Combo(values=[0.001, 0.01, 0.1], default_value=0.001, key='learn_rate', size=(5, 12),
# font=('courier', font_size)),
# sg.VSep(),
# sg.Text('Num. of\nEpochs:', size=(7, 2), font=('courier', font_size)),
# sg.Combo(values=[10, 20, 50], default_value=50, key='epochs', size=(5, 12),
# font=('courier', font_size)),
# sg.VSep(),
# sg.Text('Num. of\nLayers:', size=(7, 2), font=('courier', font_size)),
# sg.Combo(values=[1, 3, 5, 10], default_value=3, key='layers', size=(5, 12),
# font=('courier', font_size))],
# [sg.Button('', key='somekey', button_color=('black', 'gray'), size=(18, 2), font=('courier', font_size)),
# sg.Text(' ', key='some_text', font=('courier', font_size)),
# sg.VSep(),
# sg.Button('Predict on\nNew Model', key='pred_new', button_color=('black', 'yellow'),
# size=(18, 2), font=('courier', font_size)),
# sg.Text('', key='pred_new_text', font=('courier', font_size))]
]
window = sg.Window('Radar GUI', layout, size=(1920, 1080)) #.Layout(layout)
while True: # Event Loop
event, values = window.Read()
if event in (None, 'Exit'):
break
if event == 'Setup Radar':
window['md_text'].update(' ')
# window['rd_text'].update('')
window['stop_text'].update(' ')
window['start_text'].update(' ')
# std = subprocess.run(radar_cmd, cwd=radar_path, shell=True, stdin=subprocess.PIPE, # check=True,
# stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
# print('setup_radar: ', std.stderr)
pwd = subprocess.Popen(['echo', sudo_password], cwd=radar_path, stdout=subprocess.PIPE)
pwd.wait()
print('sudopass: ', pwd.stdout.read())
cmd = subprocess.Popen(['sudo', '-S'] + [radar_cmd], cwd=radar_path, stdin=pwd.stdout,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
cmd.wait()
print('setup_radar error return code: ', cmd.stderr.read())
cmd = subprocess.Popen(fpga_cmd, cwd=cwd, shell=False, stdin=subprocess.PIPE, text=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE) # , check=True)
cmd.wait()
print('fpga error return code: ', cmd.returncode)
print('fpga error2: ', cmd.stderr.read())
print('fpga stdout: ', cmd.stdout.read())
cmd = subprocess.Popen(record_cmd, cwd=cwd, shell=False, stdin=subprocess.PIPE, text=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE) # , check=True)
cmd.wait()
# cmd.stdin.write("record cf.json")
# cmd.wait()
print('record error return code: ', cmd.returncode)
print('record error2: ', cmd.stderr.read())
print('record stdout: ', cmd.stdout.read())
if cmd.returncode == 0:
window['setup_text'].update('Radar is ready to go!')
else:
window['setup_text'].update('Radar is set already!')
elif event == '1. Start Recording':
window['-IMAGE-'].update('data/md.png', size=spect_size)
# window['-VIDEO-'].update('data/rd.png')
window['md_text'].update(' ')
# window['rd_text'].update('')
window['stop_text'].update(' ')
window['start_text'].update('Go! ')
# window['setup_text'].update(' ')
window.refresh()
cmd = subprocess.Popen(start_cmd, cwd=cwd, shell=False, stdin=subprocess.PIPE, text=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
cmd.wait()
# cmd.stdin.write("start_record cf.json")
# cmd.wait()
print('start error return code: ', cmd.returncode)
print('start error2: ', cmd.stderr.read())
print('start stdout: ', cmd.stdout.read())
elif event == '2. Stop Recording':
window['stop_text'].update('Stopping... ')
window['md_text'].update(' ')
# window['rd_text'].update('')
window['start_text'].update(' ')
# window['setup_text'].update(' ')
window.refresh()
# cmd = subprocess.Popen(['sudo', '-S', 'kill', '`ps -e | grep -i gnome-terminal`'], cwd=cwd, shell=False,
# stdin=pwd.stdout, text=True,
# stdout=subprocess.PIPE, stderr=subprocess.PIPE) # , check=True)
# cmd.wait()
time.sleep(2)
pid = subprocess.check_output(['pgrep gnome-terminal'], shell=True) # , check=True)
print('pid stdoutstr: ' + str(pid.decode())[:-1] + '-')
cmd = subprocess.Popen(['kill', str(pid.decode())[:-1]], cwd=cwd, shell=False,
stdin=subprocess.PIPE, text=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE) # , check=True)
cmd.wait()
print('exit error return code: ', cmd.returncode)
print('exit error2: ', cmd.stderr.read())
print('exit stdout: ', cmd.stdout.read())
cmd = subprocess.Popen(stop_cmd, cwd=cwd, shell=False, stdin=subprocess.PIPE, text=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE) # , check=True)
cmd.wait()
# cmd.stdin.write("stop_record cf.json")
# cmd.wait()
print('stop error return code: ', cmd.returncode)
print('stop error2: ', cmd.stderr.read())
print('stop stdout: ', cmd.stdout.read())
window['stop_text'].update('Done! ')
window.refresh()
elif event == '3. Visualize':
window['md_text'].update('Generating Micro-Doppler Signature...')
window['stop_text'].update(' ')
window.refresh()
microDoppler(fname)
window['-IMAGE-'].update(data=convert_to_bytes(fname[:-4] + '_py.png', resize=spect_size))
window['md_text'].update('Done! ')
window.refresh()
elif event == '4. Predict':
window['pred_text'].update('Predicting... ')
window['md_text'].update(' ')
window.refresh()
pred = prediction(model_path, pred_filename, im_size_default)
maybe = pred[0][0] * 100
you = pred[0][1] * 100
# add offset for visualization purposes
offset = 3
if maybe > you:
you += offset
you_offset = offset
maybe_offset = 0
else:
maybe += offset
you_offset = 0
maybe_offset = offset
graph.erase()
graph.draw_rectangle(top_left=(0 * BAR_SPACING + EDGE_OFFSET, maybe),
bottom_right=(0 * BAR_SPACING + EDGE_OFFSET + BAR_WIDTH, 0), fill_color=bcols[0])
graph.draw_rectangle(top_left=(1 * BAR_SPACING + EDGE_OFFSET, you),
bottom_right=(1 * BAR_SPACING + EDGE_OFFSET + BAR_WIDTH, 0), fill_color=bcols[1])
graph.draw_text(text='MAYBE --> ' + str(round(maybe - maybe_offset, 2))+'%',
location=(0 * BAR_SPACING + EDGE_OFFSET + 70, maybe + 10), color=bcols[0], font=myfont)
graph.draw_text(text='YOU --> ' + str(round(you - you_offset, 2)) + '%',
location=(1 * BAR_SPACING + EDGE_OFFSET + 70, you + 10), color=bcols[1], font=myfont)
window['pred_text'].update('Predicted! ')
window.refresh()
elif event == 'train_start_record':
window['-IMAGE-'].update('data/md.png', size=spect_size)
# window['-VIDEO-'].update('data/rd.png')
window['md_text'].update(' ')
# window['rd_text'].update('')
window['stop_text'].update(' ')
window['train_start_record_text'].update('Go!')
# window['setup_text'].update
graph.erase()
window.refresh()
cmd = subprocess.Popen(start_cmd, cwd=cwd, shell=False, stdin=subprocess.PIPE, text=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
cmd.wait()
# cmd.stdin.write("start_record cf.json")
# cmd.wait()
print('train record start error return code: ', cmd.returncode)
print('train record start error2: ', cmd.stderr.read())
print('train record start stdout: ', cmd.stdout.read())
elif event == 'train_stop_record':
window['train_stop_record_text'].update('Stopping...')
window['md_text'].update(' ')
# window['rd_text'].update('')
window['train_start_record_text'].update('')
# window['setup_text'].update(' ')
window.refresh()
time.sleep(2)
# cmd = subprocess.Popen(['sudo', '-S', 'kill', '`ps -e | grep -i gnome-terminal`'], cwd=cwd, shell=False,
# stdin=pwd.stdout, text=True,
# stdout=subprocess.PIPE, stderr=subprocess.PIPE) # , check=True)
# cmd.wait()
# time.sleep(2)
pid = subprocess.check_output(['pgrep gnome-terminal'], shell=True) # , check=True)
print('pid stdoutstr: ' + str(pid.decode())[:-1] + '-')
cmd = subprocess.Popen(['kill', str(pid.decode())[:-1]], cwd=cwd, shell=False,
stdin=subprocess.PIPE, text=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE) # , check=True)
cmd.wait()
print('exit error return code: ', cmd.returncode)
print('exit error2: ', cmd.stderr.read())
print('exit stdout: ', cmd.stdout.read())
cmd = subprocess.Popen(stop_cmd, cwd=cwd, shell=False, stdin=subprocess.PIPE, text=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE) # , check=True)
cmd.wait()
# cmd.stdin.write("stop_record cf.json")
# cmd.wait()
print('stop error return code: ', cmd.returncode)
print('stop error2: ', cmd.stderr.read())
print('stop stdout: ', cmd.stdout.read())
window['train_stop_record_text'].update('Generating Micro-Doppler Signature...')
window.refresh()
microDoppler(fname)
window['-IMAGE-'].update(data=convert_to_bytes(fname[:-4] + '_py.png', resize=spect_size))
# rename to move it to the data folder
now = datetime.now()
date_time = now.strftime("_%Y_%m_%d_%H_%M_%S_")
src = fname[:-4] + '_py.png'
dst = src.replace('data', train_path).replace('raw', values['class_name'] + date_time)
os.rename(src, dst)
files = glob.glob(train_path + '/' + values['class_name'] + '*.png')
window['train_stop_record_text'].update('Done! Num. of files: ' + str(len(files)))
elif event == 'train':
is_split = True # whether train/test split enabled
data = preprocess(train_path, values['class_name'], im_size, is_split)
model, history = CNN_train(data, is_split, values['layers'], values['learn_rate'], 1, values['epochs'],
model_path)
elif event == 'pred_new':
window['pred_text'].update(' ')
window['pred_new_text'].update('Predicting...')
window.refresh()
pred = prediction(model_path, pred_filename, im_size)
maybe = pred[0][0] * 100
you = pred[0][1] * 100
custom_class = pred[0][2] * 100
# add offset for visualization purposes
offset = 3
if maybe > you and maybe > custom_class:
you += offset
custom_class += offset
you_offset = offset
custom_class_offset = offset
maybe_offset = 0
elif you > maybe and you > custom_class:
maybe += offset
custom_class += offset
you_offset = 0
maybe_offset = offset
custom_class_offset = offset
elif custom_class > you and custom_class > maybe:
maybe += offset
you += offset
custom_class_offset = 0
maybe_offset = offset
you_offset = offset
graph.erase()
graph.draw_rectangle(top_left=(0 * BAR_SPACING + EDGE_OFFSET, maybe),
bottom_right=(0 * BAR_SPACING + EDGE_OFFSET + BAR_WIDTH, 0), fill_color=bcols[0])
graph.draw_text(text='MAYBE --> ' + str(round(maybe - maybe_offset, 2)) + '%',
location=(0 * BAR_SPACING + EDGE_OFFSET + 70, maybe + 10), color=bcols[0], font=myfont)
graph.draw_rectangle(top_left=(1 * BAR_SPACING + EDGE_OFFSET, you),
bottom_right=(1 * BAR_SPACING + EDGE_OFFSET + BAR_WIDTH, 0), fill_color=bcols[1])
graph.draw_text(text='YOU --> ' + str(round(you - you_offset, 2)) + '%',
location=(1 * BAR_SPACING + EDGE_OFFSET + 70, you + 10), color=bcols[1], font=myfont)
graph.draw_rectangle(top_left=(2 * BAR_SPACING + EDGE_OFFSET, custom_class),
bottom_right=(2 * BAR_SPACING + EDGE_OFFSET + BAR_WIDTH, 0), fill_color=bcols[2])
graph.draw_text(text=values['class_name'].upper() + ' --> ' + str(round(custom_class - custom_class_offset, 2)) + '%',
location=(2 * BAR_SPACING + EDGE_OFFSET + 80, custom_class + 10), color=bcols[2], font=myfont)
window.refresh()
window['pred_new_text'].update('Predicted!')
window.Close()