forked from Github/frigate
switch everything to run off of tracked objects
This commit is contained in:
@@ -6,39 +6,35 @@ from collections import Counter, defaultdict
|
||||
import itertools
|
||||
|
||||
class MqttObjectPublisher(threading.Thread):
|
||||
def __init__(self, client, topic_prefix, objects_parsed, detected_objects, best_frames):
|
||||
def __init__(self, client, topic_prefix, camera):
|
||||
threading.Thread.__init__(self)
|
||||
self.client = client
|
||||
self.topic_prefix = topic_prefix
|
||||
self.objects_parsed = objects_parsed
|
||||
self._detected_objects = detected_objects
|
||||
self.best_frames = best_frames
|
||||
self.camera = camera
|
||||
|
||||
def run(self):
|
||||
prctl.set_name("MqttObjectPublisher")
|
||||
prctl.set_name(self.__class__.__name__)
|
||||
current_object_status = defaultdict(lambda: 'OFF')
|
||||
while True:
|
||||
# wait until objects have been parsed
|
||||
with self.objects_parsed:
|
||||
self.objects_parsed.wait()
|
||||
# wait until objects have been tracked
|
||||
with self.camera.objects_tracked:
|
||||
self.camera.objects_tracked.wait()
|
||||
|
||||
# make a copy of detected objects
|
||||
detected_objects = self._detected_objects.copy()
|
||||
|
||||
# total up all scores by object type
|
||||
# count objects with more than 2 entries in history by type
|
||||
obj_counter = Counter()
|
||||
for obj in itertools.chain.from_iterable(detected_objects.values()):
|
||||
obj_counter[obj['name']] += obj['score']
|
||||
for obj in self.camera.object_tracker.tracked_objects.values():
|
||||
if len(obj['history']) > 1:
|
||||
obj_counter[obj['name']] += 1
|
||||
|
||||
# report on detected objects
|
||||
for obj_name, total_score in obj_counter.items():
|
||||
new_status = 'ON' if int(total_score*100) > 100 else 'OFF'
|
||||
for obj_name, count in obj_counter.items():
|
||||
new_status = 'ON' if count > 0 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 if we have it as well
|
||||
if obj_name in self.best_frames.best_frames:
|
||||
best_frame = cv2.cvtColor(self.best_frames.best_frames[obj_name], cv2.COLOR_RGB2BGR)
|
||||
if obj_name in self.camera.best_frames.best_frames:
|
||||
best_frame = cv2.cvtColor(self.camera.best_frames.best_frames[obj_name], cv2.COLOR_RGB2BGR)
|
||||
ret, jpg = cv2.imencode('.jpg', best_frame)
|
||||
if ret:
|
||||
jpg_bytes = jpg.tobytes()
|
||||
|
||||
Reference in New Issue
Block a user