-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinference.py
133 lines (104 loc) · 3.22 KB
/
inference.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
import turtle
import sys
import csv
import serial
import time
from torch import nn
import torch
import torch.nn.functional as F
# Open the serial port with PySerial
ser = serial.Serial('/dev/ttyACM1', 9600) # IMU data
start = time.time()
queue = []
count = 0
scr = turtle.Screen()
pen = turtle.Turtle()
font = 'Arial'
fontsize = 60
style = 'bold' # You can use 'bold', 'italic', 'overstrike' or 'underline'
align = 'center'
message = "text"
class FallDetector(nn.Module):
def __init__(self):
super(FallDetector, self,).__init__()
self.lstm = nn.LSTM(6, 20, num_layers=1)
self.hidden2tag = nn.Linear(20, 3)
def forward(self, seq):
output, (h_n, c_n) = self.lstm(seq.view(len(seq), 1, -1))
tag_space = self.hidden2tag(c_n.view(1, -1))
# print(tag_space.data)
return tag_space
class CNNNetwork(nn.Module):
def __init__(self):
super(CNNNetwork, self,).__init__()
self.cnn1 = nn.Conv1d(6, 12, 3)
self.cnn2 = nn.Conv1d(12, 24, 3)
self.cnn3 = nn.Conv1d(24, 48, 3)
self.MP = nn.MaxPool1d(3)
self.lstm = nn.LSTM(48, 24, num_layers=1)
self.hidden2tag = nn.Linear(24, 3)
def forward(self, seq):
seq = seq.reshape(1, 6, 30)
seq = self.cnn1(seq)
seq = F.relu(seq)
seq = self.cnn2(seq)
seq = F.relu(seq)
seq = self.cnn3(seq)
seq = F.relu(seq)
seq = self.MP(seq)
seq = seq.reshape(-1, 48)
output, (h_n, c_n) = self.lstm(seq)
tag_space = self.hidden2tag(c_n.view(1, -1))
return tag_space
# model = FallDetector()
model = CNNNetwork()
model.load_state_dict(torch.load("model3.pt"))
model.eval()
index_map = {
0: 'Fall',
1: 'Idle',
2: "Walk"
}
def model_time():
# print(queue)
t = torch.tensor(queue, dtype=torch.float)
global count
# print("about to eval")
result = model(t)
print(result)
index = torch.argmax(result)
# if index == 2:
# if (torch.max(torch.abs(t[:, 0:3]))) > 20:
# index = 0
pen.clear()
pen.write(index_map[int(index)], font=(font, fontsize, style), align=align)
pen.hideturtle()
counter = 0
while True:
try:
line = ser.readline().decode()
if line:
# Split the line into a timestamp and data
timestamp = line.strip().split(',')
if (len(timestamp) == 6):
varlist = [timestamp[0], timestamp[1],
timestamp[2], timestamp[3], timestamp[4], timestamp[5]]
for i in range(6):
varlist[i] = float(varlist[i])
if (len(queue) < 30):
queue.append(varlist)
print(varlist)
else:
# print([timestamp[0],
# timestamp[1], timestamp[2], timestamp[3], timestamp[4], timestamp[5
# queue.pop(0)
# queue.append(varlist)
queue.pop(-1)
queue.insert(0, varlist)
if counter == 10:
model_time()
counter = 0
else:
counter += 1
except:
continue