-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTM_pyscript.py
More file actions
38 lines (35 loc) · 1.21 KB
/
TM_pyscript.py
File metadata and controls
38 lines (35 loc) · 1.21 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
from pyscript.js_modules import teach, pose, ble_library, mqtt_library
async def run_model(URL2):
s = teach.s # or s = pose.s
s.URL2 = URL2
await s.init()
def get_predictions(num_classes):
predictions = []
for i in range (0,num_classes):
divElement = document.getElementById('class' + str(i))
if divElement:
divValue = divElement.innerHTML
predictions.append(divValue)
return predictions
#initialize mqtt stuff
mqtt_broker = 'broker.hivemq.com'
port = 1883
topic_sub = '/movement' # this reads anything sent to ME35
client = mqtt_library.myClient
print('Connected to %s MQTT broker' % (mqtt_broker))
import asyncio
await run_model("https://teachablemachine.withgoogle.com/models/sPXnSlGg8/") #Change to your model link
topic_pub = '/movement'
while True:
#if ble.connected:
# predictions = get_predictions(2)
# send(predictions)
#await asyncio.sleep(2)
predictions = get_predictions(2)
values = [float(item.split(': ')[1]) for item in predictions]
print(values)
if values[0] >= 0.90:
client.publish("/movement", "run")
elif values[1] >= 0.90:
client.publish("/movement", "stop")
#else: send nothing