Skip to content

Commit 4ef6214

Browse files
authored
Tracking improvements (blakeblackshear#16484)
* norfair tracker config per object type * change default R back to 3.4 * separate trackers for static and autotracking cameras * tweak params and fix debug draw * ensure all trackers are correctly updated even when there are no detections * basic reid with histograms * check mp value * check mp value again * stationary objects won't have embeddings * don't switch trackers when autotracking is toggled after startup * improve motion detection during autotracking * use helper function * get histogram in tracker instead of detect
1 parent 82f8694 commit 4ef6214

File tree

5 files changed

+327
-53
lines changed

5 files changed

+327
-53
lines changed

frigate/motion/improved_motion.py

+38
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import numpy as np
66
from scipy.ndimage import gaussian_filter
77

8+
from frigate.camera import PTZMetrics
89
from frigate.comms.config_updater import ConfigSubscriber
910
from frigate.config import MotionConfig
1011
from frigate.motion import MotionDetector
@@ -18,6 +19,7 @@ def __init__(
1819
frame_shape,
1920
config: MotionConfig,
2021
fps: int,
22+
ptz_metrics: PTZMetrics = None,
2123
name="improved",
2224
blur_radius=1,
2325
interpolation=cv2.INTER_NEAREST,
@@ -48,6 +50,8 @@ def __init__(
4850
self.contrast_values[:, 1:2] = 255
4951
self.contrast_values_index = 0
5052
self.config_subscriber = ConfigSubscriber(f"config/motion/{name}")
53+
self.ptz_metrics = ptz_metrics
54+
self.last_stop_time = None
5155

5256
def is_calibrating(self):
5357
return self.calibrating
@@ -64,6 +68,21 @@ def detect(self, frame):
6468
if not self.config.enabled:
6569
return motion_boxes
6670

71+
# if ptz motor is moving from autotracking, quickly return
72+
# a single box that is 80% of the frame
73+
if (
74+
self.ptz_metrics.autotracker_enabled.value
75+
and not self.ptz_metrics.motor_stopped.is_set()
76+
):
77+
return [
78+
(
79+
int(self.frame_shape[1] * 0.1),
80+
int(self.frame_shape[0] * 0.1),
81+
int(self.frame_shape[1] * 0.9),
82+
int(self.frame_shape[0] * 0.9),
83+
)
84+
]
85+
6786
gray = frame[0 : self.frame_shape[0], 0 : self.frame_shape[1]]
6887

6988
# resize frame
@@ -151,6 +170,25 @@ def detect(self, frame):
151170
self.motion_frame_size[0] * self.motion_frame_size[1]
152171
)
153172

173+
# check if the motor has just stopped from autotracking
174+
# if so, reassign the average to the current frame so we begin with a new baseline
175+
if (
176+
# ensure we only do this for cameras with autotracking enabled
177+
self.ptz_metrics.autotracker_enabled.value
178+
and self.ptz_metrics.motor_stopped.is_set()
179+
and (
180+
self.last_stop_time is None
181+
or self.ptz_metrics.stop_time.value != self.last_stop_time
182+
)
183+
# value is 0 on startup or when motor is moving
184+
and self.ptz_metrics.stop_time.value != 0
185+
):
186+
self.last_stop_time = self.ptz_metrics.stop_time.value
187+
188+
self.avg_frame = resized_frame.astype(np.float32)
189+
motion_boxes = []
190+
pct_motion = 0
191+
154192
# once the motion is less than 5% and the number of contours is < 4, assume its calibrated
155193
if pct_motion < 0.05 and len(motion_boxes) <= 4:
156194
self.calibrating = False

frigate/ptz/onvif.py

-1
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,6 @@ def _move_to_preset(self, camera_name: str, preset: str) -> None:
465465
return
466466

467467
self.cams[camera_name]["active"] = True
468-
self.ptz_metrics[camera_name].motor_stopped.clear()
469468
self.ptz_metrics[camera_name].start_time.value = 0
470469
self.ptz_metrics[camera_name].stop_time.value = 0
471470
move_request = self.cams[camera_name]["move_request"]

0 commit comments

Comments
 (0)