Threaded motion estimator

This commit is contained in:
Josh Hawkins 2023-06-29 11:05:14 -05:00
parent 80e77fb856
commit 3171801607
3 changed files with 33 additions and 6 deletions

View File

@ -17,6 +17,21 @@ from frigate.util import SharedMemoryFrameManager, intersection_over_union
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class PtzMotionEstimatorThread(threading.Thread):
def __init__(self, config: CameraConfig, ptz_moving, stop_event) -> None:
threading.Thread.__init__(self)
self.name = "frigate_ptz_motion_estimator"
self.ptz_moving = ptz_moving
self.config = config
self.stop_event = stop_event
self.ptz_motion_estimator = PtzMotionEstimator(self.config, self.ptz_moving)
def run(self):
while not self.stop_event.is_set():
pass
logger.info("Exiting motion estimator...")
class PtzMotionEstimator: class PtzMotionEstimator:
def __init__(self, config: CameraConfig, ptz_moving) -> None: def __init__(self, config: CameraConfig, ptz_moving) -> None:
self.frame_manager = SharedMemoryFrameManager() self.frame_manager = SharedMemoryFrameManager()
@ -56,6 +71,10 @@ class PtzMotionEstimator:
self.frame_manager.close(frame_id) self.frame_manager.close(frame_id)
logger.debug(
f"frame time: {frame_time}, coord_transformations: {vars(self.coord_transformations)}"
)
return self.coord_transformations return self.coord_transformations
return None return None

View File

@ -6,7 +6,7 @@ from norfair import Detection, Drawable, Tracker, draw_boxes
from norfair.drawing.drawer import Drawer from norfair.drawing.drawer import Drawer
from frigate.config import CameraConfig from frigate.config import CameraConfig
from frigate.ptz_autotrack import PtzMotionEstimator from frigate.ptz_autotrack import PtzMotionEstimatorThread
from frigate.track import ObjectTracker from frigate.track import ObjectTracker
from frigate.util import intersection_over_union from frigate.util import intersection_over_union
@ -55,7 +55,9 @@ def frigate_distance(detection: Detection, tracked_object) -> float:
class NorfairTracker(ObjectTracker): class NorfairTracker(ObjectTracker):
def __init__(self, config: CameraConfig, ptz_autotracker_enabled, ptz_moving): def __init__(
self, config: CameraConfig, ptz_autotracker_enabled, ptz_moving, stop_event
):
self.tracked_objects = {} self.tracked_objects = {}
self.disappeared = {} self.disappeared = {}
self.positions = {} self.positions = {}
@ -75,7 +77,9 @@ class NorfairTracker(ObjectTracker):
hit_counter_max=self.max_disappeared, hit_counter_max=self.max_disappeared,
) )
if self.ptz_autotracker_enabled: if self.ptz_autotracker_enabled:
self.ptz_motion_estimator = PtzMotionEstimator(config, self.ptz_moving) self.ptz_motion_estimator_thread = PtzMotionEstimatorThread(
config, self.ptz_moving, stop_event
)
def register(self, track_id, obj): def register(self, track_id, obj):
rand_id = "".join(random.choices(string.ascii_lowercase + string.digits, k=6)) rand_id = "".join(random.choices(string.ascii_lowercase + string.digits, k=6))
@ -243,9 +247,11 @@ class NorfairTracker(ObjectTracker):
self.ptz_autotracker_enabled self.ptz_autotracker_enabled
and self.camera_config.onvif.autotracking.motion_estimator and self.camera_config.onvif.autotracking.motion_estimator
): ):
coord_transformations = self.ptz_motion_estimator.motion_estimator( coord_transformations = (
self.ptz_motion_estimator_thread.ptz_motion_estimator.motion_estimator(
detections, frame_time, self.camera_name detections, frame_time, self.camera_name
) )
)
tracked_objects = self.tracker.update( tracked_objects = self.tracker.update(
detections=norfair_detections, coord_transformations=coord_transformations detections=norfair_detections, coord_transformations=coord_transformations

View File

@ -478,7 +478,9 @@ def track_camera(
name, labelmap, detection_queue, result_connection, model_config, stop_event name, labelmap, detection_queue, result_connection, model_config, stop_event
) )
object_tracker = NorfairTracker(config, ptz_autotracker_enabled, ptz_moving) object_tracker = NorfairTracker(
config, ptz_autotracker_enabled, ptz_moving, stop_event
)
frame_manager = SharedMemoryFrameManager() frame_manager = SharedMemoryFrameManager()