forked from rmantena/Myo_gestureArmBand_experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestScript.py
101 lines (90 loc) · 2.66 KB
/
testScript.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
# Author : Rajiv Mantena
# rmantena.github.io
from myo import init, Hub, Feed
from sklearn.tree import DecisionTreeClassifier
import time
import math
from statistics import mode
import numpy
init()
feed = Feed()
hub = Hub()
hub.run(1000, feed)
try:
myo = feed.wait_for_single_device(timeout=2.0)
if not myo:
print("No Myo connected after 2 seconds")
print("Hello, Myo!")
X = []
y = []
# while hub.running and myo.connected:
# print('Orientation:', quat.x, quat.y, quat.z, quat.w)
print "Set hands !!!"
time.sleep(3)
time1 = time.time()
while hub.running and myo.connected:
# print "Reading 1"
quat = myo.orientation
x_q = math.ceil(quat.x * 100000) / 100000
y_q = math.ceil(quat.y * 100000) / 100000
z_q = math.ceil(quat.z * 100000) / 100000
w_q = math.ceil(quat.w * 100000) / 100000
# X.append([quat.x, quat.y, quat.z, quat.w])
X.append([x_q, y_q, z_q, w_q])
y.append(1)
# print('Reading 1:', quat.x, quat.y, quat.z, quat.w)
print('Reading 1:', x_q, y_q, z_q, w_q)
time2 = time.time()
if (time2 - time1) > 15:
break
print "Change hands !!!"
time.sleep(3)
time1 = time.time()
while hub.running and myo.connected:
# print "Reading 1"
quat = myo.orientation
x_q = math.ceil(quat.x * 100000) / 100000
y_q = math.ceil(quat.y * 100000) / 100000
z_q = math.ceil(quat.z * 100000) / 100000
w_q = math.ceil(quat.w * 100000) / 100000
# X.append([quat.x, quat.y, quat.z, quat.w])
X.append([x_q, y_q, z_q, w_q])
y.append(0)
# print('Reading 1:', quat.x, quat.y, quat.z, quat.w)
print('Reading 0:', x_q, y_q, z_q, w_q)
time2 = time.time()
if (time2 - time1) > 15:
break
print "Collected Data !!!"
# Machine Learning Magic !!!
clf = DecisionTreeClassifier(random_state=0)
clf.fit(X,y)
print "Change hands !!!"
while hub.running and myo.connected:
time.sleep(1)
time1 = time.time()
X = []
# print "Reading 1"
quat = myo.orientation
x_q = math.ceil(quat.x * 100000) / 100000
y_q = math.ceil(quat.y * 100000) / 100000
z_q = math.ceil(quat.z * 100000) / 100000
w_q = math.ceil(quat.w * 100000) / 100000
# X.append([quat.x, quat.y, quat.z, quat.w])
X.append([x_q, y_q, z_q, w_q])
y1 = clf.predict(X)
print "Result : ", y1
# print('Reading 1:', quat.x, quat.y, quat.z, quat.w)
# print('Test_data:', x_q, y_q, z_q, w_q)
'''
time2 = time.time()
if (time2 - time1) > 0.25:
# print "Testing now !!!!!"
print "Result : ", numpy.mean(clf.predict(X))
# print "Result : ", numpy.mean(y1)
break
'''
# print "X = ", X
# print "y = ", y
finally:
hub.shutdown() # !! crucial