forked from luxonis/depthai-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
58 lines (47 loc) · 1.46 KB
/
main.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
import depthai as dai
import blobconverter
import time
# Change the IP to your MQTT broker!
MQTT_BROKER = "test.mosquitto.org"
MQTT_BROKER_PORT = 1883
MQTT_TOPIC = "test_topic/detections"
pipeline = dai.Pipeline()
camRgb = pipeline.create(dai.node.ColorCamera)
camRgb.setPreviewSize(300, 300)
camRgb.setFps(10)
camRgb.setInterleaved(False)
# Define a neural network that will make predictions based on the source frames
nn = pipeline.create(dai.node.MobileNetDetectionNetwork)
nn.setConfidenceThreshold(0.5)
nn.setBlobPath(blobconverter.from_zoo(name="mobilenet-ssd", shaves=6))
camRgb.preview.link(nn.input)
script = pipeline.create(dai.node.Script)
script.setProcessor(dai.ProcessorType.LEON_CSS)
nn.out.link(script.inputs['detections'])
script_text = f"""
import time
mqttc = Client()
node.warn('Connecting to MQTT broker...')
mqttc.connect("{MQTT_BROKER}", {MQTT_BROKER_PORT}, 60)
node.warn('Successfully connected to MQTT broker!')
mqttc.loop_start()
cnt = 0
total = 0
while True:
dets = node.io['detections'].get()
total+=len(dets.detections)
cnt+=1
if cnt > 20:
avrg = str(total / 20)
cnt = 0
total = 0
node.warn(avrg)
(ok, id) = mqttc.publish("{MQTT_TOPIC}", avrg, qos=2)
"""
with open("paho-mqtt.py", "r") as f:
paho_script = f.read()
script.setScript(f"{paho_script}\n{script_text}")
with dai.Device(pipeline) as device:
print('Connected to OAK')
while not device.isClosed():
time.sleep(1)