move ffmpeg capture to a separate thread and use a queue

This commit is contained in:
Blake Blackshear
2020-03-14 15:32:51 -05:00
parent e37eba49ff
commit 4ee200a81c
4 changed files with 170 additions and 131 deletions

View File

@@ -10,7 +10,7 @@ from collections import Counter, defaultdict
import itertools
import pyarrow.plasma as plasma
import matplotlib.pyplot as plt
from frigate.util import draw_box_with_label
from frigate.util import draw_box_with_label, PlasmaManager
from frigate.edgetpu import load_labels
PATH_TO_LABELS = '/labelmap.txt'
@@ -36,6 +36,7 @@ class TrackedObjectProcessor(threading.Thread):
'current_frame': np.zeros((720,1280,3), np.uint8),
'object_id': None
})
self.plasma_client = PlasmaManager()
def get_best(self, camera, label):
if label in self.camera_data[camera]['best_objects']:
@@ -45,35 +46,8 @@ class TrackedObjectProcessor(threading.Thread):
def get_current_frame(self, camera):
return self.camera_data[camera]['current_frame']
def connect_plasma_client(self):
while True:
try:
self.plasma_client = plasma.connect("/tmp/plasma")
return
except:
print(f"TrackedObjectProcessor: unable to connect plasma client")
time.sleep(10)
def get_from_plasma(self, object_id):
while True:
try:
return self.plasma_client.get(object_id, timeout_ms=0)
except:
self.connect_plasma_client()
time.sleep(1)
def delete_from_plasma(self, object_ids):
while True:
try:
self.plasma_client.delete(object_ids)
return
except:
self.connect_plasma_client()
time.sleep(1)
def run(self):
self.connect_plasma_client()
while True:
camera, frame_time, tracked_objects = self.tracked_objects_queue.get()
@@ -85,10 +59,7 @@ class TrackedObjectProcessor(threading.Thread):
###
# Draw tracked objects on the frame
###
object_id_hash = hashlib.sha1(str.encode(f"{camera}{frame_time}"))
object_id_bytes = object_id_hash.digest()
object_id = plasma.ObjectID(object_id_bytes)
current_frame = self.get_from_plasma(object_id)
current_frame = self.plasma_client.get(f"{camera}{frame_time}")
if not current_frame is plasma.ObjectNotAvailable:
# draw the bounding boxes on the frame
@@ -117,10 +88,10 @@ class TrackedObjectProcessor(threading.Thread):
self.camera_data[camera]['current_frame'] = current_frame
# store the object id, so you can delete it at the next loop
previous_object_id = self.camera_data[camera]['object_id']
previous_object_id = f"{camera}{frame_time}"
if not previous_object_id is None:
self.delete_from_plasma([previous_object_id])
self.camera_data[camera]['object_id'] = object_id
self.plasma_client.delete(f"{camera}{frame_time}")
self.camera_data[camera]['object_id'] = f"{camera}{frame_time}"
###
# Maintain the highest scoring recent object and frame for each label