Compare commits

..
Author SHA1 Message Date
Nicolas Mowen 130815ac70 Support HEIC images for Face Recognition 2026-11-19 08:04:18 -07:00
16 changed files with 349 additions and 856 deletions
+6 -16
View File
@@ -17,10 +17,10 @@ import re
from enum import Enum
from urllib.parse import unquote
from peewee import DoesNotExist, Field
from peewee import DoesNotExist
from frigate.config import FrigateConfig
from frigate.const import CLIPS_DIR, EXPORT_DIR
from frigate.const import EXPORT_DIR
from frigate.models import Export, User
logger = logging.getLogger(__name__)
@@ -151,7 +151,6 @@ def _resolve_clip(
# /clips/thumbs/{cam}/... → camera
# /clips/previews/{cam}/... → camera
# /clips/review/thumb-{cam}-{review_id}.webp → camera (parsed)
# /clips/export/{export_id}.{ext} → camera (DB lookup)
# /clips/faces/... → admin-only
# /clips/genai-requests/... → admin-only
# /clips/preview_restart_cache/... → admin-only
@@ -176,11 +175,6 @@ def _resolve_clip(
return MediaAuthResolution.CAMERA, camera
return MediaAuthResolution.UNRESOLVED_MEDIA, None
if second == "export" and len(parts) == 3:
return _export_camera(
Export.thumb_path, os.path.join(CLIPS_DIR, "export", parts[2])
)
if second in ("faces", "genai-requests", "preview_restart_cache"):
return MediaAuthResolution.ADMIN_ONLY, None
@@ -240,19 +234,15 @@ def _resolve_export(
if len(parts) != 2:
return MediaAuthResolution.UNRESOLVED_MEDIA, None
return _export_camera(Export.video_path, os.path.join(EXPORT_DIR, parts[1]))
def _export_camera(
field: Field, full_path: str
) -> tuple[MediaAuthResolution, str | None]:
filename = parts[1]
full_path = os.path.join(EXPORT_DIR, filename)
try:
export = Export.get(field == full_path)
export = Export.get(Export.video_path == full_path)
return MediaAuthResolution.CAMERA, export.camera
except DoesNotExist:
return MediaAuthResolution.UNRESOLVED_MEDIA, None
except Exception as e:
logger.warning("Export DB lookup failed for %s: %s", full_path, e)
logger.warning("Export DB lookup failed for %s: %s", filename, e)
return MediaAuthResolution.UNRESOLVED_MEDIA, None
-1
View File
@@ -42,7 +42,6 @@ def should_update_db(prev_event: dict[str, Any], current_event: dict[str, Any])
or prev_event["recognized_license_plate"]
!= current_event["recognized_license_plate"]
or prev_event["path_data"] != current_event["path_data"]
or prev_event["max_severity"] != current_event["max_severity"]
):
return True
return False
+7 -26
View File
@@ -187,35 +187,16 @@ def build_review_description_response_format(concerns: list[str]) -> dict[str, A
Strips the `time` field (populated server-side) and drops
`other_concerns` when no concerns are configured.
Every remaining property must be listed in `required` because strict
providers (OpenAI) reject a schema with any optional property.
"""
schema = ReviewMetadata.model_json_schema()
properties = schema.setdefault("properties", {})
required = schema.setdefault("required", [])
properties.pop("time", None)
schema.get("properties", {}).pop("time", None)
if "time" in required:
required.remove("time")
if concerns:
# The model field is nullable with a default so stored metadata
# without concerns still parses. For generation, require a plain list
# instead; the model returns an empty list when no concern applies.
properties["other_concerns"] = {
"type": "array",
"items": {"type": "string"},
"description": properties["other_concerns"]["description"],
}
if "other_concerns" not in required:
required.append("other_concerns")
else:
properties.pop("other_concerns", None)
if "other_concerns" in required:
required.remove("other_concerns")
if "time" in schema.get("required", []):
schema["required"].remove("time")
if not concerns:
schema.get("properties", {}).pop("other_concerns", None)
if "other_concerns" in schema.get("required", []):
schema["required"].remove("other_concerns")
return {
"type": "json_schema",
+48 -13
View File
@@ -30,7 +30,6 @@ from frigate.const import (
UPSERT_REVIEW_SEGMENT,
)
from frigate.models import ReviewSegment
from frigate.review.severity import get_review_severity
from frigate.review.types import SeverityEnum
from frigate.track.object_processing import ManualEventState
from frigate.util.image import SharedMemoryFrameManager, calculate_16_9_crop
@@ -218,21 +217,57 @@ class ActiveObjects:
}
for o in all_objects:
severity = get_review_severity(
camera_config,
label=o["label"],
current_zones=o["current_zones"],
false_positive=o["false_positive"],
detected=o["frame_time"] == frame_time,
position_changes=o["position_changes"],
motionless_count=o["motionless_count"],
pending_loitering=o["pending_loitering"],
)
if (
o["motionless_count"] >= camera_config.detect.stationary.threshold
and not o["pending_loitering"]
):
# no stationary objects unless loitering
continue
if severity == SeverityEnum.alert:
if o["position_changes"] == 0:
# object must have moved at least once
continue
if o["frame_time"] != frame_time:
# object must be detected in this frame
continue
if o["false_positive"]:
# object must not be a false positive
continue
if (
o["label"] in camera_config.review.alerts.labels
and (
not camera_config.review.alerts.required_zones
or (
len(o["current_zones"]) > 0
and set(o["current_zones"])
& set(camera_config.review.alerts.required_zones)
)
)
and camera_config.review.alerts.enabled
):
self.categorized_objects["alerts"].append(o)
elif severity == SeverityEnum.detection:
continue
if (
(
camera_config.review.detections.labels is None
or o["label"] in camera_config.review.detections.labels
)
and (
not camera_config.review.detections.required_zones
or (
len(o["current_zones"]) > 0
and set(o["current_zones"])
& set(camera_config.review.detections.required_zones)
)
)
and camera_config.review.detections.enabled
):
self.categorized_objects["detections"].append(o)
continue
def has_active_objects(self) -> bool:
return (
-80
View File
@@ -1,80 +0,0 @@
"""Rules for which review severity a tracked object qualifies for."""
from typing import cast
from frigate.config import CameraConfig
from frigate.review.types import SeverityEnum
def get_review_severity(
camera_config: CameraConfig,
*,
label: str,
current_zones: list[str],
false_positive: bool,
detected: bool,
position_changes: int,
motionless_count: int,
pending_loitering: bool,
) -> SeverityEnum | None:
"""Get the review severity a tracked object qualifies for on the current frame.
Review segments and a tracked object's max_severity both use this, so an
object is only reported as an alert or detection when it could actually
create or extend a review segment of that severity.
Args:
camera_config: Config for the camera the object is on
label: The object's label
current_zones: Zones the object is in on the current frame
false_positive: Whether the object is still considered a false positive
detected: Whether the object was detected on the current frame
position_changes: Number of times the object has changed position
motionless_count: Number of frames the object has not moved
pending_loitering: Whether the object is in a loitering zone but has
not met the loitering time yet
Returns:
The severity the object qualifies for, or None if it does not qualify
"""
# always populated from detect fps when the config is loaded
stationary_threshold = cast(int, camera_config.detect.stationary.threshold)
# stationary objects don't count unless they are loitering
if motionless_count >= stationary_threshold and not pending_loitering:
return None
# object must have moved at least once
if position_changes == 0:
return None
if not detected or false_positive:
return None
review_config = camera_config.review
zones = set(current_zones)
if (
review_config.alerts.enabled
and label in review_config.alerts.labels
and (
not review_config.alerts.required_zones
or zones & set(review_config.alerts.required_zones)
)
):
return SeverityEnum.alert
if (
review_config.detections.enabled
and (
review_config.detections.labels is None
or label in review_config.detections.labels
)
and (
not review_config.detections.required_zones
or zones & set(review_config.detections.required_zones)
)
):
return SeverityEnum.detection
return None
@@ -1,42 +0,0 @@
"""When tracked object updates are written to the database."""
import unittest
from typing import Any
from frigate.events.maintainer import should_update_db
def _event(**overrides: Any) -> dict[str, Any]:
return {
"end_time": None,
"has_clip": False,
"has_snapshot": True,
"top_score": 0.8,
"entered_zones": ["porch"],
"average_estimated_speed": 0,
"velocity_angle": 0,
"recognized_license_plate": None,
"path_data": [],
"max_severity": None,
**overrides,
}
class TestShouldUpdateDb(unittest.TestCase):
def test_unchanged_saved_event_is_not_updated(self):
self.assertFalse(should_update_db(_event(), _event()))
def test_max_severity_change_updates_saved_event(self):
self.assertTrue(should_update_db(_event(), _event(max_severity="alert")))
def test_max_severity_change_ignored_when_not_saved(self):
self.assertFalse(
should_update_db(
_event(has_snapshot=False),
_event(has_snapshot=False, max_severity="alert"),
)
)
if __name__ == "__main__":
unittest.main()
-36
View File
@@ -7,47 +7,11 @@ from frigate.genai.prompts import (
REVIEW_DESCRIPTION_FIELD_GUIDELINES,
REVIEW_RESPONSE_STYLES,
build_review_description_prompt,
build_review_description_response_format,
build_review_summary_prompt,
get_review_field_guidelines,
)
class TestReviewDescriptionResponseFormat(unittest.TestCase):
def _schema(self, concerns: list[str]) -> dict:
response_format = build_review_description_response_format(concerns)
self.assertTrue(response_format["json_schema"]["strict"])
return response_format["json_schema"]["schema"]
def test_every_property_is_required_for_strict_mode(self):
for concerns in ([], ["a person loitering near the gate"]):
with self.subTest(concerns=concerns):
schema = self._schema(concerns)
self.assertEqual(set(schema["properties"]), set(schema["required"]))
def test_time_is_never_requested(self):
schema = self._schema(["a person loitering near the gate"])
self.assertNotIn("time", schema["properties"])
self.assertNotIn("time", schema["required"])
def test_concerns_omitted_when_not_configured(self):
schema = self._schema([])
self.assertNotIn("other_concerns", schema["properties"])
self.assertNotIn("other_concerns", schema["required"])
def test_concerns_requested_as_plain_list_when_configured(self):
schema = self._schema(["a person loitering near the gate"])
concerns = schema["properties"]["other_concerns"]
self.assertEqual(concerns["type"], "array")
self.assertEqual(concerns["items"], {"type": "string"})
self.assertNotIn("anyOf", concerns)
self.assertNotIn("default", concerns)
def test_schema_is_not_shared_between_calls(self):
self._schema(["a person loitering near the gate"])
self.assertNotIn("other_concerns", self._schema([])["properties"])
class TestReviewResponseStyle(unittest.TestCase):
def _build_prompt(self, response_style: str = "default") -> str:
review_data = {
+2 -30
View File
@@ -20,7 +20,7 @@ from frigate.api.media_auth import (
resolve_media_uri,
)
from frigate.config import FrigateConfig
from frigate.const import CLIPS_DIR, EXPORT_DIR
from frigate.const import EXPORT_DIR
from frigate.models import Event, Export, Recordings, ReviewSegment
from frigate.test.const import TEST_DB, TEST_DB_CLEANUPS, TEST_DB_PRAGMAS
@@ -247,7 +247,7 @@ class TestExportResolution(unittest.TestCase):
name=f"export-{export_id}",
date=int(datetime.datetime.now().timestamp()),
video_path=os.path.join(EXPORT_DIR, filename),
thumb_path=os.path.join(CLIPS_DIR, "export", f"{export_id}.webp"),
thumb_path=os.path.join(EXPORT_DIR, f"{filename}.jpg"),
in_progress=False,
).execute()
@@ -280,34 +280,6 @@ class TestExportResolution(unittest.TestCase):
resolution, _ = resolve_media_uri("/exports/clip.mp4", self.config)
self.assertEqual(resolution, MediaAuthResolution.UNRESOLVED_MEDIA)
def test_export_thumbnail_resolves_camera(self):
self._insert_export("back_door_abc123", "back_door", "back_door_clip.mp4")
resolution, camera = resolve_media_uri(
"/clips/export/back_door_abc123.webp", self.config
)
self.assertEqual(resolution, MediaAuthResolution.CAMERA)
self.assertEqual(camera, "back_door")
def test_unknown_export_thumbnail_is_unresolved(self):
resolution, _ = resolve_media_uri("/clips/export/missing.webp", self.config)
self.assertEqual(resolution, MediaAuthResolution.UNRESOLVED_MEDIA)
def test_export_thumbnail_gated_by_camera_access(self):
self._insert_export("back_door_abc123", "back_door", "back_door_clip.mp4")
self._insert_export("front_door_def456", "front_door", "front_door_clip.mp4")
url = "/clips/export/{}.webp"
self.assertIsNone(
deny_response_for_media_uri(
url.format("front_door_def456"), "limited_user", self.config
)
)
self.assertEqual(
deny_response_for_media_uri(
url.format("back_door_abc123"), "limited_user", self.config
),
403,
)
class TestDenyResponseForMediaUri(unittest.TestCase):
"""End-to-end decision check used by /auth."""
-253
View File
@@ -1,253 +0,0 @@
"""Review severity rules shared by review segments and tracked objects."""
import unittest
from typing import Any
from frigate.config import FrigateConfig
from frigate.review.maintainer import ActiveObjects
from frigate.review.severity import get_review_severity
from frigate.review.types import SeverityEnum
from frigate.track.tracked_object import TrackedObject
CAMERA = "cam"
FPS = 5
ZONE = "porch"
INSIDE_X = 480
OUTSIDE_X = 160
def _config(alerts_enabled: bool = True) -> FrigateConfig:
return FrigateConfig(
**{
"mqtt": {"enabled": False},
"cameras": {
CAMERA: {
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.1:554/video", "roles": ["detect"]}
]
},
"detect": {"width": 640, "height": 480, "fps": FPS},
"objects": {"track": ["person"]},
"zones": {ZONE: {"coordinates": "0.5,0,1,0,1,1,0.5,1"}},
"review": {
"alerts": {
"enabled": alerts_enabled,
"required_zones": [ZONE],
}
},
}
},
}
)
class TestGetReviewSeverity(unittest.TestCase):
def setUp(self):
self.camera_config = _config().cameras[CAMERA]
self.stationary_threshold = self.camera_config.detect.stationary.threshold
def _severity(self, camera_config=None, **overrides: Any) -> SeverityEnum | None:
kwargs = {
"label": "person",
"current_zones": [ZONE],
"false_positive": False,
"detected": True,
"position_changes": 1,
"motionless_count": 0,
"pending_loitering": False,
**overrides,
}
return get_review_severity(camera_config or self.camera_config, **kwargs)
def test_alert_in_required_zone(self):
self.assertEqual(self._severity(), SeverityEnum.alert)
def test_detection_outside_alert_required_zone(self):
self.assertEqual(self._severity(current_zones=[]), SeverityEnum.detection)
def test_detection_when_alerts_disabled(self):
camera_config = _config(alerts_enabled=False).cameras[CAMERA]
self.assertEqual(
self._severity(camera_config=camera_config), SeverityEnum.detection
)
def test_stationary_object_does_not_qualify(self):
self.assertIsNone(self._severity(motionless_count=self.stationary_threshold))
def test_stationary_loitering_object_qualifies(self):
self.assertEqual(
self._severity(
motionless_count=self.stationary_threshold, pending_loitering=True
),
SeverityEnum.alert,
)
def test_object_that_never_moved_does_not_qualify(self):
self.assertIsNone(self._severity(position_changes=0))
def test_undetected_object_does_not_qualify(self):
self.assertIsNone(self._severity(detected=False))
def test_false_positive_does_not_qualify(self):
self.assertIsNone(self._severity(false_positive=True))
class TestTrackedObjectMaxSeverity(unittest.TestCase):
"""max_severity must match what review segments would have been created."""
def setUp(self):
config = _config()
self.camera_config = config.cameras[CAMERA]
self.stationary_threshold = self.camera_config.detect.stationary.threshold
self.frame = 0
self.position_changes = 0
self.motionless_count = 0
self.last_detection = self._obj_data(OUTSIDE_X, 0.6, 1000.0)
self.obj = TrackedObject(
config.model_for_camera(CAMERA),
self.camera_config,
config.ui,
{},
{**self.last_detection, "score_history": [0.6]},
)
def _obj_data(self, x: int, score: float, frame_time: float) -> dict[str, Any]:
return {
"id": "1000.0-abcdef",
"label": "person",
"frame_time": frame_time,
"start_time": 1000.0,
"score": score,
"box": (x - 40, 200, x + 40, 360),
"area": 12800,
"ratio": 0.5,
"region": (0, 0, 640, 480),
"centroid": (x, 280),
"position_changes": self.position_changes,
"motionless_count": self.motionless_count,
"attributes": [],
"sub_label": None,
}
def _step(
self, x: int, score: float, moved: bool, detected: bool = True
) -> SeverityEnum | None:
"""Advance one frame and return the severity review segments would see."""
self.frame += 1
frame_time = 1000.0 + self.frame / FPS
if detected:
# mirror the tracker, which counts the first move and any move
# after the object was stationary as a position change
if moved:
if (
self.position_changes == 0
or self.motionless_count >= self.stationary_threshold
):
self.position_changes += 1
self.motionless_count = 0
else:
self.motionless_count += 1
self.last_detection = self._obj_data(x, score, frame_time)
_, self.last_significant, _, _ = self.obj.update(
frame_time, dict(self.last_detection), False
)
activity = ActiveObjects(frame_time, self.camera_config, [self.obj.to_dict()])
if activity.has_activity_category(SeverityEnum.alert):
return SeverityEnum.alert
if activity.has_activity_category(SeverityEnum.detection):
return SeverityEnum.detection
return None
def test_object_confirmed_while_stationary_in_zone_has_no_severity(self):
"""An object that only becomes a true positive after it stopped moving
never creates a review, so it must not be reported as an alert even
though it entered a required zone."""
review_severities = []
# walks into the zone with a low score, so it is a false positive
for i in range(10):
review_severities.append(self._step(OUTSIDE_X + i * 40, 0.6, moved=True))
# sits still until it is stationary
for _ in range(self.stationary_threshold):
review_severities.append(self._step(INSIDE_X, 0.6, moved=False))
# a re-detection while still stationary confirms it as a true positive
for _ in range(10):
review_severities.append(self._step(INSIDE_X, 0.9, moved=False))
# leaves and is no longer detected
for _ in range(10):
review_severities.append(self._step(INSIDE_X, 0.9, False, detected=False))
self.assertFalse(self.obj.false_positive)
self.assertEqual(self.obj.obj_data["position_changes"], 1)
self.assertEqual(self.obj.entered_zones, [ZONE])
self.assertEqual(set(review_severities), {None})
self.assertIsNone(self.obj.max_severity)
self.assertIsNone(self.obj.to_dict()["max_severity"])
def test_severity_change_is_significant(self):
"""Confirming an object already moving in the zone changes nothing else
that is watched, but the new severity still has to be published."""
for i in range(10):
self._step(OUTSIDE_X + i * 40, 0.6, moved=True)
self.assertEqual(self.obj.current_zones, [ZONE])
self.assertIsNone(self.obj.max_severity)
for i in range(10):
self._step(INSIDE_X + (i % 2) * 10, 0.9, moved=True)
if self.obj.max_severity is not None:
break
self.assertEqual(self.obj.max_severity, SeverityEnum.alert)
self.assertTrue(self.last_significant)
self._step(INSIDE_X, 0.9, moved=True)
self.assertFalse(self.last_significant)
def test_alert_is_kept_after_object_stops_qualifying(self):
for i in range(10):
self._step(OUTSIDE_X + i * 40, 0.9, moved=True)
self.assertEqual(self.obj.current_zones, [ZONE])
self.assertEqual(self.obj.max_severity, SeverityEnum.alert)
for _ in range(self.stationary_threshold + 5):
self._step(INSIDE_X, 0.9, moved=False)
self.assertTrue(self.obj.is_stationary())
self.assertEqual(self.obj.max_severity, SeverityEnum.alert)
def test_detection_upgrades_to_alert_and_is_not_downgraded(self):
review_severities = []
for _ in range(8):
review_severities.append(self._step(OUTSIDE_X, 0.9, moved=True))
self.assertEqual(self.obj.max_severity, SeverityEnum.detection)
for _ in range(5):
review_severities.append(self._step(INSIDE_X, 0.9, moved=True))
self.assertEqual(self.obj.max_severity, SeverityEnum.alert)
for _ in range(5):
review_severities.append(self._step(OUTSIDE_X, 0.9, moved=True))
self.assertEqual(review_severities[-1], SeverityEnum.detection)
self.assertEqual(self.obj.max_severity, SeverityEnum.alert)
self.assertIn(SeverityEnum.alert, review_severities)
if __name__ == "__main__":
unittest.main()
+40 -18
View File
@@ -511,33 +511,55 @@ class TestHttp(unittest.TestCase):
Recordings.get(Recordings.id == rec_d3_id)
def test_storage_cleanup_keeps_retained(self):
"""Ensure cleanup steps over retained segments to reach newer ones."""
"""Ensure that all recordings are cleaned up when necessary."""
config = FrigateConfig(**self.minimal_config)
storage = StorageMaintainer(config, MagicMock())
seg_len = 600
base = datetime.datetime.now().timestamp() - 7200
_insert_mock_event("123456.keep", base, base + 2.5 * seg_len, True)
id = "123456.keep"
time_keep = datetime.datetime.now().timestamp()
_insert_mock_event(
id,
time_keep,
time_keep + 30,
True,
)
rec_k_id = "1234567.keep"
rec_k2_id = "1234568.keep"
rec_k3_id = "1234569.keep"
_insert_mock_recording(
rec_k_id,
os.path.join(self.test_dir, f"{rec_k_id}.tmp"),
time_keep,
time_keep + 10,
)
_insert_mock_recording(
rec_k2_id,
os.path.join(self.test_dir, f"{rec_k2_id}.tmp"),
time_keep + 10,
time_keep + 20,
)
_insert_mock_recording(
rec_k3_id,
os.path.join(self.test_dir, f"{rec_k3_id}.tmp"),
time_keep + 20,
time_keep + 30,
)
paths = {}
for i in range(10):
rec_id = f"segment-{i}"
paths[rec_id] = os.path.join(self.test_dir, f"{rec_id}.tmp")
time_delete = datetime.datetime.now().timestamp() - 7200
for i in range(0, 59):
id = f"{123456 + i}.delete"
_insert_mock_recording(
rec_id, paths[rec_id], base + i * seg_len, base + (i + 1) * seg_len
id,
os.path.join(self.test_dir, f"{id}.tmp"),
time_delete,
time_delete + 600,
)
# Hourly bandwidth is 48 MB, so cleanup needs all 56 MB of unretained
# segments and must pass the older retained ones to get there.
storage.calculate_camera_bandwidth()
storage.reduce_storage_consumption()
retained = {"segment-0", "segment-1", "segment-2"}
remaining = {r.id for r in Recordings.select(Recordings.id)}
on_disk = {rec_id for rec_id, path in paths.items() if os.path.exists(path)}
self.assertEqual(remaining, retained)
self.assertEqual(on_disk, retained)
self.assertFalse(storage.retention_unmet)
assert Recordings.get(Recordings.id == rec_k_id)
assert Recordings.get(Recordings.id == rec_k2_id)
assert Recordings.get(Recordings.id == rec_k3_id)
def test_unmet_retention_sets_the_live_flag(self):
config = FrigateConfig(**self.minimal_config)
+30 -25
View File
@@ -17,7 +17,6 @@ from frigate.config import (
)
from frigate.const import CLIPS_DIR, REPLAY_CAMERA_PREFIX, THUMB_DIR
from frigate.detectors.detector_config import ModelConfig
from frigate.review.severity import get_review_severity
from frigate.review.types import SeverityEnum
from frigate.util.builtin import sanitize_float
from frigate.util.image import (
@@ -70,8 +69,6 @@ class TrackedObject:
self.new_zone_entered: bool = False
self.attributes: dict[str, float] = defaultdict(float)
self.false_positive = True
# highest review severity the object qualified for on any frame
self.max_severity: SeverityEnum | None = None
self.has_clip = False
self.has_snapshot = False
self.top_score = self.computed_score = 0.0
@@ -88,6 +85,36 @@ class TrackedObject:
self.path_data: list[tuple[Any, float]] = []
self.previous = self.to_dict()
@property
def max_severity(self) -> str | None:
review_config = self.camera_config.review
if (
self.camera_config.review.alerts.enabled
and self.obj_data["label"] in review_config.alerts.labels
and (
not review_config.alerts.required_zones
or set(self.entered_zones) & set(review_config.alerts.required_zones)
)
):
return SeverityEnum.alert
if (
self.camera_config.review.detections.enabled
and (
not review_config.detections.labels
or self.obj_data["label"] in review_config.detections.labels
)
and (
not review_config.detections.required_zones
or set(self.entered_zones)
& set(review_config.detections.required_zones)
)
):
return SeverityEnum.detection
return None
def _is_false_positive(self) -> bool:
# once a true positive, always a true positive
if not self.false_positive:
@@ -359,28 +386,6 @@ class TrackedObject:
self.obj_data.update(obj_data)
self.current_zones = current_zones
# use the same per-frame rules as review segments, so max_severity is
# never alert or detection unless the object could create that review
severity = get_review_severity(
self.camera_config,
label=self.obj_data["label"],
current_zones=current_zones,
false_positive=self.false_positive,
detected=detected,
position_changes=self.obj_data["position_changes"],
motionless_count=self.obj_data["motionless_count"],
pending_loitering=self.pending_loitering,
)
if severity != self.max_severity and (
severity == SeverityEnum.alert
or (severity == SeverityEnum.detection and self.max_severity is None)
):
self.max_severity = severity
# max_severity is saved with the event, so publish the change
significant_change = True
logger.debug(
f"{self.camera_config.name}: Updating {obj_data['id']}: thumb update? {thumb_update}, significant change? {significant_change}, path update? {path_update}, autotracker update? {autotracker_update} "
)
@@ -28,7 +28,7 @@ export function SummarySegment({
<div className="absolute inset-0 flex h-full cursor-pointer justify-end">
<div
className={cn(
"w-full",
"w-[10px]",
severityColors[severity],
height < 0.5 && "min-h-[0.5px]",
)}
+157 -211
View File
@@ -8,26 +8,18 @@ import React, {
import { SummarySegment } from "./SummarySegment";
import {
ConsolidatedSegmentData,
MotionData,
ReviewSegment,
ReviewSeverity,
} from "@/types/review";
import { useTimelineUtils } from "@/hooks/use-timeline-utils";
import { cn } from "@/lib/utils";
import { isMobile } from "react-device-detect";
const MIN_BOX_HEIGHT = isMobile ? 20 : 12;
const DRAG_THRESHOLD = 4;
export type SummaryTimelineProps = {
reviewTimelineRef: React.RefObject<HTMLDivElement | null>;
timelineStart: number;
timelineEnd: number;
segmentDuration: number;
events?: ReviewSegment[];
severityType?: ReviewSeverity;
motionData?: MotionData[];
motionScale?: number;
events: ReviewSegment[];
severityType: ReviewSeverity;
};
export function SummaryTimeline({
@@ -35,22 +27,16 @@ export function SummaryTimeline({
timelineStart,
timelineEnd,
segmentDuration,
events = [],
events,
severityType,
motionData,
motionScale,
}: SummaryTimelineProps) {
const summaryTimelineRef = useRef<HTMLDivElement>(null);
const visibleSectionRef = useRef<HTMLDivElement>(null);
const motionCanvasRef = useRef<HTMLCanvasElement>(null);
const [isDragging, setIsDragging] = useState(false);
const dragRef = useRef<{
startY: number;
startScrollTop: number;
insideBox: boolean;
moved: boolean;
} | null>(null);
const [scrollStartPosition, setScrollStartPosition] = useState<number>(0);
const [initialReviewTimelineScrollTop, setInitialReviewTimelineScrollTop] =
useState<number>(0);
const observer = useRef<ResizeObserver | null>(null);
@@ -143,79 +129,6 @@ export function SummaryTimeline({
segmentDuration,
]);
// Each motion sample covers motionScale seconds with a 0-100 value, and
// bins without motion are absent. Opacity is the bucket's mean value.
const drawMotion = useCallback(() => {
const canvas = motionCanvasRef.current;
const ctx = canvas?.getContext("2d");
if (!canvas || !ctx || !motionData || !motionScale) {
return;
}
const { clientWidth: width, clientHeight: height } = canvas;
const dpr = window.devicePixelRatio || 1;
canvas.width = width * dpr;
canvas.height = height * dpr;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.clearRect(0, 0, width, height);
const duration = timelineStart - timelineEnd;
if (duration <= 0 || height <= 0) {
return;
}
const rows = Math.max(
1,
Math.min(Math.floor(height), Math.ceil(duration / motionScale)),
);
const rowHeight = height / rows;
const rowSeconds = duration / rows;
const sums = new Array<number>(rows).fill(0);
motionData.forEach((item) => {
const row = Math.floor(
((timelineStart - item.start_time) / duration) * rows,
);
if (row >= 0 && row < rows) {
sums[row] += item.motion ?? 0;
}
});
const color = getComputedStyle(document.documentElement)
.getPropertyValue("--severity_significant_motion")
.trim();
const samplesPerRow = rowSeconds / motionScale;
sums.forEach((sum, row) => {
if (sum == 0) {
return;
}
const intensity = Math.min(1, sum / (100 * samplesPerRow));
ctx.fillStyle = `hsl(${color} / ${intensity})`;
ctx.fillRect(0, row * rowHeight, width, Math.ceil(rowHeight));
});
}, [motionData, motionScale, timelineStart, timelineEnd]);
useEffect(() => {
const canvas = motionCanvasRef.current;
if (!canvas) {
return;
}
drawMotion();
const resizeObserver = new ResizeObserver(drawMotion);
resizeObserver.observe(canvas);
return () => resizeObserver.disconnect();
}, [drawMotion]);
const setVisibleSectionStyles = useCallback(() => {
if (
reviewTimelineRef.current &&
@@ -231,22 +144,13 @@ export function SummaryTimeline({
} = content;
const { clientHeight: summaryTimelineVisibleHeight } = summary;
// The box keeps a minimum height so it stays visible when zoomed in,
// centered on the true visible range.
const trueHeight =
summaryTimelineVisibleHeight *
(reviewTimelineVisibleHeight / reviewTimelineFullHeight);
const trueTop =
summaryTimelineVisibleHeight * (scrolled / reviewTimelineFullHeight);
const height = Math.max(trueHeight, MIN_BOX_HEIGHT);
const top = Math.min(
Math.max(trueTop - (height - trueHeight) / 2, 0),
summaryTimelineVisibleHeight - height,
);
visibleSectionRef.current.style.top = `${top}px`;
visibleSectionRef.current.style.height = `${height}px`;
visibleSectionRef.current.style.top = `${
summaryTimelineVisibleHeight * (scrolled / reviewTimelineFullHeight)
}px`;
visibleSectionRef.current.style.height = `${
reviewTimelineVisibleHeight *
(reviewTimelineVisibleHeight / reviewTimelineFullHeight)
}px`;
}
}, [reviewTimelineRef, summaryTimelineRef, visibleSectionRef]);
@@ -280,137 +184,178 @@ export function SummaryTimeline({
segmentDuration,
]);
const scrollTopForPosition = useCallback(
(clientY: number) => {
const timeline = reviewTimelineRef.current;
const summary = summaryTimelineRef.current;
if (!timeline || !summary) {
return 0;
const timelineClick = useCallback(
(
e: React.MouseEvent<HTMLDivElement> | React.TouchEvent<HTMLDivElement>,
) => {
// prevent default only for mouse events
// to avoid chrome/android issues
if (e.nativeEvent instanceof MouseEvent) {
e.preventDefault();
}
e.stopPropagation();
const rect = summary.getBoundingClientRect();
const ratio = (clientY - rect.top) / rect.height;
return ratio * timeline.scrollHeight - timeline.clientHeight / 2;
},
[reviewTimelineRef],
);
let clientY;
if ("TouchEvent" in window && e.nativeEvent instanceof TouchEvent) {
clientY = e.nativeEvent.touches[0].clientY;
} else if (e.nativeEvent instanceof MouseEvent) {
clientY = e.nativeEvent.clientY;
}
if (
clientY &&
reviewTimelineRef.current &&
summaryTimelineRef.current &&
visibleSectionRef.current
) {
const { clientHeight: summaryTimelineVisibleHeight } =
summaryTimelineRef.current;
const scrollTimelineTo = useCallback(
(top: number, behavior: ScrollBehavior = "auto") => {
const timeline = reviewTimelineRef.current;
const rect = summaryTimelineRef.current.getBoundingClientRect();
const summaryTimelineTop = rect.top;
if (timeline) {
const maxScrollTop = timeline.scrollHeight - timeline.clientHeight;
timeline.scrollTo({
top: Math.min(Math.max(top, 0), maxScrollTop),
behavior,
const { scrollHeight: reviewTimelineHeight } =
reviewTimelineRef.current;
const { clientHeight: visibleSectionHeight } =
visibleSectionRef.current;
const visibleSectionOffset = -(visibleSectionHeight / 2);
const clickPercentage =
(clientY - summaryTimelineTop + visibleSectionOffset) /
summaryTimelineVisibleHeight;
reviewTimelineRef.current.scrollTo({
top: Math.floor(reviewTimelineHeight * clickPercentage),
behavior: "smooth",
});
}
},
[reviewTimelineRef],
[reviewTimelineRef, summaryTimelineRef, visibleSectionRef],
);
// A press outside the box jumps there on release, or snaps the box under
// the pointer once it moves and then drags it.
const handlePointerDown = useCallback(
(e: React.PointerEvent<HTMLDivElement>) => {
if (
e.button != 0 ||
!reviewTimelineRef.current ||
!visibleSectionRef.current
) {
return;
const handleMouseDown = useCallback(
(
e: React.MouseEvent<HTMLDivElement> | React.TouchEvent<HTMLDivElement>,
) => {
// prevent default only for mouse events
// to avoid chrome/android issues
if (e.nativeEvent instanceof MouseEvent) {
e.preventDefault();
}
e.stopPropagation();
setIsDragging(true);
let clientY;
if ("TouchEvent" in window && e.nativeEvent instanceof TouchEvent) {
clientY = e.nativeEvent.touches[0].clientY;
} else if (e.nativeEvent instanceof MouseEvent) {
clientY = e.nativeEvent.clientY;
}
if (clientY && summaryTimelineRef.current && reviewTimelineRef.current) {
setScrollStartPosition(clientY);
setInitialReviewTimelineScrollTop(reviewTimelineRef.current.scrollTop);
}
},
[setIsDragging, summaryTimelineRef, reviewTimelineRef],
);
const handleMouseUp = useCallback(
(e: MouseEvent | TouchEvent) => {
e.preventDefault();
e.stopPropagation();
e.currentTarget.setPointerCapture(e.pointerId);
const box = visibleSectionRef.current.getBoundingClientRect();
dragRef.current = {
startY: e.clientY,
startScrollTop: reviewTimelineRef.current.scrollTop,
insideBox: e.clientY >= box.top && e.clientY <= box.bottom,
moved: false,
};
if (isDragging) {
setIsDragging(false);
}
},
[reviewTimelineRef],
[isDragging, setIsDragging],
);
const handlePointerMove = useCallback(
(e: React.PointerEvent<HTMLDivElement>) => {
const drag = dragRef.current;
const timeline = reviewTimelineRef.current;
const summary = summaryTimelineRef.current;
if (!drag || !timeline || !summary) {
return;
}
const deltaY = e.clientY - drag.startY;
if (!drag.moved) {
if (Math.abs(deltaY) < DRAG_THRESHOLD) {
return;
const handleMouseMove = useCallback(
(e: MouseEvent | TouchEvent) => {
if (
summaryTimelineRef.current &&
reviewTimelineRef.current &&
visibleSectionRef.current
) {
// prevent default only for mouse events
// to avoid chrome/android issues
if (e instanceof MouseEvent) {
e.preventDefault();
}
e.stopPropagation();
let clientY;
if ("TouchEvent" in window && e instanceof TouchEvent) {
clientY = e.touches[0].clientY;
} else if (e instanceof MouseEvent) {
clientY = e.clientY;
}
if (isDragging && clientY) {
const { clientHeight: summaryTimelineVisibleHeight } =
summaryTimelineRef.current;
drag.moved = true;
setIsDragging(true);
const {
scrollHeight: reviewTimelineHeight,
clientHeight: reviewTimelineVisibleHeight,
} = reviewTimelineRef.current;
if (!drag.insideBox) {
drag.startScrollTop = scrollTopForPosition(drag.startY);
const { clientHeight: visibleSectionHeight } =
visibleSectionRef.current;
const deltaY =
(clientY - scrollStartPosition) *
(summaryTimelineVisibleHeight / visibleSectionHeight);
const newScrollTop = Math.min(
initialReviewTimelineScrollTop + deltaY,
reviewTimelineHeight - reviewTimelineVisibleHeight,
);
reviewTimelineRef.current.scrollTop = newScrollTop;
}
}
scrollTimelineTo(
drag.startScrollTop +
deltaY * (timeline.scrollHeight / summary.clientHeight),
);
},
[reviewTimelineRef, scrollTopForPosition, scrollTimelineTo],
[
initialReviewTimelineScrollTop,
isDragging,
reviewTimelineRef,
scrollStartPosition,
],
);
const handlePointerUp = useCallback(
(e: React.PointerEvent<HTMLDivElement>) => {
const drag = dragRef.current;
dragRef.current = null;
setIsDragging(false);
const documentRef = useRef<Document | null>(document);
useEffect(() => {
const documentInstance = documentRef.current;
if (drag && !drag.moved && !drag.insideBox) {
scrollTimelineTo(scrollTopForPosition(e.clientY), "smooth");
}
},
[scrollTopForPosition, scrollTimelineTo],
);
const handlePointerCancel = useCallback(() => {
dragRef.current = null;
setIsDragging(false);
}, []);
if (isDragging) {
documentInstance?.addEventListener("mousemove", handleMouseMove);
documentInstance?.addEventListener("touchmove", handleMouseMove);
documentInstance?.addEventListener("mouseup", handleMouseUp);
documentInstance?.addEventListener("touchend", handleMouseUp);
} else {
documentInstance?.removeEventListener("mousemove", handleMouseMove);
documentInstance?.removeEventListener("touchmove", handleMouseMove);
documentInstance?.removeEventListener("mouseup", handleMouseUp);
documentInstance?.removeEventListener("touchend", handleMouseUp);
}
return () => {
documentInstance?.removeEventListener("mousemove", handleMouseMove);
documentInstance?.removeEventListener("touchmove", handleMouseMove);
documentInstance?.removeEventListener("mouseup", handleMouseUp);
documentInstance?.removeEventListener("touchend", handleMouseUp);
};
}, [handleMouseMove, handleMouseUp, isDragging]);
return (
<div
className={cn(
"no-scrollbar relative h-full touch-none select-none overflow-hidden border-l-[1px] border-neutral-700 bg-secondary",
isDragging ? "cursor-grabbing" : "cursor-pointer",
)}
className={`no-scrollbar relative h-full select-none overflow-hidden border-l-[1px] border-neutral-700 bg-secondary`}
role="scrollbar"
onPointerDown={handlePointerDown}
onPointerMove={handlePointerMove}
onPointerUp={handlePointerUp}
onPointerCancel={handlePointerCancel}
>
{motionData && (
<canvas
ref={motionCanvasRef}
className="pointer-events-none absolute inset-0 size-full"
/>
)}
<div
ref={summaryTimelineRef}
className="relative z-10 flex h-full flex-col-reverse"
onClick={timelineClick}
onTouchEnd={timelineClick}
>
{consolidatedSegments.map((segment, index) => (
<SummarySegment
@@ -422,10 +367,11 @@ export function SummaryTimeline({
</div>
<div
ref={visibleSectionRef}
className={cn(
"absolute z-20 w-full border-y border-primary/60 bg-primary/30 shadow-[0_0_0_1px_hsl(var(--background))]",
!isDragging && "cursor-grab",
)}
onMouseDown={handleMouseDown}
onTouchStart={handleMouseDown}
className={`absolute z-20 w-full touch-none bg-primary/30 ${
isDragging ? "cursor-grabbing" : "cursor-grab"
}`}
></div>
</div>
);
+1 -2
View File
@@ -2,7 +2,6 @@ import i18n, { t } from "i18next";
import { initReactI18next } from "react-i18next";
import HttpBackend from "i18next-http-backend";
import { EventType } from "@/types/search";
import { baseUrl } from "@/api/baseUrl";
export const getTranslatedLabel = (
label: string,
@@ -34,7 +33,7 @@ i18n
fallbackLng: "en", // use en if detected lng is not available
backend: {
loadPath: `${baseUrl}locales/{{lng}}/{{ns}}.json?v=${import.meta.env.VITE_GIT_COMMIT_HASH || "unknown"}`,
loadPath: `locales/{{lng}}/{{ns}}.json?v=${import.meta.env.VITE_GIT_COMMIT_HASH || "unknown"}`,
},
ns: [
+29 -54
View File
@@ -923,7 +923,7 @@ function DetectionReview({
)}
</div>
</div>
<div className="flex w-[73px] flex-row md:w-[110px]">
<div className="flex w-[65px] flex-row md:w-[110px]">
<div className="no-scrollbar relative w-[55px] md:w-[100px]">
{loading ? (
<Skeleton className="size-full" />
@@ -951,7 +951,7 @@ function DetectionReview({
/>
)}
</div>
<div className="w-[18px] md:w-[10px]">
<div className="w-[10px]">
{loading ? (
<Skeleton className="w-full" />
) : (
@@ -1013,7 +1013,6 @@ function MotionReview({
const segmentDuration = 30;
const { data: config } = useSWR<FrigateConfig>("config");
const allowedCameras = useAllowedCameras();
const motionTimelineRef = useRef<HTMLDivElement>(null);
const reviewCameras = useMemo(() => {
if (!config) {
@@ -1701,58 +1700,34 @@ function MotionReview({
</div>
</div>
{!selectedMotionPreviewCamera && (
<div
className={cn(
"flex flex-row",
motionOnly ? "w-[55px] md:w-[100px]" : "w-[73px] md:w-[110px]",
)}
>
<div className="no-scrollbar w-[55px] overflow-y-auto md:w-[100px]">
{motionData ? (
<MotionReviewTimeline
timelineRef={motionTimelineRef}
segmentDuration={segmentDuration}
timestampSpread={15}
timelineStart={timeRangeSegments.end}
timelineEnd={timeRangeSegments.start}
motionOnly={motionOnly}
showHandlebar
handlebarTime={currentTime}
setHandlebarTime={setCurrentTime}
events={reviewItems?.all ?? []}
motion_events={motionData ?? []}
contentRef={contentRef}
onHandlebarDraggingChange={(scrubbing) => {
if (playing && scrubbing) {
setPlaying(false);
}
<div className="no-scrollbar w-[55px] overflow-y-auto md:w-[100px]">
{motionData ? (
<MotionReviewTimeline
segmentDuration={segmentDuration}
timestampSpread={15}
timelineStart={timeRangeSegments.end}
timelineEnd={timeRangeSegments.start}
motionOnly={motionOnly}
showHandlebar
handlebarTime={currentTime}
setHandlebarTime={setCurrentTime}
events={reviewItems?.all ?? []}
motion_events={motionData ?? []}
contentRef={contentRef}
onHandlebarDraggingChange={(scrubbing) => {
if (playing && scrubbing) {
setPlaying(false);
}
setScrubbing(scrubbing);
}}
dense={isMobileOnly}
isZooming={false}
zoomDirection={null}
alwaysShowMotionLine={true}
/>
) : (
<Skeleton className="size-full" />
)}
</div>
{!motionOnly && (
<div className="w-[18px] md:w-[10px]">
{motionData ? (
<SummaryTimeline
reviewTimelineRef={motionTimelineRef}
timelineStart={timeRangeSegments.end}
timelineEnd={timeRangeSegments.start}
segmentDuration={segmentDuration}
motionData={motionData}
motionScale={segmentDuration / 2}
/>
) : (
<Skeleton className="w-full" />
)}
</div>
setScrubbing(scrubbing);
}}
dense={isMobileOnly}
isZooming={false}
zoomDirection={null}
alwaysShowMotionLine={true}
/>
) : (
<Skeleton className="size-full" />
)}
</div>
)}
+28 -48
View File
@@ -70,7 +70,6 @@ import { cn } from "@/lib/utils";
import { useFullscreen } from "@/hooks/use-fullscreen";
import { useTimezone } from "@/hooks/use-date-utils";
import { useTimelineZoom } from "@/hooks/use-timeline-zoom";
import SummaryTimeline from "@/components/timeline/SummaryTimeline";
import { useTranslation } from "react-i18next";
import { useTimelineUtils } from "@/hooks/use-timeline-utils";
import {
@@ -1318,14 +1317,14 @@ function Timeline({
isDesktop
? cn(
timelineType == "timeline"
? "w-[110px] flex-shrink-0"
? "w-[100px] flex-shrink-0"
: timelineType == "detail"
? "min-w-[20rem] max-w-[30%] flex-shrink-0 flex-grow-0 basis-[30rem] md:min-w-[20rem] md:max-w-[25%] lg:min-w-[30rem] lg:max-w-[33%]"
: "w-80 flex-shrink-0",
)
: cn(
timelineType == "timeline"
? "portrait:flex-grow landscape:w-[128px] landscape:flex-shrink-0"
? "portrait:flex-grow landscape:w-[100px] landscape:flex-shrink-0"
: timelineType == "detail"
? "portrait:flex-grow landscape:w-[19rem] landscape:flex-shrink-0"
: "portrait:flex-grow landscape:w-[19rem] landscape:flex-shrink-0",
@@ -1346,51 +1345,32 @@ function Timeline({
)}
{timelineType == "timeline" ? (
!isLoading ? (
<div className="flex size-full flex-row">
<div className="relative h-full min-w-0 flex-1">
<MotionReviewTimeline
timelineRef={selectedTimelineRef}
segmentDuration={zoomSettings.segmentDuration}
timestampSpread={zoomSettings.timestampSpread}
timelineStart={timeRange.before}
timelineEnd={timeRange.after}
showHandlebar={exportRange == undefined}
showExportHandles={exportRange != undefined}
exportStartTime={exportRange?.after}
exportEndTime={exportRange?.before}
setExportStartTime={setExportStartTime}
setExportEndTime={setExportEndTime}
handlebarTime={currentTime}
setHandlebarTime={setCurrentTime}
events={mainCameraReviewItems}
motion_events={motionData ?? []}
noRecordingRanges={noRecordings ?? []}
subOnlyRanges={subOnlyRanges}
contentRef={contentRef}
onHandlebarDraggingChange={setScrubbing}
isZooming={isZooming}
zoomDirection={zoomDirection}
onZoomChange={handleZoomChange}
possibleZoomLevels={possibleZoomLevels}
currentZoomLevel={currentZoomLevel}
/>
</div>
<div
className={cn(
"flex-shrink-0",
isDesktop ? "w-[10px]" : "w-[28px]",
)}
>
<SummaryTimeline
reviewTimelineRef={selectedTimelineRef}
timelineStart={timeRange.before}
timelineEnd={timeRange.after}
segmentDuration={zoomSettings.segmentDuration}
motionData={motionData}
motionScale={Math.round(zoomSettings.segmentDuration / 2)}
/>
</div>
</div>
<MotionReviewTimeline
timelineRef={selectedTimelineRef}
segmentDuration={zoomSettings.segmentDuration}
timestampSpread={zoomSettings.timestampSpread}
timelineStart={timeRange.before}
timelineEnd={timeRange.after}
showHandlebar={exportRange == undefined}
showExportHandles={exportRange != undefined}
exportStartTime={exportRange?.after}
exportEndTime={exportRange?.before}
setExportStartTime={setExportStartTime}
setExportEndTime={setExportEndTime}
handlebarTime={currentTime}
setHandlebarTime={setCurrentTime}
events={mainCameraReviewItems}
motion_events={motionData ?? []}
noRecordingRanges={noRecordings ?? []}
subOnlyRanges={subOnlyRanges}
contentRef={contentRef}
onHandlebarDraggingChange={setScrubbing}
isZooming={isZooming}
zoomDirection={zoomDirection}
onZoomChange={handleZoomChange}
possibleZoomLevels={possibleZoomLevels}
currentZoomLevel={currentZoomLevel}
/>
) : (
<Skeleton className="size-full" />
)