From b48f6d750fc577af24562c6c47c73b545a4456bc Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Fri, 28 Apr 2023 05:02:06 -0600 Subject: [PATCH 1/6] Add annotation support (#6288) --- frigate/config.py | 3 +++ web/src/components/TimelineSummary.jsx | 12 ++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/frigate/config.py b/frigate/config.py index f3e760b6a..85685e8e9 100644 --- a/frigate/config.py +++ b/frigate/config.py @@ -269,6 +269,9 @@ class DetectConfig(FrigateBaseModel): default_factory=StationaryConfig, title="Stationary objects config.", ) + annotation_offset: int = Field( + default=0, title="Milliseconds to offset detect annotations by." + ) class FilterConfig(FrigateBaseModel): diff --git a/web/src/components/TimelineSummary.jsx b/web/src/components/TimelineSummary.jsx index 8e5af9654..7a16bb1aa 100644 --- a/web/src/components/TimelineSummary.jsx +++ b/web/src/components/TimelineSummary.jsx @@ -5,7 +5,7 @@ import { formatUnixTimestampToDateTime } from '../utils/dateUtil'; import PlayIcon from '../icons/Play'; import ExitIcon from '../icons/Exit'; import { Zone } from '../icons/Zone'; -import { useState } from 'preact/hooks'; +import { useMemo, useState } from 'preact/hooks'; import Button from './Button'; export default function TimelineSummary({ event, onFrameSelected }) { @@ -18,6 +18,14 @@ export default function TimelineSummary({ event, onFrameSelected }) { const { data: config } = useSWR('config'); + const annotationOffset = useMemo(() => { + if (!config) { + return 0; + } + + return (config.cameras[event.camera]?.detect?.annotation_offset || 0) / 1000; + }, [config, event]); + const [timeIndex, setTimeIndex] = useState(-1); const recordingParams = { @@ -53,7 +61,7 @@ export default function TimelineSummary({ event, onFrameSelected }) { const onSelectMoment = async (index) => { setTimeIndex(index); - onFrameSelected(eventTimeline[index], getSeekSeconds(eventTimeline[index].timestamp)); + onFrameSelected(eventTimeline[index], getSeekSeconds(eventTimeline[index].timestamp + annotationOffset)); }; if (!eventTimeline || !config) { From 37360edbc165ff031bf4b21b922bc0e44d703264 Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Fri, 28 Apr 2023 06:47:49 -0600 Subject: [PATCH 2/6] Show other system processes in system page (#6276) --- frigate/util.py | 9 ++++++++- web/src/routes/System.jsx | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/frigate/util.py b/frigate/util.py index 12139bf99..b01234c1a 100755 --- a/frigate/util.py +++ b/frigate/util.py @@ -824,7 +824,14 @@ def get_cpu_stats() -> dict[str, dict]: else: mem_pct = stats[9] - usages[stats[0]] = { + idx = stats[0] + + if stats[-1] == "go2rtc": + idx = "go2rtc" + elif stats[-1] == "frigate.r+": + idx = "recording" + + usages[idx] = { "cpu": stats[8], "mem": mem_pct, } diff --git a/web/src/routes/System.jsx b/web/src/routes/System.jsx index 8a5f2c28f..0320e237b 100644 --- a/web/src/routes/System.jsx +++ b/web/src/routes/System.jsx @@ -345,6 +345,33 @@ export default function System() { )} + Other Processes +
+ {['go2rtc', 'recording'].map((process) => ( +
+
+
{process}
+
+
+ + + + + + + + + + + + + +
CPU %Memory %
{cpu_usages[process]?.['cpu'] || '- '}%{cpu_usages[process]?.['mem'] || '- '}%
+
+
+ ))} +
+

System stats update automatically every {config.mqtt.stats_interval} seconds.

)} From ca7790ff65c943f4f57585f5d9a03b699535af3b Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Sun, 30 Apr 2023 08:28:21 -0600 Subject: [PATCH 3/6] Cleanup timeline entries when relevant recording segments are removed (#6319) * Cleanup timeline entries when relevant recording segments are removed * Make timeline cleanup simpler * Formatting --- frigate/record/cleanup.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/frigate/record/cleanup.py b/frigate/record/cleanup.py index 74c7eadf2..d649a2d87 100644 --- a/frigate/record/cleanup.py +++ b/frigate/record/cleanup.py @@ -12,7 +12,7 @@ from multiprocessing.synchronize import Event as MpEvent from frigate.config import RetainModeEnum, FrigateConfig from frigate.const import RECORD_DIR, SECONDS_IN_DAY -from frigate.models import Event, Recordings +from frigate.models import Event, Recordings, Timeline from frigate.record.util import remove_empty_directories logger = logging.getLogger(__name__) @@ -140,6 +140,15 @@ class RecordingCleanup(threading.Thread): Path(recording.path).unlink(missing_ok=True) deleted_recordings.add(recording.id) + # delete timeline entries relevant to this recording segment + Timeline.delete( + Timeline.timestamp.between( + recording.start_time, recording.end_time + ), + Timeline.timestamp < expire_date, + Timeline.camera == camera, + ).execute() + logger.debug(f"Expiring {len(deleted_recordings)} recordings") # delete up to 100,000 at a time max_deletes = 100000 From ad52e238cebd9d8179cbe4cb4c5abc4c75fff572 Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Sun, 30 Apr 2023 11:07:14 -0600 Subject: [PATCH 4/6] Refactor events to be more generic (#6320) * Organize event table to be more generalized * Add appropriate fields to data * Move tracked object logic to own function * Add source type to event queue * rename enum * Fix types that are used in webUI * remove redundant * Formatting * fix typing * Rename enum --- frigate/events.py | 199 +++++++++++++---------- frigate/http.py | 20 ++- frigate/models.py | 21 ++- frigate/object_processing.py | 21 ++- frigate/timeline.py | 11 +- migrations/015_event_refactor.py | 49 ++++++ web/src/components/RecordingPlaylist.jsx | 4 +- web/src/routes/Events.jsx | 16 +- 8 files changed, 221 insertions(+), 120 deletions(-) create mode 100644 migrations/015_event_refactor.py diff --git a/frigate/events.py b/frigate/events.py index 558cb2139..965651edb 100644 --- a/frigate/events.py +++ b/frigate/events.py @@ -3,6 +3,8 @@ import logging import os import queue import threading + +from enum import Enum from pathlib import Path from peewee import fn @@ -10,7 +12,6 @@ from peewee import fn from frigate.config import EventsConfig, FrigateConfig from frigate.const import CLIPS_DIR from frigate.models import Event -from frigate.timeline import TimelineSourceEnum from frigate.types import CameraMetricsTypes from frigate.util import to_relative_box @@ -21,6 +22,12 @@ from typing import Dict logger = logging.getLogger(__name__) +class EventTypeEnum(str, Enum): + # api = "api" + # audio = "audio" + tracked_object = "tracked_object" + + def should_update_db(prev_event: Event, current_event: Event) -> bool: """If current_event has updated fields and (clip or snapshot).""" if current_event["has_clip"] or current_event["has_snapshot"]: @@ -66,7 +73,9 @@ class EventProcessor(threading.Thread): while not self.stop_event.is_set(): try: - event_type, camera, event_data = self.event_queue.get(timeout=1) + source_type, event_type, camera, event_data = self.event_queue.get( + timeout=1 + ) except queue.Empty: continue @@ -75,100 +84,19 @@ class EventProcessor(threading.Thread): self.timeline_queue.put( ( camera, - TimelineSourceEnum.tracked_object, + source_type, event_type, self.events_in_process.get(event_data["id"]), event_data, ) ) - # if this is the first message, just store it and continue, its not time to insert it in the db - if event_type == "start": - self.events_in_process[event_data["id"]] = event_data - continue + if source_type == EventTypeEnum.tracked_object: + if event_type == "start": + self.events_in_process[event_data["id"]] = event_data + continue - if should_update_db(self.events_in_process[event_data["id"]], event_data): - camera_config = self.config.cameras[camera] - event_config: EventsConfig = camera_config.record.events - width = camera_config.detect.width - height = camera_config.detect.height - first_detector = list(self.config.detectors.values())[0] - - start_time = event_data["start_time"] - event_config.pre_capture - end_time = ( - None - if event_data["end_time"] is None - else event_data["end_time"] + event_config.post_capture - ) - # score of the snapshot - score = ( - None - if event_data["snapshot"] is None - else event_data["snapshot"]["score"] - ) - # detection region in the snapshot - region = ( - None - if event_data["snapshot"] is None - else to_relative_box( - width, - height, - event_data["snapshot"]["region"], - ) - ) - # bounding box for the snapshot - box = ( - None - if event_data["snapshot"] is None - else to_relative_box( - width, - height, - event_data["snapshot"]["box"], - ) - ) - - # keep these from being set back to false because the event - # may have started while recordings and snapshots were enabled - # this would be an issue for long running events - if self.events_in_process[event_data["id"]]["has_clip"]: - event_data["has_clip"] = True - if self.events_in_process[event_data["id"]]["has_snapshot"]: - event_data["has_snapshot"] = True - - event = { - Event.id: event_data["id"], - Event.label: event_data["label"], - Event.camera: camera, - Event.start_time: start_time, - Event.end_time: end_time, - Event.top_score: event_data["top_score"], - Event.score: score, - Event.zones: list(event_data["entered_zones"]), - Event.thumbnail: event_data["thumbnail"], - Event.region: region, - Event.box: box, - Event.has_clip: event_data["has_clip"], - Event.has_snapshot: event_data["has_snapshot"], - Event.model_hash: first_detector.model.model_hash, - Event.model_type: first_detector.model.model_type, - Event.detector_type: first_detector.type, - } - - ( - Event.insert(event) - .on_conflict( - conflict_target=[Event.id], - update=event, - ) - .execute() - ) - - # update the stored copy for comparison on future update messages - self.events_in_process[event_data["id"]] = event_data - - if event_type == "end": - del self.events_in_process[event_data["id"]] - self.event_processed_queue.put((event_data["id"], camera)) + self.handle_object_detection(event_type, camera, event_data) # set an end_time on events without an end_time before exiting Event.update(end_time=datetime.datetime.now().timestamp()).where( @@ -176,6 +104,99 @@ class EventProcessor(threading.Thread): ).execute() logger.info(f"Exiting event processor...") + def handle_object_detection( + self, + event_type: str, + camera: str, + event_data: Event, + ) -> None: + """handle tracked object event updates.""" + # if this is the first message, just store it and continue, its not time to insert it in the db + if should_update_db(self.events_in_process[event_data["id"]], event_data): + camera_config = self.config.cameras[camera] + event_config: EventsConfig = camera_config.record.events + width = camera_config.detect.width + height = camera_config.detect.height + first_detector = list(self.config.detectors.values())[0] + + start_time = event_data["start_time"] - event_config.pre_capture + end_time = ( + None + if event_data["end_time"] is None + else event_data["end_time"] + event_config.post_capture + ) + # score of the snapshot + score = ( + None + if event_data["snapshot"] is None + else event_data["snapshot"]["score"] + ) + # detection region in the snapshot + region = ( + None + if event_data["snapshot"] is None + else to_relative_box( + width, + height, + event_data["snapshot"]["region"], + ) + ) + # bounding box for the snapshot + box = ( + None + if event_data["snapshot"] is None + else to_relative_box( + width, + height, + event_data["snapshot"]["box"], + ) + ) + + # keep these from being set back to false because the event + # may have started while recordings and snapshots were enabled + # this would be an issue for long running events + if self.events_in_process[event_data["id"]]["has_clip"]: + event_data["has_clip"] = True + if self.events_in_process[event_data["id"]]["has_snapshot"]: + event_data["has_snapshot"] = True + + event = { + Event.id: event_data["id"], + Event.label: event_data["label"], + Event.camera: camera, + Event.start_time: start_time, + Event.end_time: end_time, + Event.zones: list(event_data["entered_zones"]), + Event.thumbnail: event_data["thumbnail"], + Event.has_clip: event_data["has_clip"], + Event.has_snapshot: event_data["has_snapshot"], + Event.model_hash: first_detector.model.model_hash, + Event.model_type: first_detector.model.model_type, + Event.detector_type: first_detector.type, + Event.data: { + "box": box, + "region": region, + "score": score, + "top_score": event_data["top_score"], + }, + } + + ( + Event.insert(event) + .on_conflict( + conflict_target=[Event.id], + update=event, + ) + .execute() + ) + + # update the stored copy for comparison on future update messages + self.events_in_process[event_data["id"]] = event_data + + if event_type == "end": + del self.events_in_process[event_data["id"]] + self.event_processed_queue.put((event_data["id"], camera)) + class EventCleanup(threading.Thread): def __init__(self, config: FrigateConfig, stop_event: MpEvent): diff --git a/frigate/http.py b/frigate/http.py index cd2a0c523..198b2be16 100644 --- a/frigate/http.py +++ b/frigate/http.py @@ -44,7 +44,6 @@ from frigate.util import ( restart_frigate, vainfo_hwaccel, get_tz_modifiers, - to_relative_box, ) from frigate.storage import StorageMaintainer from frigate.version import VERSION @@ -196,7 +195,7 @@ def send_to_plus(id): return make_response(jsonify({"success": False, "message": message}), 404) # events from before the conversion to relative dimensions cant include annotations - if any(d > 1 for d in event.box): + if any(d > 1 for d in event.data["box"]): include_annotation = None if event.end_time is None: @@ -252,8 +251,8 @@ def send_to_plus(id): event.save() if not include_annotation is None: - region = event.region - box = event.box + region = event.data["region"] + box = event.data["box"] try: current_app.plus_api.add_annotation( @@ -294,7 +293,7 @@ def false_positive(id): return make_response(jsonify({"success": False, "message": message}), 404) # events from before the conversion to relative dimensions cant include annotations - if any(d > 1 for d in event.box): + if any(d > 1 for d in event.data["box"]): message = f"Events prior to 0.13 cannot be submitted as false positives" logger.error(message) return make_response(jsonify({"success": False, "message": message}), 400) @@ -311,11 +310,15 @@ def false_positive(id): # need to refetch the event now that it has a plus_id event = Event.get(Event.id == id) - region = event.region - box = event.box + region = event.data["region"] + box = event.data["box"] # provide top score if score is unavailable - score = event.top_score if event.score is None else event.score + score = ( + (event.data["top_score"] if event.data["top_score"] else event.top_score) + if event.data["score"] is None + else event.data["score"] + ) try: current_app.plus_api.add_false_positive( @@ -756,6 +759,7 @@ def events(): Event.top_score, Event.false_positive, Event.box, + Event.data, ] if camera != "all": diff --git a/frigate/models.py b/frigate/models.py index cc1a29f31..3770121cc 100644 --- a/frigate/models.py +++ b/frigate/models.py @@ -18,22 +18,33 @@ class Event(Model): # type: ignore[misc] camera = CharField(index=True, max_length=20) start_time = DateTimeField() end_time = DateTimeField() - top_score = FloatField() - score = FloatField() + top_score = ( + FloatField() + ) # TODO remove when columns can be dropped without rebuilding table + score = ( + FloatField() + ) # TODO remove when columns can be dropped without rebuilding table false_positive = BooleanField() zones = JSONField() thumbnail = TextField() has_clip = BooleanField(default=True) has_snapshot = BooleanField(default=True) - region = JSONField() - box = JSONField() - area = IntegerField() + region = ( + JSONField() + ) # TODO remove when columns can be dropped without rebuilding table + box = ( + JSONField() + ) # TODO remove when columns can be dropped without rebuilding table + area = ( + IntegerField() + ) # TODO remove when columns can be dropped without rebuilding table retain_indefinitely = BooleanField(default=False) ratio = FloatField(default=1.0) plus_id = CharField(max_length=30) model_hash = CharField(max_length=32) detector_type = CharField(max_length=32) model_type = CharField(max_length=32) + data = JSONField() # ex: tracked object box, region, etc. class Timeline(Model): # type: ignore[misc] diff --git a/frigate/object_processing.py b/frigate/object_processing.py index db2745309..cc1667ffb 100644 --- a/frigate/object_processing.py +++ b/frigate/object_processing.py @@ -21,6 +21,7 @@ from frigate.config import ( FrigateConfig, ) from frigate.const import CLIPS_DIR +from frigate.events import EventTypeEnum from frigate.util import ( SharedMemoryFrameManager, calculate_region, @@ -656,7 +657,9 @@ class TrackedObjectProcessor(threading.Thread): self.last_motion_detected: dict[str, float] = {} def start(camera, obj: TrackedObject, current_frame_time): - self.event_queue.put(("start", camera, obj.to_dict())) + self.event_queue.put( + (EventTypeEnum.tracked_object, "start", camera, obj.to_dict()) + ) def update(camera, obj: TrackedObject, current_frame_time): obj.has_snapshot = self.should_save_snapshot(camera, obj) @@ -670,7 +673,12 @@ class TrackedObjectProcessor(threading.Thread): self.dispatcher.publish("events", json.dumps(message), retain=False) obj.previous = after self.event_queue.put( - ("update", camera, obj.to_dict(include_thumbnail=True)) + ( + EventTypeEnum.tracked_object, + "update", + camera, + obj.to_dict(include_thumbnail=True), + ) ) def end(camera, obj: TrackedObject, current_frame_time): @@ -722,7 +730,14 @@ class TrackedObjectProcessor(threading.Thread): } self.dispatcher.publish("events", json.dumps(message), retain=False) - self.event_queue.put(("end", camera, obj.to_dict(include_thumbnail=True))) + self.event_queue.put( + ( + EventTypeEnum.tracked_object, + "end", + camera, + obj.to_dict(include_thumbnail=True), + ) + ) def snapshot(camera, obj: TrackedObject, current_frame_time): mqtt_config: MqttConfig = self.config.cameras[camera].mqtt diff --git a/frigate/timeline.py b/frigate/timeline.py index c351e3e68..6b969bc0e 100644 --- a/frigate/timeline.py +++ b/frigate/timeline.py @@ -4,9 +4,8 @@ import logging import threading import queue -from enum import Enum - from frigate.config import FrigateConfig +from frigate.events import EventTypeEnum from frigate.models import Timeline from multiprocessing.queues import Queue @@ -17,12 +16,6 @@ from frigate.util import to_relative_box logger = logging.getLogger(__name__) -class TimelineSourceEnum(str, Enum): - # api = "api" - # audio = "audio" - tracked_object = "tracked_object" - - class TimelineProcessor(threading.Thread): """Handle timeline queue and update DB.""" @@ -51,7 +44,7 @@ class TimelineProcessor(threading.Thread): except queue.Empty: continue - if input_type == TimelineSourceEnum.tracked_object: + if input_type == EventTypeEnum.tracked_object: self.handle_object_detection( camera, event_type, prev_event_data, event_data ) diff --git a/migrations/015_event_refactor.py b/migrations/015_event_refactor.py new file mode 100644 index 000000000..d8a8a387c --- /dev/null +++ b/migrations/015_event_refactor.py @@ -0,0 +1,49 @@ +"""Peewee migrations + +Some examples (model - class or model name):: + + > Model = migrator.orm['model_name'] # Return model in current state by name + + > migrator.sql(sql) # Run custom SQL + > migrator.python(func, *args, **kwargs) # Run python code + > migrator.create_model(Model) # Create a model (could be used as decorator) + > migrator.remove_model(model, cascade=True) # Remove a model + > migrator.add_fields(model, **fields) # Add fields to a model + > migrator.change_fields(model, **fields) # Change fields + > migrator.remove_fields(model, *field_names, cascade=True) + > migrator.rename_field(model, old_field_name, new_field_name) + > migrator.rename_table(model, new_table_name) + > migrator.add_index(model, *col_names, unique=False) + > migrator.drop_index(model, *col_names) + > migrator.add_not_null(model, *field_names) + > migrator.drop_not_null(model, *field_names) + > migrator.add_default(model, field_name, default) + +""" + +import datetime as dt +import peewee as pw +from playhouse.sqlite_ext import * +from decimal import ROUND_HALF_EVEN +from frigate.models import Event + +try: + import playhouse.postgres_ext as pw_pext +except ImportError: + pass + +SQL = pw.SQL + + +def migrate(migrator, database, fake=False, **kwargs): + migrator.drop_not_null( + Event, "top_score", "score", "region", "box", "area", "ratio" + ) + migrator.add_fields( + Event, + data=JSONField(default={}), + ) + + +def rollback(migrator, database, fake=False, **kwargs): + pass diff --git a/web/src/components/RecordingPlaylist.jsx b/web/src/components/RecordingPlaylist.jsx index 4d6f93842..4f6996afc 100644 --- a/web/src/components/RecordingPlaylist.jsx +++ b/web/src/components/RecordingPlaylist.jsx @@ -163,7 +163,9 @@ export function EventCard({ camera, event }) {
Start: {format(start, 'HH:mm:ss')}
Duration: {duration}
-
{(event.top_score * 100).toFixed(1)}%
+
+ {((event?.data?.top_score || event.top_score) * 100).toFixed(1)}% +
diff --git a/web/src/routes/Events.jsx b/web/src/routes/Events.jsx index af0af5b67..30e3507f6 100644 --- a/web/src/routes/Events.jsx +++ b/web/src/routes/Events.jsx @@ -206,7 +206,7 @@ export default function Events({ path, ...props }) { e.stopPropagation(); setDownloadEvent((_prev) => ({ id: event.id, - box: event.box, + box: event?.data?.box || event.box, label: event.label, has_clip: event.has_clip, has_snapshot: event.has_snapshot, @@ -599,7 +599,7 @@ export default function Events({ path, ...props }) { {event.sub_label ? `${event.label.replaceAll('_', ' ')}: ${event.sub_label.replaceAll('_', ' ')}` : event.label.replaceAll('_', ' ')} - ({(event.top_score * 100).toFixed(0)}%) + ({((event?.data?.top_score || event.top_score) * 100).toFixed(0)}%)
@@ -638,7 +638,9 @@ export default function Events({ path, ...props }) { @@ -680,7 +682,9 @@ export default function Events({ path, ...props }) {
onEventFrameSelected(event, frame, seekSeconds)} + onFrameSelected={(frame, seekSeconds) => + onEventFrameSelected(event, frame, seekSeconds) + } />
) : null} From 9bf98f908d5c5b1c9b4a91075a357d095fdae8ce Mon Sep 17 00:00:00 2001 From: Blake Blackshear Date: Sun, 30 Apr 2023 13:32:36 -0500 Subject: [PATCH 5/6] add plus integration for models (#6328) --- docs/docs/integrations/plus.md | 30 +++++++-- frigate/app.py | 15 +++-- frigate/config.py | 8 ++- frigate/const.py | 1 + frigate/detectors/detector_config.py | 44 +++++++++++++ frigate/http.py | 5 ++ frigate/plus.py | 23 ++++++- frigate/test/test_config.py | 97 ++++++++++++++-------------- frigate/test/test_http.py | 6 +- 9 files changed, 166 insertions(+), 63 deletions(-) diff --git a/docs/docs/integrations/plus.md b/docs/docs/integrations/plus.md index 4bab29ef3..7ffce4941 100644 --- a/docs/docs/integrations/plus.md +++ b/docs/docs/integrations/plus.md @@ -5,13 +5,11 @@ title: Frigate+ :::info -Frigate+ is under active development and currently only offers the ability to submit your examples with annotations. Models will be available after enough examples are submitted to train a robust model. It is free to create an account and upload your examples. +Frigate+ is under active development. Models are available as a part of an invitation only beta. It is free to create an account and upload/annotate your examples. ::: -Frigate+ offers models trained from scratch and specifically designed for the way Frigate NVR analyzes video footage. They offer higher accuracy with less resources. By uploading your own labeled examples, your model can be uniquely tuned for accuracy in your specific conditions. After tuning, performance is evaluated against a broad dataset and real world examples submitted by other Frigate+ users to prevent overfitting. - -Custom models also include a more relevant set of objects for security cameras such as person, face, car, license plate, delivery truck, package, dog, cat, deer, and more. Interested in detecting an object unique to you? Upload examples to incorporate your own objects without worrying that you are reducing the accuracy of other object types in the model. +Frigate+ offers models trained from scratch and specifically designed for the way Frigate NVR analyzes video footage. They offer higher accuracy with less resources and include a more relevant set of objects for security cameras. By uploading your own labeled examples, your model can be uniquely tuned for accuracy in your specific conditions. After tuning, performance is evaluated against a broad dataset and real world examples submitted by other Frigate+ users to prevent overfitting. ## Setup @@ -35,7 +33,7 @@ You cannot use the `environment_vars` section of your configuration file to set ::: -### Submit examples +## Submit examples Once your API key is configured, you can submit examples directly from the events page in Frigate using the `SEND TO FRIGATE+` button. @@ -52,3 +50,25 @@ Snapshots must be enabled to be able to submit examples to Frigate+ You can view all of your submitted images at [https://plus.frigate.video](https://plus.frigate.video). Annotations can be added by clicking an image. ![Annotate](/img/annotate.png) + +## Use Models + +Models available in Frigate+ can be used with a special model path. No other information needs to be configured for Frigate+ models because it fetches the remaining config from Frigate+ automatically. + +```yaml +model: + path: plus://e63b7345cc83a84ed79dedfc99c16616 +``` + +Models are downloaded into the `/config/model_cache` folder and only downloaded if needed. + +You can override the labelmap for Frigate+ models like this: + +```yaml +model: + path: plus://e63b7345cc83a84ed79dedfc99c16616 + labelmap: + 3: animal + 4: animal + 5: animal +``` diff --git a/frigate/app.py b/frigate/app.py index 095a51e36..a07a4465f 100644 --- a/frigate/app.py +++ b/frigate/app.py @@ -18,7 +18,14 @@ from frigate.comms.dispatcher import Communicator, Dispatcher from frigate.comms.mqtt import MqttClient from frigate.comms.ws import WebSocketClient from frigate.config import FrigateConfig -from frigate.const import CACHE_DIR, CLIPS_DIR, CONFIG_DIR, DEFAULT_DB_PATH, RECORD_DIR +from frigate.const import ( + CACHE_DIR, + CLIPS_DIR, + CONFIG_DIR, + DEFAULT_DB_PATH, + MODEL_CACHE_DIR, + RECORD_DIR, +) from frigate.object_detection import ObjectDetectProcess from frigate.events import EventCleanup, EventProcessor from frigate.http import create_app @@ -57,7 +64,7 @@ class FrigateApp: os.environ[key] = value def ensure_dirs(self) -> None: - for d in [CONFIG_DIR, RECORD_DIR, CLIPS_DIR, CACHE_DIR]: + for d in [CONFIG_DIR, RECORD_DIR, CLIPS_DIR, CACHE_DIR, MODEL_CACHE_DIR]: if not os.path.exists(d) and not os.path.islink(d): logger.info(f"Creating directory: {d}") os.makedirs(d) @@ -81,7 +88,7 @@ class FrigateApp: config_file = config_file_yaml user_config = FrigateConfig.parse_file(config_file) - self.config = user_config.runtime_config + self.config = user_config.runtime_config(self.plus_api) for camera_name in self.config.cameras.keys(): # create camera_metrics @@ -379,6 +386,7 @@ class FrigateApp: self.init_logger() logger.info(f"Starting Frigate ({VERSION})") try: + self.ensure_dirs() try: self.init_config() except Exception as e: @@ -399,7 +407,6 @@ class FrigateApp: self.log_process.terminate() sys.exit(1) self.set_environment_vars() - self.ensure_dirs() self.set_log_levels() self.init_queues() self.init_database() diff --git a/frigate/config.py b/frigate/config.py index 85685e8e9..dc66e7896 100644 --- a/frigate/config.py +++ b/frigate/config.py @@ -19,6 +19,7 @@ from frigate.const import ( YAML_EXT, ) from frigate.detectors.detector_config import BaseDetectorConfig +from frigate.plus import PlusApi from frigate.util import ( create_mask, deep_merge, @@ -906,8 +907,7 @@ class FrigateConfig(FrigateBaseModel): title="Global timestamp style configuration.", ) - @property - def runtime_config(self) -> FrigateConfig: + def runtime_config(self, plus_api: PlusApi = None) -> FrigateConfig: """Merge camera config with globals.""" config = self.copy(deep=True) @@ -1031,6 +1031,7 @@ class FrigateConfig(FrigateBaseModel): enabled_labels.update(camera.objects.track) config.model.create_colormap(sorted(enabled_labels)) + config.model.check_and_load_plus_model(plus_api) for key, detector in config.detectors.items(): detector_config: DetectorConfig = parse_obj_as(DetectorConfig, detector) @@ -1063,6 +1064,9 @@ class FrigateConfig(FrigateBaseModel): merged_model["path"] = "/edgetpu_model.tflite" detector_config.model = ModelConfig.parse_obj(merged_model) + detector_config.model.check_and_load_plus_model( + plus_api, detector_config.type + ) detector_config.model.compute_model_hash() config.detectors[key] = detector_config diff --git a/frigate/const.py b/frigate/const.py index 8e1e42bb9..cfcdc2a53 100644 --- a/frigate/const.py +++ b/frigate/const.py @@ -1,5 +1,6 @@ CONFIG_DIR = "/config" DEFAULT_DB_PATH = f"{CONFIG_DIR}/frigate.db" +MODEL_CACHE_DIR = f"{CONFIG_DIR}/model_cache" BASE_DIR = "/media/frigate" CLIPS_DIR = f"{BASE_DIR}/clips" RECORD_DIR = f"{BASE_DIR}/recordings" diff --git a/frigate/detectors/detector_config.py b/frigate/detectors/detector_config.py index 46f92e6ab..16ffe5fd4 100644 --- a/frigate/detectors/detector_config.py +++ b/frigate/detectors/detector_config.py @@ -1,11 +1,16 @@ import hashlib +import json import logging from enum import Enum +import os from typing import Dict, List, Optional, Tuple, Union, Literal + +import requests import matplotlib.pyplot as plt from pydantic import BaseModel, Extra, Field, validator from pydantic.fields import PrivateAttr +from frigate.plus import PlusApi from frigate.util import load_labels @@ -73,6 +78,45 @@ class ModelConfig(BaseModel): } self._colormap = {} + def check_and_load_plus_model( + self, plus_api: PlusApi, detector: str = None + ) -> None: + if not self.path or not self.path.startswith("plus://"): + return + + model_id = self.path[7:] + self.path = f"/config/model_cache/{model_id}" + model_info_path = f"{self.path}.json" + + # download the model if it doesn't exist + if not os.path.isfile(self.path): + download_url = plus_api.get_model_download_url(model_id) + r = requests.get(download_url) + with open(self.path, "wb") as f: + f.write(r.content) + + # download the model info if it doesn't exist + if not os.path.isfile(model_info_path): + model_info = plus_api.get_model_info(model_id) + with open(model_info_path, "w") as f: + json.dump(model_info, f) + else: + with open(model_info_path, "r") as f: + model_info = json.load(f) + + if detector and detector not in model_info["supportedDetectors"]: + raise ValueError(f"Model does not support detector type of {detector}") + + self.width = model_info["width"] + self.height = model_info["height"] + self.input_tensor = model_info["inputShape"] + self.input_pixel_format = model_info["pixelFormat"] + self.model_type = model_info["type"] + self._merged_labelmap = { + **{int(key): val for key, val in model_info["labelMap"].items()}, + **self.labelmap, + } + def compute_model_hash(self) -> None: with open(self.path, "rb") as f: file_hash = hashlib.md5() diff --git a/frigate/http.py b/frigate/http.py index 198b2be16..706487e8f 100644 --- a/frigate/http.py +++ b/frigate/http.py @@ -866,6 +866,11 @@ def config(): config["plus"] = {"enabled": current_app.plus_api.is_active()} + for detector, detector_config in config["detectors"].items(): + detector_config["model"]["labelmap"] = current_app.frigate_config.detectors[ + detector + ].model.merged_labelmap + return jsonify(config) diff --git a/frigate/plus.py b/frigate/plus.py index 4b59b961e..7ba67a62d 100644 --- a/frigate/plus.py +++ b/frigate/plus.py @@ -3,7 +3,7 @@ import json import logging import os import re -from typing import List +from typing import Any, Dict, List import requests from frigate.const import PLUS_ENV_VAR, PLUS_API_HOST from requests.models import Response @@ -187,3 +187,24 @@ class PlusApi: if not r.ok: raise Exception(r.text) + + def get_model_download_url( + self, + model_id: str, + ) -> str: + r = self._get(f"model/{model_id}/signed_url") + + if not r.ok: + raise Exception(r.text) + + presigned_url = r.json() + + return str(presigned_url.get("url")) + + def get_model_info(self, model_id: str) -> Any: + r = self._get(f"model/{model_id}") + + if not r.ok: + raise Exception(r.text) + + return r.json() diff --git a/frigate/test/test_config.py b/frigate/test/test_config.py index b978453c1..a40093139 100644 --- a/frigate/test/test_config.py +++ b/frigate/test/test_config.py @@ -34,7 +34,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**self.minimal) assert self.minimal == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert "cpu" in runtime_config.detectors.keys() assert runtime_config.detectors["cpu"].type == DetectorTypeEnum.cpu assert runtime_config.detectors["cpu"].model.width == 320 @@ -59,7 +59,7 @@ class TestConfig(unittest.TestCase): } frigate_config = FrigateConfig(**(deep_merge(config, self.minimal))) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert "cpu" in runtime_config.detectors.keys() assert "edgetpu" in runtime_config.detectors.keys() @@ -125,7 +125,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert "dog" in runtime_config.cameras["back"].objects.track def test_override_birdseye(self): @@ -151,7 +151,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert not runtime_config.cameras["back"].birdseye.enabled assert runtime_config.cameras["back"].birdseye.mode is BirdseyeModeEnum.motion @@ -177,7 +177,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert runtime_config.cameras["back"].birdseye.enabled def test_inherit_birdseye(self): @@ -202,7 +202,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert runtime_config.cameras["back"].birdseye.enabled assert ( runtime_config.cameras["back"].birdseye.mode is BirdseyeModeEnum.continuous @@ -231,7 +231,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert "cat" in runtime_config.cameras["back"].objects.track def test_default_object_filters(self): @@ -256,7 +256,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert "dog" in runtime_config.cameras["back"].objects.filters def test_inherit_object_filters(self): @@ -284,7 +284,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert "dog" in runtime_config.cameras["back"].objects.filters assert runtime_config.cameras["back"].objects.filters["dog"].threshold == 0.7 @@ -313,7 +313,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert "dog" in runtime_config.cameras["back"].objects.filters assert runtime_config.cameras["back"].objects.filters["dog"].threshold == 0.7 @@ -343,7 +343,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() back_camera = runtime_config.cameras["back"] assert "dog" in back_camera.objects.filters assert len(back_camera.objects.filters["dog"].raw_mask) == 2 @@ -374,7 +374,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert "-rtsp_transport" in runtime_config.cameras["back"].ffmpeg_cmds[0]["cmd"] def test_ffmpeg_params_global(self): @@ -403,7 +403,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert "-re" in runtime_config.cameras["back"].ffmpeg_cmds[0]["cmd"] def test_ffmpeg_params_camera(self): @@ -433,7 +433,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert "-re" in runtime_config.cameras["back"].ffmpeg_cmds[0]["cmd"] assert "test" not in runtime_config.cameras["back"].ffmpeg_cmds[0]["cmd"] @@ -468,7 +468,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert "-re" in runtime_config.cameras["back"].ffmpeg_cmds[0]["cmd"] assert "test" in runtime_config.cameras["back"].ffmpeg_cmds[0]["cmd"] assert "test2" not in runtime_config.cameras["back"].ffmpeg_cmds[0]["cmd"] @@ -498,7 +498,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert ( runtime_config.cameras["back"].record.events.retain.objects["person"] == 30 ) @@ -576,7 +576,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert isinstance( runtime_config.cameras["back"].zones["test"].contour, np.ndarray ) @@ -608,7 +608,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() back_camera = runtime_config.cameras["back"] assert back_camera.record.events.objects is None assert back_camera.record.events.retain.objects["person"] == 30 @@ -639,7 +639,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() ffmpeg_cmds = runtime_config.cameras["back"].ffmpeg_cmds assert len(ffmpeg_cmds) == 1 assert not "clips" in ffmpeg_cmds[0]["roles"] @@ -670,7 +670,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert runtime_config.cameras["back"].detect.max_disappeared == 5 * 5 def test_motion_frame_height_wont_go_below_120(self): @@ -698,7 +698,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert runtime_config.cameras["back"].motion.frame_height == 50 def test_motion_contour_area_dynamic(self): @@ -726,7 +726,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert round(runtime_config.cameras["back"].motion.contour_area) == 30 def test_merge_labelmap(self): @@ -755,7 +755,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert runtime_config.model.merged_labelmap[7] == "truck" def test_default_labelmap_empty(self): @@ -783,7 +783,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert runtime_config.model.merged_labelmap[0] == "person" def test_default_labelmap(self): @@ -812,7 +812,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert runtime_config.model.merged_labelmap[0] == "person" def test_fails_on_invalid_role(self): @@ -871,7 +871,7 @@ class TestConfig(unittest.TestCase): } frigate_config = FrigateConfig(**config) - self.assertRaises(ValueError, lambda: frigate_config.runtime_config) + self.assertRaises(ValueError, lambda: frigate_config.runtime_config()) def test_works_on_missing_role_multiple_cams(self): config = { @@ -919,7 +919,7 @@ class TestConfig(unittest.TestCase): } frigate_config = FrigateConfig(**config) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() def test_global_detect(self): config = { @@ -946,7 +946,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert runtime_config.cameras["back"].detect.max_disappeared == 1 assert runtime_config.cameras["back"].detect.height == 1080 @@ -969,7 +969,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert runtime_config.cameras["back"].detect.max_disappeared == 25 assert runtime_config.cameras["back"].detect.height == 720 @@ -998,7 +998,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert runtime_config.cameras["back"].detect.max_disappeared == 1 assert runtime_config.cameras["back"].detect.height == 1080 assert runtime_config.cameras["back"].detect.width == 1920 @@ -1026,7 +1026,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert runtime_config.cameras["back"].snapshots.enabled assert runtime_config.cameras["back"].snapshots.height == 100 @@ -1049,7 +1049,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert runtime_config.cameras["back"].snapshots.bounding_box assert runtime_config.cameras["back"].snapshots.quality == 70 @@ -1077,7 +1077,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert runtime_config.cameras["back"].snapshots.bounding_box == False assert runtime_config.cameras["back"].snapshots.height == 150 assert runtime_config.cameras["back"].snapshots.enabled @@ -1101,7 +1101,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert not runtime_config.cameras["back"].rtmp.enabled def test_default_not_rtmp(self): @@ -1123,7 +1123,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert not runtime_config.cameras["back"].rtmp.enabled def test_global_rtmp_merge(self): @@ -1149,7 +1149,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert runtime_config.cameras["back"].rtmp.enabled def test_global_rtmp_default(self): @@ -1175,7 +1175,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert not runtime_config.cameras["back"].rtmp.enabled def test_global_jsmpeg(self): @@ -1198,7 +1198,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert runtime_config.cameras["back"].live.quality == 4 def test_default_live(self): @@ -1220,7 +1220,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert runtime_config.cameras["back"].live.quality == 8 def test_global_live_merge(self): @@ -1246,7 +1246,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert runtime_config.cameras["back"].live.quality == 7 assert runtime_config.cameras["back"].live.height == 480 @@ -1270,7 +1270,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert runtime_config.cameras["back"].timestamp_style.position == "bl" def test_default_timestamp_style(self): @@ -1292,7 +1292,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert runtime_config.cameras["back"].timestamp_style.position == "tl" def test_global_timestamp_style_merge(self): @@ -1317,7 +1317,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert runtime_config.cameras["back"].timestamp_style.position == "bl" assert runtime_config.cameras["back"].timestamp_style.thickness == 4 @@ -1341,7 +1341,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert runtime_config.cameras["back"].snapshots.retain.default == 1.5 def test_fails_on_bad_camera_name(self): @@ -1365,7 +1365,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) self.assertRaises( - ValidationError, lambda: frigate_config.runtime_config.cameras + ValidationError, lambda: frigate_config.runtime_config().cameras ) def test_fails_on_bad_segment_time(self): @@ -1392,7 +1392,8 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) self.assertRaises( - ValueError, lambda: frigate_config.runtime_config.ffmpeg.output_args.record + ValueError, + lambda: frigate_config.runtime_config().ffmpeg.output_args.record, ) def test_fails_zone_defines_untracked_object(self): @@ -1421,7 +1422,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) - self.assertRaises(ValueError, lambda: frigate_config.runtime_config.cameras) + self.assertRaises(ValueError, lambda: frigate_config.runtime_config().cameras) def test_fails_duplicate_keys(self): raw_config = """ @@ -1465,7 +1466,7 @@ class TestConfig(unittest.TestCase): frigate_config = FrigateConfig(**config) assert config == frigate_config.dict(exclude_unset=True) - runtime_config = frigate_config.runtime_config + runtime_config = frigate_config.runtime_config() assert "dog" in runtime_config.cameras["back"].objects.filters assert runtime_config.cameras["back"].objects.filters["dog"].min_ratio == 0.2 assert runtime_config.cameras["back"].objects.filters["dog"].max_ratio == 10.1 diff --git a/frigate/test/test_http.py b/frigate/test/test_http.py index bc08ec010..2c0e58df2 100644 --- a/frigate/test/test_http.py +++ b/frigate/test/test_http.py @@ -292,7 +292,7 @@ class TestHttp(unittest.TestCase): def test_config(self): app = create_app( - FrigateConfig(**self.minimal_config).runtime_config, + FrigateConfig(**self.minimal_config).runtime_config(), self.db, None, None, @@ -308,7 +308,7 @@ class TestHttp(unittest.TestCase): def test_recordings(self): app = create_app( - FrigateConfig(**self.minimal_config).runtime_config, + FrigateConfig(**self.minimal_config).runtime_config(), self.db, None, None, @@ -327,7 +327,7 @@ class TestHttp(unittest.TestCase): @patch("frigate.http.stats_snapshot") def test_stats(self, mock_stats): app = create_app( - FrigateConfig(**self.minimal_config).runtime_config, + FrigateConfig(**self.minimal_config).runtime_config(), self.db, None, None, From 19890310fea8ce04b1b5ab2581ac1a6d3f9772c5 Mon Sep 17 00:00:00 2001 From: Blake Blackshear Date: Sun, 30 Apr 2023 14:58:39 -0500 Subject: [PATCH 6/6] Bug fixes (#6332) * fix timeline delete * fix labelmap in config response * create cache dirs if needed --- frigate/http.py | 6 ++-- frigate/record/cleanup.py | 2 +- frigate/test/test_config.py | 67 +++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 4 deletions(-) diff --git a/frigate/http.py b/frigate/http.py index 706487e8f..66aaff368 100644 --- a/frigate/http.py +++ b/frigate/http.py @@ -867,9 +867,9 @@ def config(): config["plus"] = {"enabled": current_app.plus_api.is_active()} for detector, detector_config in config["detectors"].items(): - detector_config["model"]["labelmap"] = current_app.frigate_config.detectors[ - detector - ].model.merged_labelmap + detector_config["model"][ + "labelmap" + ] = current_app.frigate_config.model.merged_labelmap return jsonify(config) diff --git a/frigate/record/cleanup.py b/frigate/record/cleanup.py index d649a2d87..75784133a 100644 --- a/frigate/record/cleanup.py +++ b/frigate/record/cleanup.py @@ -141,7 +141,7 @@ class RecordingCleanup(threading.Thread): deleted_recordings.add(recording.id) # delete timeline entries relevant to this recording segment - Timeline.delete( + Timeline.delete().where( Timeline.timestamp.between( recording.start_time, recording.end_time ), diff --git a/frigate/test/test_config.py b/frigate/test/test_config.py index a40093139..7cd5e2217 100644 --- a/frigate/test/test_config.py +++ b/frigate/test/test_config.py @@ -1,3 +1,5 @@ +import json +import os import unittest import numpy as np from pydantic import ValidationError @@ -6,7 +8,9 @@ from frigate.config import ( BirdseyeModeEnum, FrigateConfig, ) +from frigate.const import MODEL_CACHE_DIR from frigate.detectors import DetectorTypeEnum +from frigate.plus import PlusApi from frigate.util import deep_merge, load_config_with_no_duplicates @@ -30,6 +34,35 @@ class TestConfig(unittest.TestCase): }, } + self.plus_model_info = { + "id": "e63b7345cc83a84ed79dedfc99c16616", + "name": "SSDLite Mobiledet", + "description": "Fine tuned model", + "trainDate": "2023-04-28T23:22:01.262Z", + "type": "ssd", + "supportedDetectors": ["edgetpu"], + "width": 320, + "height": 320, + "inputShape": "nhwc", + "pixelFormat": "rgb", + "labelMap": { + "0": "amazon", + "1": "car", + "2": "cat", + "3": "deer", + "4": "dog", + "5": "face", + "6": "fedex", + "7": "license_plate", + "8": "package", + "9": "person", + "10": "ups", + }, + } + + if not os.path.exists(MODEL_CACHE_DIR) and not os.path.islink(MODEL_CACHE_DIR): + os.makedirs(MODEL_CACHE_DIR) + def test_config_class(self): frigate_config = FrigateConfig(**self.minimal) assert self.minimal == frigate_config.dict(exclude_unset=True) @@ -815,6 +848,40 @@ class TestConfig(unittest.TestCase): runtime_config = frigate_config.runtime_config() assert runtime_config.model.merged_labelmap[0] == "person" + def test_plus_labelmap(self): + with open("/config/model_cache/test", "w") as f: + json.dump(self.plus_model_info, f) + with open("/config/model_cache/test.json", "w") as f: + json.dump(self.plus_model_info, f) + + config = { + "mqtt": {"host": "mqtt"}, + "model": {"path": "plus://test"}, + "cameras": { + "back": { + "ffmpeg": { + "inputs": [ + { + "path": "rtsp://10.0.0.1:554/video", + "roles": ["detect"], + }, + ] + }, + "detect": { + "height": 1080, + "width": 1920, + "fps": 5, + }, + } + }, + } + + frigate_config = FrigateConfig(**config) + assert config == frigate_config.dict(exclude_unset=True) + + runtime_config = frigate_config.runtime_config(PlusApi()) + assert runtime_config.model.merged_labelmap[0] == "amazon" + def test_fails_on_invalid_role(self): config = { "mqtt": {"host": "mqtt"},