Skip to content

Commit cc07a6c

Browse files
committed
har_trees: Also show durations on screen
1 parent 696e892 commit cc07a6c

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

examples/har_trees/har_live.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212
import timebased
1313
import emlearn_trees
1414

15+
# for display
16+
# mpremote mip install "github:peterhinch/micropython-nano-gui"
17+
from color_setup import ssd
18+
from gui.core.writer import Writer
19+
from gui.core.nanogui import refresh
20+
from gui.widgets.meter import Meter
21+
from gui.widgets.label import Label, ALIGN_RIGHT
22+
import gui.fonts.courier20 as fixed_font
23+
24+
# Cleanup after import frees considerable memory
25+
gc.collect()
26+
1527
def mean(arr):
1628
m = sum(arr) / float(len(arr))
1729
return m
@@ -105,6 +117,34 @@ def send_bluetooth_le(sequence, probabilities,
105117
# Turn of BLE
106118
ble.active(False)
107119

120+
121+
def render_display(durations):
122+
start_time = time.ticks_ms()
123+
124+
ssd.fill(0)
125+
126+
Writer.set_textpos(ssd, 0, 0) # In case previous tests have altered it
127+
wri = Writer(ssd, fixed_font, verbose=False)
128+
wri.set_clip(False, False, False)
129+
130+
y = 5
131+
for classname, stats in durations.items():
132+
133+
key_text = classname
134+
text1 = Label(wri, y, 10, wri.stringlen(key_text))
135+
text1.value(key_text)
136+
137+
value_text = f'{stats:.0f}s'
138+
text2 = Label(wri, y, 140, 50, align=ALIGN_RIGHT)
139+
text2.value(value_text)
140+
y += 22
141+
142+
refresh(ssd)
143+
144+
duration = time.ticks_ms() - start_time
145+
print('render-display', duration, 'ms')
146+
147+
108148
def main():
109149

110150
dataset = 'har_exercise_1'
@@ -113,7 +153,7 @@ def main():
113153
classname_index = {"LAYING": 0, "SITTING": 1, "STANDING": 2, "WALKING": 3, "WALKING_DOWNSTAIRS": 4, "WALKING_UPSTAIRS": 5}
114154
window_length = 128
115155
elif dataset == 'har_exercise_1':
116-
classname_index = {"jumpingjack": 0, "lunge": 1, "other": 2, "squat": 3}
156+
classname_index = {"jacks": 0, "lunge": 1, "other": 2, "squat": 3}
117157
window_length = 256
118158
else:
119159
raise ValueError('Unknown dataset')
@@ -186,12 +226,18 @@ def main():
186226

187227
durations[activity] += (hop_length/SAMPLERATE)
188228

229+
# Print status
189230
d = time.ticks_diff(time.ticks_ms(), start)
190231
print('classify', activity, list(out), d, 'ms')
191232
for classname, duration in durations.items():
192233
print(f'{classname}:\t\t\t{duration:.0f} seconds')
193234

235+
# Send predictions over BLE
194236
send_bluetooth_le(prediction_no, out)
237+
238+
# Update display
239+
render_display(durations)
240+
195241
prediction_no += 1
196242

197243
time.sleep_ms(100)

0 commit comments

Comments
 (0)