Skip to content

Commit 696e892

Browse files
committed
har_trees: Also keep track of duration in each state
1 parent f7d66f5 commit 696e892

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

examples/har_trees/har_live.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,12 @@ def send_bluetooth_le(sequence, probabilities,
9494

9595
payload = manufacturer_specific_advertisement(data)
9696

97-
print('ble-advertise', mac[1], data)
97+
print('ble-advertise', 'mac='+mac[1].hex(), 'data='+data.hex())
9898

9999
# send and wait until N advertisements are sent
100100
advertise_us = int(1000*advertise_interval_ms)
101101
ble.gap_advertise(advertise_us, adv_data=payload, connectable=False)
102+
# XXX: blocking wait
102103
time.sleep_ms(advertisements*advertise_interval_ms)
103104

104105
# Turn of BLE
@@ -129,8 +130,9 @@ def main():
129130
mpu = MPU6886(I2C(0, sda=21, scl=22, freq=100000))
130131

131132
# Enable FIFO at a fixed samplerate
133+
SAMPLERATE = 100
132134
mpu.fifo_enable(True)
133-
mpu.set_odr(100)
135+
mpu.set_odr(SAMPLERATE)
134136

135137
hop_length = 64
136138
chunk = bytearray(mpu.bytes_per_sample*hop_length)
@@ -150,6 +152,8 @@ def main():
150152
out = array.array('f', range(model.outputs()))
151153

152154
prediction_no = 0
155+
durations = { classname: 0.0 for classname in classname_index.keys() } # how long each class has been active
156+
MIN_PROBABILITY = 0.5 # if no class has higher, consider as "other"
153157

154158
while True:
155159

@@ -177,9 +181,15 @@ def main():
177181
model.predict(features, out)
178182
result = argmax(out)
179183
activity = class_index_to_name[result]
184+
if max(out) < MIN_PROBABILITY:
185+
activity = 'other'
186+
187+
durations[activity] += (hop_length/SAMPLERATE)
180188

181189
d = time.ticks_diff(time.ticks_ms(), start)
182-
print('classify', d, activity, out)
190+
print('classify', activity, list(out), d, 'ms')
191+
for classname, duration in durations.items():
192+
print(f'{classname}:\t\t\t{duration:.0f} seconds')
183193

184194
send_bluetooth_le(prediction_no, out)
185195
prediction_no += 1

0 commit comments

Comments
 (0)