-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyolo_mqtt_server.py
65 lines (49 loc) · 1.96 KB
/
yolo_mqtt_server.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
59
60
61
62
63
64
65
# example topic: ws52134/cam002/motion/snapshot
import paho.mqtt.client as mqtt
from darkflow.net.build import TFNet
import os
import cv2
import tensorflow as tf
import numpy as np
import datetime
from PIL import Image
from io import BytesIO
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print( "Connected with result code "+str(rc) )
topic = os.getenv('MQTT_TOPIC')
if topic is not None:
client.subscribe(topic)
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print( "on_message: " + str(msg.topic) )
try:
payload = BytesIO(msg.payload)
snapshot = Image.open(payload)
image = cv2.cvtColor(np.array(snapshot), cv2.COLOR_RGB2BGR)
starttime = datetime.datetime.now()
result = tfnet.return_predict(image)
endtime = datetime.datetime.now()
# publish full result
client.publish( msg.topic + "/detection", str(result) )
# publish detailed result - e.g. one mqtt message per detected object type
labels = list(set([entry["label"] for entry in result]))
for l in labels:
l_topic = msg.topic + "/detection/" + l
l_result = [ entry for entry in result if entry["label"] == l ]
client.publish( l_topic, str(l_result) )
except:
print("Unexpected error:", str(sys.exc_info()[0]))
user = os.getenv('MQTT_USERNAME')
pwd = os.getenv('MQTT_PASSWORD')
host = os.getenv('MQTT_HOST') or '127.0.0.1'
port = os.getenv('MQTT_PORT') or 1883
options = {"model": "/darkflow/cfg/yolo.cfg", "load": "/yolo.weights", "threshold": 0.3}
tfnet = TFNet(options)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
if (user is not None) and (pwd is not None):
client.username_pw_set(user, pwd)
client.connect(host, port, 60)
client.loop_forever()