-
Notifications
You must be signed in to change notification settings - Fork 0
/
online_squat_classify_with_depth.py
164 lines (145 loc) · 3.81 KB
/
online_squat_classify_with_depth.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
from sklearn.externals import joblib
from sklearn.svm import LinearSVC
from time import sleep
import csv
import serial
import numpy as np
import json
PORT = '/dev/cu.usbmodem1411'
FILE = 'squat_file'
BAUDE = 9600
OFFSET = 5
NUM_SENSORS = 5
ACCEL_THRESH = 150
clf_pkl = 'experimentation/clf_multilabel_calibrated_v1.pkl'
clf = joblib.load(clf_pkl)
classes_inv = {0:'hlpron', 1:'hlsup', 2:'sup', 3:'heellift', 4:'pron', 5: 'heeldom', 6: 'hdpron', 7: 'hdsup', 8: 'normal'}
# Return a dictionary with events
def classify_events(X):
Z_scores = clf.predict_proba(X)
Z_bin = clf.predict(X)
mult_scores = np.multiply(Z_scores, Z_bin)
mult_scores = mult_scores.reshape(mult_scores.shape[1],)
events = {}
idx = 0
for score in mult_scores:
if (score > 0):
events[classes_inv[idx+2]] = score
idx += 1
return events
def main():
ser = serial.Serial(PORT, BAUDE)
clf = joblib.load(clf_pkl)
file = open(FILE, 'w')
ack = input("Press any key to start system.")
print("Starting...")
print("Calibrating depth sensor...")
ser.close()
ack = input("Stand tall and press any key.")
print("Sensing top position...")
idx = 0;
upright_readings = []
ser.open()
ser.flushInput()
while idx <= 250:
line = ser.readline();
print(line)
try:
line = line.strip().decode('utf-8')
line = line.split(", ")
acc_reading = int(line[-1])
upright_readings.append(acc_reading)
idx+=1
except:
continue
upright_mean = -1 * int(np.mean(upright_readings))
print(upright_mean)
ser.close()
ack = input("Squat to lowest position and press any key.")
print("Sensing bottom position...")
idx = 0;
bottom_readings = []
ser.open()
ser.flushInput()
while idx <= 250:
line = ser.readline();
print(line)
try:
line = line.strip().decode('utf-8')
line = line.split(", ")
acc_reading = int(line[-1])
bottom_readings.append(acc_reading)
idx+=1
except:
continue
bottom_mean = -1 * int(np.mean(bottom_readings))
print(bottom_mean)
ser.close()
ack = input("Press any key to start finding errors.")
ser.open()
ser.flushInput()
accel_buffer = []
# get a starting accelerometer value
for i in range(5):
line = ser.readline();
print(line)
try:
line = line.strip().decode('utf-8')
line = line.split(", ")
acc_reading = int(line[-1])
accel_buffer.append(acc_reading)
idx+=1
except:
continue
prev_accel = -1 * int(np.mean(accel_buffer))
accel_buffer = []
try:
X = []
while True:
line = ser.readline();
line = line.strip().decode('utf-8')
line = line.split(", ")
try:
accel = int(line[-1])
del line[-1]
line = list(map(int, line))
X.append(line)
accel_buffer.append(accel)
except:
continue
if len(X) == OFFSET:
# transform F into a (1, NUM_SENSORS*OFFSET) shape feature vector
events = {}
try:
X_f = np.array(X).flatten(order='F').reshape(1, -1)
events = classify_events(X_f)
except:
print ("could not flatten")
current_accel = -1 * int(np.mean(accel_buffer))
if current_accel >= prev_accel + ACCEL_THRESH:
# ascent
percentage = (current_accel - bottom_mean) / (upright_mean - bottom_mean)
file.write("ASCENT: " + str(percentage))
print("ASCENT: " + str(percentage))
elif current_accel <= prev_accel - ACCEL_THRESH:
# descent
percentage = (upright_mean - current_accel) / (upright_mean - bottom_mean)
print("DESCENT: " + str(percentage))
file.write("DESCENT: " + str(percentage))
else:
# stable
print("STABLE: " + str(current_accel))
file.write("STABLE: " + str(current_accel))
prev_accel = current_accel
accel_buffer = []
X = []
file.write(", ")
file.write(json.dumps(events))
file.write('\n')
print(str(events))
print("_____________________")
except KeyboardInterrupt:
print("Stopping...")
ser.close()
if __name__ == "__main__":
main()