-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensor_logger.py
More file actions
315 lines (231 loc) · 8.38 KB
/
sensor_logger.py
File metadata and controls
315 lines (231 loc) · 8.38 KB
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
import dash
from dash.dependencies import Output, Input
from dash import dcc, html
from datetime import datetime
import json
import plotly.graph_objs as go
from collections import deque
from flask import Flask, request
import time
import sys
import pickle
from features import extract_features # make sure features.py is in the same directory
from util import reorient, reset_vars
import labels
import numpy as np
from scipy.ndimage.interpolation import shift
from scipy.signal import butter, freqz, filtfilt, firwin, iirnotch, lfilter, find_peaks
external_stylesheets = [
{
"href": "https://fonts.googleapis.com/css2?"
"family=Lato:wght@400;700&display=swap",
"rel": "stylesheet",
},
]
server = Flask(__name__)
app = dash.Dash(__name__, server=server, external_stylesheets=external_stylesheets)
# define maximum number of data points in the queue
# Decrease this number to improve performance
MAX_DATA_POINTS = 1000
# define how often the plot is updated in ms
# Increase this number to improve performance
UPDATE_FREQ_MS = 1000
# Store accelerometer data
accel_time = deque(maxlen=MAX_DATA_POINTS)
accel_x = deque(maxlen=MAX_DATA_POINTS)
accel_y = deque(maxlen=MAX_DATA_POINTS)
accel_z = deque(maxlen=MAX_DATA_POINTS)
sensor_data = []
# Store uncalibrated accelerometer data
accel_uncali_time = deque(maxlen=MAX_DATA_POINTS)
accel_uncali_x = deque(maxlen=MAX_DATA_POINTS)
accel_uncali_y = deque(maxlen=MAX_DATA_POINTS)
accel_uncali_z = deque(maxlen=MAX_DATA_POINTS)
sensor_uncali_data = []
# Steps
total_steps = 0
step_init = 0
stepvals =[]
available_sensor_list = []
last_update_time = time.time()
# TODO: list the class labels that you collected data for in the order of label_index (defined in labels.py)
class_names = labels.activity_labels
window_size = 100 # ~1 sec assuming 100 Hz sampling rate
step_size = 100 # no overlap
index = 0 # to keep track of how many samples we have buffered so far
reset_vars() # resets orientation variables
# Loading the classifier that you saved to disk previously
with open('classifier.pickle', 'rb') as f:
classifier = pickle.load(f)
if classifier == None:
print("Classifier is null; make sure you have trained it!")
sys.exit()
def predict(window):
"""
Given a window of accelerometer data, predict the activity label.
"""
# TODO: extract features over the window of data
name, feature_vector = extract_features(window)
# TODO: use classifier.predict(feature_vector) to predict the class label.
# Make sure your feature vector is passed in the expected format
class_label = classifier.predict([feature_vector])
# TODO: get the name of your predicted activity from 'class_names' using the returned label.
# return the activity name.
return labels.activity_labels[int(class_label[0])]
app.layout = html.Div(
[
html.Div(
children=[
html.H1(children="CS328 - Live Sensor Readings", className="header-title"),
html.P(children=["Streamed from Sensor Logger: tszheichoi.com/sensorlogger", html.Br(),
"Refer python code for implementation."], className="header-description"),
],
className="header",
),
html.Div(id="available_sensor_text", className="wrapper",),
html.Div(id="steps", className="wrapper",),
html.Div(id="graph_container", className="wrapper",),
dcc.Interval(id="counter", interval=UPDATE_FREQ_MS),
]
)
@app.callback(Output("graph_container", "children"),
Output("available_sensor_text", "children"),
Output("steps", "children"),
Input("counter", "n_intervals"))
def update_graph(_counter):
global total_steps,step_init,stepvals,filtered_signal
graphs = []
# Plot accelerometer if available
if (len(accel_time) > 0):
data_accel = [
go.Scatter(x=list(accel_time), y=list(d), name=name)
for d, name in zip([accel_x, accel_y, accel_z], ["X", "Y", "Z"])
]
graphs.append(
html.Div(
dcc.Graph(
id="accel_graph",
figure={
"data": data_accel,
"layout": go.Layout(
{
"title": "Accelerometer",
"xaxis": {"type": "date", "range": [min(accel_time), max(accel_time)]},
"yaxis": {"title": "Acceleration ms<sup>-2</sup>", "range": [-25,25]},
}
)
}
),
className="card",
)
)
# Plot uncalibrated accelerometer if available
if (len(accel_uncali_time) > 0):
data_accel_uncali = [
go.Scatter(x=list(accel_uncali_time), y=list(d), name=name)
for d, name in zip([accel_uncali_x, accel_uncali_y, accel_uncali_z], ["X", "Y", "Z"])
]
graphs.append(
html.Div(
dcc.Graph(
id="accel_uncali_graph",
figure={
"data": data_accel_uncali,
"layout": go.Layout(
{
"title": "Uncalibrated Accelerometer",
"xaxis": {"type": "date", "range": [min(accel_uncali_time), max(accel_uncali_time)]},
"yaxis": {"title": "Acceleration ms<sup>-2</sup>","range": [-25,25]},
}
)
}
),
className="card",
)
)
# Plot filtered_signal if available
# Update text for available sensors.
text_div = html.Div(
html.P(children="Available Sensors: {}".format(available_sensor_list)),
className="textcard",
)
### TODOS: Modify this code for activity recognition using acclerometer data###################################
activity = None
if len(sensor_data) > window_size:
activity = predict(np.asarray(sensor_data[-window_size:]))
#######################################################################################################
activity_div = html.Div(
html.P(children="Current Activity: {}".format(activity), style={"color": "red", "font-weight": "bold"}),
className="textcard",
)
# if (len(filtered_signal) > 0):
# data_accel_uncali = [
# go.Scatter(x=list(accel_uncali_time), y=list(d), name=name, mode = m)
# for d, name,m in zip([ filtered_signal,stepvals ], ["magnitude","steps"],['lines','markers+text'])
# ]
# graphs.append(
# html.Div(
# dcc.Graph(
# id="accel_uncali_graph",
# figure={
# "data": data_accel_uncali,
# "layout": go.Layout(
# {
# "title": "Filtered signal",
# "xaxis": {"type": "date", "range": [min(accel_uncali_time), max(accel_uncali_time)]},
# "yaxis": {"title": "Acceleration ms<sup>-2</sup>","range": [-25,25]},
# }
# )
# }
# ),
# className="card",
# )
# )
return html.Div(graphs), text_div, activity_div
# return text
@server.route("/data", methods=["POST"])
def data(): # listens to the data streamed from the sensor logger
global last_update_time
global available_sensor_list
if str(request.method) == "POST":
# Print received data
# print(f'received data: {request.data}')
# reset available sensor after 10 seconds
if time.time() - last_update_time > 10:
last_update_time = time.time()
available_sensor_list = []
# Read in data
data = json.loads(request.data)
for d in data['payload']:
# Get sensor name
sensor_name = d.get("name", None)
if sensor_name not in available_sensor_list:
available_sensor_list.append(sensor_name)
# Read accelerometer sensor data value
# modify to access different sensors
if (sensor_name == "accelerometer"):
ts = datetime.fromtimestamp(d["time"] / 1000000000)
if len(accel_time) == 0 or ts > accel_time[-1]:
accel_time.append(ts)
# modify the following based on which sensor is accessed, log the raw json for guidance
accel_x.append(d["values"]["x"])
accel_y.append(d["values"]["y"])
accel_z.append(d["values"]["z"])
sensor_data.append(reorient(d["values"]["x"], d["values"]["y"], d["values"]["z"]))
if len(sensor_data) > MAX_DATA_POINTS:
sensor_data.pop(0)
if (sensor_name == "accelerometeruncalibrated"):
ts = datetime.fromtimestamp(d["time"] / 1000000000)
if len(accel_uncali_time) == 0 or ts > accel_uncali_time[-1]:
accel_uncali_time.append(ts)
# modify the following based on which sensor is accessed, log the raw json for guidance
accel_uncali_x.append(d["values"]["x"])
accel_uncali_y.append(d["values"]["y"])
accel_uncali_z.append(d["values"]["z"])
sensor_uncali_data.append(reorient(d["values"]["x"], d["values"]["y"], d["values"]["z"]))
if len(sensor_uncali_data) > MAX_DATA_POINTS:
sensor_uncali_data.pop(0)
return "success"
if __name__ == "__main__":
# app.run_server(port=8000, host="0.0.0.0", debug=True)
app.run_server(port=8000, host="0.0.0.0")