mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-09-25 10:46:51 +03:00
CI / AMD64 Build (push) Canceled after 0s
CI / AMD64 Smoke Test (push) Canceled after 0s
CI / ARM Build (push) Canceled after 0s
CI / Jetson Jetpack 6 (push) Canceled after 0s
CI / AMD64 Extra Build (push) Canceled after 0s
CI / ARM Extra Build (push) Canceled after 0s
CI / Synaptics Build (push) Canceled after 0s
CI / Assemble and push default build (push) Canceled after 0s
* don't display audio transcription provider message as health notice * show remote provider for audio transcription in health pane * adjust trigger and notifications messages to be consistent with the rest of the settings UI * disable save buttons when there are no changes in config editor * fix audio manager crash when a camera is added at runtime The audio processor and the camera maintainer both poll the same `add` config update on their own one second timers, and the maintainer is what creates `camera_metrics[name]`. When the audio processor got there first it looked the new camera up before that entry existed, and the `KeyError` took down the whole `frigate.audio_manager` process. Whether it happens depends purely on which poll fires first, so cloning a camera from the UI fails or succeeds at random. `spawn_if_needed` now skips a camera whose metrics aren't there yet and picks it up on the next poll, the same way it already waits on a late ffmpeg update. `AudioEventMaintainer` holds the `CameraMetrics` object now instead of indexing the manager dict on every audio chunk, which drops the IPC round trips and means a removed camera can't `KeyError` out of `detect_audio` after the maintainer pops the entry. The audio process is also registered with the watchdog, since a crash there previously left audio detection dead for every camera until a full restart, and it now receives the shared `DataProcessorMetrics` so `AudioTranscriptionRealTimeProcessor` gets the same type as the other real time processors. * fix stationary max_frames dropping other tracked objects When `max_frames` was set for a label, deregistering one object rebuilt norfair's list with a filter that kept an object only if it was both not the target and already on its way out, so every other healthy object of that label was dropped along with it. Any car leaving the frame took the rest of the cars with it and they came back as new tracked objects a few frames later. The filter now removes only the target, and objects that are expiring are still reaped by norfair on the next update. * fix test * fix skip_motion_threshold permanently disabling motion detection The skip check returned before the two `accumulateWeighted` calls at the end of `detect`, so a skipped frame never made it into the background and setting `calibrating` there only picked a faster alpha for calls that never ran. `avg_frame` starts as an all zero image and a normally lit scene differs from black across nearly the whole frame, so the cameras I tested measure 0.84 to 0.98 against it. Any `skip_motion_threshold` below that number skips the first frame, leaves the background black, and skips every frame after it. Motion detection is dead for that camera until the setting is removed or Frigate restarts, with no motion boxes, no motion recordings, and no regions for the tracker since the detector stays calibrating. Startup isn't the only way in. `update_mask` zeroes the background on any motion config change, and once a camera has calibrated the first IR switch or PTZ move freezes the background on the old scene, so it can't transition to the new one, which is the case the option exists for. The frame is now blended in before the early return at the same 0.2 alpha the calibrating path uses elsewhere, so a large scene change is still suppressed while the background catches up, about a second on a 5 fps camera, and then motion comes back. * dump ffmpeg logs on every restart The record watchdog restarted ffmpeg without flushing its `LogPipe`, so a camera whose recording segments went stale never showed a single line of ffmpeg output. The dump now happens in `start_or_restart_ffmpeg` right after the stop, which covers the stale record path, the record crash path, and the audio restart. `reset_capture_thread` and the audio `log_and_restart` fallback keep their own dumps since both pass `ffmpeg_process=None`. * dump ffmpeg logs once per restart The audio restart path dumped the log pipe itself before calling the helper, so the restart dump printed a second "last 100 lines" heading over an already drained deque and split the tail that `stop_ffmpeg` flushed into its own section. The heading is now only printed when there's something under it, and the audio path leaves the dump to the restart so each failure produces one section. * keep all logpipe dumps consistent
277 lines
11 KiB
Python
277 lines
11 KiB
Python
import logging
|
||
|
||
import cv2
|
||
import numpy as np
|
||
from scipy.ndimage import gaussian_filter
|
||
|
||
from frigate.camera import PTZMetrics
|
||
from frigate.config.config import RuntimeMotionConfig
|
||
from frigate.motion import MotionDetector
|
||
from frigate.util.image import grab_cv2_contours
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
|
||
class ImprovedMotionDetector(MotionDetector):
|
||
def __init__(
|
||
self,
|
||
frame_shape: tuple[int, ...],
|
||
config: RuntimeMotionConfig,
|
||
fps: int,
|
||
ptz_metrics: PTZMetrics | None = None,
|
||
name: str = "improved",
|
||
blur_radius: int = 1,
|
||
interpolation: int = cv2.INTER_NEAREST,
|
||
contrast_frame_history: int = 50,
|
||
) -> None:
|
||
self.name = name
|
||
self.config = config
|
||
self.frame_shape = frame_shape
|
||
frame_height = config.frame_height or frame_shape[0]
|
||
self.resize_factor = frame_shape[0] / frame_height
|
||
self.motion_frame_size = (
|
||
frame_height,
|
||
frame_height * frame_shape[1] // frame_shape[0],
|
||
)
|
||
self.avg_frame = np.zeros(self.motion_frame_size, np.float32)
|
||
self.motion_frame_count = 0
|
||
self.frame_counter = 0
|
||
self.update_mask()
|
||
self.save_images = False
|
||
self.calibrating = True
|
||
self.blur_radius = blur_radius
|
||
self.interpolation = interpolation
|
||
self.contrast_values = np.zeros((contrast_frame_history, 2), np.uint8)
|
||
self.contrast_values[:, 1:2] = 255
|
||
self.contrast_values_index = 0
|
||
self.ptz_metrics = ptz_metrics
|
||
self.last_stop_time: float | None = None
|
||
|
||
def is_calibrating(self) -> bool:
|
||
return self.calibrating
|
||
|
||
def detect(self, frame: np.ndarray) -> list[tuple[int, int, int, int]]:
|
||
motion_boxes: list[tuple[int, int, int, int]] = []
|
||
|
||
if not self.config.enabled:
|
||
return motion_boxes
|
||
|
||
# if ptz motor is moving from autotracking, quickly return
|
||
# a single box that is 80% of the frame
|
||
if self.ptz_metrics is not None and (
|
||
self.ptz_metrics.autotracker_enabled.value
|
||
and not self.ptz_metrics.motor_stopped.is_set()
|
||
):
|
||
return [
|
||
(
|
||
int(self.frame_shape[1] * 0.1),
|
||
int(self.frame_shape[0] * 0.1),
|
||
int(self.frame_shape[1] * 0.9),
|
||
int(self.frame_shape[0] * 0.9),
|
||
)
|
||
]
|
||
|
||
gray = frame[0 : self.frame_shape[0], 0 : self.frame_shape[1]]
|
||
|
||
# resize frame
|
||
resized_frame = cv2.resize(
|
||
gray,
|
||
dsize=(self.motion_frame_size[1], self.motion_frame_size[0]),
|
||
interpolation=self.interpolation,
|
||
)
|
||
|
||
if self.save_images:
|
||
resized_saved = resized_frame.copy()
|
||
|
||
# Improve contrast
|
||
if self.config.improve_contrast:
|
||
# TODO tracking moving average of min/max to avoid sudden contrast changes
|
||
min_value = np.percentile(resized_frame, 4).astype(np.uint8)
|
||
max_value = np.percentile(resized_frame, 96).astype(np.uint8)
|
||
# skip contrast calcs if the image is a single color
|
||
if min_value < max_value:
|
||
# keep track of the last 50 contrast values
|
||
self.contrast_values[self.contrast_values_index] = [
|
||
min_value,
|
||
max_value,
|
||
]
|
||
self.contrast_values_index += 1
|
||
if self.contrast_values_index == len(self.contrast_values):
|
||
self.contrast_values_index = 0
|
||
|
||
avg_min, avg_max = np.mean(self.contrast_values, axis=0)
|
||
|
||
resized_frame = np.clip(resized_frame, avg_min, avg_max)
|
||
resized_frame = (
|
||
((resized_frame - avg_min) / (avg_max - avg_min)) * 255
|
||
).astype(np.uint8)
|
||
|
||
if self.save_images:
|
||
contrasted_saved = resized_frame.copy()
|
||
|
||
# mask frame
|
||
# this has to come after contrast improvement
|
||
# Setting masked pixels to zero, to match the average frame at startup
|
||
resized_frame[self.mask] = [0]
|
||
|
||
resized_frame = gaussian_filter(resized_frame, sigma=1, radius=self.blur_radius)
|
||
|
||
if self.save_images:
|
||
blurred_saved = resized_frame.copy()
|
||
|
||
if self.save_images or self.calibrating:
|
||
self.frame_counter += 1
|
||
# compare to average
|
||
frameDelta = cv2.absdiff(resized_frame, cv2.convertScaleAbs(self.avg_frame))
|
||
|
||
# compute the threshold image for the current frame
|
||
thresh = cv2.threshold(
|
||
frameDelta, self.config.threshold, 255, cv2.THRESH_BINARY
|
||
)[1]
|
||
|
||
# dilate the thresholded image to fill in holes, then find contours
|
||
# on thresholded image
|
||
thresh_dilated = cv2.dilate(thresh, None, iterations=1) # type: ignore[call-overload]
|
||
contours = cv2.findContours(
|
||
thresh_dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
|
||
)
|
||
contours = grab_cv2_contours(contours)
|
||
|
||
# loop over the contours
|
||
total_contour_area: float = 0
|
||
for c in contours:
|
||
# if the contour is big enough, count it as motion
|
||
contour_area = cv2.contourArea(c)
|
||
total_contour_area += contour_area
|
||
if contour_area > (self.config.contour_area or 0):
|
||
x, y, w, h = cv2.boundingRect(c)
|
||
motion_boxes.append(
|
||
(
|
||
int(x * self.resize_factor),
|
||
int(y * self.resize_factor),
|
||
int((x + w) * self.resize_factor),
|
||
int((y + h) * self.resize_factor),
|
||
)
|
||
)
|
||
|
||
pct_motion = total_contour_area / (
|
||
self.motion_frame_size[0] * self.motion_frame_size[1]
|
||
)
|
||
|
||
# check if the motor has just stopped from autotracking
|
||
# if so, reassign the average to the current frame so we begin with a new baseline
|
||
if self.ptz_metrics is not None and (
|
||
# ensure we only do this for cameras with autotracking enabled
|
||
self.ptz_metrics.autotracker_enabled.value
|
||
and self.ptz_metrics.motor_stopped.is_set()
|
||
and (
|
||
self.last_stop_time is None
|
||
or self.ptz_metrics.stop_time.value != self.last_stop_time
|
||
)
|
||
# value is 0 on startup or when motor is moving
|
||
and self.ptz_metrics.stop_time.value != 0
|
||
):
|
||
self.last_stop_time = self.ptz_metrics.stop_time.value
|
||
|
||
self.avg_frame = resized_frame.astype(np.float32)
|
||
motion_boxes = []
|
||
pct_motion = 0
|
||
|
||
# skip motion entirely if the scene change percentage exceeds configured
|
||
# threshold. this is useful to ignore lighting storms, IR mode switches,
|
||
# etc. rather than registering them as brief motion and then recalibrating.
|
||
# note: skipping means the frame is dropped and **no recording will be
|
||
# created**, which could hide a legitimate object if the camera is actively
|
||
# auto‑tracking. the alternative is to allow motion and accept a small
|
||
# recording that can be reviewed in the timeline. disabled by default (None).
|
||
if (
|
||
self.config.skip_motion_threshold is not None
|
||
and pct_motion > self.config.skip_motion_threshold
|
||
):
|
||
# recalibrate so we transition to the new background. the frame
|
||
# still has to be blended in here, otherwise the background stays
|
||
# frozen and every subsequent frame skips as well
|
||
self.calibrating = True
|
||
cv2.accumulateWeighted(resized_frame, self.avg_frame, 0.2)
|
||
self.motion_frame_count = 0
|
||
return []
|
||
|
||
# once the motion is less than 5% and the number of contours is < 4, assume its calibrated
|
||
if pct_motion < 0.05 and len(motion_boxes) <= 4:
|
||
self.calibrating = False
|
||
|
||
# if calibrating or the motion contours are > 80% of the image area
|
||
# (lightning, ir, ptz) recalibrate. the lightning threshold does **not**
|
||
# stop motion detection entirely; it simply halts additional processing for
|
||
# the current frame once the percentage crosses the threshold. this helps
|
||
# reduce false positive object detections and CPU usage during high‑motion
|
||
# events. recordings continue to be generated because users expect data
|
||
# while a PTZ camera is moving.
|
||
if self.calibrating or pct_motion > self.config.lightning_threshold:
|
||
self.calibrating = True
|
||
|
||
if self.save_images:
|
||
thresh_dilated = cv2.cvtColor(thresh_dilated, cv2.COLOR_GRAY2BGR)
|
||
for b in motion_boxes:
|
||
cv2.rectangle(
|
||
thresh_dilated,
|
||
(int(b[0] / self.resize_factor), int(b[1] / self.resize_factor)),
|
||
(int(b[2] / self.resize_factor), int(b[3] / self.resize_factor)),
|
||
(0, 0, 255),
|
||
2,
|
||
)
|
||
frames = [
|
||
cv2.cvtColor(resized_saved, cv2.COLOR_GRAY2BGR),
|
||
cv2.cvtColor(contrasted_saved, cv2.COLOR_GRAY2BGR),
|
||
cv2.cvtColor(blurred_saved, cv2.COLOR_GRAY2BGR),
|
||
cv2.cvtColor(frameDelta, cv2.COLOR_GRAY2BGR),
|
||
cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR),
|
||
thresh_dilated,
|
||
]
|
||
cv2.imwrite(
|
||
f"debug/frames/{self.name}-{self.frame_counter}.jpg",
|
||
(
|
||
cv2.hconcat(frames)
|
||
if self.frame_shape[0] > self.frame_shape[1]
|
||
else cv2.vconcat(frames)
|
||
),
|
||
)
|
||
|
||
if len(motion_boxes) > 0:
|
||
self.motion_frame_count += 1
|
||
if self.motion_frame_count >= 10:
|
||
# only average in the current frame if the difference persists for a bit
|
||
cv2.accumulateWeighted(
|
||
resized_frame,
|
||
self.avg_frame,
|
||
0.2 if self.calibrating else self.config.frame_alpha,
|
||
)
|
||
else:
|
||
# when no motion, just keep averaging the frames together
|
||
cv2.accumulateWeighted(
|
||
resized_frame,
|
||
self.avg_frame,
|
||
0.2 if self.calibrating else self.config.frame_alpha,
|
||
)
|
||
self.motion_frame_count = 0
|
||
|
||
return motion_boxes
|
||
|
||
def update_mask(self) -> None:
|
||
resized_mask = cv2.resize(
|
||
self.config.rasterized_mask,
|
||
dsize=(self.motion_frame_size[1], self.motion_frame_size[0]),
|
||
interpolation=cv2.INTER_AREA,
|
||
)
|
||
self.mask = np.where(resized_mask == [0])
|
||
|
||
# Reset motion detection state when mask changes
|
||
# so motion detection can quickly recalibrate with the new mask
|
||
self.avg_frame = np.zeros(self.motion_frame_size, np.float32)
|
||
self.calibrating = True
|
||
self.motion_frame_count = 0
|
||
|
||
def stop(self) -> None:
|
||
"""stop the motion detector."""
|
||
pass
|