From 80e77fb85668479e59c16d638fde79aee32b7cc8 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Sat, 24 Jun 2023 18:16:22 -0500 Subject: [PATCH 01/40] Basic functionality --- frigate/app.py | 7 +- frigate/comms/dispatcher.py | 20 +++ frigate/comms/mqtt.py | 5 + frigate/config.py | 26 +++ frigate/object_processing.py | 49 +++++- frigate/ptz.py | 150 ++++++++++++++++- frigate/ptz_autotrack.py | 269 +++++++++++++++++++++++++++++++ frigate/track/norfair_tracker.py | 29 +++- frigate/types.py | 2 + frigate/video.py | 6 +- 10 files changed, 550 insertions(+), 13 deletions(-) create mode 100644 frigate/ptz_autotrack.py diff --git a/frigate/app.py b/frigate/app.py index 9d85f461ef..79e785a13d 100644 --- a/frigate/app.py +++ b/frigate/app.py @@ -117,6 +117,11 @@ def init_config(self) -> None: "improve_contrast_enabled": mp.Value( "i", self.config.cameras[camera_name].motion.improve_contrast ), + "ptz_autotracker_enabled": mp.Value( + "i", + self.config.cameras[camera_name].onvif.autotracking.enabled, + ), + "ptz_moving": mp.Value("i", 0), "motion_threshold": mp.Value( "i", self.config.cameras[camera_name].motion.threshold ), @@ -268,7 +273,7 @@ def init_web_server(self) -> None: ) def init_onvif(self) -> None: - self.onvif_controller = OnvifController(self.config) + self.onvif_controller = OnvifController(self.config, self.camera_metrics) def init_dispatcher(self) -> None: comms: list[Communicator] = [] diff --git a/frigate/comms/dispatcher.py b/frigate/comms/dispatcher.py index b7e9e88586..d6b242937e 100644 --- a/frigate/comms/dispatcher.py +++ b/frigate/comms/dispatcher.py @@ -54,6 +54,7 @@ def __init__( self._camera_settings_handlers: dict[str, Callable] = { "detect": self._on_detect_command, "improve_contrast": self._on_motion_improve_contrast_command, + "ptz_autotracker": self._on_ptz_autotracker_command, "motion": self._on_motion_command, "motion_contour_area": self._on_motion_contour_area_command, "motion_threshold": self._on_motion_threshold_command, @@ -158,6 +159,25 @@ def _on_motion_improve_contrast_command( self.publish(f"{camera_name}/improve_contrast/state", payload, retain=True) + def _on_ptz_autotracker_command(self, camera_name: str, payload: str) -> None: + """Callback for ptz_autotracker topic.""" + ptz_autotracker_settings = self.config.cameras[camera_name].onvif.autotracking + + if payload == "ON": + if not self.camera_metrics[camera_name]["ptz_autotracker_enabled"].value: + logger.info(f"Turning on ptz autotracker for {camera_name}") + self.camera_metrics[camera_name]["ptz_autotracker_enabled"].value = True + ptz_autotracker_settings.enabled = True + elif payload == "OFF": + if self.camera_metrics[camera_name]["ptz_autotracker_enabled"].value: + logger.info(f"Turning off ptz autotracker for {camera_name}") + self.camera_metrics[camera_name][ + "ptz_autotracker_enabled" + ].value = False + ptz_autotracker_settings.enabled = False + + self.publish(f"{camera_name}/ptz_autotracker/state", payload, retain=True) + def _on_motion_contour_area_command(self, camera_name: str, payload: int) -> None: """Callback for motion contour topic.""" try: diff --git a/frigate/comms/mqtt.py b/frigate/comms/mqtt.py index 07799f9dab..8a941d7fa0 100644 --- a/frigate/comms/mqtt.py +++ b/frigate/comms/mqtt.py @@ -64,6 +64,11 @@ def _set_initial_topics(self) -> None: "ON" if camera.motion.improve_contrast else "OFF", # type: ignore[union-attr] retain=True, ) + self.publish( + f"{camera_name}/ptz_autotracker/state", + "ON" if camera.onvif.autotracking.enabled else "OFF", # type: ignore[union-attr] + retain=True, + ) self.publish( f"{camera_name}/motion_threshold/state", camera.motion.threshold, # type: ignore[union-attr] diff --git a/frigate/config.py b/frigate/config.py index b71ba1907e..14b15e07ba 100644 --- a/frigate/config.py +++ b/frigate/config.py @@ -127,11 +127,37 @@ def validate_password(cls, v, values): return v +class PtzAutotrackConfig(FrigateBaseModel): + enabled: bool = Field(default=False, title="Enable PTZ auto tracking.") + motion_estimator: bool = Field(default=False, title="Use Norfair motion estimator.") + track: List[str] = Field(default=DEFAULT_TRACKED_OBJECTS, title="Objects to track.") + required_zones: List[str] = Field( + default_factory=list, + title="List of required zones to be entered in order to begin autotracking.", + ) + size_ratio: float = Field( + default=0.5, + title="Target ratio of tracked object to field of view (0.2-0.9).", + ge=0.2, + le=0.9, + ) + return_preset: Optional[str] = Field( + title="Name of camera preset to return to when object tracking is over." + ) + timeout: int = Field( + default=5, title="Seconds to delay before returning to preset." + ) + + class OnvifConfig(FrigateBaseModel): host: str = Field(default="", title="Onvif Host") port: int = Field(default=8000, title="Onvif Port") user: Optional[str] = Field(title="Onvif Username") password: Optional[str] = Field(title="Onvif Password") + autotracking: PtzAutotrackConfig = Field( + default_factory=PtzAutotrackConfig, + title="PTZ auto tracking config.", + ) class RetainModeEnum(str, Enum): diff --git a/frigate/object_processing.py b/frigate/object_processing.py index 6d31c3cddd..3ddb08e977 100644 --- a/frigate/object_processing.py +++ b/frigate/object_processing.py @@ -22,6 +22,7 @@ ) from frigate.const import CLIPS_DIR from frigate.events.maintainer import EventTypeEnum +from frigate.ptz_autotrack import PtzAutoTrackerThread from frigate.util import ( SharedMemoryFrameManager, area, @@ -143,6 +144,7 @@ def compute_score(self): def update(self, current_frame_time, obj_data): thumb_update = False significant_change = False + autotracker_update = False # if the object is not in the current frame, add a 0.0 to the score history if obj_data["frame_time"] != current_frame_time: self.score_history.append(0.0) @@ -237,9 +239,13 @@ def update(self, current_frame_time, obj_data): if self.obj_data["frame_time"] - self.previous["frame_time"] > 60: significant_change = True + # update autotrack every second? or fps? + if self.obj_data["frame_time"] - self.previous["frame_time"] > 1: + autotracker_update = True + self.obj_data.update(obj_data) self.current_zones = current_zones - return (thumb_update, significant_change) + return (thumb_update, significant_change, autotracker_update) def to_dict(self, include_thumbnail: bool = False): (self.thumbnail_data["frame_time"] if self.thumbnail_data is not None else 0.0) @@ -438,7 +444,11 @@ def zone_filtered(obj: TrackedObject, object_config): # Maintains the state of a camera class CameraState: def __init__( - self, name, config: FrigateConfig, frame_manager: SharedMemoryFrameManager + self, + name, + config: FrigateConfig, + frame_manager: SharedMemoryFrameManager, + ptz_autotracker_thread: PtzAutoTrackerThread, ): self.name = name self.config = config @@ -456,6 +466,7 @@ def __init__( self.regions = [] self.previous_frame_id = None self.callbacks = defaultdict(list) + self.ptz_autotracker_thread = ptz_autotracker_thread def get_current_frame(self, draw_options={}): with self.current_frame_lock: @@ -477,6 +488,20 @@ def get_current_frame(self, draw_options={}): thickness = 1 color = (255, 0, 0) + # draw thicker box around ptz autotracked object + if ( + self.ptz_autotracker_thread.ptz_autotracker.tracked_object[ + self.name + ] + is not None + and obj["id"] + == self.ptz_autotracker_thread.ptz_autotracker.tracked_object[ + self.name + ].obj_data["id"] + ): + thickness = 5 + color = self.config.model.colormap[obj["label"]] + # draw the bounding boxes on the frame box = obj["box"] draw_box_with_label( @@ -590,10 +615,14 @@ def update(self, frame_time, current_detections, motion_boxes, regions): for id in updated_ids: updated_obj = tracked_objects[id] - thumb_update, significant_update = updated_obj.update( + thumb_update, significant_update, autotracker_update = updated_obj.update( frame_time, current_detections[id] ) + if autotracker_update: + for c in self.callbacks["autotrack"]: + c(self.name, updated_obj, frame_time) + if thumb_update: # ensure this frame is stored in the cache if ( @@ -749,6 +778,9 @@ def __init__( self.camera_states: dict[str, CameraState] = {} self.frame_manager = SharedMemoryFrameManager() self.last_motion_detected: dict[str, float] = {} + self.ptz_autotracker_thread = PtzAutoTrackerThread( + config, dispatcher.onvif, dispatcher.camera_metrics, self.stop_event + ) def start(camera, obj: TrackedObject, current_frame_time): self.event_queue.put( @@ -775,6 +807,9 @@ def update(camera, obj: TrackedObject, current_frame_time): ) ) + def autotrack(camera, obj: TrackedObject, current_frame_time): + self.ptz_autotracker_thread.ptz_autotracker.autotrack_object(camera, obj) + def end(camera, obj: TrackedObject, current_frame_time): # populate has_snapshot obj.has_snapshot = self.should_save_snapshot(camera, obj) @@ -823,6 +858,7 @@ def end(camera, obj: TrackedObject, current_frame_time): "type": "end", } self.dispatcher.publish("events", json.dumps(message), retain=False) + self.ptz_autotracker_thread.ptz_autotracker.end_object(camera, obj) self.event_queue.put( ( @@ -859,8 +895,11 @@ def object_status(camera, object_name, status): self.dispatcher.publish(f"{camera}/{object_name}", status, retain=False) for camera in self.config.cameras.keys(): - camera_state = CameraState(camera, self.config, self.frame_manager) + camera_state = CameraState( + camera, self.config, self.frame_manager, self.ptz_autotracker_thread + ) camera_state.on("start", start) + camera_state.on("autotrack", autotrack) camera_state.on("update", update) camera_state.on("end", end) camera_state.on("snapshot", snapshot) @@ -1002,6 +1041,7 @@ def get_current_frame_time(self, camera) -> int: return self.camera_states[camera].current_frame_time def run(self): + self.ptz_autotracker_thread.start() while not self.stop_event.is_set(): try: ( @@ -1122,4 +1162,5 @@ def run(self): event_id, camera = self.event_processed_queue.get() self.camera_states[camera].finished(event_id) + self.ptz_autotracker_thread.join() logger.info("Exiting object processor...") diff --git a/frigate/ptz.py b/frigate/ptz.py index 385a230bc9..f7971a4551 100644 --- a/frigate/ptz.py +++ b/frigate/ptz.py @@ -4,9 +4,11 @@ import site from enum import Enum +import numpy from onvif import ONVIFCamera, ONVIFError from frigate.config import FrigateConfig +from frigate.types import CameraMetricsTypes logger = logging.getLogger(__name__) @@ -26,8 +28,11 @@ class OnvifCommandEnum(str, Enum): class OnvifController: - def __init__(self, config: FrigateConfig) -> None: + def __init__( + self, config: FrigateConfig, camera_metrics: dict[str, CameraMetricsTypes] + ) -> None: self.cams: dict[str, ONVIFCamera] = {} + self.camera_metrics = camera_metrics for cam_name, cam in config.cameras.items(): if not cam.enabled: @@ -68,12 +73,51 @@ def _init_onvif(self, camera_name: str) -> bool: ptz = onvif.create_ptz_service() request = ptz.create_type("GetConfigurationOptions") request.ConfigurationToken = profile.PTZConfiguration.token + ptz_config = ptz.GetConfigurationOptions(request) + + fov_space_id = next( + ( + i + for i, space in enumerate( + ptz_config.Spaces.RelativePanTiltTranslationSpace + ) + if "TranslationSpaceFov" in space["URI"] + ), + None, + ) - # setup moving request + # setup continuous moving request move_request = ptz.create_type("ContinuousMove") move_request.ProfileToken = profile.token self.cams[camera_name]["move_request"] = move_request + # setup relative moving request for autotracking + move_request = ptz.create_type("RelativeMove") + move_request.ProfileToken = profile.token + if move_request.Translation is None and fov_space_id is not None: + move_request.Translation = ptz.GetStatus( + {"ProfileToken": profile.token} + ).Position + move_request.Translation.PanTilt.space = ptz_config["Spaces"][ + "RelativePanTiltTranslationSpace" + ][fov_space_id]["URI"] + move_request.Translation.Zoom.space = ptz_config["Spaces"][ + "RelativeZoomTranslationSpace" + ][0]["URI"] + if move_request.Speed is None: + move_request.Speed = ptz.GetStatus({"ProfileToken": profile.token}).Position + self.cams[camera_name]["relative_move_request"] = move_request + + # setup relative moving request for autotracking + move_request = ptz.create_type("AbsoluteMove") + move_request.ProfileToken = profile.token + self.cams[camera_name]["absolute_move_request"] = move_request + + # status request for autotracking + status_request = ptz.create_type("GetStatus") + status_request.ProfileToken = profile.token + self.cams[camera_name]["status_request"] = status_request + # setup existing presets try: presets: list[dict] = ptz.GetPresets({"ProfileToken": profile.token}) @@ -94,6 +138,20 @@ def _init_onvif(self, camera_name: str) -> bool: if ptz_config.Spaces and ptz_config.Spaces.ContinuousZoomVelocitySpace: supported_features.append("zoom") + if ptz_config.Spaces and ptz_config.Spaces.RelativePanTiltTranslationSpace: + supported_features.append("pt-r") + + if ptz_config.Spaces and ptz_config.Spaces.RelativeZoomTranslationSpace: + supported_features.append("zoom-r") + + if fov_space_id is not None: + supported_features.append("pt-r-fov") + self.cams[camera_name][ + "relative_fov_range" + ] = ptz_config.Spaces.RelativePanTiltTranslationSpace[fov_space_id] + + self.cams[camera_name]["relative_fov_supported"] = fov_space_id is not None + self.cams[camera_name]["features"] = supported_features self.cams[camera_name]["init"] = True @@ -143,12 +201,74 @@ def _move(self, camera_name: str, command: OnvifCommandEnum) -> None: onvif.get_service("ptz").ContinuousMove(move_request) + def _move_relative(self, camera_name: str, pan, tilt, speed) -> None: + if not self.cams[camera_name]["relative_fov_supported"]: + logger.error(f"{camera_name} does not support ONVIF RelativeMove (FOV).") + return + + logger.debug(f"{camera_name} called RelativeMove: pan: {pan} tilt: {tilt}") + self.get_camera_status(camera_name) + + if self.cams[camera_name]["active"]: + logger.warning( + f"{camera_name} is already performing an action, not moving..." + ) + return + + self.cams[camera_name]["active"] = True + self.camera_metrics[camera_name]["ptz_moving"].value = True + onvif: ONVIFCamera = self.cams[camera_name]["onvif"] + move_request = self.cams[camera_name]["relative_move_request"] + + # function takes in -1 to 1 for pan and tilt, interpolate to the values of the camera. + # The onvif spec says this can report as +INF and -INF, so this may need to be modified + pan = numpy.interp( + pan, + [-1, 1], + [ + self.cams[camera_name]["relative_fov_range"]["XRange"]["Min"], + self.cams[camera_name]["relative_fov_range"]["XRange"]["Max"], + ], + ) + tilt = numpy.interp( + tilt, + [-1, 1], + [ + self.cams[camera_name]["relative_fov_range"]["YRange"]["Min"], + self.cams[camera_name]["relative_fov_range"]["YRange"]["Max"], + ], + ) + + move_request.Speed = { + "PanTilt": { + "x": speed, + "y": speed, + }, + "Zoom": 0, + } + + # move pan and tilt separately + move_request.Translation.PanTilt.x = pan + move_request.Translation.PanTilt.y = 0 + move_request.Translation.Zoom.x = 0 + + onvif.get_service("ptz").RelativeMove(move_request) + + move_request.Translation.PanTilt.x = 0 + move_request.Translation.PanTilt.y = tilt + move_request.Translation.Zoom.x = 0 + + onvif.get_service("ptz").RelativeMove(move_request) + + self.cams[camera_name]["active"] = False + def _move_to_preset(self, camera_name: str, preset: str) -> None: if preset not in self.cams[camera_name]["presets"]: logger.error(f"{preset} is not a valid preset for {camera_name}") return self.cams[camera_name]["active"] = True + self.camera_metrics[camera_name]["ptz_moving"].value = True move_request = self.cams[camera_name]["move_request"] onvif: ONVIFCamera = self.cams[camera_name]["onvif"] preset_token = self.cams[camera_name]["presets"][preset] @@ -158,6 +278,7 @@ def _move_to_preset(self, camera_name: str, preset: str) -> None: "PresetToken": preset_token, } ) + self.camera_metrics[camera_name]["ptz_moving"].value = False self.cams[camera_name]["active"] = False def _zoom(self, camera_name: str, command: OnvifCommandEnum) -> None: @@ -216,3 +337,28 @@ def get_camera_info(self, camera_name: str) -> dict[str, any]: "features": self.cams[camera_name]["features"], "presets": list(self.cams[camera_name]["presets"].keys()), } + + def get_camera_status(self, camera_name: str) -> dict[str, any]: + if camera_name not in self.cams.keys(): + logger.error(f"Onvif is not setup for {camera_name}") + return {} + + if not self.cams[camera_name]["init"]: + self._init_onvif(camera_name) + + onvif: ONVIFCamera = self.cams[camera_name]["onvif"] + status_request = self.cams[camera_name]["status_request"] + status = onvif.get_service("ptz").GetStatus(status_request) + + self.cams[camera_name]["active"] = status.MoveStatus.PanTilt != "IDLE" + self.camera_metrics[camera_name]["ptz_moving"].value = ( + status.MoveStatus.PanTilt != "IDLE" + ) + + return { + "pan": status.Position.PanTilt.x, + "tilt": status.Position.PanTilt.y, + "zoom": status.Position.Zoom.x, + "pantilt_moving": status.MoveStatus.PanTilt, + "zoom_moving": status.MoveStatus.Zoom, + } diff --git a/frigate/ptz_autotrack.py b/frigate/ptz_autotrack.py new file mode 100644 index 0000000000..5761aa4752 --- /dev/null +++ b/frigate/ptz_autotrack.py @@ -0,0 +1,269 @@ +"""Automatically pan, tilt, and zoom on detected objects via onvif.""" + +import logging +import threading +import time +from multiprocessing.synchronize import Event as MpEvent + +import cv2 +import numpy as np +from norfair.camera_motion import MotionEstimator, TranslationTransformationGetter + +from frigate.config import CameraConfig, FrigateConfig +from frigate.ptz import OnvifController +from frigate.types import CameraMetricsTypes +from frigate.util import SharedMemoryFrameManager, intersection_over_union + +logger = logging.getLogger(__name__) + + +class PtzMotionEstimator: + def __init__(self, config: CameraConfig, ptz_moving) -> None: + self.frame_manager = SharedMemoryFrameManager() + # homography is nice (zooming) but slow, translation is pan/tilt only but fast. + self.norfair_motion_estimator = MotionEstimator( + transformations_getter=TranslationTransformationGetter() + ) + self.camera_config = config + self.coord_transformations = None + self.ptz_moving = ptz_moving + logger.debug(f"Motion estimator init for cam: {config.name}") + + def motion_estimator(self, detections, frame_time, camera_name): + if self.camera_config.onvif.autotracking.enabled and self.ptz_moving.value: + logger.debug(f"Motion estimator running for {camera_name}") + + frame_id = f"{camera_name}{frame_time}" + frame = self.frame_manager.get(frame_id, self.camera_config.frame_shape) + + # mask out detections for better motion estimation + mask = np.ones(frame.shape[:2], frame.dtype) + + detection_boxes = [x[2] for x in detections] + for detection in detection_boxes: + x1, y1, x2, y2 = detection + mask[y1:y2, x1:x2] = 0 + + # merge camera config motion mask with detections. Norfair function needs 0,1 mask + mask = np.bitwise_and(mask, self.camera_config.motion.mask).clip(max=1) + + # Norfair estimator function needs color + frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGRA) + + self.coord_transformations = self.norfair_motion_estimator.update( + frame, mask + ) + + self.frame_manager.close(frame_id) + + return self.coord_transformations + + return None + + +class PtzAutoTrackerThread(threading.Thread): + def __init__( + self, + config: FrigateConfig, + onvif: OnvifController, + camera_metrics: CameraMetricsTypes, + stop_event: MpEvent, + ) -> None: + threading.Thread.__init__(self) + self.name = "frigate_ptz_autotracker" + self.ptz_autotracker = PtzAutoTracker(config, onvif, camera_metrics) + self.stop_event = stop_event + self.config = config + + def run(self): + while not self.stop_event.is_set(): + for camera_name, cam in self.config.cameras.items(): + if cam.onvif.autotracking.enabled: + self.ptz_autotracker.camera_maintenance(camera_name) + time.sleep(1) + logger.info("Exiting autotracker...") + + +class PtzAutoTracker: + def __init__( + self, + config: FrigateConfig, + onvif: OnvifController, + camera_metrics: CameraMetricsTypes, + ) -> None: + self.config = config + self.onvif = onvif + self.camera_metrics = camera_metrics + self.tracked_object: dict[str, object] = {} + self.tracked_object_previous: dict[str, object] = {} + self.object_types = {} + self.required_zones = {} + + # if cam is set to autotrack, onvif should be set up + for camera_name, cam in self.config.cameras.items(): + if cam.onvif.autotracking.enabled: + logger.debug(f"Autotracker init for cam: {camera_name}") + + self.object_types[camera_name] = cam.onvif.autotracking.track + self.required_zones[camera_name] = cam.onvif.autotracking.required_zones + + self.tracked_object[camera_name] = None + self.tracked_object_previous[camera_name] = None + + if not onvif.cams[camera_name]["init"]: + if not self.onvif._init_onvif(camera_name): + return + if not onvif.cams[camera_name]["relative_fov_supported"]: + cam.onvif.autotracking.enabled = False + self.camera_metrics[camera_name][ + "ptz_autotracker_enabled" + ].value = False + logger.warning( + f"Disabling autotracking for {camera_name}: FOV relative movement not supported" + ) + + def _autotrack_move_ptz(self, camera, obj): + camera_config = self.config.cameras[camera] + + # # frame width and height + camera_width = camera_config.frame_shape[1] + camera_height = camera_config.frame_shape[0] + + # Normalize coordinates. top right of the fov is (1,1). + pan = 0.5 - (obj.obj_data["centroid"][0] / camera_width) + tilt = 0.5 - (obj.obj_data["centroid"][1] / camera_height) + + # Calculate zoom amount + size_ratio = camera_config.onvif.autotracking.size_ratio + int(size_ratio * camera_width) + int(size_ratio * camera_height) + + # ideas: check object velocity for camera speed? + self.onvif._move_relative(camera, -pan, tilt, 1) + + def autotrack_object(self, camera, obj): + camera_config = self.config.cameras[camera] + + # check if ptz is moving + self.onvif.get_camera_status(camera) + + if camera_config.onvif.autotracking.enabled: + # either this is a brand new object that's on our camera, has our label, entered the zone, is not a false positive, and is not initially motionless + # or one we're already tracking, which assumes all those things are already true + if ( + # new object + self.tracked_object[camera] is None + and obj.camera == camera + and obj.obj_data["label"] in self.object_types[camera] + and set(obj.entered_zones) & set(self.required_zones[camera]) + and not obj.previous["false_positive"] + and not obj.false_positive + and self.tracked_object_previous[camera] is None + ): + logger.debug(f"Autotrack: New object: {obj.to_dict()}") + self.tracked_object[camera] = obj + self.tracked_object_previous[camera] = obj + self._autotrack_move_ptz(camera, obj) + + return + + if ( + # already tracking an object + self.tracked_object[camera] is not None + and self.tracked_object_previous[camera] is not None + and obj.obj_data["id"] == self.tracked_object[camera].obj_data["id"] + and obj.obj_data["frame_time"] + != self.tracked_object_previous[camera].obj_data["frame_time"] + ): + # don't move the ptz if we're relatively close to the existing box + # should we use iou or euclidean distance or both? + # distance = math.sqrt((obj.obj_data["centroid"][0] - camera_width/2)**2 + (obj.obj_data["centroid"][1] - obj.camera_height/2)**2) + # if distance <= (self.camera_width * .15) or distance <= (self.camera_height * .15) + if ( + intersection_over_union( + self.tracked_object_previous[camera].obj_data["box"], + obj.obj_data["box"], + ) + < 0.05 + ): + logger.debug( + f"Autotrack: Existing object (do NOT move ptz): {obj.to_dict()}" + ) + return + + logger.debug(f"Autotrack: Existing object (move ptz): {obj.to_dict()}") + self.tracked_object_previous[camera] = obj + self._autotrack_move_ptz(camera, obj) + + return + + if ( + # The tracker lost an object, so let's check the previous object's region and compare it with the incoming object + # If it's within bounds, start tracking that object. + # Should we check region (maybe too broad) or expand the previous object's box a bit and check that? + self.tracked_object[camera] is None + and obj.camera == camera + and obj.obj_data["label"] in self.object_types[camera] + and not obj.previous["false_positive"] + and not obj.false_positive + and obj.obj_data["motionless_count"] == 0 + and self.tracked_object_previous[camera] is not None + ): + if ( + intersection_over_union( + self.tracked_object_previous[camera].obj_data["region"], + obj.obj_data["box"], + ) + < 0.2 + ): + logger.debug(f"Autotrack: Reacquired object: {obj.to_dict()}") + self.tracked_object[camera] = obj + self.tracked_object_previous[camera] = obj + self._autotrack_move_ptz(camera, obj) + + return + + def end_object(self, camera, obj): + if self.config.cameras[camera].onvif.autotracking.enabled: + if ( + self.tracked_object[camera] is not None + and obj.obj_data["id"] == self.tracked_object[camera].obj_data["id"] + ): + logger.debug(f"Autotrack: End object: {obj.to_dict()}") + self.tracked_object[camera] = None + self.onvif.get_camera_status(camera) + + def camera_maintenance(self, camera): + # calls get_camera_status to check/update ptz movement + # returns camera to preset after timeout when tracking is over + autotracker_config = self.config.cameras[camera].onvif.autotracking + + if autotracker_config.enabled: + # regularly update camera status + if self.camera_metrics[camera]["ptz_moving"].value: + self.onvif.get_camera_status(camera) + + # return to preset if tracking is over + if ( + self.tracked_object[camera] is None + and self.tracked_object_previous[camera] is not None + and ( + # might want to use a different timestamp here? + time.time() - self.tracked_object_previous[camera].last_published + > autotracker_config.timeout + ) + and autotracker_config.return_preset + and not self.camera_metrics[camera]["ptz_moving"].value + ): + logger.debug( + f"Autotrack: Time is {time.time()}, returning to preset: {autotracker_config.return_preset}" + ) + self.onvif._move_to_preset( + camera, + autotracker_config.return_preset.lower(), + ) + self.tracked_object_previous[camera] = None + + def disable_autotracking(self, camera): + # need to call this if autotracking is disabled by mqtt?? + self.tracked_object[camera] = None diff --git a/frigate/track/norfair_tracker.py b/frigate/track/norfair_tracker.py index b0c4621b40..48cc029ee3 100644 --- a/frigate/track/norfair_tracker.py +++ b/frigate/track/norfair_tracker.py @@ -5,7 +5,8 @@ from norfair import Detection, Drawable, Tracker, draw_boxes from norfair.drawing.drawer import Drawer -from frigate.config import DetectConfig +from frigate.config import CameraConfig +from frigate.ptz_autotrack import PtzMotionEstimator from frigate.track import ObjectTracker from frigate.util import intersection_over_union @@ -54,12 +55,16 @@ def frigate_distance(detection: Detection, tracked_object) -> float: class NorfairTracker(ObjectTracker): - def __init__(self, config: DetectConfig): + def __init__(self, config: CameraConfig, ptz_autotracker_enabled, ptz_moving): self.tracked_objects = {} self.disappeared = {} self.positions = {} - self.max_disappeared = config.max_disappeared - self.detect_config = config + self.max_disappeared = config.detect.max_disappeared + self.camera_config = config + self.detect_config = config.detect + self.ptz_autotracker_enabled = ptz_autotracker_enabled.value + self.ptz_moving = ptz_moving + self.camera_name = config.name self.track_id_map = {} # TODO: could also initialize a tracker per object class if there # was a good reason to have different distance calculations @@ -69,6 +74,8 @@ def __init__(self, config: DetectConfig): initialization_delay=0, hit_counter_max=self.max_disappeared, ) + if self.ptz_autotracker_enabled: + self.ptz_motion_estimator = PtzMotionEstimator(config, self.ptz_moving) def register(self, track_id, obj): rand_id = "".join(random.choices(string.ascii_lowercase + string.digits, k=6)) @@ -230,7 +237,19 @@ def match_and_update(self, frame_time, detections): ) ) - tracked_objects = self.tracker.update(detections=norfair_detections) + coord_transformations = None + + if ( + self.ptz_autotracker_enabled + and self.camera_config.onvif.autotracking.motion_estimator + ): + coord_transformations = self.ptz_motion_estimator.motion_estimator( + detections, frame_time, self.camera_name + ) + + tracked_objects = self.tracker.update( + detections=norfair_detections, coord_transformations=coord_transformations + ) # update or create new tracks active_ids = [] diff --git a/frigate/types.py b/frigate/types.py index 8c3e546541..29991552f1 100644 --- a/frigate/types.py +++ b/frigate/types.py @@ -16,6 +16,8 @@ class CameraMetricsTypes(TypedDict): frame_queue: Queue motion_enabled: Synchronized improve_contrast_enabled: Synchronized + ptz_autotracker_enabled: Synchronized + ptz_moving: Synchronized motion_threshold: Synchronized motion_contour_area: Synchronized process: Optional[Process] diff --git a/frigate/video.py b/frigate/video.py index c02ad15c48..4dfcbedabd 100755 --- a/frigate/video.py +++ b/frigate/video.py @@ -457,6 +457,8 @@ def receiveSignal(signalNumber, frame): detection_enabled = process_info["detection_enabled"] motion_enabled = process_info["motion_enabled"] improve_contrast_enabled = process_info["improve_contrast_enabled"] + ptz_autotracker_enabled = process_info["ptz_autotracker_enabled"] + ptz_moving = process_info["ptz_moving"] motion_threshold = process_info["motion_threshold"] motion_contour_area = process_info["motion_contour_area"] @@ -476,7 +478,7 @@ def receiveSignal(signalNumber, frame): name, labelmap, detection_queue, result_connection, model_config, stop_event ) - object_tracker = NorfairTracker(config.detect) + object_tracker = NorfairTracker(config, ptz_autotracker_enabled, ptz_moving) frame_manager = SharedMemoryFrameManager() @@ -497,6 +499,7 @@ def receiveSignal(signalNumber, frame): detection_enabled, motion_enabled, stop_event, + ptz_moving, ) logger.info(f"{name}: exiting subprocess") @@ -721,6 +724,7 @@ def process_frames( detection_enabled: mp.Value, motion_enabled: mp.Value, stop_event, + ptz_moving: mp.Value, exit_on_empty: bool = False, ): # attribute labels are not tracked and are not assigned regions From 3171801607cbbbe58cda7b637f2a3917d919959a Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Thu, 29 Jun 2023 11:05:14 -0500 Subject: [PATCH 02/40] Threaded motion estimator --- frigate/ptz_autotrack.py | 19 +++++++++++++++++++ frigate/track/norfair_tracker.py | 16 +++++++++++----- frigate/video.py | 4 +++- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/frigate/ptz_autotrack.py b/frigate/ptz_autotrack.py index 5761aa4752..3e3bf29d6c 100644 --- a/frigate/ptz_autotrack.py +++ b/frigate/ptz_autotrack.py @@ -17,6 +17,21 @@ 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: def __init__(self, config: CameraConfig, ptz_moving) -> None: self.frame_manager = SharedMemoryFrameManager() @@ -56,6 +71,10 @@ def motion_estimator(self, detections, frame_time, camera_name): self.frame_manager.close(frame_id) + logger.debug( + f"frame time: {frame_time}, coord_transformations: {vars(self.coord_transformations)}" + ) + return self.coord_transformations return None diff --git a/frigate/track/norfair_tracker.py b/frigate/track/norfair_tracker.py index 48cc029ee3..2683b05d14 100644 --- a/frigate/track/norfair_tracker.py +++ b/frigate/track/norfair_tracker.py @@ -6,7 +6,7 @@ from norfair.drawing.drawer import Drawer from frigate.config import CameraConfig -from frigate.ptz_autotrack import PtzMotionEstimator +from frigate.ptz_autotrack import PtzMotionEstimatorThread from frigate.track import ObjectTracker from frigate.util import intersection_over_union @@ -55,7 +55,9 @@ def frigate_distance(detection: Detection, tracked_object) -> float: 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.disappeared = {} self.positions = {} @@ -75,7 +77,9 @@ def __init__(self, config: CameraConfig, ptz_autotracker_enabled, ptz_moving): hit_counter_max=self.max_disappeared, ) 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): rand_id = "".join(random.choices(string.ascii_lowercase + string.digits, k=6)) @@ -243,8 +247,10 @@ def match_and_update(self, frame_time, detections): self.ptz_autotracker_enabled and self.camera_config.onvif.autotracking.motion_estimator ): - coord_transformations = self.ptz_motion_estimator.motion_estimator( - detections, frame_time, self.camera_name + coord_transformations = ( + self.ptz_motion_estimator_thread.ptz_motion_estimator.motion_estimator( + detections, frame_time, self.camera_name + ) ) tracked_objects = self.tracker.update( diff --git a/frigate/video.py b/frigate/video.py index 4dfcbedabd..2eaa439c2f 100755 --- a/frigate/video.py +++ b/frigate/video.py @@ -478,7 +478,9 @@ def receiveSignal(signalNumber, frame): 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() From f26093dc4a21b4709ea3b5fae42026bad142d9b2 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Thu, 29 Jun 2023 13:20:25 -0500 Subject: [PATCH 03/40] Revert "Threaded motion estimator" This reverts commit 3171801607cbbbe58cda7b637f2a3917d919959a. --- frigate/ptz_autotrack.py | 19 ------------------- frigate/track/norfair_tracker.py | 16 +++++----------- frigate/video.py | 4 +--- 3 files changed, 6 insertions(+), 33 deletions(-) diff --git a/frigate/ptz_autotrack.py b/frigate/ptz_autotrack.py index 3e3bf29d6c..5761aa4752 100644 --- a/frigate/ptz_autotrack.py +++ b/frigate/ptz_autotrack.py @@ -17,21 +17,6 @@ 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: def __init__(self, config: CameraConfig, ptz_moving) -> None: self.frame_manager = SharedMemoryFrameManager() @@ -71,10 +56,6 @@ def motion_estimator(self, detections, frame_time, camera_name): self.frame_manager.close(frame_id) - logger.debug( - f"frame time: {frame_time}, coord_transformations: {vars(self.coord_transformations)}" - ) - return self.coord_transformations return None diff --git a/frigate/track/norfair_tracker.py b/frigate/track/norfair_tracker.py index 2683b05d14..48cc029ee3 100644 --- a/frigate/track/norfair_tracker.py +++ b/frigate/track/norfair_tracker.py @@ -6,7 +6,7 @@ from norfair.drawing.drawer import Drawer from frigate.config import CameraConfig -from frigate.ptz_autotrack import PtzMotionEstimatorThread +from frigate.ptz_autotrack import PtzMotionEstimator from frigate.track import ObjectTracker from frigate.util import intersection_over_union @@ -55,9 +55,7 @@ def frigate_distance(detection: Detection, tracked_object) -> float: class NorfairTracker(ObjectTracker): - def __init__( - self, config: CameraConfig, ptz_autotracker_enabled, ptz_moving, stop_event - ): + def __init__(self, config: CameraConfig, ptz_autotracker_enabled, ptz_moving): self.tracked_objects = {} self.disappeared = {} self.positions = {} @@ -77,9 +75,7 @@ def __init__( hit_counter_max=self.max_disappeared, ) if self.ptz_autotracker_enabled: - self.ptz_motion_estimator_thread = PtzMotionEstimatorThread( - config, self.ptz_moving, stop_event - ) + self.ptz_motion_estimator = PtzMotionEstimator(config, self.ptz_moving) def register(self, track_id, obj): rand_id = "".join(random.choices(string.ascii_lowercase + string.digits, k=6)) @@ -247,10 +243,8 @@ def match_and_update(self, frame_time, detections): self.ptz_autotracker_enabled and self.camera_config.onvif.autotracking.motion_estimator ): - coord_transformations = ( - self.ptz_motion_estimator_thread.ptz_motion_estimator.motion_estimator( - detections, frame_time, self.camera_name - ) + coord_transformations = self.ptz_motion_estimator.motion_estimator( + detections, frame_time, self.camera_name ) tracked_objects = self.tracker.update( diff --git a/frigate/video.py b/frigate/video.py index 2eaa439c2f..4dfcbedabd 100755 --- a/frigate/video.py +++ b/frigate/video.py @@ -478,9 +478,7 @@ def receiveSignal(signalNumber, frame): name, labelmap, detection_queue, result_connection, model_config, stop_event ) - object_tracker = NorfairTracker( - config, ptz_autotracker_enabled, ptz_moving, stop_event - ) + object_tracker = NorfairTracker(config, ptz_autotracker_enabled, ptz_moving) frame_manager = SharedMemoryFrameManager() From db9a408edf88ed0905e28f7ed674bdf86a341f89 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Fri, 30 Jun 2023 12:36:46 -0500 Subject: [PATCH 04/40] Don't detect motion when ptz is moving --- frigate/video.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/frigate/video.py b/frigate/video.py index 4dfcbedabd..2d4e7fd5b2 100755 --- a/frigate/video.py +++ b/frigate/video.py @@ -767,7 +767,11 @@ def process_frames( continue # look for motion if enabled - motion_boxes = motion_detector.detect(frame) if motion_enabled.value else [] + motion_boxes = ( + motion_detector.detect(frame) + if motion_enabled.value or ptz_moving.value + else [] + ) regions = [] consolidated_detections = [] From 1a78230eae0ecfaa42dab1c511ef68e98283b916 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Fri, 30 Jun 2023 12:38:44 -0500 Subject: [PATCH 05/40] fix motion logic --- frigate/video.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frigate/video.py b/frigate/video.py index 2d4e7fd5b2..597e90da93 100755 --- a/frigate/video.py +++ b/frigate/video.py @@ -769,7 +769,7 @@ def process_frames( # look for motion if enabled motion_boxes = ( motion_detector.detect(frame) - if motion_enabled.value or ptz_moving.value + if motion_enabled.value and not ptz_moving.value else [] ) From 27eb2a6088765458039c740a6502afae1e21954a Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Fri, 30 Jun 2023 12:51:50 -0500 Subject: [PATCH 06/40] fix mypy error --- frigate/comms/mqtt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frigate/comms/mqtt.py b/frigate/comms/mqtt.py index 8a941d7fa0..287232aec3 100644 --- a/frigate/comms/mqtt.py +++ b/frigate/comms/mqtt.py @@ -66,7 +66,7 @@ def _set_initial_topics(self) -> None: ) self.publish( f"{camera_name}/ptz_autotracker/state", - "ON" if camera.onvif.autotracking.enabled else "OFF", # type: ignore[union-attr] + "ON" if camera.onvif.autotracking.enabled else "OFF", retain=True, ) self.publish( From 56d074ec4e453fb669611cff4dee846134e7d725 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Sat, 1 Jul 2023 20:26:55 -0500 Subject: [PATCH 07/40] Add threaded queue for movement for slower ptzs --- frigate/ptz_autotrack.py | 94 +++++++++++++++++++++++++++++++++------- 1 file changed, 79 insertions(+), 15 deletions(-) diff --git a/frigate/ptz_autotrack.py b/frigate/ptz_autotrack.py index 5761aa4752..c1dd62690a 100644 --- a/frigate/ptz_autotrack.py +++ b/frigate/ptz_autotrack.py @@ -1,6 +1,8 @@ """Automatically pan, tilt, and zoom on detected objects via onvif.""" +import copy import logging +import queue import threading import time from multiprocessing.synchronize import Event as MpEvent @@ -22,7 +24,9 @@ def __init__(self, config: CameraConfig, ptz_moving) -> None: self.frame_manager = SharedMemoryFrameManager() # homography is nice (zooming) but slow, translation is pan/tilt only but fast. self.norfair_motion_estimator = MotionEstimator( - transformations_getter=TranslationTransformationGetter() + transformations_getter=TranslationTransformationGetter(), + min_distance=30, + max_points=500, ) self.camera_config = config self.coord_transformations = None @@ -31,10 +35,16 @@ def __init__(self, config: CameraConfig, ptz_moving) -> None: def motion_estimator(self, detections, frame_time, camera_name): if self.camera_config.onvif.autotracking.enabled and self.ptz_moving.value: - logger.debug(f"Motion estimator running for {camera_name}") + # logger.debug( + # f"Motion estimator running for {camera_name} - frame time: {frame_time}" + # ) frame_id = f"{camera_name}{frame_time}" - frame = self.frame_manager.get(frame_id, self.camera_config.frame_shape) + yuv_frame = self.frame_manager.get( + frame_id, self.camera_config.frame_shape_yuv + ) + + frame = cv2.cvtColor(yuv_frame, cv2.COLOR_YUV2GRAY_I420) # mask out detections for better motion estimation mask = np.ones(frame.shape[:2], frame.dtype) @@ -47,7 +57,7 @@ def motion_estimator(self, detections, frame_time, camera_name): # merge camera config motion mask with detections. Norfair function needs 0,1 mask mask = np.bitwise_and(mask, self.camera_config.motion.mask).clip(max=1) - # Norfair estimator function needs color + # Norfair estimator function needs color so it can convert it right back to gray frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGRA) self.coord_transformations = self.norfair_motion_estimator.update( @@ -56,6 +66,10 @@ def motion_estimator(self, detections, frame_time, camera_name): self.frame_manager.close(frame_id) + # logger.debug( + # f"Motion estimator transformation: {self.coord_transformations.rel_to_abs((0,0))}" + # ) + return self.coord_transformations return None @@ -98,6 +112,10 @@ def __init__( self.tracked_object_previous: dict[str, object] = {} self.object_types = {} self.required_zones = {} + self.move_queue = queue.Queue() + self.move_thread = threading.Thread(target=self._process_move_queue) + self.move_thread.daemon = True # Set the thread as a daemon thread + self.move_thread.start() # if cam is set to autotrack, onvif should be set up for camera_name, cam in self.config.cameras.items(): @@ -122,6 +140,42 @@ def __init__( f"Disabling autotracking for {camera_name}: FOV relative movement not supported" ) + def _process_move_queue(self): + while True: + try: + if self.move_queue.qsize() > 1: + # Accumulate values since last moved + pan = 0 + tilt = 0 + + while not self.move_queue.empty(): + camera, queued_pan, queued_tilt = self.move_queue.get() + logger.debug( + f"queue pan: {queued_pan}, queue tilt: {queued_tilt}" + ) + pan += queued_pan + tilt += queued_tilt + else: + move_data = self.move_queue.get() + camera, pan, tilt = move_data + logger.debug(f"removing pan: {pan}, removing tilt: {tilt}") + + logger.debug(f"final pan: {pan}, final tilt: {tilt}") + + self.onvif._move_relative(camera, pan, tilt, 0.1) + + # Wait until the camera finishes moving + while self.camera_metrics[camera]["ptz_moving"].value: + pass + + except queue.Empty: + pass + + def enqueue_move(self, camera, pan, tilt): + move_data = (camera, pan, tilt) + logger.debug(f"enqueue pan: {pan}, enqueue tilt: {tilt}") + self.move_queue.put(move_data) + def _autotrack_move_ptz(self, camera, obj): camera_config = self.config.cameras[camera] @@ -139,7 +193,7 @@ def _autotrack_move_ptz(self, camera, obj): int(size_ratio * camera_height) # ideas: check object velocity for camera speed? - self.onvif._move_relative(camera, -pan, tilt, 1) + self.enqueue_move(camera, -pan, tilt) def autotrack_object(self, camera, obj): camera_config = self.config.cameras[camera] @@ -160,9 +214,11 @@ def autotrack_object(self, camera, obj): and not obj.false_positive and self.tracked_object_previous[camera] is None ): - logger.debug(f"Autotrack: New object: {obj.to_dict()}") + logger.debug( + f"Autotrack: New object: {obj.obj_data['id']} {obj.obj_data['box']} {obj.obj_data['frame_time']}" + ) self.tracked_object[camera] = obj - self.tracked_object_previous[camera] = obj + self.tracked_object_previous[camera] = copy.deepcopy(obj) self._autotrack_move_ptz(camera, obj) return @@ -184,15 +240,18 @@ def autotrack_object(self, camera, obj): self.tracked_object_previous[camera].obj_data["box"], obj.obj_data["box"], ) - < 0.05 + > 0.05 ): logger.debug( - f"Autotrack: Existing object (do NOT move ptz): {obj.to_dict()}" + f"Autotrack: Existing object (do NOT move ptz): {obj.obj_data['id']} {obj.obj_data['box']} {obj.obj_data['frame_time']}" ) + self.tracked_object_previous[camera] = copy.deepcopy(obj) return - logger.debug(f"Autotrack: Existing object (move ptz): {obj.to_dict()}") - self.tracked_object_previous[camera] = obj + logger.debug( + f"Autotrack: Existing object (move ptz): {obj.obj_data['id']} {obj.obj_data['box']} {obj.obj_data['frame_time']}" + ) + self.tracked_object_previous[camera] = copy.deepcopy(obj) self._autotrack_move_ptz(camera, obj) return @@ -216,9 +275,11 @@ def autotrack_object(self, camera, obj): ) < 0.2 ): - logger.debug(f"Autotrack: Reacquired object: {obj.to_dict()}") + logger.debug( + f"Autotrack: Reacquired object: {obj.obj_data['id']} {obj.obj_data['box']} {obj.obj_data['frame_time']}" + ) self.tracked_object[camera] = obj - self.tracked_object_previous[camera] = obj + self.tracked_object_previous[camera] = copy.deepcopy(obj) self._autotrack_move_ptz(camera, obj) return @@ -229,7 +290,9 @@ def end_object(self, camera, obj): self.tracked_object[camera] is not None and obj.obj_data["id"] == self.tracked_object[camera].obj_data["id"] ): - logger.debug(f"Autotrack: End object: {obj.to_dict()}") + logger.debug( + f"Autotrack: End object: {obj.obj_data['id']} {obj.obj_data['box']}" + ) self.tracked_object[camera] = None self.onvif.get_camera_status(camera) @@ -249,7 +312,8 @@ def camera_maintenance(self, camera): and self.tracked_object_previous[camera] is not None and ( # might want to use a different timestamp here? - time.time() - self.tracked_object_previous[camera].last_published + time.time() + - self.tracked_object_previous[camera].obj_data["frame_time"] > autotracker_config.timeout ) and autotracker_config.return_preset From a5f407dba835a482f23f4fd12460cf21572feae2 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Sat, 1 Jul 2023 22:35:06 -0500 Subject: [PATCH 08/40] Move queues per camera --- frigate/ptz_autotrack.py | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/frigate/ptz_autotrack.py b/frigate/ptz_autotrack.py index c1dd62690a..64b11961ea 100644 --- a/frigate/ptz_autotrack.py +++ b/frigate/ptz_autotrack.py @@ -5,6 +5,7 @@ import queue import threading import time +from functools import partial from multiprocessing.synchronize import Event as MpEvent import cv2 @@ -112,10 +113,8 @@ def __init__( self.tracked_object_previous: dict[str, object] = {} self.object_types = {} self.required_zones = {} - self.move_queue = queue.Queue() - self.move_thread = threading.Thread(target=self._process_move_queue) - self.move_thread.daemon = True # Set the thread as a daemon thread - self.move_thread.start() + self.move_queues = {} + self.move_threads = {} # if cam is set to autotrack, onvif should be set up for camera_name, cam in self.config.cameras.items(): @@ -128,6 +127,8 @@ def __init__( self.tracked_object[camera_name] = None self.tracked_object_previous[camera_name] = None + self.move_queues[camera_name] = queue.Queue() + if not onvif.cams[camera_name]["init"]: if not self.onvif._init_onvif(camera_name): return @@ -140,24 +141,33 @@ def __init__( f"Disabling autotracking for {camera_name}: FOV relative movement not supported" ) - def _process_move_queue(self): + return + + # movement thread per camera + self.move_threads[camera_name] = threading.Thread( + target=partial(self._process_move_queue, camera_name) + ) + self.move_threads[camera_name].daemon = True + self.move_threads[camera_name].start() + + def _process_move_queue(self, camera): while True: try: - if self.move_queue.qsize() > 1: + if self.move_queues[camera].qsize() > 1: # Accumulate values since last moved pan = 0 tilt = 0 - while not self.move_queue.empty(): - camera, queued_pan, queued_tilt = self.move_queue.get() + while not self.move_queues[camera].empty(): + queued_pan, queued_tilt = self.move_queues[camera].get() logger.debug( f"queue pan: {queued_pan}, queue tilt: {queued_tilt}" ) pan += queued_pan tilt += queued_tilt else: - move_data = self.move_queue.get() - camera, pan, tilt = move_data + move_data = self.move_queues[camera].get() + pan, tilt = move_data logger.debug(f"removing pan: {pan}, removing tilt: {tilt}") logger.debug(f"final pan: {pan}, final tilt: {tilt}") @@ -172,9 +182,9 @@ def _process_move_queue(self): pass def enqueue_move(self, camera, pan, tilt): - move_data = (camera, pan, tilt) + move_data = (pan, tilt) logger.debug(f"enqueue pan: {pan}, enqueue tilt: {tilt}") - self.move_queue.put(move_data) + self.move_queues[camera].put(move_data) def _autotrack_move_ptz(self, camera, obj): camera_config = self.config.cameras[camera] From 98c161bdde4649c1db42f302e2019178ea0adb11 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Sun, 2 Jul 2023 10:52:38 -0500 Subject: [PATCH 09/40] Move autotracker start to app.py --- frigate/app.py | 13 +++++++++++++ frigate/object_processing.py | 14 +++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/frigate/app.py b/frigate/app.py index 79e785a13d..99b2cd3e97 100644 --- a/frigate/app.py +++ b/frigate/app.py @@ -40,6 +40,7 @@ from frigate.output import output_frames from frigate.plus import PlusApi from frigate.ptz import OnvifController +from frigate.ptz_autotrack import PtzAutoTrackerThread from frigate.record.record import manage_recordings from frigate.stats import StatsEmitter, stats_init from frigate.storage import StorageMaintainer @@ -327,6 +328,15 @@ def start_detectors(self) -> None: detector_config, ) + def start_ptz_autotracker(self) -> None: + self.ptz_autotracker_thread = PtzAutoTrackerThread( + self.config, + self.dispatcher.onvif, + self.dispatcher.camera_metrics, + self.stop_event, + ) + self.ptz_autotracker_thread.start() + def start_detected_frames_processor(self) -> None: self.detected_frames_processor = TrackedObjectProcessor( self.config, @@ -336,6 +346,7 @@ def start_detected_frames_processor(self) -> None: self.event_processed_queue, self.video_output_queue, self.recordings_info_queue, + self.ptz_autotracker_thread, self.stop_event, ) self.detected_frames_processor.start() @@ -488,6 +499,7 @@ def start(self) -> None: sys.exit(1) self.start_detectors() self.start_video_output_processor() + self.start_ptz_autotracker() self.start_detected_frames_processor() self.start_camera_processors() self.start_camera_capture_processes() @@ -531,6 +543,7 @@ def stop(self) -> None: self.dispatcher.stop() self.detected_frames_processor.join() + self.ptz_autotracker_thread.join() self.event_processor.join() self.event_cleanup.join() self.stats_emitter.join() diff --git a/frigate/object_processing.py b/frigate/object_processing.py index 3ddb08e977..a5edc6f1eb 100644 --- a/frigate/object_processing.py +++ b/frigate/object_processing.py @@ -240,7 +240,10 @@ def update(self, current_frame_time, obj_data): significant_change = True # update autotrack every second? or fps? - if self.obj_data["frame_time"] - self.previous["frame_time"] > 1: + if ( + self.obj_data["frame_time"] - self.previous["frame_time"] + > 0.5 # / self.camera_config.detect.fps + ): autotracker_update = True self.obj_data.update(obj_data) @@ -619,7 +622,7 @@ def update(self, frame_time, current_detections, motion_boxes, regions): frame_time, current_detections[id] ) - if autotracker_update: + if autotracker_update or significant_update: for c in self.callbacks["autotrack"]: c(self.name, updated_obj, frame_time) @@ -763,6 +766,7 @@ def __init__( event_processed_queue, video_output_queue, recordings_info_queue, + ptz_autotracker_thread, stop_event, ): threading.Thread.__init__(self) @@ -778,9 +782,7 @@ def __init__( self.camera_states: dict[str, CameraState] = {} self.frame_manager = SharedMemoryFrameManager() self.last_motion_detected: dict[str, float] = {} - self.ptz_autotracker_thread = PtzAutoTrackerThread( - config, dispatcher.onvif, dispatcher.camera_metrics, self.stop_event - ) + self.ptz_autotracker_thread = ptz_autotracker_thread def start(camera, obj: TrackedObject, current_frame_time): self.event_queue.put( @@ -1041,7 +1043,6 @@ def get_current_frame_time(self, camera) -> int: return self.camera_states[camera].current_frame_time def run(self): - self.ptz_autotracker_thread.start() while not self.stop_event.is_set(): try: ( @@ -1162,5 +1163,4 @@ def run(self): event_id, camera = self.event_processed_queue.get() self.camera_states[camera].finished(event_id) - self.ptz_autotracker_thread.join() logger.info("Exiting object processor...") From 8f590bf5cc2445f82788da4cc839aa3dc792314c Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Sun, 2 Jul 2023 10:53:20 -0500 Subject: [PATCH 10/40] iou value for tracked object --- frigate/ptz_autotrack.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frigate/ptz_autotrack.py b/frigate/ptz_autotrack.py index 64b11961ea..de90fa0268 100644 --- a/frigate/ptz_autotrack.py +++ b/frigate/ptz_autotrack.py @@ -250,7 +250,7 @@ def autotrack_object(self, camera, obj): self.tracked_object_previous[camera].obj_data["box"], obj.obj_data["box"], ) - > 0.05 + > 0.5 ): logger.debug( f"Autotrack: Existing object (do NOT move ptz): {obj.obj_data['id']} {obj.obj_data['box']} {obj.obj_data['frame_time']}" From 3059fd83c96904c912854d376b03ad8ff659f441 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Sun, 2 Jul 2023 12:58:07 -0500 Subject: [PATCH 11/40] mqtt callback --- frigate/comms/mqtt.py | 1 + 1 file changed, 1 insertion(+) diff --git a/frigate/comms/mqtt.py b/frigate/comms/mqtt.py index 287232aec3..bd38f4cb6b 100644 --- a/frigate/comms/mqtt.py +++ b/frigate/comms/mqtt.py @@ -151,6 +151,7 @@ def _start(self) -> None: "detect", "motion", "improve_contrast", + "ptz_autotracker", "motion_threshold", "motion_contour_area", ] From f05ca2b9c68c15fdd7a54e6e4f61e8a237dbf40f Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Sun, 2 Jul 2023 13:00:12 -0500 Subject: [PATCH 12/40] tracked object should be initially motionless --- frigate/ptz_autotrack.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/frigate/ptz_autotrack.py b/frigate/ptz_autotrack.py index de90fa0268..0bdc1a32b4 100644 --- a/frigate/ptz_autotrack.py +++ b/frigate/ptz_autotrack.py @@ -96,6 +96,7 @@ def run(self): if cam.onvif.autotracking.enabled: self.ptz_autotracker.camera_maintenance(camera_name) time.sleep(1) + time.sleep(0.1) logger.info("Exiting autotracker...") @@ -223,6 +224,7 @@ def autotrack_object(self, camera, obj): and not obj.previous["false_positive"] and not obj.false_positive and self.tracked_object_previous[camera] is None + and obj.obj_data["motionless_count"] == 0 ): logger.debug( f"Autotrack: New object: {obj.obj_data['id']} {obj.obj_data['box']} {obj.obj_data['frame_time']}" From e883e00c7479553bf18fcf8b582531e20df678d6 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Sun, 2 Jul 2023 13:10:17 -0500 Subject: [PATCH 13/40] only draw thicker box if autotracking is enabled --- frigate/object_processing.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frigate/object_processing.py b/frigate/object_processing.py index a5edc6f1eb..ce8ec659d2 100644 --- a/frigate/object_processing.py +++ b/frigate/object_processing.py @@ -493,7 +493,8 @@ def get_current_frame(self, draw_options={}): # draw thicker box around ptz autotracked object if ( - self.ptz_autotracker_thread.ptz_autotracker.tracked_object[ + self.camera_config.onvif.autotracking.enabled + and self.ptz_autotracker_thread.ptz_autotracker.tracked_object[ self.name ] is not None From 98d534918a55f8bba05060ae39585fe69e00d117 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Sun, 2 Jul 2023 13:40:16 -0500 Subject: [PATCH 14/40] Init if enabled when initially disabled in config --- frigate/ptz_autotrack.py | 66 +++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/frigate/ptz_autotrack.py b/frigate/ptz_autotrack.py index 0bdc1a32b4..c92ea88028 100644 --- a/frigate/ptz_autotrack.py +++ b/frigate/ptz_autotrack.py @@ -116,40 +116,48 @@ def __init__( self.required_zones = {} self.move_queues = {} self.move_threads = {} + self.autotracker_init = {} # if cam is set to autotrack, onvif should be set up for camera_name, cam in self.config.cameras.items(): + self.autotracker_init[camera_name] = False if cam.onvif.autotracking.enabled: - logger.debug(f"Autotracker init for cam: {camera_name}") - - self.object_types[camera_name] = cam.onvif.autotracking.track - self.required_zones[camera_name] = cam.onvif.autotracking.required_zones - - self.tracked_object[camera_name] = None - self.tracked_object_previous[camera_name] = None - - self.move_queues[camera_name] = queue.Queue() - - if not onvif.cams[camera_name]["init"]: - if not self.onvif._init_onvif(camera_name): - return - if not onvif.cams[camera_name]["relative_fov_supported"]: - cam.onvif.autotracking.enabled = False - self.camera_metrics[camera_name][ - "ptz_autotracker_enabled" - ].value = False - logger.warning( - f"Disabling autotracking for {camera_name}: FOV relative movement not supported" - ) + self._autotracker_setup(cam, camera_name) - return + def _autotracker_setup(self, cam, camera_name): + logger.debug(f"Autotracker init for cam: {camera_name}") - # movement thread per camera - self.move_threads[camera_name] = threading.Thread( - target=partial(self._process_move_queue, camera_name) - ) - self.move_threads[camera_name].daemon = True - self.move_threads[camera_name].start() + self.object_types[camera_name] = cam.onvif.autotracking.track + self.required_zones[camera_name] = cam.onvif.autotracking.required_zones + + self.tracked_object[camera_name] = None + self.tracked_object_previous[camera_name] = None + + self.move_queues[camera_name] = queue.Queue() + + if not self.onvif.cams[camera_name]["init"]: + if not self.onvif._init_onvif(camera_name): + return + if not self.onvif.cams[camera_name]["relative_fov_supported"]: + cam.onvif.autotracking.enabled = False + self.camera_metrics[camera_name][ + "ptz_autotracker_enabled" + ].value = False + logger.warning( + f"Disabling autotracking for {camera_name}: FOV relative movement not supported" + ) + + return + + # movement thread per camera + if not self.move_threads or not self.move_threads[camera_name]: + self.move_threads[camera_name] = threading.Thread( + target=partial(self._process_move_queue, camera_name) + ) + self.move_threads[camera_name].daemon = True + self.move_threads[camera_name].start() + + self.autotracker_init[camera_name] = True def _process_move_queue(self, camera): while True: @@ -314,6 +322,8 @@ def camera_maintenance(self, camera): autotracker_config = self.config.cameras[camera].onvif.autotracking if autotracker_config.enabled: + if not self.autotracker_init[camera]: + self._autotracker_setup(self.config.cameras[camera], camera) # regularly update camera status if self.camera_metrics[camera]["ptz_moving"].value: self.onvif.get_camera_status(camera) From 6f170358ed2f50159a09ffb23cfcbbe7737b902e Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Sun, 2 Jul 2023 14:02:52 -0500 Subject: [PATCH 15/40] Fix init --- frigate/ptz_autotrack.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frigate/ptz_autotrack.py b/frigate/ptz_autotrack.py index c92ea88028..054f7c2c42 100644 --- a/frigate/ptz_autotrack.py +++ b/frigate/ptz_autotrack.py @@ -157,7 +157,7 @@ def _autotracker_setup(self, cam, camera_name): self.move_threads[camera_name].daemon = True self.move_threads[camera_name].start() - self.autotracker_init[camera_name] = True + self.autotracker_init[camera_name] = True def _process_move_queue(self, camera): while True: From 1be67e260682378dadeaddbb5667492bd6ce1279 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Sun, 2 Jul 2023 16:08:08 -0500 Subject: [PATCH 16/40] Thread names --- frigate/ptz_autotrack.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/frigate/ptz_autotrack.py b/frigate/ptz_autotrack.py index 054f7c2c42..3d5dce392a 100644 --- a/frigate/ptz_autotrack.py +++ b/frigate/ptz_autotrack.py @@ -85,7 +85,7 @@ def __init__( stop_event: MpEvent, ) -> None: threading.Thread.__init__(self) - self.name = "frigate_ptz_autotracker" + self.name = "ptz_autotracker" self.ptz_autotracker = PtzAutoTracker(config, onvif, camera_metrics) self.stop_event = stop_event self.config = config @@ -152,7 +152,8 @@ def _autotracker_setup(self, cam, camera_name): # movement thread per camera if not self.move_threads or not self.move_threads[camera_name]: self.move_threads[camera_name] = threading.Thread( - target=partial(self._process_move_queue, camera_name) + name=f"move_thread_{camera_name}", + target=partial(self._process_move_queue, camera_name), ) self.move_threads[camera_name].daemon = True self.move_threads[camera_name].start() From 59e539375e7b5311d4c905827b969dda44e57c9c Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Sun, 2 Jul 2023 16:52:12 -0500 Subject: [PATCH 17/40] Always use motion estimator --- frigate/config.py | 7 ------- frigate/track/norfair_tracker.py | 5 +---- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/frigate/config.py b/frigate/config.py index 14b15e07ba..fb35fe29f2 100644 --- a/frigate/config.py +++ b/frigate/config.py @@ -129,18 +129,11 @@ def validate_password(cls, v, values): class PtzAutotrackConfig(FrigateBaseModel): enabled: bool = Field(default=False, title="Enable PTZ auto tracking.") - motion_estimator: bool = Field(default=False, title="Use Norfair motion estimator.") track: List[str] = Field(default=DEFAULT_TRACKED_OBJECTS, title="Objects to track.") required_zones: List[str] = Field( default_factory=list, title="List of required zones to be entered in order to begin autotracking.", ) - size_ratio: float = Field( - default=0.5, - title="Target ratio of tracked object to field of view (0.2-0.9).", - ge=0.2, - le=0.9, - ) return_preset: Optional[str] = Field( title="Name of camera preset to return to when object tracking is over." ) diff --git a/frigate/track/norfair_tracker.py b/frigate/track/norfair_tracker.py index 48cc029ee3..c051bf13a5 100644 --- a/frigate/track/norfair_tracker.py +++ b/frigate/track/norfair_tracker.py @@ -239,10 +239,7 @@ def match_and_update(self, frame_time, detections): coord_transformations = None - if ( - self.ptz_autotracker_enabled - and self.camera_config.onvif.autotracking.motion_estimator - ): + if self.ptz_autotracker_enabled: coord_transformations = self.ptz_motion_estimator.motion_estimator( detections, frame_time, self.camera_name ) From 1f56b93824b1401d2984544b8f7cf073b99dfb44 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Sun, 2 Jul 2023 18:02:43 -0500 Subject: [PATCH 18/40] docs --- docs/docs/configuration/autotracking.md | 69 +++++++++++++++++++++++++ docs/docs/configuration/cameras.md | 2 + docs/docs/configuration/index.md | 15 ++++++ docs/docs/integrations/mqtt.md | 8 +++ frigate/config.py | 6 +-- 5 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 docs/docs/configuration/autotracking.md diff --git a/docs/docs/configuration/autotracking.md b/docs/docs/configuration/autotracking.md new file mode 100644 index 0000000000..02ec10c74d --- /dev/null +++ b/docs/docs/configuration/autotracking.md @@ -0,0 +1,69 @@ +--- +id: autotracking +title: Autotracking +--- + +An ONVIF-capable camera that supports relative movement within the field of view (FOV) can also be configured to automatically track moving objects and keep them in the center of the frame. + +## Autotracking behavior + +Once Frigate determines that an object is not a false positive and has entered one of the required zones, the autotracker will move the PTZ camera to keep the object centered in the frame until the object either moves out of the frame, the PTZ is not capable of any more movement, or Frigate loses track of it. + +Upon loss of tracking, Frigate will scan the region of the lost object for `timeout` seconds. If an object of the same type is found in that region, Frigate will track that new object. + +When tracking has ended, Frigate will return to the camera preset specified by the `return_preset` configuration entry. + +## Checking ONVIF camera support + +Frigate autotracking functions with PTZ cameras capable of relative movement within the field of view (as specified in the [ONVIF spec](https://www.onvif.org/specs/srv/ptz/ONVIF-PTZ-Service-Spec-v1712.pdf) as `RelativePanTiltTranslationSpace` having a `TranslationSpaceFov` entry). + +Many cheaper PTZs likely don't support this standard. To see if your PTZ camera does, you can download and run [this simple Python script](https://gist.github.com/hawkeye217/152a1d4ba80760dac95d46e143d37112), replacing the details on line 4 with your camera's IP address, ONVIF port, username, and password. + +## Configuration + +First, configure the ONVIF parameters for your camera, then specify the object types to track, a required zone the object must enter, and a camera preset name to return to when tracking has ended. Optionally, specify a delay in seconds before Frigate returns the camera to the preset. + +An [ONVIF connection](cameras.md) is required for autotracking to function. + +Note that `autotracking` is disabled by default but can be enabled in the configuration or by MQTT. + +```yaml +cameras: + ptzcamera: + ... + onvif: + # Required: host of the camera being connected to. + host: 0.0.0.0 + # Optional: ONVIF port for device (default: shown below). + port: 8000 + # Optional: username for login. + # NOTE: Some devices require admin to access ONVIF. + user: admin + # Optional: password for login. + password: admin + # Optional: PTZ camera object autotracking. Keeps a moving object in + # the center of the frame by automatically moving the PTZ camera. + autotracking: + # Optional: enable/disable object autotracking. (default: shown below) + enabled: False + # Optional: list of objects to track from labelmap.txt (default: shown below) + track: + - person + # Required: Begin automatically tracking an object when it enters any of the listed zones. + required_zones: + - zone_name + # Required: Name of ONVIF camera preset to return to when tracking is over. + return_preset: preset_name + # Optional: Seconds to delay before returning to preset. (default: shown below) + timeout: 10 +``` + +## Best practices and considerations + +Every PTZ camera is different, so autotracking may not perform ideally in every situation. This experimental feature was initially developed using an EmpireTech/Dahua SD1A404XB-GNR. + +The object tracker in Frigate estimates the motion of the PTZ so that tracked objects are preserved when the camera moves. In most cases (especially for faster moving objects), the default 5 fps is insufficient for the motion estimator to perform accurately. 10 fps is the current recommendation. Higher frame rates will likely not be more performant and will only slow down Frigate and the motion estimator. Adjust your camera to output 10 frames per second and change the `fps` parameter in the [detect configuration](index.md) of your configuration file. + +A fast [detector](detectors.md) is recommended. CPU detectors will not perform well or won't work at all. If Frigate already has trouble keeping track of your object, the autotracker will struggle as well. + +The autotracker queues up motion requests for the tracked object while the PTZ is moving and will move make one longer move when complete. If your PTZ's motor is slow, you may not be able to reliably autotrack fast moving objects. diff --git a/docs/docs/configuration/cameras.md b/docs/docs/configuration/cameras.md index 8f907cb3f6..1804003a5f 100644 --- a/docs/docs/configuration/cameras.md +++ b/docs/docs/configuration/cameras.md @@ -66,3 +66,5 @@ cameras: ``` then PTZ controls will be available in the cameras WebUI. + +An ONVIF-capable camera that supports relative movement within the field of view (FOV) can also be configured to automatically track moving objects and keep them in the center of the frame. For autotracking setup, see the [autotracking](autotracking.md) docs. diff --git a/docs/docs/configuration/index.md b/docs/docs/configuration/index.md index ac65a10189..9ca1354196 100644 --- a/docs/docs/configuration/index.md +++ b/docs/docs/configuration/index.md @@ -535,6 +535,21 @@ cameras: user: admin # Optional: password for login. password: admin + # Optional: PTZ camera object autotracking. Keeps a moving object in + # the center of the frame by automatically moving the PTZ camera. + autotracking: + # Optional: enable/disable object autotracking. (default: shown below) + enabled: False + # Optional: list of objects to track from labelmap.txt (default: shown below) + track: + - person + # Required: Begin automatically tracking an object when it enters any of the listed zones. + required_zones: + - zone_name + # Required: Name of ONVIF camera preset to return to when tracking is over. + return_preset: preset_name + # Optional: Seconds to delay before returning to preset. (default: shown below) + timeout: 10 # Optional: Configuration for how to sort the cameras in the Birdseye view. birdseye: diff --git a/docs/docs/integrations/mqtt.md b/docs/docs/integrations/mqtt.md index cde7605590..7fbf3b8e52 100644 --- a/docs/docs/integrations/mqtt.md +++ b/docs/docs/integrations/mqtt.md @@ -180,3 +180,11 @@ Topic to send PTZ commands to camera. | `MOVE_