-
Notifications
You must be signed in to change notification settings - Fork 26
/
thesis_code_realistic_movements.py
605 lines (525 loc) · 24 KB
/
thesis_code_realistic_movements.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
from __future__ import print_function
from collections import deque
from threading import Lock, Thread
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
import numpy as np
#np.random.seed(1)
import tensorflow as tf
from tensorflow import keras
from keras import regularizers
from keras.models import load_model
from sklearn import preprocessing
import myo
import time
import sys
import psutil
import os
# Used for bluetooth signal sending
import serial
# This training set will contain 1000 samples of 8 sensor values
global training_set
global number_of_samples
global index_training_set, middle_training_set,thumb_training_set,verification_set
global data_array
number_of_samples = 1000
data_array=[]
# 8 Sensors in armband
Sensor1 = np.zeros((1,number_of_samples))
Sensor2 = np.zeros((1,number_of_samples))
Sensor3 = np.zeros((1,number_of_samples))
Sensor4 = np.zeros((1,number_of_samples))
Sensor5 = np.zeros((1,number_of_samples))
Sensor6 = np.zeros((1,number_of_samples))
Sensor7 = np.zeros((1,number_of_samples))
Sensor8 = np.zeros((1,number_of_samples))
# 12 finger movements
index_open_training_set = np.zeros((8,number_of_samples))
middle_open_training_set = np.zeros((8,number_of_samples))
thumb_open_training_set = np.zeros((8,number_of_samples))
ring_open_training_set = np.zeros((8,number_of_samples))
pinky_open_training_set = np.zeros((8,number_of_samples))
two_open_training_set = np.zeros((8,number_of_samples))
three_open_training_set = np.zeros((8,number_of_samples))
four_open_training_set = np.zeros((8,number_of_samples))
five_open_training_set = np.zeros((8,number_of_samples))
all_fingers_closed_training_set = np.zeros((8,number_of_samples))
grasp_training_set = np.zeros((8,number_of_samples))
pick_training_set = np.zeros((8,number_of_samples))
verification_set = np.zeros((8,number_of_samples))
training_set = np.zeros((8,number_of_samples))
thumb_open_label = 0
index_open_label = 1
middle_open_label = 2
ring_open_label = 3
pinky_open_label = 4
two_open_label = 5
three_open_label = 6
four_open_label = 7
five_open_label = 8
all_fingers_closed_label = 9
grasp_label = 10
pick_label = 11
name = input("Enter name of Subject")
# Check if Myo Connect.exe process is running
def check_if_process_running():
try:
for proc in psutil.process_iter():
if proc.name()=='Myo Connect.exe':
return True
return False
except (psutil.NoSuchProcess,psutil.AccessDenied, psutil.ZombieProcess):
print (PROCNAME, " not running")
# Restart myo connect.exe process if its not running
def restart_process():
PROCNAME = "Myo Connect.exe"
for proc in psutil.process_iter():
# check whether the process name matches
if proc.name() == PROCNAME:
proc.kill()
# Wait a second
time.sleep(1)
while(check_if_process_running()==False):
path = 'C:\Program Files (x86)\Thalmic Labs\Myo Connect\Myo Connect.exe'
os.startfile(path)
time.sleep(1)
#while(check_if_process_running()==False):
# pass
print("Process started")
return True
# This class from Myo-python SDK listens to EMG signals from armband
class Listener(myo.DeviceListener):
def __init__(self, n):
self.n = n
self.lock = Lock()
self.emg_data_queue = deque(maxlen=n)
def on_connected(self, event):
print("Myo Connected")
self.started = time.time()
event.device.stream_emg(True)
def get_emg_data(self):
with self.lock:
print("H") # Ignore this
def on_emg(self, event):
with self.lock:
self.emg_data_queue.append((event.emg))
if len(list(self.emg_data_queue))>=number_of_samples:
data_array.append(list(self.emg_data_queue))
self.emg_data_queue.clear()
return False
# This method is responsible for training EMG data
def Train(conc_array):
global training_set
global index_open_training_set, middle_open_training_set, thumb_open_training_set, ring_open_training_set, pinky_open_training_set, verification_set
global two_open_training_set, three_open_training_set, four_open_training_set,five_open_training_set,all_fingers_closed_training_set,grasp_training_set,pick_training_set
global number_of_samples
verification_set = np.zeros((8,number_of_samples))
print (number_of_samples)
labels = []
print(conc_array,conc_array.shape)
# This division is to make the iterator for making labels run 30 times in inner loop and 10 times in outer loop running total 300 times for 10 finger movements
samples = conc_array.shape[0]/12
# Now we append all data in training label
# We iterate to make 12 finger movement labels.
for i in range(0,12):
for j in range(0,int(samples)):
labels.append(i)
labels = np.asarray(labels)
print(labels, len(labels),type(labels))
print(conc_array.shape[0])
permutation_function = np.random.permutation(conc_array.shape[0])
total_samples = conc_array.shape[0]
all_shuffled_data,all_shuffled_labels = np.zeros((total_samples,8)),np.zeros((total_samples,8))
all_shuffled_data,all_shuffled_labels = conc_array[permutation_function],labels[permutation_function]
print(all_shuffled_data.shape)
print(all_shuffled_labels.shape)
number_of_training_samples = np.int(np.floor(0.8*total_samples))
train_data = np.zeros((number_of_training_samples,8))
train_labels = np.zeros((number_of_training_samples,8))
print("TS ", number_of_training_samples, " S " , number_of_samples)
number_of_validation_samples = np.int(total_samples-number_of_training_samples)
train_data = all_shuffled_data[0:number_of_training_samples,:]
train_labels = all_shuffled_labels[0:number_of_training_samples,]
print("Length of train data is ", train_data.shape)
validation_data = all_shuffled_data[number_of_training_samples:total_samples,:]
validation_labels = all_shuffled_labels[number_of_training_samples:total_samples,]
print("Length of validation data is ", validation_data.shape , " validation labels is " , validation_labels.shape)
print(train_data,train_labels)
model = keras.Sequential([
# Input dimensions means input columns. Here we have 8 columns, one for each sensor
keras.layers.Dense(8, activation=tf.nn.relu,input_dim=8,kernel_regularizer=regularizers.l2(0.1)),
keras.layers.BatchNormalization(),
keras.layers.Dense(12, activation=tf.nn.softmax)])
adam_optimizer = keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
model.compile(optimizer=adam_optimizer,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
history = model.fit(train_data, train_labels, epochs=300,validation_data=(validation_data,validation_labels),batch_size=16)
model.save('C:/Users/shaya/Desktop/'+name+'_realistic_model.h5')
# Here we display the training and test loss for model
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
averages = number_of_samples/50
# Initializing array for verification_averages
verification_averages = np.zeros((int(averages),8))
while True:
while True:
try:
input("Hold a finger movement and press enter to get its classification")
hub = myo.Hub()
number_of_samples=200
listener = Listener(number_of_samples)
hub.run(listener.on_event,20000)
# Here we send the received number of samples making them a list of 1000 rows 8 columns
verification_set = np.array((data_array[0]))
data_array.clear()
break
except:
while(restart_process()!=True):
pass
# Wait for 3 seconds until Myo Connect.exe starts
time.sleep(3)
verification_set = np.absolute(verification_set)
div = 50
# We add one because iterator below starts from 1
batches = int(number_of_samples/div) + 1
for i in range(1,batches):
verification_averages[i-1,:] = np.mean(verification_set[(i-1)*div:i*div,:],axis=0)
verification_data = verification_averages
print("Verification matrix shape is " , verification_data.shape)
predictions = model.predict(verification_data,batch_size=16)
predicted_value = np.argmax(predictions[0])
print(predictions[0])
print(predicted_value)
if predicted_value == 0:
print("Thumb open")
elif predicted_value == 1:
print("Index finger open")
elif predicted_value == 2:
print("Middle finger open")
elif predicted_value == 3:
print("Ring finger open")
elif predicted_value == 4:
print("Pinky finger open")
elif predicted_value == 5:
print("Two fingers open")
elif predicted_value == 6:
print("Three fingers open")
elif predicted_value == 7:
print("Four fingers open")
elif predicted_value == 8:
print("Five fingers open")
elif predicted_value == 9:
print("All fingers closed")
elif predicted_value == 10:
print("Grasp movement")
elif predicted_value == 11:
print("Pick movement")
else:
pass
#### Here i send the predicted value to Arduino via Bluetooth so that it can open appropriate fingers ####
# While 1 is used because sometimes bluetooth port throws exception in opening the COM Port
# So i keep trying until the data is sent and confirmation received.
while(1):
try:
# Bluetooth at COM6
serialPort = serial.Serial(port="COM6",baudrate=9600,bytesize=8,timeout=2,stopbits=serial.STOPBITS_ONE)
value_to_bluetooth = str(predicted_value).encode()
if predicted_value == 10:
value_to_bluetooth = 'a'.encode()
if predicted_value == 11:
value_to_bluetooth = 'b'.encode()
serialPort.write(value_to_bluetooth)
time.sleep(1)
if serialPort.in_waiting>0:
serialString = serialPort.readline()
print(serialString)
# If we receive what we sent from Arduino bluetooth then all OK else bad value
if serialString == value_to_bluetooth:
print("Received")
else:
print("Bad value")
serialPort.close()
break
except serial.SerialException as e:
#There is no new data from serial port
print (str(e))
except TypeError as e:
print (str(e))
ser.port.close()
def main():
unrecognized_training_set = np.zeros((8,number_of_samples))
index_open_training_set = np.zeros((8,number_of_samples))
middle_open_training_set = np.zeros((8,number_of_samples))
thumb_open_training_set = np.zeros((8,number_of_samples))
ring_open_training_set = np.zeros((8,number_of_samples))
pinky_open_training_set = np.zeros((8,number_of_samples))
two_open_training_set = np.zeros((8,number_of_samples))
three_open_training_set = np.zeros((8,number_of_samples))
four_open_training_set = np.zeros((8,number_of_samples))
five_open_training_set = np.zeros((8,number_of_samples))
all_fingers_closed_training_set = np.zeros((8,number_of_samples))
grasp_training_set = np.zeros((8,number_of_samples))
pick_training_set = np.zeros((8,number_of_samples))
verification_set = np.zeros((8,number_of_samples))
training_set = np.zeros((8,number_of_samples))
# This function kills Myo Connect.exe and restarts it to make sure it is running
# Because sometimes the application does not run even when Myo Connect process is running
# So i think its a good idea to just kill if its not running and restart it
while(restart_process()!=True):
pass
# Wait for 3 seconds until Myo Connect.exe starts
time.sleep(3)
# Initialize the SDK of Myo Armband
myo.init('C:\\Users\\shaya\\AppData\\Local\\Programs\\Python\\Python36\\myo64.dll')
hub = myo.Hub()
listener = Listener(number_of_samples)
legend = ['Sensor 1','Sensor 2','Sensor 3','Sensor 4','Sensor 5','Sensor 6','Sensor 7','Sensor 8']
################## HERE WE GET TRAINING DATA FOR THUMB FINGER OPEN ########
while True:
try:
hub = myo.Hub()
listener = Listener(number_of_samples)
input("Open THUMB ")
hub.run(listener.on_event,20000)
thumb_open_training_set = np.array((data_array[0]))
print(thumb_open_training_set.shape)
data_array.clear()
break
except:
while(restart_process()!=True):
pass
# Wait for 3 seconds until Myo Connect.exe starts
time.sleep(3)
# Here we send the received number of samples making them a list of 1000 rows 8 columns just how we need to feed to tensorflow
################## HERE WE GET TRAINING DATA FOR INDEX FINGER OPEN ########
while True:
try:
input("Open index finger")
hub = myo.Hub()
listener = Listener(number_of_samples)
hub.run(listener.on_event,20000)
# Here we send the received number of samples making them a list of 1000 rows 8 columns
index_open_training_set = np.array((data_array[0]))
data_array.clear()
break
except:
while(restart_process()!=True):
pass
# Wait for 3 seconds until Myo Connect.exe starts
time.sleep(3)
################## HERE WE GET TRAINING DATA FOR MIDDLE FINGER OPEN #################
while True:
try:
input("Open MIDDLE finger")
hub = myo.Hub()
listener = Listener(number_of_samples)
hub.run(listener.on_event,20000)
middle_open_training_set = np.array((data_array[0]))
data_array.clear()
break
except:
while(restart_process()!=True):
pass
# Wait for 3 seconds until Myo Connect.exe starts
time.sleep(3)
# Here we send the received number of samples making them a list of 1000 rows 8 columns
################## HERE WE GET TRAINING DATA FOR RING FINGER OPEN ##########
while True:
try:
input("Open Ring finger")
hub = myo.Hub()
listener = Listener(number_of_samples)
hub.run(listener.on_event,20000)
ring_open_training_set = np.array((data_array[0]))
data_array.clear()
break
except:
while(restart_process()!=True):
pass
# Wait for 3 seconds until Myo Connect.exe starts
time.sleep(3)
################### HERE WE GET TRAINING DATA FOR PINKY FINGER OPEN ####################
while True:
try:
input("Open Pinky finger")
hub = myo.Hub()
listener = Listener(number_of_samples)
hub.run(listener.on_event,20000)
pinky_open_training_set = np.array((data_array[0]))
data_array.clear()
break
except:
while(restart_process()!=True):
pass
# Wait for 3 seconds until Myo Connect.exe starts
time.sleep(3)
################### HERE WE GET TRAINING DATA FOR TWO FINGER OPEN ####################
while True:
try:
input("Open Two fingers")
hub = myo.Hub()
listener = Listener(number_of_samples)
hub.run(listener.on_event,20000)
two_open_training_set = np.array((data_array[0]))
data_array.clear()
break
except:
while(restart_process()!=True):
pass
# Wait for 3 seconds until Myo Connect.exe starts
time.sleep(3)
################### HERE WE GET TRAINING DATA FOR THREE FINGER OPEN ####################
while True:
try:
input("Open Three fingers")
hub = myo.Hub()
listener = Listener(number_of_samples)
hub.run(listener.on_event,20000)
three_open_training_set = np.array((data_array[0]))
data_array.clear()
break
except:
while(restart_process()!=True):
pass
# Wait for 3 seconds until Myo Connect.exe starts
time.sleep(3)
################### HERE WE GET TRAINING DATA FOR THREE FINGER OPEN ####################
while True:
try:
input("Open Four fingers")
hub = myo.Hub()
listener = Listener(number_of_samples)
hub.run(listener.on_event,20000)
four_open_training_set = np.array((data_array[0]))
data_array.clear()
break
except:
while(restart_process()!=True):
pass
# Wait for 3 seconds until Myo Connect.exe starts
time.sleep(3)
################### HERE WE GET TRAINING DATA FOR FIVE FINGER OPEN ####################
while True:
try:
input("Open Five fingers")
hub = myo.Hub()
listener = Listener(number_of_samples)
hub.run(listener.on_event,20000)
five_open_training_set = np.array((data_array[0]))
data_array.clear()
break
except:
while(restart_process()!=True):
pass
# Wait for 3 seconds until Myo Connect.exe starts
time.sleep(3)
################### HERE WE GET TRAINING DATA FOR ALL FINGERS CLOSED ####################
while True:
try:
input("Make all fingers closed")
hub = myo.Hub()
listener = Listener(number_of_samples)
hub.run(listener.on_event,20000)
all_fingers_closed_training_set = np.array((data_array[0]))
data_array.clear()
break
except:
while(restart_process()!=True):
pass
# Wait for 3 seconds until Myo Connect.exe starts
time.sleep(3)
################### HERE WE GET TRAINING DATA FOR GRASP MOVEMENT ####################
while True:
try:
input("Make Grasp movement")
hub = myo.Hub()
listener = Listener(number_of_samples)
hub.run(listener.on_event,20000)
grasp_training_set = np.array((data_array[0]))
data_array.clear()
break
except:
while(restart_process()!=True):
pass
# Wait for 3 seconds until Myo Connect.exe starts
time.sleep(3)
################### HERE WE GET TRAINING DATA FOR PICK MOVEMENT ####################
while True:
try:
input("Make Pick movement")
hub = myo.Hub()
listener = Listener(number_of_samples)
hub.run(listener.on_event,20000)
pick_training_set = np.array((data_array[0]))
data_array.clear()
break
except:
while(restart_process()!=True):
pass
# Wait for 3 seconds until Myo Connect.exe starts
time.sleep(3)
# Absolute of finger open data
thumb_open_training_set = np.absolute(thumb_open_training_set)
index_open_training_set = np.absolute(index_open_training_set)
middle_open_training_set = np.absolute(middle_open_training_set)
ring_open_training_set = np.absolute(ring_open_training_set)
pinky_open_training_set = np.absolute(pinky_open_training_set)
# Absolute of finger close data
two_open_training_set = np.absolute(two_open_training_set)
three_open_training_set = np.absolute(three_open_training_set)
four_open_training_set = np.absolute(four_open_training_set)
five_open_training_set = np.absolute(five_open_training_set)
all_fingers_closed_training_set = np.absolute(all_fingers_closed_training_set)
grasp_training_set = np.absolute(grasp_training_set)
pick_training_set = np.absolute(pick_training_set)
div = 50
averages = int(number_of_samples/div)
thumb_open_averages = np.zeros((int(averages),8))
index_open_averages = np.zeros((int(averages),8))
middle_open_averages = np.zeros((int(averages),8))
ring_open_averages = np.zeros((int(averages),8))
pinky_open_averages = np.zeros((int(averages),8))
two_open_averages = np.zeros((int(averages),8))
three_open_averages = np.zeros((int(averages),8))
four_open_averages = np.zeros((int(averages),8))
five_open_averages = np.zeros((int(averages),8))
all_fingers_closed_averages = np.zeros((int(averages),8))
grasp_averages = np.zeros((int(averages),8))
pick_averages = np.zeros((int(averages),8))
# Here we are calculating the mean values of all finger open data set and storing them as n/50 samples because 50 batches of n samples is equal to n/50 averages
for i in range(1,averages+1):
thumb_open_averages[i-1,:] = np.mean(thumb_open_training_set[(i-1)*div:i*div,:],axis=0)
index_open_averages[i-1,:] = np.mean(index_open_training_set[(i-1)*div:i*div,:],axis=0)
middle_open_averages[i-1,:] = np.mean(middle_open_training_set[(i-1)*div:i*div,:],axis=0)
ring_open_averages[i-1,:] = np.mean(ring_open_training_set[(i-1)*div:i*div,:],axis=0)
pinky_open_averages[i-1,:] = np.mean(pinky_open_training_set[(i-1)*div:i*div,:],axis=0)
two_open_averages[i-1,:] = np.mean(two_open_training_set[(i-1)*div:i*div,:],axis=0)
three_open_averages[i-1,:] = np.mean(three_open_training_set[(i-1)*div:i*div,:],axis=0)
four_open_averages[i-1,:] = np.mean(four_open_training_set[(i-1)*div:i*div,:],axis=0)
five_open_averages[i-1,:] = np.mean(five_open_training_set[(i-1)*div:i*div,:],axis=0)
all_fingers_closed_averages[i-1,:] = np.mean(all_fingers_closed_training_set[(i-1)*div:i*div,:],axis=0)
grasp_averages[i-1,:] = np.mean(grasp_training_set[(i-1)*div:i*div,:],axis=0)
pick_averages[i-1,:] = np.mean(pick_training_set[(i-1)*div:i*div,:],axis=0)
# Here we stack all the data row wise
conc_array = np.concatenate([thumb_open_averages,index_open_averages,middle_open_averages,ring_open_averages,pinky_open_averages,two_open_averages,three_open_averages,four_open_averages,five_open_averages,all_fingers_closed_averages,grasp_averages,pick_averages],axis=0)
print(conc_array.shape)
np.savetxt('C:/Users/shaya/Desktop/'+name+'.txt', conc_array, fmt='%i')
# In this method the EMG data gets trained and verified
Train(conc_array)
if __name__ == '__main__':
main()