-
Notifications
You must be signed in to change notification settings - Fork 0
/
online_squat_classify.py
69 lines (59 loc) · 1.5 KB
/
online_squat_classify.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
from sklearn.externals import joblib
from sklearn.svm import LinearSVC
import csv
import serial
import numpy as np
PORT = '/dev/cu.usbmodem1411'
BAUDE = 9600
OFFSET = 5
NUM_SENSORS = 5
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):
#print(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],)
#print(mult_scores)
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)
ack = input("Press any key to start system.")
print("Starting...")
try:
X = []
while True:
line = ser.readline();
line = line.strip().decode('utf-8')
line = line.split(", ")
try:
line = list(map(int, line))
X.append(line)
except:
continue
if len(X) == OFFSET:
#print ("OFFSET...")
# transform F into a (1, NUM_SENSORS*OFFSET) shape feature vector
try:
X_f = np.array(X).flatten(order='F').reshape(1, -1)
events = classify_events(X_f)
print(events)
X = []
except:
print ("could not flatten")
X= []
continue
except KeyboardInterrupt:
print("Stopping...")
if __name__ == "__main__":
main()