use a topic prefix and implement last will messages

This commit is contained in:
blakeblackshear 2019-02-10 14:43:21 -06:00
parent d43c17238c
commit a3c5dd91a1
2 changed files with 13 additions and 13 deletions

View File

@ -40,13 +40,15 @@ Access the mjpeg stream at http://localhost:5000
- Use SSDLite models - Use SSDLite models
## Future improvements ## Future improvements
- [x] Switch to MQTT prefix
- [x] Add last will and availability for MQTT
- [ ] Add ability to turn detection on and off via MQTT
- [ ] Add a max size for motion and objects - [ ] Add a max size for motion and objects
- [ ] Filter out detected objects that are not the right size - [ ] Filter out detected objects that are not the right size
- [ ] Merge bounding boxes that span multiple regions - [ ] Merge bounding boxes that span multiple regions
- [ ] Switch to a config file
- [ ] Allow motion regions to be different than object detection regions
- [x] Change color of bounding box if motion detected - [x] Change color of bounding box if motion detected
- [ ] Switch to MQTT prefix
- [ ] Add last will and availability for MQTT
- [ ] Add ability to turn detection on and off via MQTT
- [x] Look for a subset of object types - [x] Look for a subset of object types
- [ ] Try and reduce CPU usage by simplifying the tensorflow model to just include the objects we care about - [ ] Try and reduce CPU usage by simplifying the tensorflow model to just include the objects we care about
- [x] MQTT messages when detected objects change - [x] MQTT messages when detected objects change
@ -55,8 +57,6 @@ Access the mjpeg stream at http://localhost:5000
- [x] Parallel processing to increase FPS - [x] Parallel processing to increase FPS
- [ ] Look into GPU accelerated decoding of RTSP stream - [ ] Look into GPU accelerated decoding of RTSP stream
- [ ] Send video over a socket and use JSMPEG - [ ] Send video over a socket and use JSMPEG
- [ ] Switch to a config file
- [ ] Allow motion regions to be different than object detection regions
## Building Tensorflow from source for CPU optimizations ## Building Tensorflow from source for CPU optimizations
https://www.tensorflow.org/install/source#docker_linux_builds https://www.tensorflow.org/install/source#docker_linux_builds

View File

@ -25,8 +25,7 @@ PATH_TO_CKPT = '/frozen_inference_graph.pb'
PATH_TO_LABELS = '/label_map.pbtext' PATH_TO_LABELS = '/label_map.pbtext'
MQTT_HOST = os.getenv('MQTT_HOST') MQTT_HOST = os.getenv('MQTT_HOST')
MQTT_MOTION_TOPIC = os.getenv('MQTT_MOTION_TOPIC') MQTT_TOPIC_PREFIX = os.getenv('MQTT_TOPIC_PREFIX')
MQTT_OBJECT_TOPIC = os.getenv('MQTT_OBJECT_TOPIC')
MQTT_OBJECT_CLASSES = os.getenv('MQTT_OBJECT_CLASSES') MQTT_OBJECT_CLASSES = os.getenv('MQTT_OBJECT_CLASSES')
# TODO: make dynamic? # TODO: make dynamic?
@ -105,13 +104,14 @@ class ObjectParser(threading.Thread):
DETECTED_OBJECTS = detected_objects DETECTED_OBJECTS = detected_objects
time.sleep(0.1) time.sleep(0.1)
class MqttPublisher(threading.Thread): class MqttPublisher(threading.Thread):
def __init__(self, host, motion_topic, object_topic, object_classes, motion_flags): def __init__(self, host, topic_prefix, object_classes, motion_flags):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.client = mqtt.Client() self.client = mqtt.Client()
self.client.will_set(topic_prefix+'/available', payload='offline', qos=1, retain=True)
self.client.connect(host, 1883, 60) self.client.connect(host, 1883, 60)
self.client.loop_start() self.client.loop_start()
self.motion_topic = motion_topic self.client.publish(topic_prefix+'/available', 'online', retain=True)
self.object_topic = object_topic self.topic_prefix = topic_prefix
self.object_classes = object_classes self.object_classes = object_classes
self.motion_flags = motion_flags self.motion_flags = motion_flags
@ -135,7 +135,7 @@ class MqttPublisher(threading.Thread):
new_payload = json.dumps(payload, sort_keys=True) new_payload = json.dumps(payload, sort_keys=True)
if new_payload != last_sent_payload: if new_payload != last_sent_payload:
last_sent_payload = new_payload last_sent_payload = new_payload
self.client.publish(self.object_topic, new_payload, retain=False) self.client.publish(self.topic_prefix+'/objects', new_payload, retain=False)
motion_status = 'OFF' motion_status = 'OFF'
if any(obj.value == 1 for obj in self.motion_flags): if any(obj.value == 1 for obj in self.motion_flags):
@ -143,7 +143,7 @@ class MqttPublisher(threading.Thread):
if motion_status != last_motion: if motion_status != last_motion:
last_motion = motion_status last_motion = motion_status
self.client.publish(self.motion_topic, motion_status, retain=False) self.client.publish(self.topic_prefix+'/motion', motion_status, retain=False)
time.sleep(0.1) time.sleep(0.1)
@ -219,7 +219,7 @@ def main():
object_parser = ObjectParser([region['output_array'] for region in regions]) object_parser = ObjectParser([region['output_array'] for region in regions])
object_parser.start() object_parser.start()
mqtt_publisher = MqttPublisher(MQTT_HOST, MQTT_MOTION_TOPIC, MQTT_OBJECT_TOPIC, mqtt_publisher = MqttPublisher(MQTT_HOST, MQTT_TOPIC_PREFIX,
MQTT_OBJECT_CLASSES.split(','), MQTT_OBJECT_CLASSES.split(','),
[region['motion_detected'] for region in regions]) [region['motion_detected'] for region in regions])
mqtt_publisher.start() mqtt_publisher.start()