2019-02-26 03:27:02 +01:00
|
|
|
import json
|
2019-07-14 15:31:21 +02:00
|
|
|
import cv2
|
2019-02-26 03:27:02 +01:00
|
|
|
import threading
|
2019-12-14 22:18:21 +01:00
|
|
|
from collections import Counter, defaultdict
|
2019-02-26 03:27:02 +01:00
|
|
|
|
|
|
|
class MqttObjectPublisher(threading.Thread):
|
2019-12-14 22:18:21 +01:00
|
|
|
def __init__(self, client, topic_prefix, objects_parsed, detected_objects, best_frames):
|
2019-02-26 03:27:02 +01:00
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.client = client
|
|
|
|
self.topic_prefix = topic_prefix
|
|
|
|
self.objects_parsed = objects_parsed
|
|
|
|
self._detected_objects = detected_objects
|
2019-12-14 22:18:21 +01:00
|
|
|
self.best_frames = best_frames
|
2019-02-26 03:27:02 +01:00
|
|
|
|
|
|
|
def run(self):
|
2019-12-14 22:18:21 +01:00
|
|
|
current_object_status = defaultdict(lambda: 'OFF')
|
2019-02-26 03:27:02 +01:00
|
|
|
while True:
|
|
|
|
# wait until objects have been parsed
|
|
|
|
with self.objects_parsed:
|
|
|
|
self.objects_parsed.wait()
|
|
|
|
|
2019-12-14 22:18:21 +01:00
|
|
|
# make a copy of detected objects
|
2019-02-26 03:27:02 +01:00
|
|
|
detected_objects = self._detected_objects.copy()
|
|
|
|
|
2019-12-14 22:18:21 +01:00
|
|
|
# total up all scores by object type
|
|
|
|
obj_counter = Counter()
|
|
|
|
for obj in detected_objects:
|
|
|
|
obj_counter[obj['name']] += obj['score']
|
|
|
|
|
|
|
|
# report on detected objects
|
|
|
|
for obj_name, total_score in obj_counter.items():
|
|
|
|
new_status = 'ON' if int(total_score*100) > 100 else 'OFF'
|
|
|
|
if new_status != current_object_status[obj_name]:
|
|
|
|
current_object_status[obj_name] = new_status
|
|
|
|
self.client.publish(self.topic_prefix+'/'+obj_name, new_status, retain=False)
|
|
|
|
# send the snapshot over mqtt as well
|
|
|
|
if not self.best_frames.best_frames[obj_name] is None:
|
|
|
|
ret, jpg = cv2.imencode('.jpg', self.best_frames.best_frames[obj_name])
|
|
|
|
if ret:
|
|
|
|
jpg_bytes = jpg.tobytes()
|
|
|
|
self.client.publish(self.topic_prefix+'/'+obj_name+'/snapshot', jpg_bytes, retain=True)
|
|
|
|
|
|
|
|
# expire any objects that are ON and no longer detected
|
|
|
|
expired_objects = [obj_name for obj_name, status in current_object_status.items() if status == 'ON' and not obj_name in obj_counter]
|
|
|
|
for obj_name in expired_objects:
|
|
|
|
self.client.publish(self.topic_prefix+'/'+obj_name, 'OFF', retain=False)
|