Compare commits

..
25 Commits
Author SHA1 Message Date
Josh Hawkins bcf81e7c65 fix the model lookup KeyError for cameras added at runtime (#24026) 2026-08-18 16:25:18 -05:00
Josh Hawkins 90e56b9137 Add import/export for camera group layouts and per-camera streaming settings (#24025)
* add import/export for camera group layouts and streaming settings

Camera group layouts and per-camera streaming settings are stored in the browser's IndexedDB, so they are tied to a single browser on a single device. Users with more than one device have to rebuild every group layout and re-pick every camera's stream settings by hand, and clearing browser data loses the work.

Add a Backup & Restore card to Settings > UI Settings that exports these settings to a JSON file and imports that file on another device. Import shows a confirmation dialog with per-section counts, switches for layouts, streaming settings, and UI preferences, and warnings about camera groups or cameras in the file that are not on this server.

Server-side storage is deliberately avoided. These are per-device presentation settings: a layout arranged for a desktop is wrong on a tablet, and continuous full-resolution streams that are free on a wired LAN are not on a phone. An explicit file moves settings only when the user chooses to move them.

Implementation notes:

- web/src/utils/uiSettingsTransfer.ts owns a registry of transferable IndexedDB keys. Each entry records whether the key is user-namespaced, matching which persistence hook wrote it, plus a zod schema for its value.
- Only registry-known keys are ever written, and only when their value passes that schema. The file format deliberately lets unknown keys survive parsing, so this filter is what prevents a hand-edited file from writing arbitrary storage keys or out-of-range values.
- Export falls back to the legacy un-namespaced key, because the username migration runs lazily on first mount of each owning hook.
- Streaming settings merge per group rather than replacing the whole map, so groups configured only on the receiving device survive.
- Import writes storage and then reloads, because useUserPersistence reads a key only on mount and StreamingSettingsProvider would otherwise write its stale in-memory state back over the import.
- playbackBandwidthEstimate, frigate-search-history, and live-layout are excluded: the first two are measurements and user data rather than preferences, and live-layout's default is derived from the device.

* merge imported streaming settings per camera instead of per group
2026-08-18 16:25:18 -05:00
Nicolas MowenandJosh Hawkins 4fb13b72ef Implement UI for managing multiple models (#24023)
* Implement hardware detection and UI management

* Cleanup Frigate+ detection

* Don't count model as changed

* Fixes for audio map error

* Add descriptions

* Enforce that all model must exist

* Fix hardware picking

* Docs fixes

* WebUI cleanup

* Cleanup handling of scenes

* UI refinement

* Cleanup recommended UI

* test fixews
2026-08-18 16:25:18 -05:00
Josh Hawkins 5aee6861a8 Base emergency cleanup on the streams a camera is currently recording (#24022)
* gate emergency cleanup bandwidth on the streams a camera currently records

* settle bandwidth samples per stream instead of per camera

* fix mypy
2026-08-18 16:25:18 -05:00
Nicolas MowenandJosh Hawkins 591464e82f Refactor detector and model management (#23995)
* Refactor detector and model management

* Fix model resolution field
2026-08-18 16:25:18 -05:00
Ersa Oktavian RamadanandJosh Hawkins 13a7d8ecae Add audio labelmap grouping (#24004)
Allow audio classes to be grouped under a shared configured label.

Keep audio overrides separate from object labels and retain only the highest-scoring grouped detection.

Refs #23967
2026-08-18 16:25:18 -05:00
Josh Hawkins ac73ad210e Show main and sub stream usage separately in Storage Metrics (#24015)
* backend

* frontend

* docs

* test

* report null instead of 0 for a stream with no cached bandwidth sample
2026-08-18 16:25:18 -05:00
Josh Hawkins c6ec533430 Refactor MQTT (#24010)
* refactor mqtt so that Frigate owns the transport lifecycle instead of delegating it to paho

* release the shutdown barrier on worker crash and replay retained publishes the broker never acked

* collapse in-flight retained values by topic and release the shutdown barrier from a finally

* replay the outage buffer before the publish queue so newer values are not reverted
2026-08-18 16:25:18 -05:00
Josh Hawkins dcf711d5a0 Refactor birdseye activity modes as a list and add alerts/detections (#24012)
* backend

* tests

* frontend and i18n

* e2e test schema

* docs
2026-08-18 16:25:18 -05:00
Josh Hawkins 83750a9596 Improve History's seek startup time and recordings query performance (#24011)
* serve a segment startup ladder so seeks begin playing sooner

nginx-vod was handed one 10s segment per recording file, so every playlist start had to download and decode a full segment before the first frame. Declare real keyframe data per clip and let nginx cut short leading segments from it.

- add vod_bootstrap_segment_durations 1000/2000/4000 so each playlist starts with 1s/2s/4s segments before settling at 10s
- emit real clip-relative keyFrameDurations (plus firstKeyFrameOffset when nonzero) from the recording keyframe index; rows without an index keep the whole-clip declaration, the only safe cut without keyframe knowledge
- drop the manifest's segment_duration field, which was always inert: nginx-vod parses only camelCase segmentDuration
- rebuild the player source at the seek target, quantized to a 10s grid, so the ladder applies to every seek and seek URLs stay repeatable for nginx's mapping and response caches
- route the seek model, in-range checks, and the stale-report guard through the source window rather than the chunk range
- bridge repositioning seeks (>2s from the last played timestamp) through the preview player and hold the release anchor one commit, so neither path paints a stale frame
- clear a pending loading timer before replacing it; an orphaned timer escaped onPlaying's clearTimeout and flashed loading mid-playback

* keep recordings queries on their indexes

Several recordings queries degraded into full scans or large sorts on big databases: the planner ignored index order, or the query shape gave it nothing tight to seek on. Reshape them into bounded seeks and add the composite index the per-stream lookups need.

- index recordings on (camera, stream_type, start_time DESC) and drop the (camera, stream_type) index it supersedes
- walk the recordings summary day by day with EXISTS probes and per-camera MIN/MAX seeks, skipping ahead over empty gaps instead of bucketing every row for the requested cameras
- run the summary endpoint on the event loop rather than the threadpool
- bound the unavailable-recordings query by start_time per camera and merge the results in Python
- bound the expire query's start_time so it seeks the retention window instead of scanning a camera's whole history
- enumerate deleted cameras with one index seek each rather than a camera NOT IN (...) scan
- compute bandwidth with segment_size filtered in a CASE projection; as a WHERE predicate it baited the planner into the (camera, segment_size) index plus a full sort of the camera's history
- fall back to a 1000-segment window when the recent 100 are all zero-size, so an ingest glitch doesn't report zero bandwidth
- limit the needs_refresh count instead of counting every segment
- cover sub-only and sparse calendar days, midnight-spanning day attribution, multi-camera gap merging, deleted-camera expiry, and zero-size segment runs

* fix mypy
2026-08-18 16:25:18 -05:00
Josh Hawkins 83fbce1533 Enable PTZ control setup in the Add Camera Wizard (#23444)
* add ptz controls to camera via wizard when onvif has already been probed

* i18n

* add e2e test

* backend add and remove subscriber

* tweaks

* turn on switch by default if pan and/or tilt capability is available

* fix test
2026-08-18 16:25:18 -05:00
Josh Hawkins 19fdc11ab3 Add sub stream recording with adaptive quality playback (#24009)
* add sub stream recording with adaptive quality playback

Optionally record a second, lower bitrate stream alongside the main
recording stream via a `record_sub` input role and `record.sub` config block, with its own retention windows.
Recordings rows now carry the stream type plus the media details needed to serve both streams from one manifest: video codec, audio presence, audio codec and rate, and a record-time keyframe index.

Playback resolves coverage across both streams and merges them into a single VOD sequence, falling back to a discontinuity manifest with per-clip init segments when the media signatures differ. The player exposes a quality selector, and an auto governor picks the stream from stall time, bandwidth, codec support, and the save-data hint.

* fix tests and i18n
2026-08-18 16:25:18 -05:00
Josh Hawkins a85304e26e stop creating a config subscriber per capture thread (#24002) 2026-08-18 16:25:18 -05:00
Josh Hawkins 4bfd89fd90 Guard lookups when adding/deleting cameras at runtime (#23994)
* Guard object processor queue handlers against unknown cameras

* Skip embeddings post processing for removed cameras

* End review segments for removed cameras

* Drop queued autotracker moves for removed cameras

* Release tracked event thumbnails when skipping a removed camera

* Add locked accessors for camera states

* Read camera states through the processor accessors

* Guard output and recording paths against cameras not yet known

* Resolve camera state once in ONVIF, notification, and transcription paths
2026-08-18 16:25:18 -05:00
Ersa Oktavian RamadanandJosh Hawkins 811887c70d Refactor Birdseye activity types as composable booleans (#23940)
* Add combined motion and object Birdseye mode

Add a motion_objects mode that keeps Birdseye active when motion is detected or a confirmed tracked object is present, including stationary objects.

Wire the mode through configuration, runtime commands, API schemas, documentation, and UI labels. Exclude false-positive trackers and add regression coverage for Birdseye activation and MQTT validation.

* Refactor Birdseye activity types as booleans

Replace combination-specific Birdseye modes with composable boolean activity types for motion, active objects, stationary objects, and continuous display.

Preserve legacy single-mode configuration and MQTT inputs, support canonical comma-separated MQTT combinations, and allow scalar YAML values to be replaced by nested settings through the config API.

* Preserve OpenVINO config translations

Regenerate the configuration translations with the OpenVINO detector schema available so the unrelated production detector labels remain intact.

* Preserve partial Birdseye mode overrides

Allow an empty activity selection with a canonical NONE MQTT state so partial camera and profile overrides can disable inherited flags without failing validation.

Add regression coverage for camera and profile inheritance, document the NONE contract, and keep the generated schema fixture scoped to Birdseye.

* Address Birdseye activity review feedback

Move scalar mode compatibility into the 0.18-1 config migration and reject empty activity selections instead of publishing a NONE state.

Pass activity signals through a frozen dataclass, preserve existing active-object tracker behavior, and require confirmed stationary objects. Revert the generic YAML mutation and cover migration, inheritance, MQTT, and activation regressions.

* Move Birdseye migration to 0.19

Use the 0.19-0 configuration revision for converting scalar Birdseye modes to composable activity flags, and update the migration regression coverage accordingly.

* Remove Birdseye migration test

Drop the dedicated config migration test as requested during review while retaining the 0.19-0 migration implementation.
2026-08-18 16:25:18 -05:00
Josh Hawkins e6812f9282 Fix birdseye layout overlap with mixed landscape/portrait cameras (#22917)
* fix birdseye layout calculation

replace the two pass layout with a single pass pixel space algorithm

* add test
2026-08-18 16:25:18 -05:00
Nicolas MowenandJosh Hawkins 923a0c8310 Don't require object type for parameter in categorized names tool 2026-08-18 16:25:18 -05:00
13a1711adf Dynamically resolve Intel NPU (#23761)
* Add support for newer Intel NPU busy time counter

* Resolve Intel NPU device dynamically

---------

Co-authored-by: Filious Louis <1417132+fjlouis@users.noreply.github.com>
2026-08-18 16:25:18 -05:00
DoFabienandJosh Hawkins 6ca5b59281 Improve recording timeline and VOD query performance (#23862)
* Improve recording timeline and VOD query performance

* Add recording query boundary tests
2026-08-18 16:25:18 -05:00
Nicolas MowenandJosh Hawkins 7f929cb0dc GenAI Chat Prompt Refinements (#23864)
* Prompt refactoring and optimization

* Update spec
2026-08-18 16:25:18 -05:00
Nicolas MowenandJosh Hawkins d99ce0a9ed Update to 0.19 2026-08-18 16:25:18 -05:00
Josh HawkinsandGitHub 036bae4ea9 Return a specific 404 when starting a debug replay with no recordings in range (#24024)
CI / AMD64 Build (push) Canceled after 0s
CI / ARM Build (push) Canceled after 0s
CI / Jetson Jetpack 6 (push) Canceled after 0s
CI / Assemble and push default build (push) Canceled after 0s
CI / AMD64 Extra Build (push) Canceled after 0s
CI / ARM Extra Build (push) Canceled after 0s
CI / Synaptics Build (push) Canceled after 0s
2026-08-18 10:08:52 -05:00
dtigheandGitHub 8384a8c5b3 Fix Gemini tool calling on 3.6+ by using documented function response role (#24013)
Gemini 3.6 and newer reject role="function" on the function response
Content with 400 INVALID_ARGUMENT, breaking any chat query that triggers
a tool call. The tool call itself succeeds; only the hand-back to the
model fails, and because the error surfaces mid-stream the request still
returns HTTP 200, so it is easy to miss.

Google's function calling documentation specifies role="user" for
returning function results:

    contents.append(response.candidates[0].content)
    contents.append(types.Content(role="user", parts=[function_response_part]))

https://ai.google.dev/gemini-api/docs/generate-content/function-calling

Verified with my local setup.
2026-08-18 08:16:47 -06:00
Josh HawkinsandGitHub 77fc2ce174 Miscellaneous fixes (0.18 beta) (#24016)
CI / AMD64 Build (push) Canceled after 0s
CI / ARM Build (push) Canceled after 0s
CI / Jetson Jetpack 6 (push) Canceled after 0s
CI / AMD64 Extra Build (push) Canceled after 0s
CI / ARM Extra Build (push) Canceled after 0s
CI / Synaptics Build (push) Canceled after 0s
CI / Assemble and push default build (push) Canceled after 0s
* fix classification drawer closing instead of scrolling when list is long on mobile

* add qwen3.8 to genai docs

* add titles to more clearly separate model types
2026-08-18 07:01:22 -06:00
Josh HawkinsandGitHub 8425a76558 Miscellaneous fixes (0.18 beta) (#23993)
CI / AMD64 Build (push) Canceled after 0s
CI / ARM Build (push) Canceled after 0s
CI / Jetson Jetpack 6 (push) Canceled after 0s
CI / AMD64 Extra Build (push) Canceled after 0s
CI / ARM Extra Build (push) Canceled after 0s
CI / Synaptics Build (push) Canceled after 0s
CI / Assemble and push default build (push) Canceled after 0s
* subscribe to add in webpush

* add docs for detector cpu usage

* rebuild notification camera access when a camera is added at runtime

* document how frigate shows CPU usage metrics

* add faq about version key in config
2026-08-16 12:39:28 -06:00
123 changed files with 6190 additions and 3590 deletions
File diff suppressed because it is too large Load Diff
+52 -47
View File
@@ -56,17 +56,6 @@ mqtt:
# 2 = exactly once
qos: 0
# Optional: Detectors configuration. Defaults to a single CPU detector
detectors:
# Required: name of the detector
detector_name:
# Required: type of the detector
# Frigate provides many types, see https://docs.frigate.video/configuration/object_detectors for more details (default: shown below)
# Additional detector types can also be plugged in.
# Detectors may require additional configuration.
# Refer to the Detectors configuration page for more information.
type: cpu
# Optional: Database configuration
database:
# The path to store the SQLite DB (default: shown below)
@@ -157,44 +146,56 @@ auth:
- front_door
- back_yard
# Optional: model modifications
# Optional: object detection models. Defaults to a single model on a CPU detector.
# NOTE: The default values are for the EdgeTPU detector.
# Other detectors will require the model config to be set.
model:
# Required: path to the model. Frigate+ models use plus://<model_id> (default: automatic based on detector)
path: /edgetpu_model.tflite
# Required: path to the labelmap (default: shown below)
labelmap_path: /labelmap.txt
# Required: Object detection model input width (default: shown below)
width: 320
# Required: Object detection model input height (default: shown below)
height: 320
# Required: Object detection model input colorspace
# Valid values are rgb, bgr, or yuv. (default: shown below)
input_pixel_format: rgb
# Required: Object detection model input tensor format
# Valid values are nhwc, nchw, hwnc, or hwcn (default: shown below)
input_tensor: nhwc
# Optional: Data type of the model input tensor
# Valid values are float, float_denorm, or int (default: shown below)
input_dtype: int
# Required: Object detection model architecture, used by detectors that support more
# than one model type (openvino, onnx, rknn, memryx, axengine, synaptics, and others)
# Valid values are ssd, yolox, yolonas, yolo-generic, rfdetr, dfine (default: shown below)
model_type: ssd
# Required: Label name modifications. These are merged into the standard labelmap.
labelmap:
2: vehicle
# Optional: Map of object labels to their attribute labels (default: depends on model)
attributes_map:
person:
- amazon
- face
car:
- amazon
- fedex
- license_plate
- ups
models:
# Optional: the camera environment this model is for (default: shown below)
# Cameras select a model by setting detect -> scene to a matching value, and
# a model with a scene of all is used by any camera that does not set one.
# Valid values are all, indoor, outdoor, indoor_thermal, outdoor_thermal
- scene: all
# Required: hardware this model runs on, as <detector> or <detector>:<device>
# See https://docs.frigate.video/configuration/object_detectors for the
# detectors available and the devices each one accepts. All of a model's
# devices must use the same detector. Listing the same device more than once
# runs additional inference processes on it.
devices:
- edgetpu:pci:0
# Required: path to the model. Frigate+ models use plus://<model_id> (default: automatic based on detector)
path: /edgetpu_model.tflite
# Required: path to the labelmap (default: shown below)
labelmap_path: /labelmap.txt
# Required: Object detection model input width (default: shown below)
width: 320
# Required: Object detection model input height (default: shown below)
height: 320
# Required: Object detection model input colorspace
# Valid values are rgb, bgr, or yuv. (default: shown below)
input_pixel_format: rgb
# Required: Object detection model input tensor format
# Valid values are nhwc, nchw, hwnc, or hwcn (default: shown below)
input_tensor: nhwc
# Optional: Data type of the model input tensor
# Valid values are float, float_denorm, or int (default: shown below)
input_dtype: int
# Required: Object detection model architecture, used by detectors that support more
# than one model type (openvino, onnx, rknn, memryx, axengine, synaptics, and others)
# Valid values are ssd, yolox, yolonas, yolo-generic, rfdetr, dfine (default: shown below)
model_type: ssd
# Required: Label name modifications. These are merged into the standard labelmap.
labelmap:
2: vehicle
# Optional: Map of object labels to their attribute labels (default: depends on model)
attributes_map:
person:
- amazon
- face
car:
- amazon
- fedex
- license_plate
- ups
# Optional: Audio Events Configuration
# NOTE: Can be overridden at the camera level
@@ -314,6 +315,10 @@ detect:
width: 1280
# Optional: height of the frame for the input with the detect role (default: use native stream resolution)
height: 720
# Optional: the environment this camera looks at, which picks the model it runs on
# (default: the model with a scene of all)
# Valid values are all, indoor, outdoor, indoor_thermal, outdoor_thermal
scene: outdoor
# Optional: desired fps for your camera for the input with the detect role (default: shown below)
# NOTE: Recommended value of 5. Ideally, try and reduce your FPS on the camera.
fps: 5
+18 -16
View File
@@ -177,7 +177,7 @@ Custom models may also require different input tensor formats. The colorspace co
<ConfigTabs>
<TabItem value="ui">
Navigate to <NavPath path="Settings > System > Detectors and model" /> and open the **Custom Model** tab to configure the model path, dimensions, and input format.
Navigate to <NavPath path="Settings > System > Detection models" /> and, on the model you want to change, open the **Custom Model** tab to configure the model path, dimensions, and input format.
| Field | Description |
| --------------------------------------------- | ------------------------------------ |
@@ -192,12 +192,14 @@ Navigate to <NavPath path="Settings > System > Detectors and model" /> and open
```yaml
# Optional: model config
model:
path: /path/to/model
width: 320
height: 320
input_tensor: "nhwc"
input_pixel_format: "bgr"
models:
- devices:
- openvino:GPU
path: /path/to/model
width: 320
height: 320
input_tensor: "nhwc"
input_pixel_format: "bgr"
```
</TabItem>
@@ -214,15 +216,15 @@ If the labelmap is customized then the labels used for alerts will need to be ad
The labelmap can be customized to your needs. A common reason to do this is to combine multiple object types that are easily confused when you don't need to be as granular such as car/truck. By default, truck is renamed to car because they are often confused. You cannot add new object types, but you can change the names of existing objects in the model.
```yaml
model:
labelmap:
2: vehicle
3: vehicle
5: vehicle
7: vehicle
15: animal
16: animal
17: animal
models:
- labelmap:
2: vehicle
3: vehicle
5: vehicle
7: vehicle
15: animal
16: animal
17: animal
```
Note that if you rename objects in the labelmap, you will also need to update your `objects -> track` list as well.
+16 -21
View File
@@ -154,7 +154,7 @@ Here are some common starter configuration examples. These can be configured thr
1. Navigate to <NavPath path="Settings > System > MQTT" /> and configure the MQTT connection to your Home Assistant Mosquitto broker
2. Navigate to <NavPath path="Settings > Global configuration > FFmpeg" /> and set **Hardware acceleration arguments** to `Raspberry Pi (H.264)`
3. Navigate to <NavPath path="Settings > System > Detectors and model" /> and add a detector with **Type** `EdgeTPU` and **Device** `usb`
3. Navigate to <NavPath path="Settings > System > Detection models" /> and select **Coral EdgeTPU (USB)** from the **Hardware** dropdown
4. Navigate to <NavPath path="Settings > Global configuration > Recording" /> and set **Enable recording** to on, **Motion retention > Retention days** to `7`, **Alert retention > Event retention > Retention days** to `30`, **Alert retention > Event retention > Retention mode** to `motion`, **Detection retention > Event retention > Retention days** to `30`, **Detection retention > Event retention > Retention mode** to `motion`
5. Navigate to <NavPath path="Settings > Global configuration > Snapshots" /> and set **Enable snapshots** to on, **Snapshot retention > Default retention** to `30`
6. Navigate to <NavPath path="Settings > Global configuration > Camera management" /> and add your camera with the appropriate RTSP stream URL
@@ -172,10 +172,9 @@ mqtt:
ffmpeg:
hwaccel_args: preset-rpi-64-h264
detectors:
coral:
type: edgetpu
device: usb
models:
- devices:
- edgetpu:usb
record:
enabled: True
@@ -233,7 +232,7 @@ cameras:
1. Navigate to <NavPath path="Settings > System > MQTT" /> and set **Enable MQTT** to off
2. Navigate to <NavPath path="Settings > Global configuration > FFmpeg" /> and set **Hardware acceleration arguments** to `VAAPI (Intel/AMD GPU)`
3. Navigate to <NavPath path="Settings > System > Detectors and model" /> and add a detector with **Type** `EdgeTPU` and **Device** `usb`
3. Navigate to <NavPath path="Settings > System > Detection models" /> and select **Coral EdgeTPU (USB)** from the **Hardware** dropdown
4. Navigate to <NavPath path="Settings > Global configuration > Recording" /> and set **Enable recording** to on, **Motion retention > Retention days** to `7`, **Alert retention > Event retention > Retention days** to `30`, **Alert retention > Event retention > Retention mode** to `motion`, **Detection retention > Event retention > Retention days** to `30`, **Detection retention > Event retention > Retention mode** to `motion`
5. Navigate to <NavPath path="Settings > Global configuration > Snapshots" /> and set **Enable snapshots** to on, **Snapshot retention > Default retention** to `30`
6. Navigate to <NavPath path="Settings > Global configuration > Camera management" /> and add your camera with the appropriate RTSP stream URL
@@ -249,10 +248,9 @@ mqtt:
ffmpeg:
hwaccel_args: preset-vaapi
detectors:
coral:
type: edgetpu
device: usb
models:
- devices:
- edgetpu:usb
record:
enabled: True
@@ -310,8 +308,8 @@ cameras:
1. Navigate to <NavPath path="Settings > System > MQTT" /> and configure the connection to your MQTT broker
2. Navigate to <NavPath path="Settings > Global configuration > FFmpeg" /> and set **Hardware acceleration arguments** to `VAAPI (Intel/AMD GPU)`
3. Navigate to <NavPath path="Settings > System > Detectors and model" /> and add a detector with **Type** `openvino` and **Device** `AUTO`
4. On the same page, in the **Custom Model** tab, configure the OpenVINO model path and settings
3. Navigate to <NavPath path="Settings > System > Detection models" /> and select **Intel GPU** from the **Hardware** dropdown
4. On the same model, open the **Custom Model** tab and configure the OpenVINO model path and settings
5. Navigate to <NavPath path="Settings > Global configuration > Recording" /> and set **Enable recording** to on, **Motion retention > Retention days** to `7`, **Alert retention > Event retention > Retention days** to `30`, **Alert retention > Event retention > Retention mode** to `motion`, **Detection retention > Event retention > Retention days** to `30`, **Detection retention > Event retention > Retention mode** to `motion`
6. Navigate to <NavPath path="Settings > Global configuration > Snapshots" /> and set **Enable snapshots** to on, **Snapshot retention > Default retention** to `30`
7. Navigate to <NavPath path="Settings > Global configuration > Camera management" /> and add your camera with the appropriate RTSP stream URL
@@ -329,15 +327,12 @@ mqtt:
ffmpeg:
hwaccel_args: preset-vaapi
detectors:
ov:
type: openvino
device: AUTO
model:
width: 300
height: 300
input_tensor: nhwc
models:
- devices:
- openvino:AUTO
width: 300
height: 300
input_tensor: nhwc
input_pixel_format: bgr
path: /openvino-model/ssdlite_mobilenet_v2.xml
labelmap_path: /openvino-model/coco_91cl_bkgr.txt
+9 -5
View File
@@ -59,13 +59,17 @@ Running Generative AI models on CPU is not recommended, as high inference times
### Recommended Local Models
#### Vision models
You must use a vision-capable model with Frigate. The following models are recommended for local deployment of the `descriptions` and `chat` roles:
| Model | Notes |
| ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `qwen3-vl` | Strong visual and situational understanding, enhanced ability to identify smaller objects and interactions with object. |
| `qwen3.6` | Strong situational understanding, but missing DeepStack from qwen3-vl leading to worse performance for identifying objects in people's hand and other small details. |
| `gemma4` | Strong situational understanding, sometimes resorts to more vague terms like 'interacts' instead of assigning a specific action. |
| Model | Notes |
| ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `qwen3-vl` | Strong visual and situational understanding, enhanced ability to identify smaller objects and interactions with object. |
| `qwen3.6`/`qwen3.8` | Strong situational understanding, but missing DeepStack from qwen3-vl leading to worse performance for identifying objects in people's hand and other small details. |
| `gemma4` | Strong situational understanding, sometimes resorts to more vague terms like 'interacts' instead of assigning a specific action. |
#### Embedding models
The `embeddings` role needs a different kind of model. Text queries are matched against the stored image embeddings, so the model must be trained to place images and text into the same vector space. A chat or description model will still return vectors when asked, but those vectors are not trained for retrieval and text searches will return poor matches with no error to indicate why.
+112 -65
View File
@@ -68,12 +68,66 @@ Frigate supports multiple different detectors that work on different types of ha
:::note
Multiple detectors can not be mixed for object detection (ex: OpenVINO and Coral EdgeTPU can not be used for object detection at the same time).
A single model can not be spread across different detector types (ex: OpenVINO and Coral EdgeTPU can not run the same model at the same time). Configuring more than one model, each on its own detector type, is supported.
This does not affect using hardware for accelerating other tasks such as [semantic search](./semantic_search.md)
:::
### Configuring models and hardware
Object detection is configured with a `models` list. Each entry describes one model and the hardware it runs on:
```yaml
models:
- devices:
- openvino:GPU
path: /config/model_cache/yolov9-s.onnx
model_type: yolo-generic
width: 320
height: 320
```
Each entry in `devices` is a detector type, optionally followed by a colon and a device for that detector, such as `edgetpu:pci:0`, `openvino:NPU`, or `tensorrt:0`. The per-detector sections below document the device values each one accepts. Listing several devices runs the model on all of them, and listing the **same** device more than once runs additional inference processes against it, which can improve throughput on hardware that keeps up with more than one stream:
```yaml
models:
- devices:
- openvino:GPU
- openvino:GPU
```
Coral EdgeTPU and MemryX accelerators can only be opened by one process, so those devices can not be repeated.
### Running more than one model
Cameras can be split across models by scene, which is useful when indoor and outdoor cameras benefit from differently trained models. Each model declares the `scene` it is for, and each camera picks one with `detect -> scene`:
```yaml
models:
- scene: outdoor
path: plus://your-outdoor-model
devices:
- edgetpu:pci:0
- scene: indoor
path: /config/model_cache/indoor.onnx
model_type: yolo-generic
devices:
- openvino:GPU
cameras:
driveway:
detect:
scene: outdoor
...
hallway:
detect:
scene: indoor
...
```
Available scenes are `all`, `indoor`, `outdoor`, `indoor_thermal`, and `outdoor_thermal`. A model with a scene of `all` is used by every camera that does not set one, and `all` is the default when a model does not declare a scene. Changing a camera's scene requires a restart.
### Choosing a model size
Along with picking a detector for your hardware, you will choose a model's **input resolution** (such as `320x320` or `640x640`) and, for model families like YOLOv9, a **variant size** (`tiny`, `small`, etc.). Both affect the balance between accuracy and the inference time your hardware can sustain.
@@ -92,11 +146,11 @@ The best detection accuracy comes from a model trained on images that look like
# Officially Supported Detectors
Frigate provides a number of builtin detector types. By default, Frigate will use a single CPU detector. Other detectors may require additional configuration as described below. When using multiple detectors they will run in dedicated processes, but pull from a common queue of detection requests from across all cameras.
Frigate provides a number of builtin detector types. By default, Frigate will use a single CPU detector. Other detectors may require additional configuration as described below. Each of a model's devices runs in a dedicated process, and they pull from a common queue of detection requests from the cameras assigned to that model.
## Edge TPU Detector
The Edge TPU detector type runs TensorFlow Lite models utilizing the Google Coral delegate for hardware acceleration. To configure an Edge TPU detector, set the `"type"` attribute to `"edgetpu"`.
The Edge TPU detector type runs TensorFlow Lite models utilizing the Google Coral delegate for hardware acceleration. To use it, prefix a model's device with `edgetpu`.
The Edge TPU device can be specified using the `"device"` attribute according to the [Documentation for the TensorFlow Lite Python API](https://coral.ai/docs/edgetpu/multiple-edgetpu/#using-the-tensorflow-lite-python-api). If not set, the delegate will use the first device it finds.
@@ -111,16 +165,15 @@ See [common Edge TPU troubleshooting steps](/troubleshooting/edgetpu) if the Edg
<ConfigTabs>
<TabItem value="ui">
Navigate to <NavPath path="Settings > System > Detectors and model" /> and select **EdgeTPU** from the detector type dropdown and click **Add**, then set device to `usb`.
Navigate to <NavPath path="Settings > System > Detection models" /> and select **Coral EdgeTPU (USB)** from the **Hardware** dropdown.
</TabItem>
<TabItem value="yaml">
```yaml
detectors:
coral:
type: edgetpu
device: usb
models:
- devices:
- edgetpu:usb
```
</TabItem>
@@ -131,19 +184,16 @@ detectors:
<ConfigTabs>
<TabItem value="ui">
Navigate to <NavPath path="Settings > System > Detectors and model" /> and select **EdgeTPU** from the detector type dropdown and click **Add** to add multiple detectors, specifying `usb:0` and `usb:1` as the device for each.
Navigate to <NavPath path="Settings > System > Detection models" /> and select **Coral EdgeTPU (USB)** from the **Hardware** dropdown and check each Coral the model should run on.
</TabItem>
<TabItem value="yaml">
```yaml
detectors:
coral1:
type: edgetpu
device: usb:0
coral2:
type: edgetpu
device: usb:1
models:
- devices:
- edgetpu:usb:0
- edgetpu:usb:1
```
</TabItem>
@@ -156,16 +206,15 @@ _warning: may have [compatibility issues](https://github.com/blakeblackshear/fri
<ConfigTabs>
<TabItem value="ui">
Navigate to <NavPath path="Settings > System > Detectors and model" /> and select **EdgeTPU** from the detector type dropdown and click **Add**, then leave the device field empty.
Navigate to <NavPath path="Settings > System > Detection models" /> and select the **Coral EdgeTPU** entry from the **Hardware** dropdown.
</TabItem>
<TabItem value="yaml">
```yaml
detectors:
coral:
type: edgetpu
device: ""
models:
- devices:
- 'edgetpu:'
```
</TabItem>
@@ -176,16 +225,15 @@ detectors:
<ConfigTabs>
<TabItem value="ui">
Navigate to <NavPath path="Settings > System > Detectors and model" /> and select **EdgeTPU** from the detector type dropdown and click **Add**, then set device to `pci`.
Navigate to <NavPath path="Settings > System > Detection models" /> and select **Coral EdgeTPU (PCIe)** from the **Hardware** dropdown.
</TabItem>
<TabItem value="yaml">
```yaml
detectors:
coral:
type: edgetpu
device: pci
models:
- devices:
- edgetpu:pci
```
</TabItem>
@@ -196,19 +244,16 @@ detectors:
<ConfigTabs>
<TabItem value="ui">
Navigate to <NavPath path="Settings > System > Detectors and model" /> and select **EdgeTPU** from the detector type dropdown and click **Add** to add multiple detectors, specifying `pci:0` and `pci:1` as the device for each.
Navigate to <NavPath path="Settings > System > Detection models" /> and select **Coral EdgeTPU (PCIe)** from the **Hardware** dropdown and check each Coral the model should run on.
</TabItem>
<TabItem value="yaml">
```yaml
detectors:
coral1:
type: edgetpu
device: pci:0
coral2:
type: edgetpu
device: pci:1
models:
- devices:
- edgetpu:pci:0
- edgetpu:pci:1
```
</TabItem>
@@ -219,19 +264,16 @@ detectors:
<ConfigTabs>
<TabItem value="ui">
Navigate to <NavPath path="Settings > System > Detectors and model" /> and select **EdgeTPU** from the detector type dropdown and click **Add** to add multiple detectors with different device types (e.g., `usb` and `pci`).
Navigate to <NavPath path="Settings > System > Detection models" /> and select **Coral EdgeTPU (USB)** from the **Hardware** dropdown. USB and PCIe Corals are listed as separate hardware, so mixing the two on one model has to be done in YAML.
</TabItem>
<TabItem value="yaml">
```yaml
detectors:
coral_usb:
type: edgetpu
device: usb
coral_pci:
type: edgetpu
device: pci
models:
- devices:
- edgetpu:usb
- edgetpu:pci
```
</TabItem>
@@ -273,7 +315,7 @@ Hailo8 supports all models in the Hailo Model Zoo that include HailoRT post-proc
## OpenVINO Detector
The OpenVINO detector type runs an OpenVINO IR model on AMD and Intel CPUs, Intel GPUs and Intel NPUs. To configure an OpenVINO detector, set the `"type"` attribute to `"openvino"`.
The OpenVINO detector type runs an OpenVINO IR model on AMD and Intel CPUs, Intel GPUs and Intel NPUs. To use it, prefix a model's device with `openvino`.
The OpenVINO device to be used is specified using the `"device"` attribute according to the naming conventions in the [Device Documentation](https://docs.openvino.ai/2025/openvino-workflow/running-inference/inference-devices-and-modes.html). The most common devices are `CPU`, `GPU`, or `NPU`.
@@ -286,13 +328,10 @@ OpenVINO is supported on 6th Gen Intel platforms (Skylake) and newer. It will al
When using many cameras one detector may not be enough to keep up. Multiple detectors can be defined assuming GPU resources are available. An example configuration would be:
```yaml
detectors:
ov_0:
type: openvino
device: GPU # or NPU
ov_1:
type: openvino
device: GPU # or NPU
models:
- devices:
- openvino:GPU # or NPU
- openvino:GPU # or NPU
```
:::
@@ -313,6 +352,12 @@ Intel NPUs cannot be used under Home Assistant OS, which does not include the NP
## Apple Silicon detector
:::warning
The network-based detectors (Deepstack and the Apple Silicon client) are being reworked. Their extra options no longer have a place in the config, so only the endpoint carried in the device string is honored right now: Deepstack ignores `api_key` and `api_timeout`, and the Apple Silicon client ignores `request_timeout_ms` and `linger_ms`. Anything else is dropped when your config is migrated.
:::
The NPU in Apple Silicon can't be accessed from within a container, so the [Apple Silicon detector client](https://github.com/frigate-nvr/apple-silicon-detector) must first be setup. It is recommended to use the Frigate docker image with `-standard-arm64` suffix, for example `ghcr.io/blakeblackshear/frigate:stable-standard-arm64`.
### Setup {#setup-apple-silicon}
@@ -453,11 +498,10 @@ If the correct build is used for your GPU then the GPU will be detected and used
When using many cameras one detector may not be enough to keep up. Multiple detectors can be defined assuming GPU resources are available. An example configuration would be:
```yaml
detectors:
onnx_0:
type: onnx
onnx_1:
type: onnx
models:
- devices:
- onnx
- onnx
```
:::
@@ -470,7 +514,7 @@ detectors:
## CPU Detector (not recommended)
The CPU detector type runs a TensorFlow Lite model utilizing the CPU without hardware acceleration. It is recommended to use a hardware accelerated detector type instead for better performance. To configure a CPU based detector, set the `"type"` attribute to `"cpu"`.
The CPU detector type runs a TensorFlow Lite model utilizing the CPU without hardware acceleration. It is recommended to use a hardware accelerated detector type instead for better performance. To use it, set a model's device to `cpu`.
:::danger
@@ -480,7 +524,7 @@ The CPU detector is not recommended for general use. If you do not have GPU or E
The number of threads used by the interpreter can be specified using the `"num_threads"` attribute, and defaults to `3.`
A TensorFlow Lite model is provided in the container at `/cpu_model.tflite` and is used by this detector type by default. To provide your own model, bind mount the file into the container and provide the path with `model.path`.
A TensorFlow Lite model is provided in the container at `/cpu_model.tflite` and is used by this detector type by default. To provide your own model, bind mount the file into the container and provide the path with the model's `path`.
### Configuration {#configuration-cpu}
@@ -490,6 +534,12 @@ When using CPU detectors, you can add one CPU detector per camera. Adding more d
## Deepstack / CodeProject.AI Server Detector
:::warning
The network-based detectors (Deepstack and the Apple Silicon client) are being reworked. Their extra options no longer have a place in the config, so only the endpoint carried in the device string is honored right now: Deepstack ignores `api_key` and `api_timeout`, and the Apple Silicon client ignores `request_timeout_ms` and `linger_ms`. Anything else is dropped when your config is migrated.
:::
The Deepstack / CodeProject.AI Server detector for Frigate allows you to integrate Deepstack and CodeProject.AI object detection capabilities into Frigate. CodeProject.AI and DeepStack are open-source AI platforms that can be run on various devices such as the Raspberry Pi, Nvidia Jetson, and other compatible hardware. It is important to note that the integration is performed over the network, so the inference times may not be as fast as native Frigate detectors, but it still provides an efficient and reliable solution for object detection and tracking.
### Setup {#setup-deepstack}
@@ -552,7 +602,7 @@ For detailed instructions on compiling models, refer to the [MemryX Compiler](ht
3. Depending on the model, the compiler may also generate a cropped post-processing network. If present, it will be named with the suffix `_post.onnx`.
4. Bind-mount the `.zip` file into the container and specify its path using `model.path` in your config.
4. Bind-mount the `.zip` file into the container and specify its path using the model's `path` in your config.
5. Update `labelmap_path` to match your custom model's labels.
@@ -682,13 +732,10 @@ If no custom model is provided, the RKNN detector downloads a default model from
When using many cameras one detector may not be enough to keep up. Multiple detectors can be defined assuming NPU resources are available. An example configuration would be:
```yaml
detectors:
rknn_0:
type: rknn
num_cores: 0
rknn_1:
type: rknn
num_cores: 0
models:
- devices:
- rknn:0
- rknn:0
```
:::
+16 -21
View File
@@ -204,8 +204,8 @@ You need to refer to **Configure hardware acceleration** above to enable the con
<ConfigTabs>
<TabItem value="ui">
1. Navigate to <NavPath path="Settings > System > Detectors and model" /> and add a detector with **Type** `OpenVINO` and **Device** `GPU`
2. On the same page, in the **Custom Model** tab, configure the model settings for OpenVINO:
1. Navigate to <NavPath path="Settings > System > Detection models" /> and select **Intel GPU** from the **Hardware** dropdown
2. On the same model, open the **Custom Model** tab and configure the model settings for OpenVINO:
| Field | Value |
| ---------------------------------------- | ------------------------------------------ |
@@ -222,15 +222,12 @@ You need to refer to **Configure hardware acceleration** above to enable the con
```yaml {3-6,9-15,20-21}
mqtt: ...
detectors: # <---- add detectors
ov:
type: openvino # <---- use openvino detector
device: GPU
# We will use the default MobileNet_v2 model from OpenVINO.
model:
width: 300
height: 300
models: # <---- add models
- devices:
- openvino:GPU # <---- use the openvino detector on the GPU
# We will use the default MobileNet_v2 model from OpenVINO.
width: 300
height: 300
input_tensor: nhwc
input_pixel_format: bgr
path: /openvino-model/ssdlite_mobilenet_v2.xml
@@ -273,7 +270,7 @@ services:
<ConfigTabs>
<TabItem value="ui">
Navigate to <NavPath path="Settings > System > Detectors and model" /> and add a detector with **Type** `EdgeTPU` and **Device** `usb`.
Navigate to <NavPath path="Settings > System > Detection models" /> and select **Coral EdgeTPU (USB)** from the **Hardware** dropdown.
</TabItem>
<TabItem value="yaml">
@@ -281,10 +278,9 @@ Navigate to <NavPath path="Settings > System > Detectors and model" /> and add a
```yaml {3-6,11-12}
mqtt: ...
detectors: # <---- add detectors
coral:
type: edgetpu
device: usb
models: # <---- add models
- devices:
- edgetpu:usb
cameras:
name_of_your_camera:
@@ -321,10 +317,9 @@ If you are using YAML to configure Frigate instead of the UI, your configuration
mqtt:
enabled: False
detectors:
coral:
type: edgetpu
device: usb
models:
- devices:
- edgetpu:usb
cameras:
name_of_your_camera:
@@ -357,7 +352,7 @@ In order to review activity in the Frigate UI, recordings need to be enabled.
```yaml {16-17}
mqtt: ...
detectors: ...
models: ...
cameras:
name_of_your_camera:
+11 -11
View File
@@ -59,13 +59,12 @@ You can view all of your submitted images at [https://plus.frigate.video](https:
Once you have [requested your first model](../plus/first_model.md) and gotten your own model ID, it 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.
You can either choose the new model from the <NavPath path="Settings > System > Detectors and model" /> pane in the Frigate UI (the **Frigate+ Model** tab), or manually set the model at the root level in your config:
You can either choose the new model from the <NavPath path="Settings > System > Detection models" /> pane in the Frigate UI (on the **Frigate+** tab of the model you want to change), or set it on that model in your config:
```yaml
detectors: ...
model:
path: plus://<your_model_id>
models:
- devices: ...
path: plus://<your_model_id>
```
:::note
@@ -79,10 +78,11 @@ Models are downloaded into the `/config/model_cache` folder and only downloaded
If needed, you can override the labelmap for Frigate+ models. This is not recommended as renaming labels will break the Submit to Frigate+ feature if the labels are not available in Frigate+.
```yaml
model:
path: plus://<your_model_id>
labelmap:
3: animal
4: animal
5: animal
models:
- devices: ...
path: plus://<your_model_id>
labelmap:
3: animal
4: animal
5: animal
```
+4 -5
View File
@@ -30,16 +30,15 @@ Models available in Frigate+ can be used with a special model path. No other inf
<ConfigTabs>
<TabItem value="ui">
Navigate to <NavPath path="Settings > System > Detectors and model" />. In the **Detection Model** section, choose the **Frigate+** tab. Select your new Frigate+ model from the **Available Frigate+ models** dropdown, then click **Save**. Restart Frigate to apply the change.
Navigate to <NavPath path="Settings > System > Detection models" />. On the model you want to change, choose the **Frigate+** tab and select your new Frigate+ model from the **Available Frigate+ models** dropdown, then click **Save**. Restart Frigate to apply the change.
</TabItem>
<TabItem value="yaml">
```yaml
detectors: ...
model:
path: plus://<your_model_id>
models:
- devices: ...
path: plus://<your_model_id>
```
:::tip
+1 -1
View File
@@ -131,7 +131,7 @@ The process was killed by the CPU for executing an unsupported instruction. Ther
<FaqItem id="onnx-invalidprotobuf" question="ONNX Runtime InvalidProtobuf / failed to load model">
ONNX Runtime could not parse the model file. The file exists but its contents are not a valid ONNX model, usually a corrupted or interrupted download in `model_cache`, or the wrong file pointed at by `model.path`. Delete the cached model file so Frigate re-downloads it, and confirm `model.path` points at an actual `.onnx` model. See [ONNX detector configuration](/configuration/object_detectors#onnx).
ONNX Runtime could not parse the model file. The file exists but its contents are not a valid ONNX model, usually a corrupted or interrupted download in `model_cache`, or the wrong file pointed at by a model's `path`. Delete the cached model file so Frigate re-downloads it, and confirm the model's `path` points at an actual `.onnx` model. See [ONNX detector configuration](/configuration/object_detectors#onnx).
</FaqItem>
+41 -1
View File
@@ -3,7 +3,31 @@ id: cpu
title: High CPU Usage
---
High CPU usage can impact Frigate's performance and responsiveness. This guide outlines the most effective configuration changes to help reduce CPU consumption and optimize resource usage.
High CPU usage can impact Frigate's performance and responsiveness. This guide explains how to interpret the CPU values Frigate reports and outlines the most effective configuration changes to help reduce CPU consumption and optimize resource usage.
## Understanding Frigate's Reported CPU Usage
Frigate's CPU percentages often look much higher than what the host reports. Usually both numbers are correct and are simply measured against different denominators, so confirm you actually have a problem before tuning anything.
### Per-process values are relative to a single core
The values Frigate reports for FFmpeg, capture, detect, detector, and other processes follow the same convention as `top`: 100% means one CPU core is fully saturated, not that the whole system is saturated. A multithreaded process such as FFmpeg can legitimately report well over 100%.
Host and hypervisor tools instead report a percentage of the machine's total capacity across all cores. This includes `docker stats`, the `htop` summary, the Proxmox summary graph, the Unraid dashboard, Synology Resource Monitor, and Home Assistant's system monitor sensors. To reconcile the two:
```
host percentage ≈ (sum of Frigate's process percentages) / (number of cores)
```
On a 4 core system, an FFmpeg process reporting 100% is consuming one quarter of the machine, so the host will show roughly 25 to 30% once the remaining Frigate processes are included. That same 100% on a 16 core system is about 6%. Frigate's own warning thresholds use the per-core convention as well, so an FFmpeg process is flagged at 20% of a single core, not 20% of the system.
### Instantaneous samples and averages measure different things
Frigate collects stats every 15 seconds, and the `cpu` value covers only the interval since the previous collection. The `cpu_average` value in the stats API and MQTT payload is the average across the entire life of the process, and it is what the high CPU usage warnings are based on. Host dashboards generally plot data averaged over a longer window, so a single Frigate sample can show a peak that a host graph never displays. A process that has just started, such as FFmpeg after a camera reconnect, reports 0 until it has been sampled twice.
### The system-wide value depends on what the container can see
The system CPU value is read from `/proc/stat`. Under Docker that file belongs to the host, so the value covers the entire machine including workloads unrelated to Frigate, and it will not match `docker stats` for the Frigate container. Under an LXC container, lxcfs virtualizes `/proc/stat` and the value reflects only the cores assigned to the container. In a virtual machine, the guest sees only its assigned vCPUs while the hypervisor divides by every physical thread on the node, so guest and host percentages will not agree even when both are accurate.
## 1. Hardware Acceleration for Video Decoding
@@ -72,3 +96,19 @@ The model you use significantly impacts detector performance. Frigate provides d
- Larger models (640x640): Slower inference, can sometimes have higher accuracy on very large objects that take up a majority of the frame.
For more detail on picking the right size, see [Choosing a model size](../configuration/object_detectors.md#choosing-a-model-size).
## 3. Reducing Detector CPU Usage
**Priority: High**
The **Detector CPU Usage** metric measures the CPU spent converting frames into the tensor format the model expects and post-processing the model's output. It does not include inference, so this value can be high even when you've configured a GPU, NPU, or Coral for object detection.
This metric scales with how many detections per second Frigate runs and how expensive each one is to prepare. Tuning [motion detection](../configuration/motion_detection) is usually the first recommendation to reduce the number of detections. Additionally, you can:
- **Lower `detect -> fps`.** 5 is the recommended value for nearly all cameras. Running at 10 doubles the frames eligible for detection and is one of the largest contributors to this metric.
- **Use a 320x320 model.** A 640x640 model has 4 times as many pixels to transpose, convert, and copy on every inference.
- **Prefer a model that takes integer input.** Models configured with `input_dtype: float` require each frame to be converted to float32 and normalized on the CPU first. Models taking `int` input, such as the tflite models used by the Edge TPU, skip that step.
- **Do not match the detect resolution to the model resolution.** The detect stream should match your camera's aspect ratio, for example `1280x720`, not the model's input size. Frigate crops and scales regions of motion itself, so an oversized detect stream only adds work.
- **Tune stationary object behavior.** Objects that never settle into a stationary state are re-detected continuously. Raising `detect -> stationary -> interval` reduces how often detection runs on objects that are already parked. See [stationary objects](../configuration/stationary_objects).
Adding [more detector instances](#multiple-detector-instances) spreads this work across more CPU cores, but does not reduce the total CPU used.
+6
View File
@@ -133,6 +133,12 @@ cameras:
height: 720
```
### What is the `version` key in my config file?
`version` records the config format that your config was last migrated to. On startup Frigate compares it against the format the running version expects, and if it is older it copies your config to `/config/backup_config.yaml`, rewrites it to the new format, and updates `version` as the final step. A config with no `version` key is assumed to predate 0.14 and is migrated from there.
Frigate manages this key for you, so do not set or edit it. Raising it makes Frigate skip migrations your config still needs, and lowering it re-runs migrations against config that has already been converted. Either can leave you with a config that no longer validates.
### Why does Frigate keep creating new tracked objects for my parked car?
Stationary tracking is designed to _prevent_ this: a parked car should remain a single tracked object rather than generating new ones. If you're repeatedly getting new tracked objects for the same car, it's likely that Frigate is losing the object and re-detecting it as a new one.
+2 -2
View File
@@ -40,7 +40,7 @@ Deleting a group also clears any custom layout you saved for it.
## Rearranging a camera group layout
On desktop and tablet, each camera group has its own freely-arrangeable grid. Enter **Edit Layout** mode from the layout button in the lower-right corner: camera tiles gain a drag handle and corner resize handles. Drag a tile to reposition it and drag a corner to resize it (the aspect ratio is preserved). Exit edit mode to save. The layout is stored in your browser per device, so each device can have its own arrangement.
On desktop and tablet, each camera group has its own freely-arrangeable grid. Enter **Edit Layout** mode from the layout button in the lower-right corner: camera tiles gain a drag handle and corner resize handles. Drag a tile to reposition it and drag a corner to resize it (the aspect ratio is preserved). Exit edit mode to save. The layout is stored in your browser per device, so each device can have its own arrangement, and layouts can be exported to a file and imported on another device.
The default **All Cameras** dashboard is not manually arrangeable. It automatically sizes tiles based on each camera's aspect ratio (wide cameras span two columns, tall cameras span two rows).
@@ -68,7 +68,7 @@ For non-default groups, the context menu also exposes **Streaming Settings** for
- the **streaming method**: **No Streaming**, **Smart Streaming** (recommended), or **Continuous Streaming** (higher bandwidth), and
- **compatibility mode**, for devices that have trouble rendering the default player.
These settings are saved per group and per device in your browser, not in your config file.
These settings are saved per group and per device in your browser, not in your config file, and can be exported to a file and imported on another device.
## The single-camera view
+1 -2
View File
@@ -63,8 +63,7 @@ SYSTEM_NAV: dict[str, tuple[str, str]] = {
"environment_vars": ("System", "Environment variables"),
"telemetry": ("System", "Telemetry"),
"birdseye": ("System", "Birdseye"),
"detectors": ("System", "Detectors and model"),
"model": ("System", "Detectors and model"),
"models": ("System", "Detection models"),
}
# All known top-level config section keys
+104 -1
View File
@@ -4010,6 +4010,49 @@ paths:
security:
- frigateAdminAuth: []
x-required-role: admin
/hardware/probe:
get:
tags:
- Hardware
summary: Probe Hardware
description: |-
**Access:** Admin role required.
Get the object detection hardware attached to this system.
Args:
refresh: Probe again instead of returning the cached result
Returns:
Every kind of detection hardware that was found
operationId: probe_hardware_hardware_probe_get
parameters:
- name: refresh
in: query
required: false
schema:
type: boolean
default: false
title: Refresh
responses:
'200':
description: Successful Response
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/DetectionHardware'
title: Response Probe Hardware Hardware Probe Get
'422':
description: Validation Error
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPValidationError'
security:
- frigateAdminAuth: []
x-required-role: admin
/events:
get:
tags:
@@ -7263,7 +7306,9 @@ paths:
schema:
$ref: '#/components/schemas/DebugReplayStartResponse'
'400':
description: Invalid camera, time range, or no recordings
description: Invalid camera or time range
'404':
description: No recordings in the requested time range
'409':
description: A replay session is already active
'422':
@@ -7840,6 +7885,46 @@ components:
required:
- ids
title: DeleteFaceImagesBody
DetectionHardware:
properties:
key:
type: string
title: Hardware key
description: Stable identifier for this kind of hardware.
detector:
type: string
title: Detector type
description: The detector that drives this hardware.
name:
type: string
title: Hardware name
description: Human readable name for this kind of hardware.
units:
items:
$ref: '#/components/schemas/HardwareUnit'
type: array
title: Units
description: Each physical piece of this hardware that was found.
count:
type: integer
title: Unit count
description: How many units were found.
unlimited:
type: boolean
title: Unlimited detectors
description: Whether this hardware can run more inference processes
than there are units.
type: object
required:
- key
- detector
- name
- units
- count
- unlimited
title: DetectionHardware
description: A kind of detection hardware, and every unit of it that was
found.
EventCreateResponse:
properties:
success:
@@ -8567,6 +8652,24 @@ components:
title: Detail
type: object
title: HTTPValidationError
HardwareUnit:
properties:
device:
type: string
title: Device string
description: The value to put in a model's devices list, for example
'edgetpu:pci:1'.
label:
type: string
title: Unit label
description: How to identify this unit among others of the same kind,
for example 'PCIe 1'.
type: object
required:
- device
- label
title: HardwareUnit
description: One physical piece of hardware.
Last24HoursReview:
properties:
reviewed_alert:
+35 -29
View File
@@ -292,10 +292,6 @@ def config(request: Request):
config: dict[str, dict[str, Any]] = config_obj.model_dump(
mode="json", warnings="none", exclude_none=True
)
config["detectors"] = {
name: detector.model_dump(mode="json", warnings="none", exclude_none=True)
for name, detector in config_obj.detectors.items()
}
# remove environment_vars for non-admin users
if request.headers.get("remote-role") != "admin":
@@ -376,31 +372,28 @@ def config(request: Request):
config["go2rtc"]["streams"][stream_name] = cleaned
config["plus"] = {"enabled": request.app.frigate_config.plus_api.is_active()}
config["model"]["colormap"] = config_obj.model.colormap
config["model"]["all_attributes"] = config_obj.model.all_attributes
config["model"]["non_logo_attributes"] = config_obj.model.non_logo_attributes
# Add model plus data if plus is enabled
if config["plus"]["enabled"]:
model_path = config.get("model", {}).get("path")
if model_path:
model_json_path = FilePath(model_path).with_suffix(".json")
for index, model in enumerate(config_obj.models):
model_dict = config["models"][index]
model_dict["colormap"] = model.colormap
model_dict["all_attributes"] = model.all_attributes
model_dict["non_logo_attributes"] = model.non_logo_attributes
model_dict["labelmap"] = model.merged_labelmap
if not config["plus"]["enabled"]:
continue
# Add model plus data if plus is enabled
model_dict["plus"] = None
if model.path:
model_json_path = FilePath(model.path).with_suffix(".json")
try:
with open(model_json_path) as f:
model_plus_data = json.load(f)
config["model"]["plus"] = model_plus_data
except FileNotFoundError:
config["model"]["plus"] = None
except json.JSONDecodeError:
config["model"]["plus"] = None
else:
config["model"]["plus"] = None
# use merged labelamp
for detector_config in config["detectors"].values():
detector_config["model"]["labelmap"] = (
request.app.frigate_config.model.merged_labelmap
)
model_dict["plus"] = json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
pass
return JSONResponse(content=config)
@@ -1337,8 +1330,18 @@ def categorized_object_names(
@router.get("/audio_labels", dependencies=[Depends(allow_any_authenticated())])
def get_audio_labels():
def get_audio_labels(request: Request):
labels = load_labels("/audio-labelmap.txt", prefill=521)
# configured overrides group several audio classes under one label, and the
# detector merges them over the defaults at runtime. Offer them here too, or
# a grouped label could never be picked in the UI.
config: FrigateConfig = request.app.frigate_config
labels.update(config.audio.labelmap)
for camera in config.cameras.values():
labels.update(camera.audio.labelmap)
return JSONResponse(content=labels)
@@ -1360,11 +1363,14 @@ def plusModels(request: Request, filterByCurrentModelDetector: bool = False):
modelList = models["list"]
config: FrigateConfig = request.app.frigate_config
primary_model = config.primary_model
# current model type
modelType = request.app.frigate_config.model.model_type
modelType = primary_model.model_type
# current detectorType for comparing to supportedDetectors
detectorType = list(request.app.frigate_config.detectors.values())[0].type
detectorType = config.devices_for_model(primary_model)[0].detector
validModels = []
+11 -1
View File
@@ -13,6 +13,7 @@ from frigate.api.auth import require_role
from frigate.api.defs.tags import Tags
from frigate.jobs.debug_replay import (
ExportDebugReplaySource,
NoRecordingsError,
RecordingDebugReplaySource,
start_debug_replay_job,
)
@@ -74,7 +75,8 @@ class DebugReplayStopResponse(BaseModel):
response_model=DebugReplayStartResponse,
status_code=202,
responses={
400: {"description": "Invalid camera, time range, or no recordings"},
400: {"description": "Invalid camera or time range"},
404: {"description": "No recordings in the requested time range"},
409: {"description": "A replay session is already active"},
},
dependencies=[Depends(require_role(["admin"]))],
@@ -113,6 +115,14 @@ async def start_debug_replay(request: Request, body: DebugReplayStartBody):
},
status_code=409,
)
except NoRecordingsError:
return JSONResponse(
content={
"success": False,
"message": "No recordings found in the selected time range",
},
status_code=404,
)
except ValueError:
logger.exception("Rejected debug replay start request")
return JSONResponse(
+1
View File
@@ -8,6 +8,7 @@ class Tags(Enum):
chat = "Chat"
events = "Events"
export = "Export"
hardware = "Hardware"
classification = "Classification"
logs = "Logs"
media = "Media"
+2
View File
@@ -21,6 +21,7 @@ from frigate.api import (
debug_replay,
event,
export,
hardware,
media,
motion_search,
notification,
@@ -145,6 +146,7 @@ def create_fastapi_app(
app.include_router(preview.router)
app.include_router(notification.router)
app.include_router(export.router)
app.include_router(hardware.router)
app.include_router(event.router)
app.include_router(media.router)
app.include_router(motion_search.router)
+30
View File
@@ -0,0 +1,30 @@
"""Hardware discovery APIs."""
import logging
from fastapi import APIRouter, Depends
from frigate.api.auth import require_role
from frigate.api.defs.tags import Tags
from frigate.detectors.hardware import DetectionHardware, hardware_prober
logger = logging.getLogger(__name__)
router = APIRouter(tags=[Tags.hardware])
@router.get(
"/hardware/probe",
response_model=list[DetectionHardware],
dependencies=[Depends(require_role(["admin"]))],
)
def probe_hardware(refresh: bool = False) -> list[DetectionHardware]:
"""Get the object detection hardware attached to this system.
Args:
refresh: Probe again instead of returning the cached result
Returns:
Every kind of detection hardware that was found
"""
return hardware_prober.probe(refresh=refresh)
+1 -1
View File
@@ -941,7 +941,7 @@ async def event_snapshot(
timestamp_style=request.app.frigate_config.cameras[
event.camera
].timestamp_style,
colormap=request.app.frigate_config.model.colormap,
colormap=request.app.frigate_config.model_for_camera(event.camera).colormap,
)
except DoesNotExist:
# see if the object is currently being tracked
+39 -22
View File
@@ -49,6 +49,8 @@ from frigate.debug_replay import (
DebugReplayManager,
cleanup_replay_cameras,
)
from frigate.detectors.detector_config import SceneEnum
from frigate.detectors.device import build_detector_config, runner_names
from frigate.embeddings import EmbeddingProcess, EmbeddingsContext
from frigate.events.audio import AudioProcessor
from frigate.events.cleanup import EventCleanup
@@ -69,6 +71,7 @@ from frigate.models import (
User,
)
from frigate.object_detection.base import ObjectDetectProcess
from frigate.object_detection.util import detection_frame_size
from frigate.output.output import OutputProcess
from frigate.ptz.autotrack import PtzAutoTrackerThread
from frigate.ptz.onvif import OnvifController
@@ -98,7 +101,9 @@ class FrigateApp:
self.metrics_manager = manager
self.audio_process: mp.Process | None = None
self.stop_event = stop_event
self.detection_queue: Queue = mp.Queue()
self.detection_queues: dict[SceneEnum, Queue] = {
model.scene: mp.Queue() for model in config.models
}
self.detectors: dict[str, ObjectDetectProcess] = {}
self.detection_shms: list[mp.shared_memory.SharedMemory] = []
self.log_queue: Queue = mp.Queue()
@@ -344,20 +349,19 @@ class FrigateApp:
self.dispatcher.profile_manager = self.profile_manager
def start_detectors(self) -> None:
model_cameras: dict[SceneEnum, list[str]] = {
model.scene: [] for model in self.config.models
}
for name in self.config.cameras.keys():
model = self.config.model_for_camera(name)
model_cameras[model.scene].append(name)
try:
largest_frame = max(
[
det.model.height * det.model.width * 3
if det.model is not None
else 320
for det in self.config.detectors.values()
]
)
shm_in = UntrackedSharedMemory(
name=name,
create=True,
size=largest_frame,
size=detection_frame_size(model),
)
except FileExistsError:
shm_in = UntrackedSharedMemory(name=name)
@@ -372,15 +376,26 @@ class FrigateApp:
self.detection_shms.append(shm_in)
self.detection_shms.append(shm_out)
for name, detector_config in self.config.detectors.items():
self.detectors[name] = ObjectDetectProcess(
name,
self.detection_queue,
list(self.config.cameras.keys()),
self.config,
detector_config,
self.stop_event,
)
# a device may be listed more than once to run additional inference
# processes on it, so names are only unique once de-duplicated
all_devices = [
device
for model in self.config.models
for device in self.config.devices_for_model(model)
]
names = iter(runner_names(all_devices))
for model in self.config.models:
for device in self.config.devices_for_model(model):
name = next(names)
self.detectors[name] = ObjectDetectProcess(
name,
self.detection_queues[model.scene],
model_cameras[model.scene],
self.config,
build_detector_config(device, model),
self.stop_event,
)
def start_ptz_autotracker(self) -> None:
self.ptz_autotracker_thread = PtzAutoTrackerThread(
@@ -411,7 +426,7 @@ class FrigateApp:
def start_camera_processor(self) -> None:
self.camera_maintainer = CameraMaintainer(
self.config,
self.detection_queue,
self.detection_queues,
self.detected_frames_queue,
self.camera_metrics,
self.ptz_metrics,
@@ -675,8 +690,10 @@ class FrigateApp:
for detector in self.detectors.values():
detector.stop()
empty_and_close_queue(self.detection_queue)
logger.info("Detection queue closed")
for detection_queue in self.detection_queues.values():
empty_and_close_queue(detection_queue)
logger.info("Detection queues closed")
self.detected_frames_processor.join()
empty_and_close_queue(self.detected_frames_queue)
+2 -1
View File
@@ -18,6 +18,7 @@ from frigate.config.camera.updater import (
CameraConfigUpdateEnum,
CameraConfigUpdateSubscriber,
)
from frigate.detectors.detector_config import NON_LOGO_ATTRIBUTES
logger = logging.getLogger(__name__)
@@ -178,7 +179,7 @@ class CameraActivityManager:
return
for label in camera_config.objects.track:
if label in self.config.model.non_logo_attributes:
if label in NON_LOGO_ATTRIBUTES:
continue
new_count = all_objects[label]
+12 -16
View File
@@ -15,7 +15,9 @@ from frigate.config.camera.updater import (
CameraConfigUpdateSubscriber,
)
from frigate.const import REPLAY_CAMERA_PREFIX
from frigate.detectors.detector_config import SceneEnum
from frigate.models import Regions
from frigate.object_detection.util import detection_frame_size
from frigate.util.builtin import empty_and_close_queue
from frigate.util.image import SharedMemoryFrameManager, UntrackedSharedMemory
from frigate.util.object import get_camera_regions_grid
@@ -29,7 +31,7 @@ class CameraMaintainer(threading.Thread):
def __init__(
self,
config: FrigateConfig,
detection_queue: Queue,
detection_queues: dict[SceneEnum, Queue],
detected_frames_queue: Queue,
camera_metrics: DictProxy,
ptz_metrics: dict[str, PTZMetrics],
@@ -38,7 +40,7 @@ class CameraMaintainer(threading.Thread):
):
super().__init__(name="camera_processor")
self.config = config
self.detection_queue = detection_queue
self.detection_queues = detection_queues
self.detected_frames_queue = detected_frames_queue
self.stop_event = stop_event
self.camera_metrics = camera_metrics
@@ -79,10 +81,11 @@ class CameraMaintainer(threading.Thread):
# create or update region grids for each camera
for camera in self.config.cameras.values():
assert camera.name is not None
model = self.config.model_for_camera(camera.name)
self.region_grids[camera.name] = get_camera_regions_grid(
camera.name,
camera.detect,
max(self.config.model.width, self.config.model.height),
max(model.width, model.height),
)
def __calculate_shm_frame_count(self) -> int:
@@ -114,6 +117,7 @@ class CameraMaintainer(threading.Thread):
return
camera_stop_event = self.__ensure_camera_stop_event(name)
model = self.config.model_for_camera(name)
if runtime:
self.camera_metrics[name] = CameraMetrics(self.metrics_manager)
@@ -123,32 +127,24 @@ class CameraMaintainer(threading.Thread):
self.region_grids[name] = get_camera_regions_grid(
name,
config.detect,
max(self.config.model.width, self.config.model.height),
max(model.width, model.height),
)
try:
largest_frame = max(
[
det.model.height * det.model.width * 3
if det.model is not None
else 320
for det in self.config.detectors.values()
]
)
UntrackedSharedMemory(name=f"out-{name}", create=True, size=20 * 6 * 4)
UntrackedSharedMemory(
name=name,
create=True,
size=largest_frame,
size=detection_frame_size(model),
)
except FileExistsError:
pass
camera_process = CameraTracker(
config,
self.config.model,
self.config.model.merged_labelmap,
self.detection_queue,
model,
model.merged_labelmap,
self.detection_queues[model.scene],
self.detected_frames_queue,
self.camera_metrics[name],
self.ptz_metrics[name],
+6 -11
View File
@@ -40,6 +40,7 @@ class CameraState:
self.name = name
self.config = config
self.camera_config = config.cameras[name]
self.model = config.model_for_camera(name)
self.frame_manager = frame_manager
self.best_objects: dict[str, TrackedObject] = {}
self.tracked_objects: dict[str, TrackedObject] = {}
@@ -101,9 +102,7 @@ class CameraState:
thickness = 1
else:
thickness = 2
color = self.config.model.colormap.get(
obj["label"], (255, 255, 255)
)
color = self.model.colormap.get(obj["label"], (255, 255, 255))
else:
thickness = 1
color = (255, 0, 0)
@@ -125,9 +124,7 @@ class CameraState:
and obj["frame_time"] == frame_time
):
thickness = 5
color = self.config.model.colormap.get(
obj["label"], (255, 255, 255)
)
color = self.model.colormap.get(obj["label"], (255, 255, 255))
# debug autotracking zooming - show the zoom factor box
if (
@@ -261,9 +258,7 @@ class CameraState:
if draw_options.get("paths"):
for obj in tracked_objects.values():
if obj["frame_time"] == frame_time and obj["path_data"]:
color = self.config.model.colormap.get(
obj["label"], (255, 255, 255)
)
color = self.model.colormap.get(obj["label"], (255, 255, 255))
path_points = [
(
@@ -366,7 +361,7 @@ class CameraState:
for id in new_ids:
logger.debug(f"{self.name}: New tracked object ID: {id}")
new_obj = tracked_objects[id] = TrackedObject(
self.config.model,
self.model,
self.camera_config,
self.config.ui,
self.frame_cache,
@@ -510,7 +505,7 @@ class CameraState:
sub_label = None
if obj.obj_data.get("sub_label"):
if obj.obj_data["sub_label"][0] in self.config.model.all_attributes:
if obj.obj_data["sub_label"][0] in self.model.all_attributes:
label = obj.obj_data["sub_label"][0]
else:
label = f"{object_type}-verified"
+2 -1
View File
@@ -261,10 +261,11 @@ class Dispatcher:
if camera not in self.config.cameras:
return None
model = self.config.model_for_camera(camera)
grid = get_camera_regions_grid(
camera,
self.config.cameras[camera].detect,
max(self.config.model.width, self.config.model.height),
max(model.width, model.height),
)
return grid
+5 -1
View File
@@ -83,7 +83,9 @@ class WebPushClient(Communicator):
# notification and auth config updater
self.global_config_subscriber = ConfigSubscriber("config/")
self.config_subscriber = CameraConfigUpdateSubscriber(
self.config, self.config.cameras, [CameraConfigUpdateEnum.notifications]
self.config,
self.config.cameras,
[CameraConfigUpdateEnum.add, CameraConfigUpdateEnum.notifications],
)
self._refresh_user_cameras()
@@ -217,6 +219,8 @@ class WebPushClient(Communicator):
self.suspended_cameras[camera] = 0
self.last_camera_notification_time[camera] = 0
self._refresh_user_cameras()
if topic == "reviews":
decoded = json.loads(payload)
camera = decoded["before"]["camera"]
+7
View File
@@ -1,5 +1,7 @@
from pydantic import Field, model_validator
from frigate.detectors.detector_config import SceneEnum
from ..base import FrigateBaseModel
__all__ = ["DetectConfig", "StationaryConfig", "StationaryMaxFramesConfig"]
@@ -60,6 +62,11 @@ class DetectConfig(FrigateBaseModel):
title="Detect width",
description="Width (pixels) of frames used for the detect stream; leave empty to use the native stream resolution.",
)
scene: SceneEnum = Field(
default=SceneEnum.all,
title="Detect scene",
description="The environment this camera looks at, used to pick which of the configured models runs on it. Cameras left on 'all' run the model configured with a scene of 'all'.",
)
fps: int = Field(
default=5,
title="Detect FPS",
+1
View File
@@ -96,6 +96,7 @@ class CameraConfigUpdateSubscriber:
return
elif update_type == CameraConfigUpdateEnum.remove:
self.config.cameras.pop(camera, None)
self.config.drop_camera_model(camera)
self.camera_configs.pop(camera, None)
return
+250 -59
View File
@@ -11,7 +11,6 @@ from pydantic import (
BaseModel,
ConfigDict,
Field,
TypeAdapter,
ValidationInfo,
field_validator,
model_validator,
@@ -19,8 +18,9 @@ from pydantic import (
from ruamel.yaml import YAML
from frigate.const import REGEX_JSON
from frigate.detectors import DetectorConfig, ModelConfig
from frigate.detectors.detector_config import BaseDetectorConfig
from frigate.detectors import ModelConfig
from frigate.detectors.detector_config import SceneEnum
from frigate.detectors.device import DeviceParseError, DeviceSpec, parse_device
from frigate.plus import PlusApi
from frigate.util.builtin import (
deep_merge,
@@ -79,9 +79,14 @@ logger = logging.getLogger(__name__)
yaml = YAML()
# Pydantic field default applied when an existing config omits `detectors:`.
# Pydantic field default applied when an existing config omits `models:`.
# Kept as cpu tflite for backwards compatibility with 0.17 configs.
DEFAULT_DETECTORS = {"cpu": {"type": "cpu"}}
DEFAULT_MODELS = [{"devices": ["cpu"]}]
def _default_models() -> list[ModelConfig]:
return [ModelConfig.model_validate(model) for model in DEFAULT_MODELS]
# Used by the openvino branch below and rendered into the new-config YAML
# template so first-time setups default to openvino on CPU.
@@ -93,7 +98,7 @@ DEFAULT_MODEL = {
"path": "/openvino-model/ssdlite_mobilenet_v2.xml",
"labelmap_path": "/openvino-model/coco_91cl_bkgr.txt",
}
NEW_CONFIG_DETECTORS = {"ov": {"type": "openvino", "device": "CPU"}}
NEW_CONFIG_MODELS = [{"devices": ["openvino:CPU"], **DEFAULT_MODEL}]
DEFAULT_DETECT_DIMENSIONS = {"width": 1280, "height": 720}
@@ -109,7 +114,7 @@ DEFAULT_CONFIG = f"""
mqtt:
enabled: False
{_render_default_yaml({"detectors": NEW_CONFIG_DETECTORS, "model": DEFAULT_MODEL})}
{_render_default_yaml({"models": NEW_CONFIG_MODELS})}
cameras: {{}} # No cameras defined, UI wizard should be used
version: {CURRENT_CONFIG_VERSION}
"""
@@ -520,16 +525,11 @@ class FrigateConfig(FrigateBaseModel):
description="User interface preferences such as timezone, time/date formatting, and units.",
)
# Detector config
detectors: dict[str, BaseDetectorConfig] = Field(
default=DEFAULT_DETECTORS,
title="Detector hardware",
description="Configuration for object detectors (CPU, GPU, ONNX backends) and any detector-specific model settings.",
)
model: ModelConfig = Field(
default_factory=ModelConfig,
title="Detection model",
description="Settings to configure a custom object detection model and its input shape.",
# Detection model config
models: list[ModelConfig] = Field(
default_factory=_default_models,
title="Detection models",
description="Object detection models and the hardware each one runs on. Cameras pick a model by matching their detect.scene against a model's scene.",
)
# GenAI config (named provider configs: name -> GenAIConfig)
@@ -644,11 +644,226 @@ class FrigateConfig(FrigateBaseModel):
)
_plus_api: PlusApi
_model_devices: dict[SceneEnum, list[DeviceSpec]]
_camera_models: dict[str, ModelConfig]
_all_attributes: list[str]
_all_attribute_logos: list[str]
_all_attributes_map: dict[str, list[str]]
_all_labels: set[str]
@property
def plus_api(self) -> PlusApi:
return self._plus_api
@property
def all_attributes(self) -> list[str]:
"""Every attribute label across all configured models."""
return self._all_attributes
@property
def all_attribute_logos(self) -> list[str]:
"""Every logo attribute label across all configured models."""
return self._all_attribute_logos
@property
def all_attributes_map(self) -> dict[str, list[str]]:
"""Object label to attribute labels, merged across all configured models."""
return self._all_attributes_map
@property
def all_labels(self) -> set[str]:
"""Every object label across all configured models."""
return self._all_labels
@property
def primary_model(self) -> ModelConfig:
"""The model used when no specific camera is in play."""
for model in self.models:
if model.scene == SceneEnum.all:
return model
return self.models[0]
def model_for_camera(self, camera_name: str) -> ModelConfig:
"""Get the detection model a camera runs on.
Cameras added at runtime (wizard, clone, debug replay) are inserted
into cameras after parse, so they miss the cache built during
post_validation and are resolved here on first lookup.
Args:
camera_name: Name of the camera
Returns:
The model matching the camera's detect scene
"""
model = self._camera_models.get(camera_name)
if model is None:
camera = self.cameras.get(camera_name)
scene = camera.detect.scene if camera is not None else SceneEnum.all
model = self._resolve_camera_model(camera_name, scene)
self._camera_models[camera_name] = model
return model
def drop_camera_model(self, camera_name: str) -> None:
"""Forget the cached model for a camera removed at runtime.
A later re-add resolves fresh, so a camera recreated under the same
name with a different detect scene doesn't inherit the removed
camera's model.
Args:
camera_name: Name of the removed camera
"""
self._camera_models.pop(camera_name, None)
def devices_for_model(self, model: ModelConfig) -> list[DeviceSpec]:
"""Get the parsed hardware devices a model runs on.
Args:
model: One of the configured models
Returns:
The parsed device specs, in config order
"""
return self._model_devices[model.scene]
def _load_model(self, model: ModelConfig, detector: str) -> ModelConfig:
"""Apply detector specific defaults to a model and load its weights and labels.
Args:
model: The configured model
detector: The detector type the model runs on
Returns:
The loaded model
"""
model_config = model.model_dump(exclude_unset=True, warnings="none")
if "path" not in model_config:
if detector == "cpu" or detector.endswith("_tfl"):
model_config["path"] = "/cpu_model.tflite"
elif detector == "edgetpu":
model_config["path"] = "/edgetpu_model.tflite"
elif detector == "openvino":
for default_key, default_value in DEFAULT_MODEL.items():
model_config.setdefault(default_key, default_value)
loaded = ModelConfig.model_validate(model_config)
loaded.check_and_load_plus_model(self.plus_api, detector)
loaded.compute_model_hash()
return loaded
def _load_models(self) -> None:
"""Validate the configured models and load each one."""
if not self.models:
raise ValueError("At least one model must be configured under models")
model_devices: dict[SceneEnum, list[DeviceSpec]] = {}
# device string -> the scene of the model that already claimed it
claimed_devices: dict[str, SceneEnum] = {}
for index, model in enumerate(self.models):
scene = model.scene.value
if model.scene in model_devices:
raise ValueError(
f"Multiple models are configured with a scene of '{scene}'. Each model must use a different scene."
)
if not model.devices:
raise ValueError(
f"Model '{scene}' must list at least one entry under devices."
)
try:
devices = [parse_device(device) for device in model.devices]
except DeviceParseError as err:
raise ValueError(
f"Model '{scene}' has an invalid device: {err}"
) from err
detectors = {device.detector for device in devices}
if len(detectors) > 1:
raise ValueError(
f"Model '{scene}' mixes the {', '.join(sorted(detectors))} detectors. All of a model's devices must use the same detector."
)
for device in devices:
if device.raw in claimed_devices and not device.shareable:
other = claimed_devices[device.raw]
where = (
f"twice by model '{scene}'"
if other == model.scene
else f"by both the '{other.value}' and '{scene}' models"
)
raise ValueError(
f"Device '{device.raw}' is used {where}, but it can only run one detection process."
)
claimed_devices[device.raw] = model.scene
self.models[index] = self._load_model(model, devices[0].detector)
model_devices[model.scene] = devices
attributes: set[str] = set()
attribute_logos: set[str] = set()
attributes_map: dict[str, set[str]] = {}
labels: set[str] = set()
for model in self.models:
attributes.update(model.all_attributes)
attribute_logos.update(model.all_attribute_logos)
labels.update(model.merged_labelmap.values())
for label, label_attributes in model.attributes_map.items():
attributes_map.setdefault(label, set()).update(label_attributes)
self._model_devices = model_devices
self._all_attributes = sorted(attributes)
self._all_attribute_logos = sorted(attribute_logos)
self._all_attributes_map = {
label: sorted(label_attributes)
for label, label_attributes in sorted(attributes_map.items())
}
self._all_labels = labels
def _resolve_camera_model(self, name: str, scene: SceneEnum) -> ModelConfig:
"""Resolve which model a camera runs on.
A camera may name a scene no model is configured for, which is valid as
long as an 'all' model is there to fall back to.
Args:
name: Name of the camera
scene: The camera's detect scene, which defaults to 'all'
Returns:
The model the camera runs on
"""
by_scene = {model.scene: model for model in self.models}
model = by_scene.get(scene)
if model is not None:
return model
default = by_scene.get(SceneEnum.all)
if default is None:
raise ValueError(
f"Camera '{name}' has a detect scene of '{scene.value}', but no model is configured for that scene or for 'all'."
)
logger.warning(
"Camera '%s' has a detect scene of '%s', but no model is configured for that scene, so the 'all' model is used",
name,
scene.value,
)
return default
@model_validator(mode="after")
def post_validation(self, info: ValidationInfo) -> Self:
# Load plus api from context, if possible.
@@ -693,8 +908,10 @@ class FrigateConfig(FrigateBaseModel):
"'embeddings' in its roles for semantic search."
)
self._load_models()
# set default min_score for object attributes
for attribute in self.model.all_attributes:
for attribute in self.all_attributes:
existing = self.objects.filters.get(attribute)
if existing is None:
self.objects.filters[attribute] = FilterConfig(min_score=0.7)
@@ -744,44 +961,7 @@ class FrigateConfig(FrigateBaseModel):
exclude_unset=True,
)
for key, detector in self.detectors.items():
adapter = TypeAdapter(DetectorConfig)
model_dict = (
detector
if isinstance(detector, dict)
else detector.model_dump(warnings="none")
)
detector_config: BaseDetectorConfig = adapter.validate_python(model_dict)
# users should not set model themselves
if detector_config.model:
logger.warning(
"The model key should be specified at the root level of the config, not under detectors. The nested model key will be ignored."
)
detector_config.model = None
model_config = self.model.model_dump(exclude_unset=True, warnings="none")
if detector_config.model_path:
model_config["path"] = detector_config.model_path
if "path" not in model_config:
if detector_config.type == "cpu" or detector_config.type.endswith(
"_tfl"
):
model_config["path"] = "/cpu_model.tflite"
elif detector_config.type == "edgetpu":
model_config["path"] = "/edgetpu_model.tflite"
elif detector_config.type == "openvino":
for default_key, default_value in DEFAULT_MODEL.items():
model_config.setdefault(default_key, default_value)
model = ModelConfig.model_validate(model_config)
model.check_and_load_plus_model(self.plus_api, detector_config.type)
model.compute_model_hash()
labelmap_objects = model.merged_labelmap.values()
detector_config.model = model
self.detectors[key] = detector_config
self._camera_models = {}
for name, camera in self.cameras.items():
modified_global_config = global_config.copy()
@@ -808,6 +988,9 @@ class FrigateConfig(FrigateBaseModel):
{"name": name, **merged_config}
)
camera_model = self._resolve_camera_model(name, camera_config.detect.scene)
self._camera_models[name] = camera_model
if camera_config.ffmpeg.hwaccel_args == "auto":
camera_config.ffmpeg.hwaccel_args = self.ffmpeg.hwaccel_args
@@ -1028,7 +1211,7 @@ class FrigateConfig(FrigateBaseModel):
verify_profile_overrides_match_base(camera_config)
verify_autotrack_zones(camera_config)
verify_motion_and_detect(camera_config)
verify_objects_track(camera_config, labelmap_objects)
verify_objects_track(camera_config, camera_model.merged_labelmap.values())
verify_lpr_and_face(self, camera_config)
# Validate camera profiles reference top-level profile definitions
@@ -1045,8 +1228,16 @@ class FrigateConfig(FrigateBaseModel):
config.name = name
self.objects.parse_all_objects(self.cameras)
self.model.create_colormap(sorted(self.objects.all_objects))
self.model.check_and_load_plus_model(self.plus_api)
# every model shares one colormap so a label is drawn the same color no
# matter which model detected it, so filter attributes across all models
# rather than letting each model filter with only its own
colored_labels = sorted(
set(self.objects.all_objects) - set(self.all_attributes)
)
for model in self.models:
model.create_colormap(colored_labels)
# Check audio transcription and audio detection requirements
if self.audio_transcription.enabled:
@@ -72,7 +72,7 @@ class LicensePlateProcessingMixin:
# Object config
self.lp_objects: list[str] = []
for obj, attributes in self.config.model.attributes_map.items():
for obj, attributes in self.config.all_attributes_map.items():
if "license_plate" in attributes:
self.lp_objects.append(obj)
@@ -234,8 +234,8 @@ class ReviewDescriptionProcessor(PostProcessorApi):
final_data,
thumbs,
camera_config.review.genai,
list(self.config.model.merged_labelmap.values()),
self.config.model.all_attributes,
sorted(self.config.all_labels),
self.config.all_attributes,
),
).start()
+34 -5
View File
@@ -3,7 +3,7 @@ import json
import logging
import os
from enum import Enum
from typing import Any
from typing import Any, ClassVar
import requests
from pydantic import BaseModel, ConfigDict, Field
@@ -15,6 +15,9 @@ from frigate.util.builtin import generate_color_palette, load_labels
logger = logging.getLogger(__name__)
# attributes that are recognized rather than shown as a logo
NON_LOGO_ATTRIBUTES = ["face", "license_plate"]
class PixelFormatEnum(str, Enum):
rgb = "rgb"
@@ -44,7 +47,27 @@ class ModelTypeEnum(str, Enum):
yologeneric = "yolo-generic"
class SceneEnum(str, Enum):
"""The camera environment a detection model is intended for."""
all = "all"
indoor = "indoor"
outdoor = "outdoor"
indoor_thermal = "indoor_thermal"
outdoor_thermal = "outdoor_thermal"
class ModelConfig(BaseModel):
scene: SceneEnum = Field(
default=SceneEnum.all,
title="Model scene",
description="The camera environment this model is used for. Cameras select a model by setting detect.scene to a matching value, and 'all' is used by any camera that does not set one.",
)
devices: list[str] = Field(
default_factory=list,
title="Detection hardware",
description="Hardware this model runs on, as '<detector>' or '<detector>:<device>' (for example 'edgetpu:pci:0' or 'openvino:GPU'). Listing the same device more than once runs additional inference processes on it.",
)
path: str | None = Field(
None,
title="Custom object detector model path",
@@ -111,7 +134,7 @@ class ModelConfig(BaseModel):
@property
def non_logo_attributes(self) -> list[str]:
return ["face", "license_plate"]
return NON_LOGO_ATTRIBUTES
@property
def all_attributes(self) -> list[str]:
@@ -201,9 +224,7 @@ class ModelConfig(BaseModel):
unique_attributes.update(attributes)
self._all_attributes = list(unique_attributes)
self._all_attribute_logos = list(
unique_attributes - set(["face", "license_plate"])
)
self._all_attribute_logos = list(unique_attributes - set(NON_LOGO_ATTRIBUTES))
self._merged_labelmap = {
**{int(key): val for key, val in model_info["labelMap"].items()},
@@ -234,6 +255,14 @@ class ModelConfig(BaseModel):
class BaseDetectorConfig(BaseModel):
# how the trailing part of a device string ("openvino:GPU" -> "GPU") maps onto
# this detector's fields, and whether the same device may be listed more than
# once to run additional inference processes against it. Most accelerators
# multiplex fine, so this is opt-out rather than opt-in.
device_spec_field: ClassVar[str] = "device"
device_spec_type: ClassVar[type] = str
shareable: ClassVar[bool] = True
# the type field must be defined in all subclasses
type: str = Field(
default="cpu",
+19 -1
View File
@@ -2,7 +2,7 @@ import importlib
import logging
import pkgutil
from enum import Enum
from typing import Annotated, Union
from typing import Annotated, Union, get_args
from pydantic import Field
@@ -39,3 +39,21 @@ DetectorConfig = Annotated[
Union[tuple(BaseDetectorConfig.__subclasses__())], # noqa: UP007
Field(discriminator="type"),
]
def _discriminator_value(config_class: type[BaseDetectorConfig]) -> str | None:
"""Read the Literal value of a detector config class' type field."""
field = config_class.model_fields.get("type")
if field is None:
return None
values = get_args(field.annotation)
return values[0] if values else None
config_types: dict[str, type[BaseDetectorConfig]] = {
key: config_class
for config_class in BaseDetectorConfig.__subclasses__()
if (key := _discriminator_value(config_class)) is not None
}
+113
View File
@@ -0,0 +1,113 @@
"""Parsing of detection hardware device strings."""
import logging
from dataclasses import dataclass
from pydantic import TypeAdapter, ValidationError
from frigate.detectors.detector_config import BaseDetectorConfig, ModelConfig
from frigate.detectors.detector_types import DetectorConfig, config_types
logger = logging.getLogger(__name__)
_detector_adapter: TypeAdapter[BaseDetectorConfig] = TypeAdapter(DetectorConfig)
@dataclass(frozen=True)
class DeviceSpec:
"""A parsed `<detector>` or `<detector>:<device>` string."""
raw: str
detector: str
device: str | None
@property
def shareable(self) -> bool:
"""Whether this device may be listed more than once."""
return config_types[self.detector].shareable
class DeviceParseError(ValueError):
pass
def parse_device(raw: str) -> DeviceSpec:
"""Parse a device string into its detector type and detector specific device.
Args:
raw: The configured device string, for example 'edgetpu:pci:0'
Returns:
The parsed spec
Raises:
DeviceParseError: If the detector type is unknown or the device is not
valid for that detector
"""
detector, separator, device = raw.partition(":")
if detector not in config_types:
raise DeviceParseError(
f"'{raw}' does not name a known detector. Available detectors are {', '.join(sorted(config_types))}"
)
spec = DeviceSpec(raw=raw, detector=detector, device=device if separator else None)
# surface a bad device now rather than when the detection process starts
build_detector_config(spec, None)
return spec
def build_detector_config(
spec: DeviceSpec, model: ModelConfig | None
) -> BaseDetectorConfig:
"""Build the detector config a device string describes.
Args:
spec: The parsed device spec
model: The model this detector runs, if it has been resolved yet
Returns:
The validated detector config
Raises:
DeviceParseError: If the device is not valid for this detector type
"""
config: dict[str, object] = {"type": spec.detector, "model": model}
if spec.device is not None:
config_class = config_types[spec.detector]
try:
config[config_class.device_spec_field] = config_class.device_spec_type(
spec.device
)
except ValueError as err:
raise DeviceParseError(
f"'{spec.raw}' is not a valid {spec.detector} device: {err}"
) from err
try:
return _detector_adapter.validate_python(config)
except ValidationError as err:
raise DeviceParseError(f"'{spec.raw}' is not a valid device: {err}") from err
def runner_names(devices: list[DeviceSpec]) -> list[str]:
"""Build a unique name for each device, since a shareable device may repeat.
Args:
devices: Every device spec across every configured model, in config order
Returns:
A name per device, suffixed with '#2', '#3', etc. on repeats
"""
names: list[str] = []
seen: dict[str, int] = {}
for spec in devices:
count = seen.get(spec.raw, 0) + 1
seen[spec.raw] = count
names.append(spec.raw if count == 1 else f"{spec.raw}#{count}")
return names
+368
View File
@@ -0,0 +1,368 @@
"""Discovery of object detection hardware attached to the system.
Every probe here is a filesystem read. Nothing shells out, initializes a
runtime, or opens a device, so this is cheap enough to run from the API process
while detector children hold the hardware.
Hardware is reported whether or not this image ships a detector that can drive
it. Matching hardware to an image is a separate concern.
"""
import logging
import os
from glob import glob
from pydantic import BaseModel, Field
from frigate.const import SUPPORTED_RK_SOCS
from frigate.detectors.detector_types import config_types
from frigate.util.services import enumerate_drm_devices
logger = logging.getLogger(__name__)
# roots the probes read from, so tests can point them at a fixture tree
SYS_ROOT = "/sys"
DEV_ROOT = "/dev"
PROC_ROOT = "/proc"
ETC_ROOT = "/etc"
# a Coral reports as Global Unichip until its firmware is loaded, then as Google
CORAL_USB_IDS = {("1a6e", "089a"), ("18d1", "9302")}
INTEL_DRM_DRIVERS = ("i915", "xe")
AMD_DRM_DRIVERS = ("amdgpu",)
class HardwareUnit(BaseModel):
"""One physical piece of hardware."""
device: str = Field(
title="Device string",
description="The value to put in a model's devices list, for example 'edgetpu:pci:1'.",
)
label: str = Field(
title="Unit label",
description="How to identify this unit among others of the same kind, for example 'PCIe 1'.",
)
class DetectionHardware(BaseModel):
"""A kind of detection hardware, and every unit of it that was found."""
key: str = Field(
title="Hardware key",
description="Stable identifier for this kind of hardware.",
)
detector: str = Field(
title="Detector type",
description="The detector that drives this hardware.",
)
name: str = Field(
title="Hardware name",
description="Human readable name for this kind of hardware.",
)
units: list[HardwareUnit] = Field(
title="Units",
description="Each physical piece of this hardware that was found.",
)
count: int = Field(
title="Unit count",
description="How many units were found.",
)
unlimited: bool = Field(
title="Unlimited detectors",
description="Whether this hardware can run more inference processes than there are units.",
)
def _read(path: str) -> str | None:
"""Read a small file, returning None if it cannot be read."""
try:
with open(path) as f:
return f.read().strip()
except OSError:
return None
def _is_shareable(detector: str) -> bool:
"""Whether a detector lets the same device run more than one process."""
config_class = config_types.get(detector)
# a detector missing from this image is assumed to behave like most of them
return config_class.shareable if config_class else True
def _hardware(
key: str, detector: str, name: str, units: list[HardwareUnit]
) -> DetectionHardware:
return DetectionHardware(
key=key,
detector=detector,
name=name,
units=units,
count=len(units),
unlimited=_is_shareable(detector),
)
def detect_coral_pci() -> DetectionHardware | None:
"""Find PCIe and M.2 Coral accelerators, which register as apex devices."""
names = sorted(
os.path.basename(path) for path in glob(f"{SYS_ROOT}/class/apex/apex_*")
)
if not names:
return None
units = [
HardwareUnit(device=f"edgetpu:pci:{index}", label=f"PCIe {index}")
for index in range(len(names))
]
return _hardware("edgetpu:pci", "edgetpu", "Coral EdgeTPU (PCIe)", units)
def detect_coral_usb() -> DetectionHardware | None:
"""Find USB Coral accelerators by their USB vendor and product ids."""
found = 0
for device_dir in sorted(glob(f"{SYS_ROOT}/bus/usb/devices/*")):
vendor = _read(os.path.join(device_dir, "idVendor"))
product = _read(os.path.join(device_dir, "idProduct"))
if vendor and product and (vendor.lower(), product.lower()) in CORAL_USB_IDS:
found += 1
if not found:
return None
units = [
HardwareUnit(device=f"edgetpu:usb:{index}", label=f"USB {index}")
for index in range(found)
]
return _hardware("edgetpu:usb", "edgetpu", "Coral EdgeTPU (USB)", units)
def _drm_devices(drivers: tuple[str, ...]) -> list[str]:
"""PCI addresses of DRM devices bound to one of the given drivers."""
return sorted(
pdev for pdev, driver in enumerate_drm_devices().items() if driver in drivers
)
def detect_intel_gpu() -> DetectionHardware | None:
"""Find Intel GPUs through their DRM driver."""
pdevs = _drm_devices(INTEL_DRM_DRIVERS)
if not pdevs:
return None
# OpenVINO reports a lone GPU as "GPU" and enumerates them as GPU.0, GPU.1
# only when there is more than one
if len(pdevs) == 1:
units = [HardwareUnit(device="openvino:GPU", label=pdevs[0])]
else:
units = [
HardwareUnit(device=f"openvino:GPU.{index}", label=pdev)
for index, pdev in enumerate(pdevs)
]
return _hardware("openvino:GPU", "openvino", "Intel GPU", units)
def detect_intel_npu() -> DetectionHardware | None:
"""Find Intel NPUs, which register as accel devices bound to intel_vpu."""
units = []
for accel_path in sorted(glob(f"{SYS_ROOT}/class/accel/accel*")):
try:
driver = os.path.basename(os.readlink(f"{accel_path}/device/driver"))
except OSError:
continue
if driver != "intel_vpu":
continue
units.append(
HardwareUnit(device="openvino:NPU", label=os.path.basename(accel_path))
)
if not units:
return None
# OpenVINO has no way to address a specific NPU, so only the first is usable
return _hardware("openvino:NPU", "openvino", "Intel NPU", units[:1])
def detect_amd_gpu() -> DetectionHardware | None:
"""Find AMD GPUs through their DRM driver."""
pdevs = _drm_devices(AMD_DRM_DRIVERS)
if not pdevs:
return None
# ROCm runs through onnx, whose MIGraphX provider takes no device index, so
# only one is addressable
units = [HardwareUnit(device="onnx", label=pdevs[0])]
return _hardware("onnx:amd", "onnx", "AMD GPU", units)
def detect_nvidia_gpu() -> DetectionHardware | None:
"""Find discrete Nvidia GPUs through the nvidia driver's proc entries."""
units = []
for index, gpu_dir in enumerate(sorted(glob(f"{PROC_ROOT}/driver/nvidia/gpus/*"))):
information = _read(os.path.join(gpu_dir, "information")) or ""
name = f"GPU {index}"
for line in information.splitlines():
if line.startswith("Model:"):
name = line.split(":", 1)[1].strip()
break
units.append(HardwareUnit(device=f"onnx:{index}", label=name))
if not units:
return None
# the model name is more useful as the hardware name when there is only one
name = units[0].label if len(units) == 1 else "NVIDIA GPU"
return _hardware("onnx:nvidia", "onnx", name, units)
def detect_jetson() -> DetectionHardware | None:
"""Find an Nvidia Jetson, whose integrated GPU runs through tensorrt."""
is_jetson = os.path.isfile(f"{ETC_ROOT}/nv_tegra_release") or os.path.exists(
f"{SYS_ROOT}/devices/gpu.0/load"
)
if not is_jetson:
return None
units = [HardwareUnit(device="tensorrt:0", label="Integrated GPU")]
return _hardware("tensorrt", "tensorrt", "NVIDIA Jetson", units)
def _dev_units(pattern: str, device: str, label: str) -> list[HardwareUnit]:
"""Build units from device nodes matching a glob."""
return [
HardwareUnit(device=device.format(index=index), label=f"{label} {index}")
for index in range(len(glob(f"{DEV_ROOT}/{pattern}")))
]
def detect_hailo() -> DetectionHardware | None:
"""Find Hailo accelerators by their device nodes."""
nodes = sorted(glob(f"{DEV_ROOT}/hailo*"))
if not nodes:
return None
# the hailo runtime schedules across every attached device itself, so there
# is nothing to address individually
units = [HardwareUnit(device="hailo8l:PCIe", label=os.path.basename(nodes[0]))]
return _hardware("hailo8l", "hailo8l", "Hailo", units)
def detect_memryx() -> DetectionHardware | None:
"""Find MemryX accelerators by their device nodes."""
units = _dev_units("memx*", "memryx:PCIe:{index}", "PCIe")
if not units:
return None
return _hardware("memryx", "memryx", "MemryX MX3", units)
def detect_rockchip() -> DetectionHardware | None:
"""Find a Rockchip NPU by reading the SoC from the device tree."""
compatible = _read(f"{PROC_ROOT}/device-tree/compatible")
if not compatible:
return None
soc = compatible.split(",")[-1].strip("\x00")
if soc not in SUPPORTED_RK_SOCS:
return None
units = [HardwareUnit(device="rknn", label=soc.upper())]
return _hardware("rknn", "rknn", f"Rockchip NPU ({soc.upper()})", units)
def detect_axengine() -> DetectionHardware | None:
"""Find an AXERA accelerator by its control device node."""
if not os.path.exists(f"{DEV_ROOT}/axcl_host"):
return None
units = [HardwareUnit(device="axengine", label="AXERA")]
return _hardware("axengine", "axengine", "AXERA NPU", units)
def detect_synaptics() -> DetectionHardware | None:
"""Find a Synaptics NPU by its device node."""
if not os.path.exists(f"{DEV_ROOT}/synap"):
return None
units = [HardwareUnit(device="synaptics", label="Synaptics")]
return _hardware("synaptics", "synaptics", "Synaptics NPU", units)
def detect_cpu() -> DetectionHardware:
"""The CPU, which is always available."""
units = [HardwareUnit(device="cpu", label="CPU")]
return _hardware("cpu", "cpu", "CPU", units)
# ordered so accelerators are offered ahead of the CPU fallback
PROBES = (
detect_coral_pci,
detect_coral_usb,
detect_hailo,
detect_memryx,
detect_intel_npu,
detect_intel_gpu,
detect_nvidia_gpu,
detect_jetson,
detect_amd_gpu,
detect_rockchip,
detect_axengine,
detect_synaptics,
detect_cpu,
)
class HardwareProber:
"""Probes for detection hardware, caching the result for the process."""
_hardware: list[DetectionHardware] | None = None
def probe(self, refresh: bool = False) -> list[DetectionHardware]:
"""Get the detection hardware attached to this system.
Args:
refresh: Probe again instead of using the cached result
Returns:
Every kind of detection hardware that was found
"""
if self._hardware is not None and not refresh:
return self._hardware
found = []
for probe in PROBES:
try:
hardware = probe()
except Exception:
logger.warning("Failed to probe for %s", probe.__name__, exc_info=True)
continue
if hardware is not None:
found.append(hardware)
logger.debug("Detected hardware: %s", [h.key for h in found])
self._hardware = found
return found
hardware_prober = HardwareProber()
+4 -1
View File
@@ -1,5 +1,5 @@
import logging
from typing import Literal
from typing import ClassVar, Literal
from pydantic import ConfigDict, Field
@@ -27,6 +27,9 @@ class CpuDetectorConfig(BaseDetectorConfig):
title="CPU",
)
device_spec_field: ClassVar[str] = "num_threads"
device_spec_type: ClassVar[type] = int
type: Literal[DETECTOR_KEY]
num_threads: int = Field(
default=3,
+4 -1
View File
@@ -1,7 +1,7 @@
import logging
import math
import os
from typing import Literal
from typing import ClassVar, Literal
import cv2
import numpy as np
@@ -28,6 +28,9 @@ class EdgeTpuDetectorConfig(BaseDetectorConfig):
title="EdgeTPU",
)
# a TPU can only be opened by one process
shareable: ClassVar[bool] = False
type: Literal[DETECTOR_KEY]
device: str = Field(
default=None,
+4 -1
View File
@@ -5,7 +5,7 @@ import shutil
import urllib.request
import zipfile
from queue import Queue
from typing import Literal
from typing import ClassVar, Literal
import cv2
import numpy as np
@@ -37,6 +37,9 @@ class MemryXDetectorConfig(BaseDetectorConfig):
title="MemryX",
)
# an accelerator can only be opened by one process
shareable: ClassVar[bool] = False
type: Literal[DETECTOR_KEY]
device: str = Field(
default="PCIe",
+1 -1
View File
@@ -28,7 +28,7 @@ class OvDetectorConfig(BaseDetectorConfig):
type: Literal[DETECTOR_KEY]
device: str = Field(
default=None,
default="AUTO",
title="Device Type",
description="The device to use for OpenVINO inference (e.g. 'CPU', 'GPU', 'NPU').",
)
+4 -1
View File
@@ -2,7 +2,7 @@ import logging
import os.path
import re
import urllib.request
from typing import Literal
from typing import ClassVar, Literal
import cv2
import numpy as np
@@ -35,6 +35,9 @@ class RknnDetectorConfig(BaseDetectorConfig):
title="RKNN",
)
device_spec_field: ClassVar[str] = "num_cores"
device_spec_type: ClassVar[type] = int
type: Literal[DETECTOR_KEY]
num_cores: int = Field(
default=0,
+3 -1
View File
@@ -14,7 +14,7 @@ try:
except ModuleNotFoundError:
TRT_SUPPORT = False
from typing import Literal
from typing import ClassVar, Literal
from pydantic import ConfigDict, Field
@@ -53,6 +53,8 @@ class TensorRTDetectorConfig(BaseDetectorConfig):
title="TensorRT",
)
device_spec_type: ClassVar[type] = int
type: Literal[DETECTOR_KEY]
device: int = Field(
default=0, title="GPU Device Index", description="The GPU device index to use."
+5 -8
View File
@@ -159,7 +159,8 @@ class EventProcessor(threading.Thread):
if width is None or height is None:
return
first_detector = list(self.config.detectors.values())[0]
camera_model = self.config.model_for_camera(camera)
camera_detector = self.config.devices_for_model(camera_model)[0].detector
start_time = event_data["start_time"]
end_time = (
@@ -229,13 +230,9 @@ class EventProcessor(threading.Thread):
Event.thumbnail: event_data.get("thumbnail"),
Event.has_clip: event_data["has_clip"],
Event.has_snapshot: event_data["has_snapshot"],
Event.model_hash: first_detector.model.model_hash
if first_detector.model
else None,
Event.model_type: first_detector.model.model_type
if first_detector.model
else None,
Event.detector_type: first_detector.type,
Event.model_hash: camera_model.model_hash,
Event.model_type: camera_model.model_type,
Event.detector_type: camera_detector,
Event.data: {
"box": box,
"region": region,
+2 -2
View File
@@ -245,7 +245,7 @@ class GeminiClient(GenAIClient):
)
gemini_messages.append(
types.Content(
role="function",
role="user",
parts=[
types.Part.from_function_response(
name=msg.get("name")
@@ -501,7 +501,7 @@ class GeminiClient(GenAIClient):
)
gemini_messages.append(
types.Content(
role="function",
role="user",
parts=[
types.Part.from_function_response(
name=msg.get("name")
+5 -1
View File
@@ -115,6 +115,10 @@ def query_recordings(source_camera: str, start_ts: float, end_ts: float) -> Mode
return cast(ModelSelect, query)
class NoRecordingsError(ValueError):
"""Raised when no recordings exist in the requested time range."""
class DebugReplaySource(ABC):
"""Abstract source for a debug replay session.
@@ -187,7 +191,7 @@ class RecordingDebugReplaySource(DebugReplaySource):
raise ValueError("End time must be after start time")
if not query_recordings(self._camera, self._start_ts, self._end_ts).count():
raise ValueError(
raise NoRecordingsError(
f"No recordings found for camera '{self._camera}' in the specified time range"
)
+13 -1
View File
@@ -5,7 +5,19 @@ import threading
from numpy import ndarray
from frigate.detectors.detector_config import InputTensorEnum
from frigate.detectors.detector_config import InputTensorEnum, ModelConfig
def detection_frame_size(model: ModelConfig) -> int:
"""Get the shared memory size a camera needs to hand frames to a model.
Args:
model: The model the camera runs on
Returns:
Size in bytes of one model input frame
"""
return model.height * model.width * 3
class RequestStore:
+2 -2
View File
@@ -481,7 +481,7 @@ class ReviewSegmentMaintainer(threading.Thread):
if not object["sub_label"]:
segment.detections[object["id"]] = object["label"]
elif object["sub_label"][0] in self.config.model.all_attributes:
elif object["sub_label"][0] in self.config.all_attributes:
segment.detections[object["id"]] = object["sub_label"][0]
else:
segment.detections[object["id"]] = f"{object['label']}-verified"
@@ -619,7 +619,7 @@ class ReviewSegmentMaintainer(threading.Thread):
for object in activity.get_all_objects():
if not object["sub_label"]:
detections[object["id"]] = object["label"]
elif object["sub_label"][0] in self.config.model.all_attributes:
elif object["sub_label"][0] in self.config.all_attributes:
detections[object["id"]] = object["sub_label"][0]
else:
detections[object["id"]] = f"{object['label']}-verified"
+14 -13
View File
@@ -322,19 +322,20 @@ async def set_gpu_stats(
async def set_npu_usages(config: FrigateConfig, all_stats: dict[str, Any]) -> None:
stats: dict[str, dict] = {}
for detector in config.detectors.values():
if detector.type == "rknn":
# Rockchip NPU usage
rk_usage = get_rockchip_npu_stats()
stats["rockchip"] = rk_usage
elif detector.type == "openvino" and detector.device == "NPU":
# OpenVINO NPU usage
ov_usage = get_openvino_npu_stats()
stats["openvino"] = ov_usage
elif detector.type == "axengine":
# AXERA NPU usage
axcl_usage = get_axcl_npu_stats()
stats["axengine"] = axcl_usage
for model in config.models:
for device in config.devices_for_model(model):
if device.detector == "rknn":
# Rockchip NPU usage
rk_usage = get_rockchip_npu_stats()
stats["rockchip"] = rk_usage
elif device.detector == "openvino" and device.device == "NPU":
# OpenVINO NPU usage
ov_usage = get_openvino_npu_stats()
stats["openvino"] = ov_usage
elif device.detector == "axengine":
# AXERA NPU usage
axcl_usage = get_axcl_npu_stats()
stats["axengine"] = axcl_usage
if stats:
all_stats["npu_usages"] = stats
+111 -52
View File
@@ -9,6 +9,10 @@ from pathlib import Path
from peewee import SQL, Case, fn
from frigate.config import FrigateConfig
from frigate.config.camera.updater import (
CameraConfigUpdateEnum,
CameraConfigUpdateSubscriber,
)
from frigate.const import (
RECORD_DIR,
REPLAY_CAMERA_PREFIX,
@@ -24,6 +28,7 @@ bandwidth_equation = Recordings.segment_size / (
)
MAX_CALCULATED_BANDWIDTH = 10000 # 10Gb/hr
BANDWIDTH_SAMPLE_TARGET = 50
class StorageMaintainer(threading.Thread):
@@ -34,6 +39,41 @@ class StorageMaintainer(threading.Thread):
self.config = config
self.stop_event = stop_event
self.camera_storage_stats: dict[str, dict] = {}
self.config_subscriber = CameraConfigUpdateSubscriber(
self.config,
self.config.cameras,
[CameraConfigUpdateEnum.record],
)
def _recording_stream_types(self, camera: str) -> tuple[str, ...]:
"""Return the stream types the camera is currently recording."""
camera_config = self.config.cameras.get(camera)
if camera_config is None or not camera_config.record.enabled:
return ()
if camera_config.record.sub.enabled:
return (STREAM_TYPE_MAIN, STREAM_TYPE_SUB)
return (STREAM_TYPE_MAIN,)
def expected_hourly_bandwidth(self) -> float:
"""Return the MB/hr the cameras are expected to write.
Only the streams a camera currently records are counted, so toggling
recording or sub stream recording is reflected without waiting for the
existing segments of a stopped stream to expire.
"""
total = 0.0
for camera, stats in self.camera_storage_stats.items():
stream_bandwidths = stats.get("bandwidth_by_stream", {})
total += sum(
stream_bandwidths.get(stream_type, 0)
for stream_type in self._recording_stream_types(camera)
)
return round(total, 2)
def _recent_stream_bandwidth(
self, camera: str, stream_type: str, window: int
@@ -64,6 +104,32 @@ class StorageMaintainer(threading.Thread):
avg: float | None = Recordings.select(fn.AVG(SQL("bw"))).from_(recent).scalar()
return avg
def _stream_sample_count(self, camera: str, stream_type: str) -> int:
"""Count a stream's non-zero segments, stopping at the sample target."""
count: int = (
Recordings.select(Recordings.id)
.where(
Recordings.camera == camera,
Recordings.stream_type == stream_type,
Recordings.segment_size > 0,
)
.limit(BANDWIDTH_SAMPLE_TARGET)
.count()
)
return count
def _needs_refresh(self, camera: str) -> bool:
"""Return whether a stream the camera records still lacks samples.
Counted per stream rather than per camera: a stream that starts
recording later has no samples of its own yet, and a camera-wide count
would report it settled on the strength of another stream's history.
"""
return any(
self._stream_sample_count(camera, stream_type) < BANDWIDTH_SAMPLE_TARGET
for stream_type in self._recording_stream_types(camera)
)
def calculate_camera_bandwidth(self) -> None:
"""Calculate an average MB/hr for each camera."""
for camera in self.config.cameras.keys():
@@ -71,56 +137,45 @@ class StorageMaintainer(threading.Thread):
if camera.startswith(REPLAY_CAMERA_PREFIX):
continue
# cameras with < 50 segments should be refreshed to keep size accurate
# when few segments are available
if self.camera_storage_stats.get(camera, {}).get("needs_refresh", True):
self.camera_storage_stats[camera] = {
"needs_refresh": (
Recordings.select(Recordings.id)
.where(Recordings.camera == camera, Recordings.segment_size > 0)
.limit(50)
.count()
< 50
)
}
if not self.camera_storage_stats.get(camera, {}).get("needs_refresh", True):
continue
# calculate MB/hr from the last 100 segments of each stream
# type and sum the rates; mixing streams would average small
# sub segments against large main segments and underestimate
# the true write rate
bandwidth_by_stream: dict[str, float] = {}
for stream_type in (STREAM_TYPE_MAIN, STREAM_TYPE_SUB):
avg_bw = self._recent_stream_bandwidth(camera, stream_type, 100)
if avg_bw is None:
# the recent window can be all zero-size ingest
# glitches; look further back before concluding
# the stream writes nothing
avg_bw = self._recent_stream_bandwidth(
camera, stream_type, 1000
)
if avg_bw is not None:
bandwidth_by_stream[stream_type] = round(avg_bw * 3600, 2)
# calculate MB/hr from the last 100 segments of each stream
# type and sum the rates; mixing streams would average small
# sub segments against large main segments and underestimate
# the true write rate
bandwidth_by_stream: dict[str, float] = {}
for stream_type in (STREAM_TYPE_MAIN, STREAM_TYPE_SUB):
avg_bw = self._recent_stream_bandwidth(camera, stream_type, 100)
if avg_bw is None:
# the recent window can be all zero-size ingest
# glitches; look further back before concluding
# the stream writes nothing
avg_bw = self._recent_stream_bandwidth(camera, stream_type, 1000)
if avg_bw is not None:
bandwidth_by_stream[stream_type] = round(avg_bw * 3600, 2)
bandwidth = round(sum(bandwidth_by_stream.values()), 2)
bandwidth = round(sum(bandwidth_by_stream.values()), 2)
if bandwidth > MAX_CALCULATED_BANDWIDTH:
logger.warning(
f"{camera} has a bandwidth of {bandwidth} MB/hr which exceeds the expected maximum. This typically indicates an issue with the cameras recordings."
)
# scale each stream so the per stream values still sum to
# the clamped total the UI displays alongside them
scale = MAX_CALCULATED_BANDWIDTH / bandwidth
bandwidth_by_stream = {
stream_type: round(value * scale, 2)
for stream_type, value in bandwidth_by_stream.items()
}
bandwidth = MAX_CALCULATED_BANDWIDTH
self.camera_storage_stats[camera]["bandwidth"] = bandwidth
self.camera_storage_stats[camera]["bandwidth_by_stream"] = (
bandwidth_by_stream
if bandwidth > MAX_CALCULATED_BANDWIDTH:
logger.warning(
f"{camera} has a bandwidth of {bandwidth} MB/hr which exceeds the expected maximum. This typically indicates an issue with the cameras recordings."
)
logger.debug(f"{camera} has a bandwidth of {bandwidth} MiB/hr.")
# scale each stream so the per stream values still sum to
# the clamped total the UI displays alongside them
scale = MAX_CALCULATED_BANDWIDTH / bandwidth
bandwidth_by_stream = {
stream_type: round(value * scale, 2)
for stream_type, value in bandwidth_by_stream.items()
}
bandwidth = MAX_CALCULATED_BANDWIDTH
self.camera_storage_stats[camera] = {
"needs_refresh": self._needs_refresh(camera),
"bandwidth": bandwidth,
"bandwidth_by_stream": bandwidth_by_stream,
}
logger.debug(f"{camera} has a bandwidth of {bandwidth} MiB/hr.")
def calculate_camera_usages(self) -> dict[str, dict]:
"""Calculate the storage usage of each camera."""
@@ -175,9 +230,7 @@ class StorageMaintainer(threading.Thread):
"""Return if storage needs cleanup."""
# currently runs cleanup if less than 1 hour of space is left
# disk_usage should not spin up disks
hourly_bandwidth = sum(
[b["bandwidth"] for b in self.camera_storage_stats.values()]
)
hourly_bandwidth = self.expected_hourly_bandwidth()
remaining_storage = round(shutil.disk_usage(RECORD_DIR).free / pow(2, 20), 1)
logger.debug(
f"Storage cleanup check: {hourly_bandwidth} hourly with remaining storage: {remaining_storage}."
@@ -188,9 +241,7 @@ class StorageMaintainer(threading.Thread):
"""Remove oldest hour of recordings."""
logger.debug("Starting storage cleanup.")
deleted_segments_size = 0
hourly_bandwidth = sum(
[b["bandwidth"] for b in self.camera_storage_stats.values()]
)
hourly_bandwidth = self.expected_hourly_bandwidth()
recordings = (
Recordings.select(
@@ -350,10 +401,17 @@ class StorageMaintainer(threading.Thread):
"""Check every 5 minutes if storage needs to be cleaned up."""
if self.config.safe_mode:
logger.info("Safe mode enabled, skipping storage maintenance")
self.config_subscriber.stop()
return
self.calculate_camera_bandwidth()
while not self.stop_event.wait(300):
updated_topics = self.config_subscriber.check_for_updates()
for camera in updated_topics.get(CameraConfigUpdateEnum.record.name, []):
if camera in self.camera_storage_stats:
self.camera_storage_stats[camera]["needs_refresh"] = True
if not self.camera_storage_stats or True in [
r["needs_refresh"] for r in self.camera_storage_stats.values()
]:
@@ -366,4 +424,5 @@ class StorageMaintainer(threading.Thread):
)
self.reduce_storage_consumption()
self.config_subscriber.stop()
logger.info("Exiting storage maintainer...")
@@ -2,6 +2,7 @@
from unittest.mock import patch
from frigate.jobs.debug_replay import NoRecordingsError
from frigate.models import Event, Recordings, ReviewSegment
from frigate.test.http_api.base_http_test import AuthTestClient, BaseTestHttp
@@ -66,6 +67,32 @@ class TestDebugReplayAPI(BaseTestHttp):
# (CodeQL: information exposure through an exception).
self.assertEqual(body["message"], "Invalid debug replay parameters")
def test_start_returns_404_when_no_recordings(self):
with patch(
"frigate.api.debug_replay.start_debug_replay_job",
side_effect=NoRecordingsError(
"No recordings found for camera 'front' in the specified time range"
),
):
with AuthTestClient(self.app) as client:
resp = client.post(
"/debug_replay/start",
json={
"camera": "front",
"start_time": 100,
"end_time": 200,
},
)
self.assertEqual(resp.status_code, 404)
body = resp.json()
self.assertFalse(body["success"])
# Message is hard-coded so we don't echo exception text back to clients
# (CodeQL: information exposure through an exception).
self.assertEqual(
body["message"], "No recordings found in the selected time range"
)
def test_start_returns_409_when_session_already_active(self):
with patch(
"frigate.api.debug_replay.start_debug_replay_job",
@@ -0,0 +1,55 @@
"""Tests for the audio labels API."""
import unittest
from unittest.mock import patch
from frigate.models import Event
from frigate.test.http_api.base_http_test import AuthTestClient, BaseTestHttp
class TestHttpAudioLabels(BaseTestHttp):
def setUp(self):
super().setUp([Event])
def _labels(self, config: dict | None = None) -> dict[str, str]:
if config:
self.minimal_config.update(config)
app = self.create_app()
with patch(
"frigate.api.app.load_labels", return_value={0: "speech", 1: "bark"}
):
with AuthTestClient(app) as client:
response = client.get("/audio_labels")
self.assertEqual(response.status_code, 200)
return response.json()
def test_the_default_labels_are_returned(self):
self.assertEqual(self._labels(), {"0": "speech", "1": "bark"})
def test_a_global_labelmap_override_is_offered(self):
# grouping several classes under one label makes that label selectable
labels = self._labels({"audio": {"labelmap": {0: "noise", 1: "noise"}}})
self.assertEqual(set(labels.values()), {"noise"})
def test_a_camera_labelmap_override_is_offered(self):
labels = self._labels(
{
"cameras": {
"front_door": {
**self.minimal_config["cameras"]["front_door"],
"audio": {"labelmap": {1: "dogs"}},
}
}
}
)
self.assertEqual(labels["1"], "dogs")
self.assertEqual(labels["0"], "speech")
if __name__ == "__main__":
unittest.main(verbosity=2)
@@ -9,6 +9,32 @@ from frigate.config.camera.updater import (
CameraConfigUpdateSubscriber,
)
from frigate.const import SUB_CACHE_TAG
from frigate.detectors.detector_config import SceneEnum
def _build_scene_frigate_config(scene: str | None) -> FrigateConfig:
detect = {"height": 1080, "width": 1920, "fps": 5}
if scene is not None:
detect["scene"] = scene
return FrigateConfig(
**{
"mqtt": {"host": "mqtt"},
"models": [
{"devices": ["cpu"]},
{"scene": "outdoor", "devices": ["openvino:CPU"]},
],
"cameras": {
"front_door": {
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.1:554/video", "roles": ["detect"]}
]
},
"detect": detect,
}
},
}
)
def _build_camera_config(sub_enabled: bool) -> CameraConfig:
@@ -86,6 +112,32 @@ class TestRecordUpdateRecreatesFfmpegCmds(unittest.TestCase):
assert not _has_sub_output(camera_config)
@patch("frigate.detectors.detector_config.load_labels")
def test_removed_camera_readded_without_scene_gets_fresh_model(self, mock_labels):
mock_labels.return_value = {}
config = _build_scene_frigate_config("outdoor")
subscriber = CameraConfigUpdateSubscriber(
config, {}, [CameraConfigUpdateEnum.add, CameraConfigUpdateEnum.remove]
)
assert config.model_for_camera("front_door").scene == SceneEnum.outdoor
subscriber.subscriber.check_for_update.side_effect = [
("config/cameras/front_door/remove", config.cameras["front_door"]),
(None, None),
]
subscriber.check_for_updates()
# recreating the camera through the wizard leaves the scene unset,
# so the removed camera's cached model must not carry over
readded = _build_scene_frigate_config(None).cameras["front_door"]
subscriber.subscriber.check_for_update.side_effect = [
("config/cameras/front_door/add", readded),
(None, None),
]
subscriber.check_for_updates()
assert config.model_for_camera("front_door").scene == SceneEnum.all
def test_unchanged_record_update_keeps_existing_cmds(self):
camera_config = _build_camera_config(sub_enabled=False)
subscriber = CameraConfigUpdateSubscriber(
+226 -38
View File
@@ -11,6 +11,8 @@ from ruamel.yaml.constructor import DuplicateKeyError
from frigate.config import BirdseyeModeEnum, FrigateConfig, RetainModeEnum
from frigate.const import MODEL_CACHE_DIR
from frigate.detectors import DetectorTypeEnum
from frigate.detectors.detector_config import SceneEnum
from frigate.detectors.device import build_detector_config, runner_names
from frigate.util.builtin import deep_merge
@@ -65,49 +67,236 @@ class TestConfig(unittest.TestCase):
def test_config_class(self):
frigate_config = FrigateConfig(**self.minimal)
assert "cpu" in frigate_config.detectors.keys()
assert frigate_config.detectors["cpu"].type == DetectorTypeEnum.cpu
assert frigate_config.detectors["cpu"].model.width == 320
model = frigate_config.primary_model
assert model.scene == SceneEnum.all
assert model.width == 320
assert frigate_config.devices_for_model(model)[0].detector == (
DetectorTypeEnum.cpu
)
@patch("frigate.detectors.detector_config.load_labels")
def test_detector_custom_model_path(self, mock_labels):
def test_model_custom_path(self, mock_labels):
mock_labels.return_value = {}
config = {
"detectors": {
"cpu": {
"type": "cpu",
"model_path": "/cpu_model.tflite",
"models": [
# needs to be a file that will exist, doesn't matter what
{"path": "/etc/hosts", "width": 512, "devices": ["openvino:GPU"]},
],
}
frigate_config = FrigateConfig(**(deep_merge(config, self.minimal)))
model = frigate_config.primary_model
assert model.path == "/etc/hosts"
assert model.width == 512
detector_config = build_detector_config(
frigate_config.devices_for_model(model)[0], model
)
assert detector_config.type == DetectorTypeEnum.openvino
assert detector_config.device == "GPU"
assert detector_config.model.path == "/etc/hosts"
@patch("frigate.detectors.detector_config.load_labels")
def test_model_default_paths_per_detector(self, mock_labels):
mock_labels.return_value = {}
for devices, expected in (
(["cpu"], "/cpu_model.tflite"),
(["edgetpu:pci:0"], "/edgetpu_model.tflite"),
(["openvino:CPU"], "/openvino-model/ssdlite_mobilenet_v2.xml"),
):
config = {"models": [{"devices": devices}]}
frigate_config = FrigateConfig(**(deep_merge(config, self.minimal)))
assert frigate_config.primary_model.path == expected
@patch("frigate.detectors.detector_config.load_labels")
def test_camera_picks_model_by_scene(self, mock_labels):
mock_labels.return_value = {}
config = {
"models": [
{"scene": "outdoor", "devices": ["cpu"], "width": 320},
{"scene": "indoor", "devices": ["openvino:CPU"], "width": 300},
],
"cameras": {
"back": {
"detect": {"scene": "outdoor"},
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.1:554/video", "roles": ["detect"]},
]
},
},
"edgetpu": {
"type": "edgetpu",
"model_path": "/edgetpu_model.tflite",
},
"openvino": {
"type": "openvino",
"front": {
"detect": {"scene": "indoor"},
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.2:554/video", "roles": ["detect"]},
]
},
},
},
# needs to be a file that will exist, doesn't matter what
"model": {"path": "/etc/hosts", "width": 512},
}
frigate_config = FrigateConfig(**(deep_merge(config, self.minimal)))
assert "cpu" in frigate_config.detectors.keys()
assert "edgetpu" in frigate_config.detectors.keys()
assert "openvino" in frigate_config.detectors.keys()
assert frigate_config.model_for_camera("back").scene == SceneEnum.outdoor
assert frigate_config.model_for_camera("front").scene == SceneEnum.indoor
assert frigate_config.model_for_camera("back").width == 320
assert frigate_config.model_for_camera("front").width == 300
assert frigate_config.detectors["cpu"].type == DetectorTypeEnum.cpu
assert frigate_config.detectors["edgetpu"].type == DetectorTypeEnum.edgetpu
assert frigate_config.detectors["openvino"].type == DetectorTypeEnum.openvino
@patch("frigate.detectors.detector_config.load_labels")
def test_camera_requires_a_scene_without_a_default(self, mock_labels):
mock_labels.return_value = {}
config = {
"models": [
{"scene": "outdoor", "devices": ["cpu"]},
{"scene": "indoor", "devices": ["openvino:CPU"]},
],
}
assert frigate_config.detectors["cpu"].num_threads == 3
assert frigate_config.detectors["edgetpu"].device is None
assert frigate_config.detectors["openvino"].device is None
with self.assertRaises(ValidationError):
FrigateConfig(**(deep_merge(config, self.minimal)))
assert frigate_config.model.path == "/etc/hosts"
assert frigate_config.detectors["cpu"].model.path == "/cpu_model.tflite"
assert frigate_config.detectors["edgetpu"].model.path == "/edgetpu_model.tflite"
assert frigate_config.detectors["openvino"].model.path == "/etc/hosts"
@patch("frigate.detectors.detector_config.load_labels")
def test_camera_scene_without_a_model_falls_back_to_all(self, mock_labels):
mock_labels.return_value = {}
config = {
"models": [{"devices": ["cpu"]}],
"cameras": {
"back": {
"detect": {"scene": "outdoor"},
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.1:554/video", "roles": ["detect"]},
]
},
},
},
}
frigate_config = FrigateConfig(**(deep_merge(config, self.minimal)))
assert frigate_config.model_for_camera("back").scene == SceneEnum.all
@patch("frigate.detectors.detector_config.load_labels")
def test_model_for_camera_resolves_camera_added_after_parse(self, mock_labels):
mock_labels.return_value = {}
config = {
"models": [
{"devices": ["cpu"], "width": 320},
{"scene": "outdoor", "devices": ["openvino:CPU"], "width": 416},
],
}
frigate_config = FrigateConfig(**(deep_merge(deepcopy(config), self.minimal)))
# runtime camera adds (wizard, clone, debug replay) insert an already
# resolved camera into the shared config without re-running parse
added = deepcopy(self.minimal)
added["cameras"]["new_cam"] = {
"detect": {"height": 1080, "width": 1920, "fps": 5, "scene": "outdoor"},
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.2:554/video", "roles": ["detect"]},
]
},
}
new_config = FrigateConfig(**(deep_merge(deepcopy(config), added)))
frigate_config.cameras["new_cam"] = new_config.cameras["new_cam"]
assert frigate_config.model_for_camera("new_cam").scene == SceneEnum.outdoor
assert frigate_config.model_for_camera("new_cam").width == 416
@patch("frigate.detectors.detector_config.load_labels")
def test_model_for_camera_unknown_camera_uses_default_model(self, mock_labels):
mock_labels.return_value = {}
config = {
"models": [
{"devices": ["cpu"], "width": 320},
{"scene": "outdoor", "devices": ["openvino:CPU"], "width": 416},
],
}
frigate_config = FrigateConfig(**(deep_merge(deepcopy(config), self.minimal)))
# a caller racing a runtime remove may still name the popped camera
assert frigate_config.model_for_camera("removed").scene == SceneEnum.all
@patch("frigate.detectors.detector_config.load_labels")
def test_camera_scene_without_a_model_or_a_default(self, mock_labels):
mock_labels.return_value = {}
config = {
"models": [{"scene": "indoor", "devices": ["cpu"]}],
"cameras": {
"back": {
"detect": {"scene": "outdoor"},
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.1:554/video", "roles": ["detect"]},
]
},
},
},
}
with self.assertRaises(ValidationError):
FrigateConfig(**(deep_merge(config, self.minimal)))
@patch("frigate.detectors.detector_config.load_labels")
def test_models_must_use_unique_scenes(self, mock_labels):
mock_labels.return_value = {}
config = {
"models": [
{"scene": "outdoor", "devices": ["cpu"]},
{"scene": "outdoor", "devices": ["openvino:CPU"]},
],
}
with self.assertRaises(ValidationError):
FrigateConfig(**(deep_merge(config, self.minimal)))
@patch("frigate.detectors.detector_config.load_labels")
def test_model_devices_must_share_a_detector(self, mock_labels):
mock_labels.return_value = {}
config = {"models": [{"devices": ["cpu", "openvino:CPU"]}]}
with self.assertRaises(ValidationError):
FrigateConfig(**(deep_merge(config, self.minimal)))
@patch("frigate.detectors.detector_config.load_labels")
def test_model_requires_a_known_detector(self, mock_labels):
mock_labels.return_value = {}
config = {"models": [{"devices": ["not_a_detector:0"]}]}
with self.assertRaises(ValidationError):
FrigateConfig(**(deep_merge(config, self.minimal)))
@patch("frigate.detectors.detector_config.load_labels")
def test_model_requires_a_device(self, mock_labels):
mock_labels.return_value = {}
config = {"models": [{"devices": []}]}
with self.assertRaises(ValidationError):
FrigateConfig(**(deep_merge(config, self.minimal)))
@patch("frigate.detectors.detector_config.load_labels")
def test_shareable_devices_may_repeat(self, mock_labels):
mock_labels.return_value = {}
config = {"models": [{"devices": ["openvino:GPU", "openvino:GPU"]}]}
frigate_config = FrigateConfig(**(deep_merge(config, self.minimal)))
devices = frigate_config.devices_for_model(frigate_config.primary_model)
assert runner_names(devices) == ["openvino:GPU", "openvino:GPU#2"]
@patch("frigate.detectors.detector_config.load_labels")
def test_exclusive_devices_may_not_repeat(self, mock_labels):
mock_labels.return_value = {}
config = {"models": [{"devices": ["edgetpu:pci:0", "edgetpu:pci:0"]}]}
with self.assertRaises(ValidationError):
FrigateConfig(**(deep_merge(config, self.minimal)))
def test_invalid_mqtt_config(self):
config = {
@@ -1131,7 +1320,7 @@ class TestConfig(unittest.TestCase):
def test_merge_labelmap(self):
config = {
"mqtt": {"host": "mqtt"},
"model": {"labelmap": {7: "truck"}},
"models": [{"labelmap": {7: "truck"}, "devices": ["cpu"]}],
"cameras": {
"back": {
"ffmpeg": {
@@ -1152,7 +1341,7 @@ class TestConfig(unittest.TestCase):
}
frigate_config = FrigateConfig(**config)
assert frigate_config.model.merged_labelmap[7] == "truck"
assert frigate_config.primary_model.merged_labelmap[7] == "truck"
def test_audio_labelmap_inheritance_is_separate_from_model_labelmap(self):
config = deep_merge(
@@ -1174,7 +1363,7 @@ class TestConfig(unittest.TestCase):
70: "dogs",
75: "dogs",
}
assert frigate_config.model.merged_labelmap[69] != "dogs"
assert frigate_config.primary_model.merged_labelmap[69] != "dogs"
def test_default_labelmap_empty(self):
config = {
@@ -1199,12 +1388,12 @@ class TestConfig(unittest.TestCase):
}
frigate_config = FrigateConfig(**config)
assert frigate_config.model.merged_labelmap[0] == "person"
assert frigate_config.primary_model.merged_labelmap[0] == "person"
def test_default_labelmap(self):
config = {
"mqtt": {"host": "mqtt"},
"model": {"width": 320, "height": 320},
"models": [{"width": 320, "height": 320, "devices": ["cpu"]}],
"cameras": {
"back": {
"ffmpeg": {
@@ -1225,7 +1414,7 @@ class TestConfig(unittest.TestCase):
}
frigate_config = FrigateConfig(**config)
assert frigate_config.model.merged_labelmap[0] == "person"
assert frigate_config.primary_model.merged_labelmap[0] == "person"
def test_plus_labelmap(self):
with open(os.path.join(MODEL_CACHE_DIR, "test"), "w") as f:
@@ -1235,8 +1424,7 @@ class TestConfig(unittest.TestCase):
config = {
"mqtt": {"host": "mqtt"},
"detectors": {"cpu": {"type": "cpu"}},
"model": {"path": "plus://test"},
"models": [{"path": "plus://test", "devices": ["cpu"]}],
"cameras": {
"back": {
"ffmpeg": {
@@ -1257,7 +1445,7 @@ class TestConfig(unittest.TestCase):
}
frigate_config = FrigateConfig(**config)
assert frigate_config.model.merged_labelmap[0] == "amazon"
assert frigate_config.primary_model.merged_labelmap[0] == "amazon"
def test_fails_on_invalid_role(self):
config = {
+225
View File
@@ -0,0 +1,225 @@
"""Tests for migrating detectors and model into the models list."""
import logging
import os
import tempfile
import unittest
from unittest.mock import patch
from ruamel.yaml import YAML
from frigate.util.config import (
CURRENT_CONFIG_VERSION,
migrate_frigate_config,
migrate_models,
)
class TestMigrateModels(unittest.TestCase):
def test_single_cpu_detector(self):
migrated = migrate_models({"detectors": {"cpu": {"type": "cpu"}}})
self.assertEqual(migrated["models"], [{"scene": "all", "devices": ["cpu"]}])
self.assertNotIn("detectors", migrated)
def test_model_settings_are_carried_over(self):
migrated = migrate_models(
{
"detectors": {"coral": {"type": "edgetpu", "device": "pci:0"}},
"model": {"path": "plus://abc", "width": 320},
}
)
self.assertEqual(
migrated["models"],
[
{
"scene": "all",
"path": "plus://abc",
"width": 320,
"devices": ["edgetpu:pci:0"],
}
],
)
self.assertNotIn("model", migrated)
def test_multiple_corals_become_multiple_devices(self):
migrated = migrate_models(
{
"detectors": {
"coral1": {"type": "edgetpu", "device": "pci:0"},
"coral2": {"type": "edgetpu", "device": "pci:1"},
}
}
)
self.assertEqual(
migrated["models"][0]["devices"], ["edgetpu:pci:0", "edgetpu:pci:1"]
)
def test_several_detectors_on_one_device_stay_separate(self):
# a repeated device is now what running two inference processes on one
# piece of hardware looks like
migrated = migrate_models(
{
"detectors": {
"ov_0": {"type": "openvino", "device": "GPU"},
"ov_1": {"type": "openvino", "device": "GPU"},
}
}
)
self.assertEqual(
migrated["models"][0]["devices"], ["openvino:GPU", "openvino:GPU"]
)
def test_repeated_exclusive_devices_are_collapsed(self):
# two detectors both grabbing the first TPU was never really two TPUs
migrated = migrate_models(
{
"detectors": {
"coral_0": {"type": "edgetpu", "device": "usb"},
"coral_1": {"type": "edgetpu", "device": "usb"},
}
}
)
self.assertEqual(migrated["models"][0]["devices"], ["edgetpu:usb"])
def test_detectors_that_named_the_device_field_differently(self):
migrated = migrate_models(
{
"detectors": {
"rk": {"type": "rknn", "num_cores": 2},
}
}
)
self.assertEqual(migrated["models"][0]["devices"], ["rknn:2"])
def test_empty_edgetpu_device_is_kept(self):
# an empty device selects a native Coral, which is not the same as
# letting the delegate pick
migrated = migrate_models(
{"detectors": {"coral": {"type": "edgetpu", "device": ""}}}
)
self.assertEqual(migrated["models"][0]["devices"], ["edgetpu:"])
def test_model_path_overrides_the_model(self):
migrated = migrate_models(
{
"detectors": {
"coral": {"type": "edgetpu", "model_path": "/custom.tflite"}
},
"model": {"path": "/ignored.tflite", "width": 320},
}
)
self.assertEqual(migrated["models"][0]["path"], "/custom.tflite")
def test_no_detectors_falls_back_to_cpu(self):
migrated = migrate_models({"model": {"width": 320}})
self.assertEqual(migrated["models"][0]["devices"], ["cpu"])
def test_dropped_remote_detector_options_are_logged(self):
with self.assertLogs("frigate.util.config", level=logging.ERROR) as logs:
migrated = migrate_models(
{
"detectors": {
"ds": {
"type": "deepstack",
"api_url": "http://host:5000/v1/vision/detection",
"api_key": "secret",
}
}
}
)
self.assertEqual(
migrated["models"][0]["devices"],
["deepstack:http://host:5000/v1/vision/detection"],
)
self.assertTrue(any("api_key" in message for message in logs.output))
def test_mixed_detector_types_are_logged(self):
with self.assertLogs("frigate.util.config", level=logging.ERROR) as logs:
migrate_models(
{
"detectors": {
"ov": {"type": "openvino", "device": "GPU"},
"coral": {"type": "edgetpu", "device": "pci:0"},
}
}
)
self.assertTrue(any("more than one type" in message for message in logs.output))
def test_other_keys_are_untouched(self):
migrated = migrate_models(
{"mqtt": {"host": "mqtt"}, "detectors": {"cpu": {"type": "cpu"}}}
)
self.assertEqual(migrated["mqtt"], {"host": "mqtt"})
class TestMigrateConfigFile(unittest.TestCase):
"""The full file migration, which is gated on shape as well as version."""
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
self.addCleanup(self.temp_dir.cleanup)
self.config_file = os.path.join(self.temp_dir.name, "config.yml")
patcher = patch("frigate.util.config.CONFIG_DIR", self.temp_dir.name)
patcher.start()
self.addCleanup(patcher.stop)
def _migrate(self, config: str) -> dict:
with open(self.config_file, "w") as f:
f.write(config)
migrate_frigate_config(self.config_file)
with open(self.config_file) as f:
return YAML().load(f)
def test_migrates_a_config_already_stamped_with_the_current_version(self):
# 0.19 is unreleased, so a dev config can be current and still use
# the pre-models keys
migrated = self._migrate(
"mqtt:\n"
" enabled: false\n"
"detectors:\n"
" ov:\n"
" type: openvino\n"
" device: GPU\n"
"cameras: {}\n"
f"version: {CURRENT_CONFIG_VERSION}\n"
)
self.assertEqual(migrated["models"][0]["devices"], ["openvino:GPU"])
self.assertNotIn("detectors", migrated)
def test_a_migrated_config_is_left_alone(self):
migrated = self._migrate(
"mqtt:\n"
" enabled: false\n"
"models:\n"
" - scene: all\n"
" devices:\n"
" - openvino:GPU\n"
"cameras: {}\n"
f"version: {CURRENT_CONFIG_VERSION}\n"
)
self.assertEqual(
migrated["models"], [{"scene": "all", "devices": ["openvino:GPU"]}]
)
self.assertFalse(
os.path.exists(os.path.join(self.temp_dir.name, "backup_config.yaml"))
)
if __name__ == "__main__":
unittest.main(verbosity=2)
+2 -1
View File
@@ -9,6 +9,7 @@ from unittest.mock import MagicMock, patch
from frigate.debug_replay import DebugReplayManager
from frigate.jobs.debug_replay import (
DebugReplayJob,
NoRecordingsError,
RecordingDebugReplaySource,
cancel_debug_replay_job,
get_active_runner,
@@ -129,7 +130,7 @@ class TestStartDebugReplayJob(unittest.TestCase):
empty_qs = MagicMock()
empty_qs.count.return_value = 0
with patch("frigate.jobs.debug_replay.query_recordings", return_value=empty_qs):
with self.assertRaises(ValueError):
with self.assertRaises(NoRecordingsError):
start_debug_replay_job(
source=RecordingDebugReplaySource(
source_camera="front",
+94
View File
@@ -0,0 +1,94 @@
"""Tests for parsing detection hardware device strings."""
import unittest
from frigate.detectors.detector_config import ModelConfig
from frigate.detectors.device import (
DeviceParseError,
build_detector_config,
parse_device,
runner_names,
)
class TestParseDevice(unittest.TestCase):
def test_bare_detector_has_no_device(self):
spec = parse_device("cpu")
self.assertEqual(spec.detector, "cpu")
self.assertIsNone(spec.device)
def test_device_is_everything_after_the_first_colon(self):
spec = parse_device("edgetpu:pci:0")
self.assertEqual(spec.detector, "edgetpu")
self.assertEqual(spec.device, "pci:0")
def test_trailing_colon_keeps_an_empty_device(self):
# an empty edgetpu device selects a native Coral
spec = parse_device("edgetpu:")
self.assertEqual(spec.detector, "edgetpu")
self.assertEqual(spec.device, "")
def test_unknown_detector_is_rejected(self):
with self.assertRaises(DeviceParseError):
parse_device("not_a_detector:0")
def test_device_that_the_detector_cannot_use_is_rejected(self):
# tensorrt takes a gpu index
with self.assertRaises(DeviceParseError):
parse_device("tensorrt:the-fast-one")
class TestBuildDetectorConfig(unittest.TestCase):
def _build(self, raw: str):
return build_detector_config(parse_device(raw), ModelConfig())
def test_device_lands_on_the_detector_field(self):
for raw, expected in (
("edgetpu:usb", "usb"),
("edgetpu:pci:1", "pci:1"),
("openvino:GPU.1", "GPU.1"),
("onnx:CPU", "CPU"),
("memryx:PCIe:0", "PCIe:0"),
):
with self.subTest(raw=raw):
self.assertEqual(self._build(raw).device, expected)
def test_detectors_that_name_the_field_something_else(self):
self.assertEqual(self._build("cpu:4").num_threads, 4)
self.assertEqual(self._build("rknn:2").num_cores, 2)
def test_device_is_coerced_to_the_detector_field_type(self):
self.assertEqual(self._build("tensorrt:1").device, 1)
def test_omitted_device_falls_back_to_the_detector_default(self):
self.assertEqual(self._build("cpu").num_threads, 3)
self.assertEqual(self._build("rknn").num_cores, 0)
self.assertEqual(self._build("openvino").device, "AUTO")
self.assertIsNone(self._build("edgetpu").device)
def test_the_model_is_attached(self):
model = ModelConfig(path="/cpu_model.tflite")
self.assertIs(build_detector_config(parse_device("cpu"), model).model, model)
class TestRunnerNames(unittest.TestCase):
def test_unique_devices_keep_their_name(self):
devices = [parse_device("edgetpu:pci:0"), parse_device("edgetpu:pci:1")]
self.assertEqual(runner_names(devices), ["edgetpu:pci:0", "edgetpu:pci:1"])
def test_repeated_devices_are_numbered(self):
devices = [parse_device("openvino:GPU")] * 3
self.assertEqual(
runner_names(devices),
["openvino:GPU", "openvino:GPU#2", "openvino:GPU#3"],
)
if __name__ == "__main__":
unittest.main(verbosity=2)
+247
View File
@@ -0,0 +1,247 @@
"""Tests for detection hardware discovery."""
import os
import tempfile
import unittest
from unittest.mock import patch
from frigate.detectors import hardware
from frigate.detectors.detector_types import config_types
from frigate.detectors.hardware import HardwareProber
def write(path: str, content: str = "") -> None:
"""Create a file and any parent directories."""
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w") as f:
f.write(content)
class HardwareProbeTestCase(unittest.TestCase):
"""Points every probe at an empty fixture tree, so nothing is found by default."""
def setUp(self):
self.root = tempfile.TemporaryDirectory()
self.addCleanup(self.root.cleanup)
for name in ("SYS_ROOT", "DEV_ROOT", "PROC_ROOT", "ETC_ROOT"):
sub = os.path.join(self.root.name, name.split("_")[0].lower())
os.makedirs(sub, exist_ok=True)
patcher = patch.object(hardware, name, sub)
patcher.start()
self.addCleanup(patcher.stop)
setattr(self, name.lower(), sub)
drm = patch.object(hardware, "enumerate_drm_devices", return_value={})
self.drm = drm.start()
self.addCleanup(drm.stop)
def probe(self) -> dict[str, hardware.DetectionHardware]:
return {found.key: found for found in HardwareProber().probe()}
class TestNoHardware(HardwareProbeTestCase):
def test_only_the_cpu_is_reported(self):
self.assertEqual(list(self.probe()), ["cpu"])
def test_the_cpu_is_unlimited(self):
self.assertTrue(self.probe()["cpu"].unlimited)
class TestCoral(HardwareProbeTestCase):
def test_each_apex_device_is_a_unit(self):
for name in ("apex_0", "apex_1"):
os.makedirs(os.path.join(self.sys_root, "class", "apex", name))
coral = self.probe()["edgetpu:pci"]
self.assertEqual(coral.count, 2)
self.assertEqual(
[unit.device for unit in coral.units],
["edgetpu:pci:0", "edgetpu:pci:1"],
)
def test_a_coral_is_not_unlimited(self):
os.makedirs(os.path.join(self.sys_root, "class", "apex", "apex_0"))
self.assertFalse(self.probe()["edgetpu:pci"].unlimited)
def test_usb_corals_are_found_by_their_usb_ids(self):
usb = os.path.join(self.sys_root, "bus", "usb", "devices")
# a Coral reports as Global Unichip before its firmware loads
write(os.path.join(usb, "1-1", "idVendor"), "1a6e")
write(os.path.join(usb, "1-1", "idProduct"), "089a")
# and as Google afterwards
write(os.path.join(usb, "1-2", "idVendor"), "18d1")
write(os.path.join(usb, "1-2", "idProduct"), "9302")
coral = self.probe()["edgetpu:usb"]
self.assertEqual(coral.count, 2)
self.assertEqual(coral.units[0].device, "edgetpu:usb:0")
def test_other_usb_devices_are_ignored(self):
usb = os.path.join(self.sys_root, "bus", "usb", "devices")
write(os.path.join(usb, "1-1", "idVendor"), "046d")
write(os.path.join(usb, "1-1", "idProduct"), "0825")
self.assertNotIn("edgetpu:usb", self.probe())
class TestGpus(HardwareProbeTestCase):
def test_a_single_intel_gpu_is_the_unnumbered_device(self):
self.drm.return_value = {"0000:00:02.0": "i915"}
gpu = self.probe()["openvino:GPU"]
self.assertEqual([unit.device for unit in gpu.units], ["openvino:GPU"])
self.assertTrue(gpu.unlimited)
def test_multiple_intel_gpus_are_numbered(self):
self.drm.return_value = {"0000:00:02.0": "i915", "0000:03:00.0": "xe"}
gpu = self.probe()["openvino:GPU"]
self.assertEqual(
[unit.device for unit in gpu.units],
["openvino:GPU.0", "openvino:GPU.1"],
)
def test_non_gpu_drm_devices_are_ignored(self):
self.drm.return_value = {"0000:00:02.0": "virtio-mmio"}
self.assertNotIn("openvino:GPU", self.probe())
def test_amd_gpus_run_through_onnx(self):
self.drm.return_value = {"0000:03:00.0": "amdgpu"}
self.assertEqual(self.probe()["onnx:amd"].units[0].device, "onnx")
def test_an_intel_npu_is_found_by_its_driver(self):
accel = os.path.join(self.sys_root, "class", "accel", "accel0", "device")
os.makedirs(accel)
os.symlink("/drivers/intel_vpu", os.path.join(accel, "driver"))
self.assertEqual(self.probe()["openvino:NPU"].units[0].device, "openvino:NPU")
def test_other_accel_devices_are_ignored(self):
accel = os.path.join(self.sys_root, "class", "accel", "accel0", "device")
os.makedirs(accel)
os.symlink("/drivers/something_else", os.path.join(accel, "driver"))
self.assertNotIn("openvino:NPU", self.probe())
class TestNvidia(HardwareProbeTestCase):
def _add_gpu(self, address: str, model: str) -> None:
write(
os.path.join(
self.proc_root, "driver", "nvidia", "gpus", address, "information"
),
f"Model: \t {model}\nIRQ: \t 62\n",
)
def test_the_model_name_is_read_from_proc(self):
self._add_gpu("0000:01:00.0", "NVIDIA GeForce RTX 3060")
gpu = self.probe()["onnx:nvidia"]
self.assertEqual(gpu.name, "NVIDIA GeForce RTX 3060")
self.assertEqual(gpu.units[0].device, "onnx:0")
def test_multiple_gpus_are_indexed(self):
self._add_gpu("0000:01:00.0", "NVIDIA GeForce RTX 3060")
self._add_gpu("0000:02:00.0", "NVIDIA GeForce RTX 4090")
gpu = self.probe()["onnx:nvidia"]
self.assertEqual(gpu.name, "NVIDIA GPU")
self.assertEqual([unit.device for unit in gpu.units], ["onnx:0", "onnx:1"])
self.assertEqual(gpu.units[1].label, "NVIDIA GeForce RTX 4090")
def test_a_jetson_runs_through_tensorrt(self):
write(os.path.join(self.etc_root, "nv_tegra_release"), "# R36 (release)")
self.assertEqual(self.probe()["tensorrt"].units[0].device, "tensorrt:0")
class TestAccelerators(HardwareProbeTestCase):
def test_hailo_is_found_by_its_device_node(self):
write(os.path.join(self.dev_root, "hailo0"))
self.assertEqual(self.probe()["hailo8l"].units[0].device, "hailo8l:PCIe")
def test_each_memryx_node_is_a_unit(self):
write(os.path.join(self.dev_root, "memx0"))
write(os.path.join(self.dev_root, "memx1"))
memryx = self.probe()["memryx"]
self.assertEqual(
[unit.device for unit in memryx.units],
["memryx:PCIe:0", "memryx:PCIe:1"],
)
self.assertFalse(memryx.unlimited)
def test_a_supported_rockchip_soc_is_reported(self):
write(
os.path.join(self.proc_root, "device-tree", "compatible"),
"rockchip,rk3588\x00",
)
self.assertEqual(self.probe()["rknn"].units[0].device, "rknn")
def test_an_unsupported_soc_is_ignored(self):
write(
os.path.join(self.proc_root, "device-tree", "compatible"),
"nvidia,tegra\x00",
)
self.assertNotIn("rknn", self.probe())
def test_axengine_is_found_by_its_control_node(self):
write(os.path.join(self.dev_root, "axcl_host"))
self.assertEqual(self.probe()["axengine"].units[0].device, "axengine")
def test_synaptics_is_found_by_its_device_node(self):
write(os.path.join(self.dev_root, "synap"))
self.assertEqual(self.probe()["synaptics"].units[0].device, "synaptics")
class TestProber(HardwareProbeTestCase):
def test_the_result_is_cached_until_refreshed(self):
prober = HardwareProber()
self.assertNotIn("edgetpu:pci", {found.key for found in prober.probe()})
os.makedirs(os.path.join(self.sys_root, "class", "apex", "apex_0"))
self.assertNotIn("edgetpu:pci", {found.key for found in prober.probe()})
self.assertIn(
"edgetpu:pci", {found.key for found in prober.probe(refresh=True)}
)
def test_a_failing_probe_does_not_break_the_rest(self):
with patch.object(hardware, "detect_hailo", side_effect=OSError("boom")):
self.assertIn("cpu", self.probe())
def test_unlimited_tracks_the_detector_shareable_flag(self):
for name in ("apex_0",):
os.makedirs(os.path.join(self.sys_root, "class", "apex", name))
write(os.path.join(self.dev_root, "memx0"))
self.drm.return_value = {"0000:00:02.0": "i915"}
for found in self.probe().values():
config_class = config_types.get(found.detector)
if config_class is None:
continue
with self.subTest(hardware=found.key):
self.assertEqual(found.unlimited, config_class.shareable)
if __name__ == "__main__":
unittest.main(verbosity=2)
+9 -9
View File
@@ -108,7 +108,7 @@ class TestGpuStats(unittest.TestCase):
@patch("frigate.util.services.time.sleep")
@patch("frigate.util.services.time.monotonic")
@patch("frigate.util.services._read_intel_drm_fdinfo")
@patch("frigate.util.services._enumerate_drm_devices")
@patch("frigate.util.services.enumerate_drm_devices")
def test_intel_gpu_stats_fdinfo(
self, drm_devices, read_fdinfo, monotonic, sleep, get_names
):
@@ -187,7 +187,7 @@ class TestGpuStats(unittest.TestCase):
@patch("frigate.util.services.time.sleep")
@patch("frigate.util.services.time.monotonic")
@patch("frigate.util.services._read_intel_drm_fdinfo")
@patch("frigate.util.services._enumerate_drm_devices")
@patch("frigate.util.services.enumerate_drm_devices")
def test_intel_gpu_stats_xe_capacity(
self, drm_devices, read_fdinfo, monotonic, sleep, get_names
):
@@ -246,7 +246,7 @@ class TestGpuStats(unittest.TestCase):
@patch("frigate.stats.intel_gpu_info.intel_gpu_name_resolver.get_names")
@patch("frigate.util.services.time.sleep")
@patch("frigate.util.services._read_intel_drm_fdinfo")
@patch("frigate.util.services._enumerate_drm_devices")
@patch("frigate.util.services.enumerate_drm_devices")
def test_intel_gpu_stats_no_clients_reports_idle(
self, drm_devices, read_fdinfo, sleep, get_names
):
@@ -274,7 +274,7 @@ class TestGpuStats(unittest.TestCase):
@patch("frigate.util.services.time.sleep")
@patch("frigate.util.services._read_intel_drm_fdinfo")
@patch("frigate.util.services._enumerate_drm_devices")
@patch("frigate.util.services.enumerate_drm_devices")
def test_intel_gpu_stats_clients_without_engine_counters(
self, drm_devices, read_fdinfo, sleep
):
@@ -301,7 +301,7 @@ class TestGpuStats(unittest.TestCase):
read_fdinfo.assert_called_once()
@patch("frigate.util.services._read_intel_drm_fdinfo")
@patch("frigate.util.services._enumerate_drm_devices")
@patch("frigate.util.services.enumerate_drm_devices")
def test_intel_gpu_stats_no_intel_device(self, drm_devices, read_fdinfo):
# Only a non-Intel GPU is visible in sysfs; /proc is never scanned
drm_devices.return_value = {"0000:01:00.0": "nvidia"}
@@ -310,7 +310,7 @@ class TestGpuStats(unittest.TestCase):
read_fdinfo.assert_not_called()
@patch("frigate.util.services._read_intel_drm_fdinfo")
@patch("frigate.util.services._enumerate_drm_devices")
@patch("frigate.util.services.enumerate_drm_devices")
@patch("frigate.util.services._resolve_intel_gpu_pdev")
def test_intel_gpu_stats_unresolvable_device_hint(
self, resolve_pdev, drm_devices, read_fdinfo
@@ -324,7 +324,7 @@ class TestGpuStats(unittest.TestCase):
read_fdinfo.assert_not_called()
@patch("frigate.util.services._read_intel_drm_fdinfo")
@patch("frigate.util.services._enumerate_drm_devices")
@patch("frigate.util.services.enumerate_drm_devices")
@patch("frigate.util.services._resolve_intel_gpu_pdev")
def test_intel_gpu_stats_hint_resolves_to_non_intel_gpu(
self, resolve_pdev, drm_devices, read_fdinfo
@@ -342,7 +342,7 @@ class TestGpuStats(unittest.TestCase):
read_fdinfo.assert_not_called()
@patch("frigate.util.services._read_intel_drm_fdinfo")
@patch("frigate.util.services._enumerate_drm_devices")
@patch("frigate.util.services.enumerate_drm_devices")
def test_intel_gpu_stats_unreadable_proc(self, drm_devices, read_fdinfo):
# A scan failure (None) is a different condition than a scan that
# finds no clients ({}) and must not report idle
@@ -355,7 +355,7 @@ class TestGpuStats(unittest.TestCase):
@patch("frigate.util.services.time.sleep")
@patch("frigate.util.services.time.monotonic")
@patch("frigate.util.services._read_intel_drm_fdinfo")
@patch("frigate.util.services._enumerate_drm_devices")
@patch("frigate.util.services.enumerate_drm_devices")
def test_intel_gpu_stats_clients_lost_between_samples(
self, drm_devices, read_fdinfo, monotonic, sleep, get_names
):
+101 -3
View File
@@ -36,7 +36,10 @@ class TestHttp(unittest.TestCase):
"front_door": {
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.1:554/video", "roles": ["detect"]}
{
"path": "rtsp://10.0.0.1:554/video",
"roles": ["detect", "record"],
}
]
},
"detect": {
@@ -44,6 +47,7 @@ class TestHttp(unittest.TestCase):
"width": 1920,
"fps": 5,
},
"record": {"enabled": True},
}
},
}
@@ -53,7 +57,10 @@ class TestHttp(unittest.TestCase):
"front_door": {
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.1:554/video", "roles": ["detect"]}
{
"path": "rtsp://10.0.0.1:554/video",
"roles": ["detect", "record"],
}
]
},
"detect": {
@@ -61,11 +68,15 @@ class TestHttp(unittest.TestCase):
"width": 1920,
"fps": 5,
},
"record": {"enabled": True},
},
"back_door": {
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.2:554/video", "roles": ["detect"]}
{
"path": "rtsp://10.0.0.2:554/video",
"roles": ["detect", "record"],
}
]
},
"detect": {
@@ -73,6 +84,7 @@ class TestHttp(unittest.TestCase):
"width": 1920,
"fps": 5,
},
"record": {"enabled": True},
},
},
}
@@ -326,6 +338,92 @@ class TestHttp(unittest.TestCase):
assert streams[STREAM_TYPE_SUB]["bandwidth"] is None
assert streams[STREAM_TYPE_MAIN]["bandwidth"] == 7200
def test_expected_bandwidth_follows_recording_config(self):
"""Only the streams a camera currently records count toward cleanup."""
config = FrigateConfig(**self.minimal_config)
storage = StorageMaintainer(config, MagicMock())
record = config.cameras["front_door"].record
time_keep = datetime.datetime.now().timestamp()
_insert_mock_recording(
"1234567.frontdoor",
os.path.join(self.test_dir, "main.tmp"),
time_keep,
time_keep + 10,
seg_size=20,
seg_dur=10,
)
_insert_mock_recording(
"1234568.frontdoor",
os.path.join(self.test_dir, "sub.tmp"),
time_keep,
time_keep + 10,
seg_size=2,
seg_dur=10,
stream_type=STREAM_TYPE_SUB,
)
storage.calculate_camera_bandwidth()
# sub segments are still on disk but the camera no longer records them
assert storage.expected_hourly_bandwidth() == 7200
record.sub.enabled = True
assert storage.expected_hourly_bandwidth() == 7920
record.enabled = False
assert storage.expected_hourly_bandwidth() == 0
def test_stream_stays_dirty_until_it_has_its_own_samples(self):
"""A stream enabled before its first segment keeps the camera dirty.
The record config update can be handled on a tick before ffmpeg has
written anything, so a camera-wide segment count would settle the camera
on the strength of the other stream's history and never measure the new
stream at all.
"""
config = FrigateConfig(**self.minimal_config)
storage = StorageMaintainer(config, MagicMock())
record = config.cameras["front_door"].record
stats = storage.camera_storage_stats
time_keep = datetime.datetime.now().timestamp()
for i in range(60):
_insert_mock_recording(
f"main_{i}.frontdoor",
os.path.join(self.test_dir, f"main_{i}.tmp"),
time_keep + i * 10,
time_keep + i * 10 + 10,
seg_size=20,
seg_dur=10,
)
storage.calculate_camera_bandwidth()
assert stats["front_door"]["needs_refresh"] is False
record.sub.enabled = True
stats["front_door"]["needs_refresh"] = True
storage.calculate_camera_bandwidth()
assert stats["front_door"]["needs_refresh"] is True
assert storage.expected_hourly_bandwidth() == 7200
for i in range(60):
_insert_mock_recording(
f"sub_{i}.frontdoor",
os.path.join(self.test_dir, f"sub_{i}.tmp"),
time_keep + 5000 + i * 10,
time_keep + 5000 + i * 10 + 10,
seg_size=2,
seg_dur=10,
stream_type=STREAM_TYPE_SUB,
)
storage.calculate_camera_bandwidth()
assert stats["front_door"]["needs_refresh"] is False
assert storage.expected_hourly_bandwidth() == 7920
def test_camera_usages_with_no_recordings(self):
"""A camera with no segments reports zero usage and no streams."""
config = FrigateConfig(**self.minimal_config)
+1 -1
View File
@@ -210,7 +210,7 @@ class TrackedObjectProcessor(threading.Thread):
if obj.obj_data.get("sub_label"):
sub_label = obj.obj_data["sub_label"][0]
if sub_label in self.config.model.all_attribute_logos:
if sub_label in self.config.all_attribute_logos:
self.dispatcher.publish(
f"{camera}/{sub_label}/snapshot",
jpg_bytes,
+113 -1
View File
@@ -23,6 +23,25 @@ logger = logging.getLogger(__name__)
CURRENT_CONFIG_VERSION = "0.19-0"
DEFAULT_CONFIG_FILE = os.path.join(CONFIG_DIR, "config.yml")
# the detector field that used to hold the device, for detectors that named it
# something other than "device"
DETECTOR_DEVICE_FIELDS = {
"cpu": "num_threads",
"rknn": "num_cores",
"deepstack": "api_url",
"degirum": "location",
"zmq": "endpoint",
}
# detector options that have no equivalent in a device string. The remote
# detectors that use them are being reworked, so they are dropped rather than
# carried over.
DROPPED_DETECTOR_OPTIONS = {
"deepstack": ["api_timeout", "api_key"],
"degirum": ["zoo", "token"],
"zmq": ["request_timeout_ms", "linger_ms"],
}
def resolve_ffmpeg_path(path: str, binary: str = "ffmpeg") -> str:
"""Resolve an ffmpeg version alias or custom path to a binary path.
@@ -87,7 +106,11 @@ def migrate_frigate_config(config_file: str):
previous_version = str(config.get("version", "0.13"))
if previous_version == CURRENT_CONFIG_VERSION:
# 0.19 is unreleased, so a config may already be stamped with the current
# version and still use the pre-models detectors and model keys
needs_models = "detectors" in config or "model" in config
if previous_version == CURRENT_CONFIG_VERSION and not needs_models:
logger.info("frigate config does not need migration...")
return
@@ -155,6 +178,12 @@ def migrate_frigate_config(config_file: str):
yaml.dump(new_config, f)
previous_version = "0.19-0"
if needs_models:
logger.info("Migrating frigate detectors and model to models...")
new_config = migrate_models(new_config)
with open(config_file, "w") as f:
yaml.dump(new_config, f)
logger.info("Finished frigate config migration...")
@@ -708,6 +737,89 @@ def migrate_019_0(config: dict[str, dict[str, Any]]) -> dict[str, dict[str, Any]
return new_config
def migrate_models(config: dict[str, dict[str, Any]]) -> dict[str, dict[str, Any]]:
"""Merge the detectors and model keys into a single models list.
Every config before this change ran one model across all of its detectors,
so this always produces exactly one model.
Args:
config: The loaded config
Returns:
The config with a models list in place of detectors and model
"""
# imported lazily so loading the detector plugins is not a cost of importing
# this module
from frigate.detectors.detector_types import config_types
new_config = config.copy()
detectors: dict[str, Any] = new_config.pop("detectors", None) or {}
model: dict[str, Any] = new_config.pop("model", None) or {}
devices: list[str] = []
model_path: str | None = None
for name, detector in detectors.items():
detector = detector or {}
detector_type = detector.get("type", "cpu")
device = detector.get(DETECTOR_DEVICE_FIELDS.get(detector_type, "device"))
device_string = detector_type if device is None else f"{detector_type}:{device}"
# repeating a device now means running an extra inference process on it,
# which is what several detectors on one device used to mean. Only
# collapse repeats of hardware that can serve a single process.
config_class = config_types.get(detector_type)
shareable = config_class.shareable if config_class else True
if shareable or device_string not in devices:
devices.append(device_string)
dropped = [
option
for option in DROPPED_DETECTOR_OPTIONS.get(detector_type, [])
if option in detector
]
if dropped:
logger.error(
"Detector '%s' had the %s options set, which are no longer supported and have been removed",
name,
", ".join(dropped),
)
detector_model_path = detector.get("model_path")
if detector_model_path:
if model_path is None:
model_path = detector_model_path
elif model_path != detector_model_path:
logger.warning(
"Detector '%s' set a different model_path than an earlier detector, using '%s' for the migrated model",
name,
model_path,
)
detector_types = {device.partition(":")[0] for device in devices}
if len(detector_types) > 1:
logger.error(
"Detectors of more than one type (%s) were configured. A model now runs on one detector type, so the migrated config will need to be corrected by hand",
", ".join(sorted(detector_types)),
)
entry: dict[str, Any] = {"scene": "all", **model}
if model_path:
entry["path"] = model_path
# a config with no detectors ran a single cpu detector
entry["devices"] = devices or ["cpu"]
new_config["models"] = [entry]
return new_config
def get_relative_coordinates(
mask: str | list | None,
frame_shape: tuple[int, int],
+3 -3
View File
@@ -47,10 +47,10 @@ def get_categorized_object_names(
"""
tracked_objects = _get_tracked_objects(config, allowed_cameras)
names: dict[str, set[str]] = {}
logos = set(config.model.all_attribute_logos)
logos = set(config.all_attribute_logos)
# 1. detector logo attributes, only for objects that are actually tracked
for label, label_attributes in config.model.attributes_map.items():
for label, label_attributes in config.all_attributes_map.items():
if label not in tracked_objects:
continue
@@ -126,7 +126,7 @@ def _objects_with_attribute(
"""
objects = {
label
for label, label_attributes in config.model.attributes_map.items()
for label, label_attributes in config.all_attributes_map.items()
if attribute in label_attributes and label in tracked_objects
}
+9 -38
View File
@@ -2,45 +2,16 @@
from typing import Any
from pydantic import BaseModel, TypeAdapter
from pydantic import BaseModel
def get_config_schema(config_class: type[BaseModel]) -> dict[str, Any]:
"""Get the JSON schema for FrigateConfig.
Args:
config_class: The config model to describe
Returns:
The JSON schema
"""
Returns the JSON schema for FrigateConfig with polymorphic detectors.
This utility patches the FrigateConfig schema to include the full polymorphic
definitions for detectors. By default, Pydantic's schema for Dict[str, BaseDetectorConfig]
only includes the base class fields. This function replaces it with a reference
to the DetectorConfig union, which includes all available detector subclasses.
"""
# Import here to ensure all detector plugins are loaded through the detectors module
from frigate.detectors import DetectorConfig
# Get the base schema for FrigateConfig
schema = config_class.model_json_schema()
# Get the schema for the polymorphic DetectorConfig union
detector_adapter: TypeAdapter = TypeAdapter(DetectorConfig)
detector_schema = detector_adapter.json_schema()
# Ensure $defs exists in FrigateConfig schema
if "$defs" not in schema:
schema["$defs"] = {}
# Merge $defs from DetectorConfig into FrigateConfig schema
# This includes the specific schemas for each detector plugin (OvDetectorConfig, etc.)
if "$defs" in detector_schema:
schema["$defs"].update(detector_schema["$defs"])
# Extract the union schema (oneOf/discriminator) and add it as a definition
detector_union_schema = {k: v for k, v in detector_schema.items() if k != "$defs"}
schema["$defs"]["DetectorConfig"] = detector_union_schema
# Update the 'detectors' property to use the polymorphic DetectorConfig definition
if "detectors" in schema.get("properties", {}):
schema["properties"]["detectors"]["additionalProperties"] = {
"$ref": "#/$defs/DetectorConfig"
}
return schema
return config_class.model_json_schema()
+2 -2
View File
@@ -315,7 +315,7 @@ def _resolve_intel_gpu_pdev(device: str | None) -> str | None:
return pdev if _PCI_ADDRESS_RE.match(pdev) else None
def _enumerate_drm_devices() -> dict[str, str]:
def enumerate_drm_devices() -> dict[str, str]:
"""Map each PCI-attached DRM device to its bound kernel driver.
Reads /sys/class/drm, which reflects every GPU on the host even when only
@@ -517,7 +517,7 @@ def get_intel_gpu_stats(
)
return None
drm_devices = _enumerate_drm_devices()
drm_devices = enumerate_drm_devices()
intel_pdevs = {
pdev: driver
for pdev, driver in drm_devices.items()
+2
View File
@@ -56,6 +56,7 @@ from frigate.api import (
debug_replay,
event,
export,
hardware,
media,
motion_search,
notification,
@@ -150,6 +151,7 @@ def build_app() -> FastAPI:
preview.router,
notification.router,
export.router,
hardware.router,
event.router,
media.router,
motion_search.router,
+6 -85
View File
@@ -210,78 +210,6 @@ def generate_section_translation(config_class: type) -> dict[str, Any]:
return extract_translations_from_schema(schema)
def get_detector_translations(
config_schema: dict[str, Any],
) -> tuple[dict[str, Any], dict[str, Any], set[str]]:
"""Build detector type translations with nested fields based on schema definitions.
Returns a tuple of (type_translations, shared_fields, nested_field_keys).
Shared fields (identical across all detector types) are returned separately
to avoid duplication in the output.
"""
defs = config_schema.get("$defs", {})
detector_schema = defs.get("DetectorConfig", {})
discriminator = detector_schema.get("discriminator", {})
mapping = discriminator.get("mapping", {})
# First pass: collect all nested fields per detector type
all_nested: dict[str, dict[str, Any]] = {}
type_meta: dict[str, dict[str, str]] = {}
for detector_type, ref in mapping.items():
if not isinstance(ref, str) or not ref.startswith("#/$defs/"):
continue
ref_name = ref.split("/")[-1]
ref_schema = defs.get(ref_name, {})
if not ref_schema:
continue
meta: dict[str, str] = {}
title = ref_schema.get("title")
description = ref_schema.get("description")
if title:
meta["label"] = title
if description:
meta["description"] = description
type_meta[detector_type] = meta
nested = extract_translations_from_schema(ref_schema, defs=defs)
all_nested[detector_type] = {
k: v for k, v in nested.items() if k not in ("label", "description")
}
# Find fields that are identical across all types that have them
shared_fields: dict[str, Any] = {}
if all_nested:
# Collect all field keys across all types
all_keys: set[str] = set()
for nested in all_nested.values():
all_keys.update(nested.keys())
for key in all_keys:
values = [nested[key] for nested in all_nested.values() if key in nested]
if len(values) == len(all_nested) and all(v == values[0] for v in values):
shared_fields[key] = values[0]
# Build per-type translations with only unique (non-shared) fields
type_translations: dict[str, Any] = {}
nested_field_keys: set[str] = set()
for detector_type, nested in all_nested.items():
type_entry: dict[str, Any] = {}
type_entry.update(type_meta.get(detector_type, {}))
unique_fields = {k: v for k, v in nested.items() if k not in shared_fields}
if unique_fields:
type_entry.update(unique_fields)
nested_field_keys.update(unique_fields.keys())
if type_entry:
type_translations[detector_type] = type_entry
return type_translations, shared_fields, nested_field_keys
def main():
"""Main function to generate config translations."""
@@ -337,6 +265,12 @@ def main():
if args and len(args) > 1:
field_type = args[1] # Get value type from Dict[key, value]
# Handle List[SomeModel] - extract the item type
if origin is list:
args = get_args(field_type)
if args:
field_type = args[0]
# Start with field's top-level metadata (label, description)
section_data = get_field_translations(field_info)
@@ -351,19 +285,6 @@ def main():
}
section_data.update(nested_without_root)
if field_name == "detectors":
detector_types, shared_fields, detector_field_keys = (
get_detector_translations(config_schema)
)
# Add shared fields at the base detectors level
section_data.update(shared_fields)
# Add per-type translations (only unique fields per type)
section_data.update(detector_types)
for key in detector_field_keys:
if key == "type":
continue
section_data.pop(key, None)
if field_name == "objects":
# Produce a parallel `filters_attribute` block alongside `filters`,
# with object-wording rewritten for attribute filters (face,
+2 -2
View File
@@ -122,7 +122,7 @@ class ProcessClip:
self.camera_name,
self.frame_queue,
self.frame_shape,
self.config.model,
self.config.model_for_camera(self.camera_name),
self.camera_config.detect,
self.frame_manager,
motion_detector,
@@ -248,7 +248,7 @@ def process(path, label, output, debug_path):
json_config = {
"mqtt": {"enabled": False},
"detectors": {"coral": {"type": "edgetpu", "device": "usb"}},
"models": [{"devices": ["edgetpu:usb"]}],
"cameras": {
"camera": {
"ffmpeg": {
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -102,11 +102,12 @@ def generate_config():
snapshot = config.model_dump()
# Runtime-computed fields not in the Pydantic dump
all_attrs = set()
for attrs in snapshot.get("model", {}).get("attributes_map", {}).values():
all_attrs.update(attrs)
snapshot["model"]["all_attributes"] = sorted(all_attrs)
snapshot["model"]["colormap"] = {}
for model in snapshot.get("models", []):
all_attrs = set()
for attrs in model.get("attributes_map", {}).values():
all_attrs.update(attrs)
model["all_attributes"] = sorted(all_attrs)
model["colormap"] = {}
return snapshot
+39
View File
@@ -0,0 +1,39 @@
/**
* Detection hardware as reported by GET /api/hardware/probe.
*
* A mixed payload on purpose: two Corals exercise the per-unit checkboxes and
* the "already used by another model" state, while the Intel GPU exercises the
* unlimited detector-count dropdown.
*/
export const DETECTION_HARDWARE = [
{
key: "edgetpu:pci",
detector: "edgetpu",
name: "Coral EdgeTPU (PCIe)",
units: [
{ device: "edgetpu:pci:0", label: "PCIe 0" },
{ device: "edgetpu:pci:1", label: "PCIe 1" },
],
count: 2,
unlimited: false,
},
{
key: "openvino:GPU",
detector: "openvino",
name: "Intel GPU",
units: [
{ device: "openvino:GPU.0", label: "0000:00:02.0" },
{ device: "openvino:GPU.1", label: "0000:03:00.0" },
],
count: 2,
unlimited: true,
},
{
key: "cpu",
detector: "cpu",
name: "CPU",
units: [{ device: "cpu", label: "CPU" }],
count: 1,
unlimited: true,
},
];
+7
View File
@@ -14,6 +14,7 @@ import {
type DeepPartial,
configFactory,
} from "../fixtures/mock-data/config";
import { DETECTION_HARDWARE } from "../fixtures/mock-data/hardware";
import { adminProfile, type UserProfile } from "../fixtures/mock-data/profile";
import { BASE_STATS, statsFactory } from "../fixtures/mock-data/stats";
@@ -41,6 +42,7 @@ export interface ApiMockOverrides {
faces?: Record<string, unknown>;
configRaw?: string;
configSchema?: Record<string, unknown>;
hardware?: unknown[];
}
export class ApiMocker {
@@ -178,6 +180,11 @@ export class ApiMocker {
route.fulfill({ json: { success: true, require_restart: false } }),
);
// Detection hardware discovery
await this.page.route("**/api/hardware/probe**", (route) =>
route.fulfill({ json: overrides?.hardware ?? DETECTION_HARDWARE }),
);
// Go2RTC streams
await this.page.route("**/api/go2rtc/streams**", (route) =>
route.fulfill({ json: {} }),
@@ -0,0 +1,404 @@
/**
* Detection models settings page tests -- HIGH tier.
*
* Covers picking hardware per model: exclusive units (Corals) are checkboxes
* that can only be claimed by one model, unlimited hardware (a GPU) gets a
* detector-count dropdown, and the whole models list saves in one PUT.
*/
import { readFileSync } from "node:fs";
import { resolve, dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { test, expect } from "../../fixtures/frigate-test";
import type { Page } from "@playwright/test";
import { configFactory } from "../../fixtures/mock-data/config";
const __dirname = dirname(fileURLToPath(import.meta.url));
const CONFIG_SCHEMA = JSON.parse(
readFileSync(
resolve(__dirname, "../../fixtures/mock-data/config-schema.json"),
"utf-8",
),
);
const PAGE = "/settings?page=systemDetectorsAndModel";
type Model = {
scene: string;
devices: string[];
path?: string | null;
input_tensor?: string;
input_pixel_format?: string;
input_dtype?: string;
model_type?: string;
labelmap?: Record<string, string>;
attributes_map?: Record<string, string[]>;
plus?: { id: string; name: string } | null;
width?: number;
height?: number;
};
const PLUS_MODEL = {
id: "abc123",
name: "yolov9-s",
baseModel: "yolov9",
trainDate: "2026-01-02T03:04:05Z",
isBaseModel: true,
supportedDetectors: ["openvino"],
width: 320,
height: 320,
};
type SavedConfig = { config_data?: { models?: Model[] } };
async function installRoutes(page: Page, models: Model[], plusEnabled = false) {
const config = configFactory({
models,
plus: { enabled: plusEnabled },
} as never);
const saves: SavedConfig[] = [];
await page.route("**/api/config/schema.json", (route) =>
route.fulfill({ json: CONFIG_SCHEMA }),
);
await page.route("**/api/config", (route) =>
route.request().method() === "GET"
? route.fulfill({ json: config })
: route.fulfill({ json: { success: true } }),
);
await page.route("**/api/config/raw_paths", (route) =>
route.fulfill({ json: { models } }),
);
await page.route("**/api/plus/models", (route) =>
route.fulfill({ json: [PLUS_MODEL] }),
);
await page.route("**/api/config/set", async (route) => {
saves.push(route.request().postDataJSON() as SavedConfig);
await route.fulfill({ json: { success: true, require_restart: false } });
});
return saves;
}
const openPage = async (frigateApp: {
goto: (url: string) => Promise<void>;
}) => {
await frigateApp.goto(PAGE);
};
test.describe("Detection models settings @high", () => {
test("renders a card per configured model", async ({ frigateApp }) => {
await installRoutes(frigateApp.page, [
{ scene: "all", devices: ["cpu"] },
{ scene: "outdoor", devices: ["edgetpu:pci:0"] },
]);
await openPage(frigateApp);
const root = frigateApp.page.locator("#pageRoot");
await expect(root).toContainText("All cameras");
await expect(root).toContainText("Outdoor");
});
test("unlimited hardware offers a detector count", async ({ frigateApp }) => {
await installRoutes(frigateApp.page, [
{ scene: "all", devices: ["openvino:GPU.0"] },
]);
await openPage(frigateApp);
await expect(
frigateApp.page.getByText("Detectors", { exact: true }),
).toBeVisible();
// the trigger shows the bare count; the recommendation is a second line on
// the matching option, so the dropdown has to be open to see it
await expect(
frigateApp.page.locator("#models-0-detector-count"),
).toHaveText("1");
await frigateApp.page.locator("#models-0-detector-count").click();
// three cameras in the mock config, so one detector is recommended
await expect(
frigateApp.page.getByRole("option", {
name: /Recommended for 3 cameras/,
}),
).toHaveText(/^1/);
});
test("a detector count above the recommendation is unlabelled", async ({
frigateApp,
}) => {
await installRoutes(frigateApp.page, [
{ scene: "all", devices: ["openvino:GPU.0", "openvino:GPU.0"] },
]);
await openPage(frigateApp);
// two detectors are configured while one is recommended, so neither the
// trigger nor the selected option carries a recommendation
await expect(
frigateApp.page.locator("#models-0-detector-count"),
).toHaveText("2");
await frigateApp.page.locator("#models-0-detector-count").click();
await expect(
frigateApp.page.getByRole("option", { name: /^2/ }),
).not.toContainText("Recommended");
});
test("exclusive hardware offers one checkbox per unit", async ({
frigateApp,
}) => {
await installRoutes(frigateApp.page, [
{ scene: "all", devices: ["edgetpu:pci:0"] },
]);
await openPage(frigateApp);
await expect(
frigateApp.page.locator("#models-0-edgetpu\\:pci\\:0"),
).toBeChecked();
await expect(
frigateApp.page.locator("#models-0-edgetpu\\:pci\\:1"),
).not.toBeChecked();
});
test("a unit claimed by another model cannot be picked", async ({
frigateApp,
}) => {
await installRoutes(frigateApp.page, [
{ scene: "all", devices: ["edgetpu:pci:0"] },
{ scene: "outdoor", devices: ["edgetpu:pci:1"] },
]);
await openPage(frigateApp);
// the first card's checkbox for the unit the second model holds
await expect(
frigateApp.page.locator("#models-0-edgetpu\\:pci\\:1").first(),
).toBeDisabled();
});
test("adding a model appends a card with an unused scene", async ({
frigateApp,
}) => {
await installRoutes(frigateApp.page, [{ scene: "all", devices: ["cpu"] }]);
await openPage(frigateApp);
await frigateApp.page.getByRole("button", { name: "Add model" }).click();
// "all" is taken, so the new card takes the next available scene
await expect(frigateApp.page.locator("#pageRoot")).toContainText("Indoor");
});
test("hardware is summarized rather than listed device by device", async ({
frigateApp,
}) => {
await installRoutes(frigateApp.page, [
{ scene: "all", devices: ["openvino:GPU.0", "openvino:GPU.0"] },
]);
await openPage(frigateApp);
await expect(frigateApp.page.locator("#pageRoot")).toContainText(
"Intel GPU (2) \u2022 3 cameras",
);
await expect(frigateApp.page.locator("#pageRoot")).not.toContainText(
"openvino:GPU, openvino:GPU",
);
});
test("a saved Frigate+ model opens on the Frigate+ tab", async ({
frigateApp,
}) => {
// the backend resolves plus:// to a cache path before serving the config
// back, so the plus metadata is the only signal the model is a Plus one
await installRoutes(
frigateApp.page,
[
{
scene: "all",
devices: ["openvino:GPU.0"],
path: "/config/model_cache/abc123",
plus: PLUS_MODEL,
},
],
true,
);
await openPage(frigateApp);
await expect(
frigateApp.page.getByRole("tab", { name: "Frigate+" }),
).toHaveAttribute("data-state", "active");
await expect(frigateApp.page.locator("#pageRoot")).toContainText(
"yolov9-s",
);
});
test("picking a Frigate+ model stays on the tab and saves a plus path", async ({
frigateApp,
}) => {
const saves = await installRoutes(
frigateApp.page,
[
{
scene: "all",
devices: ["openvino:GPU.0"],
path: "/config/custom.onnx",
},
],
true,
);
await openPage(frigateApp);
await frigateApp.page.getByRole("tab", { name: "Frigate+" }).click();
await frigateApp.page.getByRole("combobox").last().click();
await frigateApp.page.getByRole("option").first().click();
await expect(
frigateApp.page.getByRole("tab", { name: "Frigate+" }),
).toHaveAttribute("data-state", "active");
await frigateApp.page.getByRole("button", { name: /^Save$/ }).click();
await expect.poll(() => saves.length).toBeGreaterThan(0);
expect(saves.at(-1)?.config_data?.models?.[0].path).toBe("plus://abc123");
});
test("a freshly opened page is not reported as modified", async ({
frigateApp,
}) => {
// `/api/config` serializes with exclude_none, so a nullable field such as
// labelmap_path is absent rather than null. The form materializes it, and
// that must not read as an edit.
await installRoutes(frigateApp.page, [
{
scene: "all",
devices: ["openvino:GPU.0", "openvino:GPU.0"],
path: "/config/model_cache/abc123",
width: 320,
height: 320,
input_tensor: "nchw",
input_pixel_format: "rgb",
input_dtype: "float",
model_type: "yolo-generic",
labelmap: {},
attributes_map: {},
},
]);
await openPage(frigateApp);
await expect(
frigateApp.page.getByRole("button", { name: /^Save$/ }),
).toBeVisible();
await expect(frigateApp.page.getByText("Modified")).toHaveCount(0);
});
test("the scene, hardware and detector count fields are described", async ({
frigateApp,
}) => {
await installRoutes(frigateApp.page, [
{ scene: "all", devices: ["openvino:GPU.0"] },
]);
await openPage(frigateApp);
const root = frigateApp.page.locator("#pageRoot");
await expect(root).toContainText("The environment this model is for");
await expect(root).toContainText(
"The hardware this model runs its detection on",
);
await expect(root).toContainText("How many detection processes to run");
});
test("per unit hardware explains why a claimed unit is unavailable", async ({
frigateApp,
}) => {
await installRoutes(frigateApp.page, [
{ scene: "all", devices: ["edgetpu:pci:0"] },
]);
await openPage(frigateApp);
// the count dropdown is replaced by checkboxes, so it gets its own copy
await expect(frigateApp.page.locator("#pageRoot")).toContainText(
"Each unit runs its own detection process",
);
});
test("removing the default model blocks saving", async ({ frigateApp }) => {
// a camera that names no scene runs the "all" model, so deleting it would
// leave those cameras with nothing to fall back to
await installRoutes(frigateApp.page, [
{ scene: "all", devices: ["cpu"] },
{ scene: "outdoor", devices: ["edgetpu:pci:0"] },
]);
await openPage(frigateApp);
await frigateApp.page
.getByRole("button", { name: "Delete" })
.first()
.click();
await expect(frigateApp.page.locator("#pageRoot")).toContainText(
"One model must use a scene of 'All cameras'",
);
await expect(
frigateApp.page.getByRole("button", { name: /^Save$/ }),
).toBeDisabled();
});
test("a second GPU can be assigned to a model", async ({ frigateApp }) => {
// shareable hardware can report several addressable units; every one of
// them must be reachable, not just the first
const saves = await installRoutes(frigateApp.page, [
{ scene: "all", devices: ["openvino:GPU.0"] },
]);
await openPage(frigateApp);
await frigateApp.page.locator("#models-0-openvino\\:GPU\\.1").click();
await frigateApp.page.getByRole("button", { name: /^Save$/ }).click();
await expect.poll(() => saves.length).toBeGreaterThan(0);
expect(saves.at(-1)?.config_data?.models?.[0].devices).toEqual([
"openvino:GPU.0",
"openvino:GPU.1",
]);
});
test("detectors are spread across every selected GPU", async ({
frigateApp,
}) => {
const saves = await installRoutes(frigateApp.page, [
{ scene: "all", devices: ["openvino:GPU.0", "openvino:GPU.1"] },
]);
await openPage(frigateApp);
await frigateApp.page.locator("#models-0-detector-count").click();
await frigateApp.page
.getByRole("option", { name: "4", exact: true })
.click();
await frigateApp.page.getByRole("button", { name: /^Save$/ }).click();
await expect.poll(() => saves.length).toBeGreaterThan(0);
expect(saves.at(-1)?.config_data?.models?.[0].devices).toEqual([
"openvino:GPU.0",
"openvino:GPU.1",
"openvino:GPU.0",
"openvino:GPU.1",
]);
});
test("saving writes the whole models list in one request", async ({
frigateApp,
}) => {
const saves = await installRoutes(frigateApp.page, [
{ scene: "all", devices: ["edgetpu:pci:0"] },
]);
await openPage(frigateApp);
await frigateApp.page.locator("#models-0-edgetpu\\:pci\\:1").click();
await frigateApp.page.getByRole("button", { name: /^Save$/ }).click();
await expect.poll(() => saves.length).toBeGreaterThan(0);
const models = saves.at(-1)?.config_data?.models;
expect(models).toHaveLength(1);
expect(models?.[0].devices).toEqual(["edgetpu:pci:0", "edgetpu:pci:1"]);
});
});
@@ -1,55 +0,0 @@
/**
* Detectors and model settings page tests -- HIGH tier.
*
* Tests rendering of the merged page and navigation from the Frigate+ page.
*/
import { test, expect } from "../../fixtures/frigate-test";
test.describe("Detectors and model Settings @high", () => {
test("page renders with detector and model cards", async ({ frigateApp }) => {
await frigateApp.goto("/settings?page=systemDetectorsAndModel");
await frigateApp.page.waitForTimeout(2000);
await expect(frigateApp.page.locator("#pageRoot")).toBeVisible();
const text = await frigateApp.page.textContent("#pageRoot");
expect(text).toContain("Detectors and model");
expect(text?.toLowerCase()).toContain("detector hardware");
expect(text?.toLowerCase()).toContain("detection model");
});
test("Frigate+ page links to the merged page", async ({ frigateApp }) => {
await frigateApp.goto("/settings?page=frigateplus");
await frigateApp.page.waitForTimeout(2000);
const button = frigateApp.page.getByRole("button", {
name: /Change in Detectors and model/,
});
// Button only appears when Frigate+ is enabled in the test config; skip
// the click assertion if it's not present.
if ((await button.count()) > 0) {
await button.first().click();
await frigateApp.page.waitForURL(/page=systemDetectorsAndModel/);
await expect(frigateApp.page.locator("#pageRoot")).toContainText(
"Detectors and model",
);
} else {
test.skip(
true,
"Frigate+ not enabled in this test config; skipping link assertion",
);
}
});
test("old systemDetectionModel deep-link no longer routes here", async ({
frigateApp,
}) => {
await frigateApp.goto("/settings?page=systemDetectionModel");
await frigateApp.page.waitForTimeout(2000);
// The old page key is no longer in allSettingsViews; the router
// falls back to its default settings page (uiSettings).
const text = await frigateApp.page.textContent("#pageRoot");
expect(text).not.toContain("Detection model");
});
});
@@ -0,0 +1,444 @@
/**
* UI settings import/export tests -- MEDIUM tier.
*
* Covers exporting browser-stored layouts, streaming settings, and UI
* preferences to a file, and importing that file back.
*/
import { readFileSync } from "node:fs";
import { test, expect } from "../../fixtures/frigate-test";
import type { Page } from "@playwright/test";
// the mocked profile is admin, so useUserPersistence keys are namespaced
const OUTDOOR_LAYOUT_KEY = "outdoor-draggable-layout:admin";
const STREAMING_KEY = "streaming-settings:admin";
const OUTDOOR_LAYOUT = [
{ i: "front_door", x: 0, y: 0, w: 6, h: 4 },
{ i: "backyard", x: 6, y: 0, w: 6, h: 4 },
];
const STREAMING_SETTINGS = {
outdoor: {
front_door: {
streamName: "front_door",
streamType: "smart",
compatibilityMode: false,
playAudio: false,
volume: 1,
},
},
};
async function writeIdb(page: Page, entries: Record<string, unknown>) {
await page.evaluate(async (data) => {
await new Promise<void>((resolve, reject) => {
const request = indexedDB.open("keyval-store", 1);
request.onupgradeneeded = () =>
request.result.createObjectStore("keyval");
request.onerror = () => reject(request.error);
request.onsuccess = () => {
const tx = request.result.transaction("keyval", "readwrite");
const store = tx.objectStore("keyval");
Object.entries(data).forEach(([key, value]) => store.put(value, key));
tx.oncomplete = () => resolve();
tx.onerror = () => reject(tx.error);
};
});
}, entries);
}
async function readIdb(page: Page, key: string) {
return page.evaluate(async (target) => {
return new Promise((resolve, reject) => {
const request = indexedDB.open("keyval-store", 1);
request.onupgradeneeded = () =>
request.result.createObjectStore("keyval");
request.onerror = () => reject(request.error);
request.onsuccess = () => {
const tx = request.result.transaction("keyval", "readonly");
const get = tx.objectStore("keyval").get(target);
get.onsuccess = () => resolve(get.result ?? null);
get.onerror = () => reject(get.error);
};
});
}, key);
}
function importPayload(overrides: Record<string, unknown> = {}) {
return {
type: "frigate-ui-settings",
version: 1,
exported_at: "2026-08-17T14:22:31.000Z",
frigate_version: "0.15.0-test",
sections: {
layouts: { outdoor: OUTDOOR_LAYOUT, patio: OUTDOOR_LAYOUT },
streaming: STREAMING_SETTINGS,
preferences: { playbackRate: 2, weekStartsOn: 1 },
},
...overrides,
};
}
async function chooseImportText(page: Page, contents: string) {
await page.locator('input[type="file"]').setInputFiles({
name: "frigate-ui-settings-2026-08-17.json",
mimeType: "application/json",
buffer: Buffer.from(contents),
});
}
async function chooseImportFile(page: Page, payload: unknown) {
await chooseImportText(page, JSON.stringify(payload));
}
async function confirmImport(page: Page) {
// Import triggers a real window.location.reload(), not a client-side
// route change, so #pageRoot is already present and waiting for it
// alone can race the navigation. Wait for the "load" event first so
// the evaluate() calls below run against the post-reload document.
await Promise.all([
page.waitForEvent("load"),
page.getByRole("button", { name: "Import" }).click(),
]);
await page.waitForSelector("#pageRoot");
}
async function clearIdb(page: Page) {
await page.evaluate(async () => {
await new Promise<void>((resolve, reject) => {
const request = indexedDB.open("keyval-store", 1);
request.onupgradeneeded = () =>
request.result.createObjectStore("keyval");
request.onerror = () => reject(request.error);
request.onsuccess = () => {
const tx = request.result.transaction("keyval", "readwrite");
tx.objectStore("keyval").clear();
tx.oncomplete = () => resolve();
tx.onerror = () => reject(tx.error);
};
});
});
}
test.describe("UI settings import/export @medium", () => {
test("exports stored layouts, streaming settings, and preferences", async ({
frigateApp,
}) => {
await frigateApp.goto("/settings?page=uiSettings");
await writeIdb(frigateApp.page, {
[OUTDOOR_LAYOUT_KEY]: OUTDOOR_LAYOUT,
[STREAMING_KEY]: STREAMING_SETTINGS,
"playbackRate:admin": 2,
});
const downloadPromise = frigateApp.page.waitForEvent("download");
await frigateApp.page
.getByRole("button", { name: "Export Settings" })
.click();
const download = await downloadPromise;
expect(download.suggestedFilename()).toMatch(
/^frigate-ui-settings-\d{4}-\d{2}-\d{2}\.json$/,
);
const path = await download.path();
const payload = JSON.parse(readFileSync(path!, "utf-8"));
expect(payload.type).toBe("frigate-ui-settings");
expect(payload.version).toBe(1);
expect(payload.frigate_version).toBe("0.15.0-test");
expect(payload.sections.layouts.outdoor).toEqual(OUTDOOR_LAYOUT);
expect(payload.sections.streaming).toEqual(STREAMING_SETTINGS);
expect(payload.sections.preferences.playbackRate).toBe(2);
});
test("omits settings that were never stored", async ({ frigateApp }) => {
await frigateApp.goto("/settings?page=uiSettings");
const downloadPromise = frigateApp.page.waitForEvent("download");
await frigateApp.page
.getByRole("button", { name: "Export Settings" })
.click();
const download = await downloadPromise;
const path = await download.path();
const payload = JSON.parse(readFileSync(path!, "utf-8"));
expect(payload.sections.layouts).toEqual({});
expect(payload.sections.streaming).toEqual({});
expect(payload.sections.preferences.playbackRate).toBeUndefined();
});
test("imports every section and reloads", async ({ frigateApp }) => {
await frigateApp.goto("/settings?page=uiSettings");
await chooseImportFile(frigateApp.page, importPayload());
await expect(
frigateApp.page.getByText("Camera group layouts (2 groups)"),
).toBeVisible();
await expect(
frigateApp.page.getByText("Streaming settings (1 camera)"),
).toBeVisible();
await expect(
frigateApp.page.getByText("UI preferences (2 settings)"),
).toBeVisible();
await expect(frigateApp.page.getByText(/patio/)).toBeVisible();
await confirmImport(frigateApp.page);
expect(await readIdb(frigateApp.page, OUTDOOR_LAYOUT_KEY)).toEqual(
OUTDOOR_LAYOUT,
);
expect(await readIdb(frigateApp.page, STREAMING_KEY)).toEqual(
STREAMING_SETTINGS,
);
expect(await readIdb(frigateApp.page, "playbackRate:admin")).toBe(2);
});
test("hides the unknown-group warning when layouts are switched off", async ({
frigateApp,
}) => {
await frigateApp.goto("/settings?page=uiSettings");
// patio is a layout-only group absent from this server, so the warning
// is meaningful only while the layouts section is being imported
await chooseImportFile(frigateApp.page, importPayload());
const warning = frigateApp.page.getByText(/patio/);
await expect(warning).toBeVisible();
await frigateApp.page.getByText("Camera group layouts (2 groups)").click();
await expect(warning).toBeHidden();
await frigateApp.page.getByText("Camera group layouts (2 groups)").click();
await expect(warning).toBeVisible();
});
test("leaves a section untouched when its switch is off", async ({
frigateApp,
}) => {
await frigateApp.goto("/settings?page=uiSettings");
await writeIdb(frigateApp.page, { [STREAMING_KEY]: {} });
await chooseImportFile(frigateApp.page, importPayload());
await frigateApp.page.getByText("Streaming settings (1 camera)").click();
await confirmImport(frigateApp.page);
expect(await readIdb(frigateApp.page, STREAMING_KEY)).toEqual({});
expect(await readIdb(frigateApp.page, OUTDOOR_LAYOUT_KEY)).toEqual(
OUTDOOR_LAYOUT,
);
});
test("rejects a file that is not a Frigate settings export", async ({
frigateApp,
}) => {
await frigateApp.goto("/settings?page=uiSettings");
await chooseImportFile(frigateApp.page, { type: "something-else" });
await expect(
frigateApp.page.getByText(
"Failed to import settings: file is not a Frigate settings export",
),
).toBeVisible();
expect(await readIdb(frigateApp.page, OUTDOOR_LAYOUT_KEY)).toBeNull();
});
test("ignores preference keys that are not in the registry", async ({
frigateApp,
}) => {
await frigateApp.goto("/settings?page=uiSettings");
await chooseImportFile(
frigateApp.page,
importPayload({
sections: {
layouts: {},
streaming: {},
preferences: { playbackRate: 2, notARealSetting: "malicious" },
},
}),
);
await expect(
frigateApp.page.getByText("UI preferences (1 setting)"),
).toBeVisible();
await confirmImport(frigateApp.page);
expect(await readIdb(frigateApp.page, "notARealSetting")).toBeNull();
expect(await readIdb(frigateApp.page, "notARealSetting:admin")).toBeNull();
expect(await readIdb(frigateApp.page, "playbackRate:admin")).toBe(2);
});
test("round trips an export back through import", async ({ frigateApp }) => {
await frigateApp.goto("/settings?page=uiSettings");
await writeIdb(frigateApp.page, {
[OUTDOOR_LAYOUT_KEY]: OUTDOOR_LAYOUT,
[STREAMING_KEY]: STREAMING_SETTINGS,
"playbackRate:admin": 2,
"weekStartsOn:admin": 1,
});
const downloadPromise = frigateApp.page.waitForEvent("download");
await frigateApp.page
.getByRole("button", { name: "Export Settings" })
.click();
const download = await downloadPromise;
const filename = download.suggestedFilename();
const path = await download.path();
const bytes = readFileSync(path!);
await clearIdb(frigateApp.page);
// A plain page.reload() is not enough here: the settings view strips
// the `page` query param from the URL (history.replaceState) shortly
// after mount, and on mobile that param is what reopens the uiSettings
// drawer. Re-navigate with the param present so the reload is a real
// fresh load of the uiSettings pane rather than the settings list.
await frigateApp.goto("/settings?page=uiSettings");
// A no-op import would still pass every assertion below, so prove the
// store is actually empty before feeding the export back in.
expect(await readIdb(frigateApp.page, OUTDOOR_LAYOUT_KEY)).toBeNull();
await frigateApp.page.locator('input[type="file"]').setInputFiles({
name: filename,
mimeType: "application/json",
buffer: Buffer.from(bytes),
});
await expect(
frigateApp.page.getByText("UI preferences (2 settings)"),
).toBeVisible();
await confirmImport(frigateApp.page);
expect(await readIdb(frigateApp.page, OUTDOOR_LAYOUT_KEY)).toEqual(
OUTDOOR_LAYOUT,
);
expect(await readIdb(frigateApp.page, STREAMING_KEY)).toEqual(
STREAMING_SETTINGS,
);
expect(await readIdb(frigateApp.page, "playbackRate:admin")).toBe(2);
expect(await readIdb(frigateApp.page, "weekStartsOn:admin")).toBe(1);
});
test("merges imported streaming settings with existing groups and cameras", async ({
frigateApp,
}) => {
// indoor is a group the file never mentions. outdoor.backyard is a
// camera inside a group the file DOES mention, so a shallow group-level
// merge would silently drop it. Both must survive.
const EXISTING_STREAMING = {
indoor: {
garage: {
streamName: "garage",
streamType: "continuous",
compatibilityMode: true,
playAudio: true,
volume: 0.5,
},
},
outdoor: {
backyard: {
streamName: "backyard",
streamType: "no-streaming",
compatibilityMode: false,
playAudio: false,
volume: 0,
},
},
};
await frigateApp.goto("/settings?page=uiSettings");
await writeIdb(frigateApp.page, { [STREAMING_KEY]: EXISTING_STREAMING });
await chooseImportFile(frigateApp.page, importPayload());
await expect(
frigateApp.page.getByText("Streaming settings (1 camera)"),
).toBeVisible();
await confirmImport(frigateApp.page);
expect(await readIdb(frigateApp.page, STREAMING_KEY)).toEqual({
indoor: EXISTING_STREAMING.indoor,
outdoor: {
...EXISTING_STREAMING.outdoor,
...STREAMING_SETTINGS.outdoor,
},
});
});
test("rejects a file that is not valid JSON", async ({ frigateApp }) => {
await frigateApp.goto("/settings?page=uiSettings");
await chooseImportText(frigateApp.page, "this is not json");
await expect(
frigateApp.page.getByText(
"Failed to import settings: file is not valid JSON",
),
).toBeVisible();
expect(await readIdb(frigateApp.page, OUTDOOR_LAYOUT_KEY)).toBeNull();
});
test("rejects a file from a newer version of Frigate", async ({
frigateApp,
}) => {
await frigateApp.goto("/settings?page=uiSettings");
await chooseImportFile(frigateApp.page, importPayload({ version: 99 }));
await expect(
frigateApp.page.getByText(
"Failed to import settings: file requires a newer version of Frigate",
),
).toBeVisible();
expect(await readIdb(frigateApp.page, OUTDOOR_LAYOUT_KEY)).toBeNull();
});
test("rejects a file whose sections are malformed", async ({
frigateApp,
}) => {
await frigateApp.goto("/settings?page=uiSettings");
// correct type and version, so this only fails at schema validation
await chooseImportFile(
frigateApp.page,
importPayload({
sections: { layouts: "nope", streaming: {}, preferences: {} },
}),
);
await expect(
frigateApp.page.getByText("Failed to import settings: file is malformed"),
).toBeVisible();
expect(await readIdb(frigateApp.page, OUTDOOR_LAYOUT_KEY)).toBeNull();
});
test("rejects a valid file that carries no settings", async ({
frigateApp,
}) => {
await frigateApp.goto("/settings?page=uiSettings");
await chooseImportFile(
frigateApp.page,
importPayload({
sections: { layouts: {}, streaming: {}, preferences: {} },
}),
);
await expect(
frigateApp.page.getByText(
"Failed to import settings: file contains no settings",
),
).toBeVisible();
expect(await readIdb(frigateApp.page, OUTDOOR_LAYOUT_KEY)).toBeNull();
});
});
+2
View File
@@ -129,6 +129,8 @@
"saving": "Saving…",
"cancel": "Cancel",
"close": "Close",
"expand": "Expand",
"collapse": "Collapse",
"copy": "Copy",
"copiedToClipboard": "Copied to clipboard",
"back": "Back",
@@ -98,6 +98,10 @@
"label": "Detect width",
"description": "Width (pixels) of frames used for the detect stream; leave empty to use the native stream resolution."
},
"scene": {
"label": "Detect scene",
"description": "The environment this camera looks at, used to pick which of the configured models runs on it. Cameras left on 'all' run the model configured with a scene of 'all'."
},
"fps": {
"label": "Detect FPS",
"description": "Desired frames per second to run detection on; lower values reduce CPU usage (recommended value is 5, only set higher - at most 10 - if tracking extremely fast moving objects)."
+13 -164
View File
@@ -275,172 +275,17 @@
"description": "Unit system for display (metric or imperial) used in the UI and MQTT."
}
},
"detectors": {
"label": "Detector hardware",
"description": "Configuration for object detectors (CPU, GPU, ONNX backends) and any detector-specific model settings.",
"type": {
"label": "Type"
"models": {
"label": "Detection models",
"description": "Object detection models and the hardware each one runs on. Cameras pick a model by matching their detect.scene against a model's scene.",
"scene": {
"label": "Model scene",
"description": "The camera environment this model is used for. Cameras select a model by setting detect.scene to a matching value, and 'all' is used by any camera that does not set one."
},
"model": {
"label": "Detector specific model configuration",
"description": "Detector-specific model configuration options (path, input size, etc.).",
"path": {
"label": "Custom object detector model path",
"description": "Path to a custom detection model file (or plus://<model_id> for Frigate+ models)."
},
"labelmap_path": {
"label": "Label map for custom object detector",
"description": "Path to a labelmap file that maps numeric classes to string labels for the detector."
},
"width": {
"label": "Object detection model input width",
"description": "Width of the model input tensor in pixels."
},
"height": {
"label": "Object detection model input height",
"description": "Height of the model input tensor in pixels."
},
"labelmap": {
"label": "Labelmap customization",
"description": "Overrides or remapping entries to merge into the standard labelmap."
},
"attributes_map": {
"label": "Map of object labels to their attribute labels",
"description": "Mapping from object labels to attribute labels used to attach metadata (for example 'car' -> ['license_plate'])."
},
"input_tensor": {
"label": "Model Input Tensor Shape",
"description": "Tensor format expected by the model: 'nhwc' or 'nchw'."
},
"input_pixel_format": {
"label": "Model Input Pixel Color Format",
"description": "Pixel colorspace expected by the model: 'rgb', 'bgr', or 'yuv'."
},
"input_dtype": {
"label": "Model Input D Type",
"description": "Data type of the model input tensor (for example 'float32')."
},
"model_type": {
"label": "Object Detection Model Type",
"description": "Detector model architecture type (ssd, yolox, yolonas, yolo-generic, rfdetr, dfine) used by some detectors for optimization."
}
"devices": {
"label": "Detection hardware",
"description": "Hardware this model runs on, as '<detector>' or '<detector>:<device>' (for example 'edgetpu:pci:0' or 'openvino:GPU'). Listing the same device more than once runs additional inference processes on it."
},
"model_path": {
"label": "Detector specific model path",
"description": "File path to the detector model binary if required by the chosen detector."
},
"axengine": {
"label": "AXEngine NPU",
"description": "AXERA AX650N/AX8850N NPU detector running compiled .axmodel files via the AXEngine runtime."
},
"cpu": {
"label": "CPU",
"description": "CPU TFLite detector that runs TensorFlow Lite models on the host CPU without hardware acceleration. Not recommended.",
"num_threads": {
"label": "Number of detection threads",
"description": "The number of threads used for CPU-based inference."
}
},
"deepstack": {
"label": "DeepStack",
"description": "DeepStack/CodeProject.AI detector that sends images to a remote DeepStack HTTP API for inference. Not recommended.",
"api_url": {
"label": "DeepStack API URL",
"description": "The URL of the DeepStack API."
},
"api_timeout": {
"label": "DeepStack API timeout (in seconds)",
"description": "Maximum time allowed for a DeepStack API request."
},
"api_key": {
"label": "DeepStack API key (if required)",
"description": "Optional API key for authenticated DeepStack services."
}
},
"edgetpu": {
"label": "EdgeTPU",
"description": "EdgeTPU detector that runs TensorFlow Lite models compiled for Coral EdgeTPU using the EdgeTPU delegate.",
"device": {
"label": "Device Type",
"description": "The device to use for EdgeTPU inference (e.g. 'usb', 'pci')."
}
},
"hailo8l": {
"label": "Hailo-8/Hailo-8L",
"description": "Hailo-8/Hailo-8L detector using HEF models and the HailoRT SDK for inference on Hailo hardware.",
"device": {
"label": "Device Type",
"description": "The device to use for Hailo inference (e.g. 'PCIe', 'M.2')."
}
},
"memryx": {
"label": "MemryX",
"description": "MemryX MX3 detector that runs compiled DFP models on MemryX accelerators.",
"device": {
"label": "Device Path",
"description": "The device to use for MemryX inference (e.g. 'PCIe')."
}
},
"onnx": {
"label": "ONNX",
"description": "ONNX detector for running ONNX models; will use available acceleration backends (CUDA/ROCm/OpenVINO) when available.",
"device": {
"label": "Device Type",
"description": "The device to use for ONNX inference (e.g. 'AUTO', 'CPU', 'GPU')."
}
},
"openvino": {
"label": "OpenVINO",
"description": "OpenVINO detector for AMD and Intel CPUs, Intel GPUs and Intel VPU hardware.",
"device": {
"label": "Device Type",
"description": "The device to use for OpenVINO inference (e.g. 'CPU', 'GPU', 'NPU')."
}
},
"rknn": {
"label": "RKNN",
"description": "RKNN detector for Rockchip NPUs; runs compiled RKNN models on Rockchip hardware.",
"num_cores": {
"label": "Number of NPU cores to use.",
"description": "The number of NPU cores to use (0 for auto)."
}
},
"synaptics": {
"label": "Synaptics",
"description": "Synaptics NPU detector for models in .synap format using the Synap SDK on Synaptics hardware."
},
"teflon_tfl": {
"label": "Teflon",
"description": "Teflon delegate detector for TFLite using Mesa Teflon delegate library to accelerate inference on supported GPUs."
},
"tensorrt": {
"label": "TensorRT",
"description": "TensorRT detector for Nvidia Jetson devices using serialized TensorRT engines for accelerated inference.",
"device": {
"label": "GPU Device Index",
"description": "The GPU device index to use."
}
},
"zmq": {
"label": "ZMQ IPC",
"description": "ZMQ IPC detector that offloads inference to an external process via a ZeroMQ IPC endpoint.",
"endpoint": {
"label": "ZMQ IPC endpoint",
"description": "The ZMQ endpoint to connect to."
},
"request_timeout_ms": {
"label": "ZMQ request timeout in milliseconds",
"description": "Timeout for ZMQ requests in milliseconds."
},
"linger_ms": {
"label": "ZMQ socket linger in milliseconds",
"description": "Socket linger period in milliseconds."
}
}
},
"model": {
"label": "Detection model",
"description": "Settings to configure a custom object detection model and its input shape.",
"path": {
"label": "Custom object detector model path",
"description": "Path to a custom detection model file (or plus://<model_id> for Frigate+ models)."
@@ -621,6 +466,10 @@
"label": "Detect width",
"description": "Width (pixels) of frames used for the detect stream; leave empty to use the native stream resolution."
},
"scene": {
"label": "Detect scene",
"description": "The environment this camera looks at, used to pick which of the configured models runs on it. Cameras left on 'all' run the model configured with a scene of 'all'."
},
"fps": {
"label": "Detect FPS",
"description": "Desired frames per second to run detection on; lower values reduce CPU usage (recommended value is 5, only set higher - at most 10 - if tracking extremely fast moving objects)."
@@ -31,5 +31,8 @@
},
"detect": {
"dimensionMustBeEven": "Must be an even number."
},
"models": {
"defaultRequired": "One model must use a scene of 'All cameras'. Without it, any camera that does not choose a scene has no model to fall back to."
}
}
+1
View File
@@ -21,6 +21,7 @@
"toast": {
"error": "Failed to start debug replay: {{error}}",
"alreadyActive": "A replay session is already active",
"noRecordings": "No recordings found in the selected time range",
"stopError": "Failed to stop debug replay: {{error}}",
"goToReplay": "Go to Replay"
}
+77 -42
View File
@@ -75,7 +75,7 @@
"systemTelemetry": "Telemetry",
"systemBirdseye": "Birdseye",
"systemFfmpeg": "FFmpeg",
"systemDetectorsAndModel": "Detectors and model",
"systemDetectorsAndModel": "Detection models",
"systemMqtt": "MQTT",
"systemGo2rtcStreams": "go2rtc streams",
"integrationSemanticSearch": "Semantic search",
@@ -179,6 +179,31 @@
"desc": "Streaming settings for each camera group are stored in your browser's local storage.",
"clearAll": "Clear All Streaming Settings"
},
"backupRestore": {
"title": "Backup & Restore",
"transfer": {
"label": "Device Settings File",
"desc": "Camera group layouts, streaming settings, and UI preferences are stored in your browser. Export them to a file to back them up or to move them to another device.",
"export": "Export Settings",
"import": "Import Settings"
},
"importDialog": {
"title": "Import Settings",
"desc": "Choose what to apply from this file. Frigate will reload when the import finishes.",
"exportedFrom": "Exported {{date}} from Frigate config version {{version}}",
"layouts_one": "Camera group layouts ({{count}} group)",
"layouts_other": "Camera group layouts ({{count}} groups)",
"streaming_one": "Streaming settings ({{count}} camera)",
"streaming_other": "Streaming settings ({{count}} cameras)",
"preferences_one": "UI preferences ({{count}} setting)",
"preferences_other": "UI preferences ({{count}} settings)",
"unknownGroups_one": "Camera group {{groups}} is not on this server. Its settings will be saved but unused.",
"unknownGroups_other": "Camera groups {{groups}} are not on this server. Their settings will be saved but unused.",
"unknownCameras_one": "Camera {{cameras}} is not on this server. Its streaming settings will be ignored.",
"unknownCameras_other": "Cameras {{cameras}} are not on this server. Their streaming settings will be ignored.",
"confirm": "Import"
}
},
"recordingsViewer": {
"title": "Recordings Viewer",
"defaultPlaybackRate": {
@@ -198,11 +223,20 @@
"toast": {
"success": {
"clearStoredLayout": "Cleared stored layout for {{cameraName}}",
"clearStreamingSettings": "Cleared streaming settings for all camera groups."
"clearStreamingSettings": "Cleared streaming settings for all camera groups.",
"exportUiSettings": "Exported settings",
"importUiSettings": "Imported settings"
},
"error": {
"clearStoredLayoutFailed": "Failed to clear stored layout: {{errorMessage}}",
"clearStreamingSettingsFailed": "Failed to clear streaming settings: {{errorMessage}}"
"clearStreamingSettingsFailed": "Failed to clear streaming settings: {{errorMessage}}",
"exportUiSettingsFailed": "Failed to export settings",
"importNothingToApply": "Failed to import settings: file contains no settings",
"importInvalidJson": "Failed to import settings: file is not valid JSON",
"importWrongType": "Failed to import settings: file is not a Frigate settings export",
"importUnsupportedVersion": "Failed to import settings: file requires a newer version of Frigate",
"importInvalidSchema": "Failed to import settings: file is malformed",
"importUiSettingsFailed": "Failed to import settings"
}
}
},
@@ -1272,31 +1306,6 @@
"error": "Failed to save config changes: {{errorMessage}}"
}
},
"detectorsAndModel": {
"title": "Detectors and model",
"description": "Configure the detector backend that runs object detection and the model it uses. Changes are saved together so the detector and model stay in sync.",
"cardTitles": {
"detector": "Detector Hardware",
"model": "Detection Model"
},
"tabs": {
"plus": "Frigate+",
"custom": "Custom Model"
},
"mismatch": {
"warning": "The current Frigate+ model \"{{model}}\" requires the {{required}} detector. Pick a compatible model below or switch to Custom Model before saving."
},
"plusModel": {
"requiresDetector": "Requires: {{detector}}",
"noModelSelected": "Select a Frigate+ model"
},
"toast": {
"saveSuccess": "Detectors and model settings saved. Restart Frigate to apply changes.",
"saveError": "Failed to save detector and model settings"
},
"unsavedChanges": "Unsaved detector and model changes",
"restartRequired": "Restart required (detector or model changed)"
},
"triggers": {
"documentTitle": "Triggers",
"semanticSearch": {
@@ -1616,16 +1625,6 @@
"detect": {
"title": "Detection Settings"
},
"detectors": {
"title": "Detector Settings",
"singleType": "Only one {{type}} detector is allowed.",
"keyRequired": "Detector name is required.",
"keyDuplicate": "Detector name already exists.",
"noSchema": "No detector schemas are available.",
"none": "No detector instances configured.",
"add": "Add detector",
"addCustomKey": "Add custom key"
},
"record": {
"title": "Recording Settings"
},
@@ -1943,6 +1942,7 @@
"detect": {
"fpsGreaterThanFive": "Setting the detect FPS higher than 5 is not recommended. Higher values may cause performance issues and will not provide any benefit.",
"disabled": "Object detection is disabled. Snapshots, review items, and enrichments such as face recognition, license plate recognition, and Generative AI will not function.",
"sceneWithoutModel": "No detection model is configured for this scene, so this camera falls back to the model with a scene of 'All cameras'. Add a model for this scene to give the camera its own.",
"resolutionShouldBeMultipleOfFour": "For best results, detect width and height should be multiples of 4. Other even values may produce visual artifacts or slight distortion in the detect stream.",
"aspectRatioMismatch": "The width and height you've entered don't match the aspect ratio of your current detect resolution. This may produce a stretched or distorted image.",
"maxFramesSet": "Setting max frames overrides default behavior and disables stationary object tracking. There are very few situations where this is needed, use with caution.",
@@ -1981,15 +1981,50 @@
"snapshots": {
"detectDisabled": "Object detection is disabled. Snapshots are generated from tracked objects and will not be created."
},
"detectors": {
"mixedTypes": "All detectors must use the same type. Remove existing detectors to use a different type.",
"mixedTypesSuggestion": "All detectors must use the same type. Remove existing detectors or select {{type}}."
},
"semanticSearch": {
"jinav2SmallModelSize": "The 'small' size with the Jina V2 model has high RAM and inference cost. The 'large' model with a discrete GPU is recommended."
},
"onvif": {
"autotrackingNoZones": "Autotracking requires at least one zone. Define a zone for this camera in Masks / Zones, then set it as a required zone below."
}
},
"detectionModels": {
"title": "Detection models",
"description": "Configure the object detection models and the hardware each one runs on. Cameras choose a model by their scene in the camera's detect settings.",
"addModel": "Add model",
"cameras_one": "{{count}} camera",
"cameras_other": "{{count}} cameras",
"scene": {
"label": "Scene",
"description": "The environment this model is for. Cameras pick a model by setting the same scene in their detect settings, and the model with a scene of all is used by any camera that does not set one."
},
"scenes": {
"all": "All cameras",
"indoor": "Indoor",
"outdoor": "Outdoor",
"indoor_thermal": "Indoor thermal",
"outdoor_thermal": "Outdoor thermal"
},
"hardware": {
"label": "Hardware",
"placeholder": "Select hardware",
"loading": "Looking for detection hardware...",
"none": "No hardware selected",
"claimedBy": "used by {{scene}}",
"detectorCount": "Detectors",
"countRecommended_one": "Recommended for {{count}} camera",
"countRecommended_other": "Recommended for {{count}} cameras",
"unrecognized": "This model is configured for hardware that was not found on this system: {{devices}}",
"description": "The hardware this model runs its detection on.",
"detectorCountDescription": "How many detection processes to run on this hardware. More detectors keep up with more cameras, at the cost of extra device memory.",
"unitsDescription": "Each unit runs its own detection process. A unit already used by another model can not be selected."
},
"tabs": {
"plus": "Frigate+",
"custom": "Custom Model"
},
"plusModel": {
"noModelSelected": "Select a Frigate+ model"
}
}
}
+3 -6
View File
@@ -13,6 +13,7 @@ import { cn } from "@/lib/utils";
import { TooltipPortal } from "@radix-ui/react-tooltip";
import useContextMenu from "@/hooks/use-contextmenu";
import { getTranslatedLabel } from "@/utils/i18n";
import { isAttributeOfLabel } from "@/utils/modelUtil";
type SearchThumbnailProps = {
searchResult: SearchResult;
@@ -58,9 +59,7 @@ export default function SearchThumbnail({
}
if (
config.model.attributes_map[searchResult.label]?.includes(
searchResult.sub_label,
)
isAttributeOfLabel(config, searchResult.label, searchResult.sub_label)
) {
return searchResult.sub_label;
}
@@ -82,9 +81,7 @@ export default function SearchThumbnail({
}
if (
config.model.attributes_map[searchResult.label]?.includes(
searchResult.sub_label,
)
isAttributeOfLabel(config, searchResult.label, searchResult.sub_label)
) {
return "";
}
@@ -32,6 +32,7 @@ import {
} from "@/types/frigateConfig";
import { ClassificationDatasetResponse } from "@/types/classification";
import { getTranslatedLabel } from "@/utils/i18n";
import { isAttributeLabel } from "@/utils/modelUtil";
import { zodResolver } from "@hookform/resolvers/zod";
import axios from "axios";
import { useCallback, useEffect, useMemo, useState } from "react";
@@ -99,7 +100,7 @@ export default function ClassificationModelEditDialog({
}
cameraConfig.objects.track.forEach((label) => {
if (!config.model.all_attributes.includes(label)) {
if (!isAttributeLabel(config, label)) {
labels.add(label);
}
});
@@ -27,6 +27,7 @@ import useSWR from "swr";
import { FrigateConfig } from "@/types/frigateConfig";
import { getTranslatedLabel } from "@/utils/i18n";
import { useDocDomain } from "@/hooks/use-doc-domain";
import { isAttributeLabel } from "@/utils/modelUtil";
import {
Popover,
PopoverContent,
@@ -72,7 +73,7 @@ export default function Step1NameAndDefine({
}
cameraConfig.objects.track.forEach((label) => {
if (!config.model.all_attributes.includes(label)) {
if (!isAttributeLabel(config, label)) {
labels.add(label);
}
});
@@ -110,6 +110,21 @@ const detect: SectionConfigOverrides = {
return Math.abs(newRatio - savedRatio) > 0.01;
},
},
{
key: "detect-scene-without-model",
field: "scene",
position: "after",
messageKey: "configMessages.detect.sceneWithoutModel",
severity: "warning",
docLink: "/configuration/object_detectors#running-more-than-one-model",
condition: (ctx) => {
const scene = ctx.formData?.scene as string | undefined;
if (!scene || scene === "all") return false;
const models = ctx.fullConfig?.models;
if (!models) return false;
return !models.some((model) => model.scene === scene);
},
},
{
key: "fps-greater-than-five",
field: "fps",
@@ -153,6 +168,7 @@ const detect: SectionConfigOverrides = {
],
fieldOrder: [
"enabled",
"scene",
"width",
"height",
"fps",
@@ -170,6 +186,11 @@ const detect: SectionConfigOverrides = {
tracking: ["min_initialized", "max_disappeared"],
},
uiSchema: {
scene: {
"ui:options": {
enumI18nPrefix: "detectionModels.scenes",
},
},
annotation_offset: {
"ui:options": {
signed: true,
@@ -186,6 +207,7 @@ const detect: SectionConfigOverrides = {
},
global: {
restartRequired: [
"scene",
"fps",
"width",
"height",
@@ -195,6 +217,7 @@ const detect: SectionConfigOverrides = {
},
camera: {
restartRequired: [
"scene",
"fps",
"width",
"height",
@@ -211,6 +234,7 @@ const detect: SectionConfigOverrides = {
hiddenFields: [
"enabled",
"enabled_in_config",
"scene",
"min_initialized",
"max_disappeared",
"annotation_offset",
@@ -1,28 +0,0 @@
import type { SectionConfigOverrides } from "./types";
const detectorHiddenFields = [
"*.model.labelmap",
"*.model.attributes_map",
"*.model",
"*.model_path",
];
const detectors: SectionConfigOverrides = {
base: {
sectionDocs: "/configuration/object_detectors",
fieldOrder: [],
advancedFields: [],
hiddenFields: detectorHiddenFields,
uiSchema: {
"ui:field": "DetectorHardwareField",
"ui:options": {
multiInstanceTypes: ["cpu", "onnx", "openvino", "edgetpu"],
typeOrder: ["onnx", "openvino", "edgetpu"],
hiddenByType: {},
hiddenFields: detectorHiddenFields,
},
},
},
};
export default detectors;
@@ -1,8 +1,24 @@
import type { SectionConfigOverrides } from "./types";
const model: SectionConfigOverrides = {
// scene and devices are rendered by ModelsField itself; the rest of each model
// is delegated back to the schema form
const modelFields = [
"path",
"labelmap_path",
"width",
"height",
"input_pixel_format",
"input_tensor",
"input_dtype",
"model_type",
];
const models: SectionConfigOverrides = {
base: {
sectionDocs: "/configuration/object_detectors#model",
sectionDocs: "/configuration/object_detectors",
// the default-model rule must be enforced as the list is edited, not
// only when the form is submitted
liveValidate: true,
fieldMessages: [
{
key: "model-optimized-for-320",
@@ -36,51 +52,46 @@ const model: SectionConfigOverrides = {
},
},
],
// every model field takes effect only when the detection processes restart
restartRequired: [
"path",
"labelmap_path",
"width",
"height",
"scene",
"devices",
...modelFields,
"labelmap",
"attributes_map",
"input_tensor",
"input_pixel_format",
"input_dtype",
"model_type",
],
fieldOrder: [
"path",
"labelmap_path",
"width",
"height",
"input_pixel_format",
"input_tensor",
"input_dtype",
"model_type",
],
advancedFields: [
"input_pixel_format",
"input_tensor",
"input_dtype",
"model_type",
],
].map((field) => `*.${field}`),
hiddenFields: [
"labelmap",
"attributes_map",
"colormap",
"all_attributes",
"non_logo_attributes",
"plus",
"*.labelmap",
"*.attributes_map",
"*.colormap",
"*.all_attributes",
"*.non_logo_attributes",
"*.plus",
],
uiSchema: {
path: {
"ui:options": { size: "md" },
},
labelmap_path: {
"ui:options": { size: "md" },
"ui:field": "ModelsField",
items: {
path: {
"ui:options": { size: "md" },
},
labelmap_path: {
"ui:options": { size: "md" },
},
input_pixel_format: {
"ui:options": { advanced: true, size: "xs" },
},
input_tensor: {
"ui:options": { advanced: true, size: "xs" },
},
input_dtype: {
"ui:options": { advanced: true, size: "xs" },
},
model_type: {
"ui:options": { advanced: true, size: "xs" },
},
},
},
},
};
export default model;
export default models;
@@ -2,6 +2,7 @@ import type { FormValidation } from "@rjsf/utils";
import type { TFunction } from "i18next";
import { validateDetectDimensions } from "./detect";
import { validateFfmpegInputRoles } from "./ffmpeg";
import { validateDefaultModelExists } from "./models";
import { validateProxyRoleHeader } from "./proxy";
export type SectionValidation = (
@@ -28,6 +29,11 @@ export function getSectionValidation({
return (formData, errors) => validateFfmpegInputRoles(formData, errors, t);
}
if (sectionPath === "models") {
return (formData, errors) =>
validateDefaultModelExists(formData, errors, t);
}
if (sectionPath === "proxy" && level === "global") {
return (formData, errors) => validateProxyRoleHeader(formData, errors, t);
}
@@ -0,0 +1,31 @@
import type { FormValidation } from "@rjsf/utils";
import type { TFunction } from "i18next";
import { isJsonObject } from "@/lib/utils";
const DEFAULT_SCENE = "all";
/**
* A camera that names no scene runs the model whose scene is `all`. Without one
* the backend rejects the config outright once a second model exists, and with
* a single model it silently runs every camera on whatever that model is. Both
* are surprising, so require the default to be present.
*/
export function validateDefaultModelExists(
formData: unknown,
errors: FormValidation,
t: TFunction,
): FormValidation {
if (!Array.isArray(formData) || formData.length === 0) {
return errors;
}
const hasDefault = formData.some(
(model) => isJsonObject(model) && model.scene === DEFAULT_SCENE,
);
if (!hasDefault) {
errors.addError?.(t("models.defaultRequired", { ns: "config/validation" }));
}
return errors;
}
@@ -23,7 +23,6 @@ import birdseye from "./section-configs/birdseye";
import classification from "./section-configs/classification";
import database from "./section-configs/database";
import detect from "./section-configs/detect";
import detectors from "./section-configs/detectors";
import environmentVars from "./section-configs/environment_vars";
import faceRecognition from "./section-configs/face_recognition";
import ffmpeg from "./section-configs/ffmpeg";
@@ -31,7 +30,7 @@ import genai from "./section-configs/genai";
import live from "./section-configs/live";
import logger from "./section-configs/logger";
import lpr from "./section-configs/lpr";
import model from "./section-configs/model";
import models from "./section-configs/models";
import motion from "./section-configs/motion";
import mqtt from "./section-configs/mqtt";
import networking from "./section-configs/networking";
@@ -76,8 +75,7 @@ export const sectionConfigs: Record<string, SectionConfigOverrides> = {
logger,
environment_vars: environmentVars,
telemetry,
detectors,
model,
models,
genai,
classification,
};
@@ -72,8 +72,7 @@ const SECTIONS_WITHOUT_OVERRIDE_BADGE = new Set([
"environment_vars",
"telemetry",
"birdseye",
"detectors",
"model",
"models",
]);
type CameraEntryProps = {
@@ -16,7 +16,7 @@ import { getEffectiveAttributeLabels } from "@/utils/configUtil";
* Sections that require special handling at the global level.
* Add new section paths here as needed.
*/
const SPECIAL_CASE_SECTIONS = ["motion", "detectors", "genai"] as const;
const SPECIAL_CASE_SECTIONS = ["motion", "genai"] as const;
/**
* Check if a section requires special case handling.
@@ -36,8 +36,6 @@ export function isSpecialCaseSection(
/**
* Modify schema for sections that need defaults stripped or other modifications.
*
* - detectors: Strip the "default" field to prevent RJSF from merging the
* default {"cpu": {"type": "cpu"}} with stored detector keys.
* - genai: Inject a default provider value on the additionalProperties shape.
* - objects: Promote tracked attribute labels (face, license_plate, courier
* logos) from `filters.additionalProperties` to explicit
@@ -63,12 +61,6 @@ export function modifySchemaForSection(
return schema;
}
// detectors: Remove default to prevent merging with stored keys
if (sectionPath === "detectors" && "default" in schema) {
const { default: _, ...schemaWithoutDefault } = schema;
return schemaWithoutDefault;
}
if (sectionPath === "genai") {
const additional = schema.additionalProperties;
if (
@@ -270,8 +262,6 @@ function modifyObjectsSchema(
* - motion: Has anyOf schema with [null, MotionConfig]. When stored value is
* null, derive defaults from the non-null anyOf branch to avoid showing
* changes when navigating to the page.
* - detectors: Return empty object since the schema default would add unwanted
* keys to the stored configuration.
*/
export function getEffectiveDefaultsForSection(
sectionPath: string,
@@ -305,11 +295,6 @@ export function getEffectiveDefaultsForSection(
return applySchemaDefaults(motionSchema as RJSFSchema, {});
}
// detectors: Return empty object to avoid adding default keys
if (sectionPath === "detectors") {
return {};
}
return schemaDefaults;
}
@@ -424,27 +409,6 @@ export function sanitizeOverridesForSection(
return flattened;
};
// detectors: Strip readonly model fields that are generated on startup
// and should never be persisted back to the config file.
if (sectionPath === "detectors") {
const overridesObj = overrides as JsonObject;
const cleaned: JsonObject = {};
Object.entries(overridesObj).forEach(([key, value]) => {
if (!isJsonObject(value)) {
cleaned[key] = value;
return;
}
const cleanedValue = { ...value } as JsonObject;
delete cleanedValue.model;
delete cleanedValue.model_path;
cleaned[key] = cleanedValue;
});
return cleaned;
}
if (sectionPath === "logger") {
const overridesObj = overrides as JsonObject;
const logs = overridesObj.logs;
@@ -1,956 +0,0 @@
import type {
ErrorSchema,
FieldPathList,
FieldProps,
RJSFSchema,
UiSchema,
} from "@rjsf/utils";
import { toFieldPathId } from "@rjsf/utils";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import {
LuChevronDown,
LuChevronRight,
LuPlus,
LuTrash2,
} from "react-icons/lu";
import { applySchemaDefaults } from "@/lib/config-schema";
import { cn, isJsonObject, mergeUiSchema } from "@/lib/utils";
import { ConfigFormContext, JsonObject } from "@/types/configForm";
import { requiresRestartForFieldPath } from "@/utils/configUtil";
import RestartRequiredIndicator from "@/components/indicators/RestartRequiredIndicator";
import { Button } from "@/components/ui/button";
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger,
} from "@/components/ui/collapsible";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { humanizeKey } from "../utils/i18n";
type DetectorHardwareFieldOptions = {
multiInstanceTypes?: string[];
hiddenByType?: Record<string, string[]>;
hiddenFields?: string[];
typeOrder?: string[];
};
type DetectorSchemaEntry = {
type: string;
schema: RJSFSchema;
};
const DEFAULT_MULTI_INSTANCE_TYPES = ["cpu", "onnx", "openvino"];
const EMPTY_HIDDEN_BY_TYPE: Record<string, string[]> = {};
const EMPTY_HIDDEN_FIELDS: string[] = [];
const EMPTY_TYPE_ORDER: string[] = [];
const isSchemaObject = (schema: unknown): schema is RJSFSchema =>
typeof schema === "object" && schema !== null;
const getUnionSchemas = (schema?: RJSFSchema): RJSFSchema[] => {
if (!schema) {
return [];
}
const schemaObj = schema as Record<string, unknown>;
const union = schemaObj.oneOf ?? schemaObj.anyOf;
if (Array.isArray(union)) {
return union.filter(isSchemaObject) as RJSFSchema[];
}
return [schema];
};
const getTypeValues = (schema: RJSFSchema): string[] => {
const schemaObj = schema as Record<string, unknown>;
const properties = schemaObj.properties as
| Record<string, unknown>
| undefined;
const typeSchema = properties?.type as Record<string, unknown> | undefined;
const values: string[] = [];
if (typeof typeSchema?.const === "string") {
values.push(typeSchema.const);
}
if (Array.isArray(typeSchema?.enum)) {
typeSchema.enum.forEach((value) => {
if (typeof value === "string") {
values.push(value);
}
});
}
return values;
};
const buildHiddenUiSchema = (paths: string[]): UiSchema => {
const result: UiSchema = {};
paths.forEach((path) => {
if (!path) {
return;
}
const segments = path.split(".").filter(Boolean);
if (segments.length === 0) {
return;
}
let cursor = result;
segments.forEach((segment, index) => {
if (index === segments.length - 1) {
cursor[segment] = {
...(cursor[segment] as UiSchema | undefined),
"ui:widget": "hidden",
} as UiSchema;
return;
}
const existing = (cursor[segment] as UiSchema | undefined) ?? {};
cursor[segment] = existing;
cursor = existing;
});
});
return result;
};
const getInstanceType = (value: unknown): string | undefined => {
if (!isJsonObject(value)) {
return undefined;
}
const typeValue = value.type;
return typeof typeValue === "string" && typeValue.length > 0
? typeValue
: undefined;
};
export function DetectorHardwareField(props: FieldProps) {
const {
schema,
uiSchema,
registry,
fieldPathId,
formData: rawFormData,
errorSchema,
disabled,
readonly,
hideError,
onBlur,
onFocus,
onChange,
} = props;
const formContext = registry.formContext as ConfigFormContext | undefined;
const configNamespace =
formContext?.i18nNamespace ??
(formContext?.level === "camera" ? "config/cameras" : "config/global");
const { t: fallbackT } = useTranslation(["common", configNamespace]);
const t = formContext?.t ?? fallbackT;
const sectionPrefix = formContext?.sectionI18nPrefix ?? "detectors";
const restartRequired = formContext?.restartRequired;
const defaultRequiresRestart = formContext?.requiresRestart ?? true;
const options =
(uiSchema?.["ui:options"] as DetectorHardwareFieldOptions | undefined) ??
{};
const multiInstanceTypes =
options.multiInstanceTypes ?? DEFAULT_MULTI_INSTANCE_TYPES;
const hiddenByType = options.hiddenByType ?? EMPTY_HIDDEN_BY_TYPE;
const hiddenFields = options.hiddenFields ?? EMPTY_HIDDEN_FIELDS;
const typeOrder = options.typeOrder ?? EMPTY_TYPE_ORDER;
const multiInstanceSet = useMemo(
() => new Set(multiInstanceTypes),
[multiInstanceTypes],
);
const globalHiddenFields = useMemo(
() =>
hiddenFields
.map((path) => (path.startsWith("*.") ? path.slice(2) : path))
.filter((path) => path.length > 0),
[hiddenFields],
);
const detectorConfigSchema = useMemo(() => {
const additional = (schema as RJSFSchema | undefined)?.additionalProperties;
if (isSchemaObject(additional)) {
return additional as RJSFSchema;
}
const rootSchema = registry.rootSchema as Record<string, unknown>;
const defs =
(rootSchema?.$defs as Record<string, unknown> | undefined) ??
(rootSchema?.definitions as Record<string, unknown> | undefined);
const fallback = defs?.DetectorConfig;
return isSchemaObject(fallback) ? (fallback as RJSFSchema) : undefined;
}, [schema, registry.rootSchema]);
const detectorSchemas = useMemo<DetectorSchemaEntry[]>(() => {
const entries: DetectorSchemaEntry[] = [];
getUnionSchemas(detectorConfigSchema).forEach((schema) => {
const types = getTypeValues(schema);
types.forEach((type) => {
entries.push({ type, schema });
});
});
return entries;
}, [detectorConfigSchema]);
const detectorSchemaByType = useMemo(() => {
const map = new Map<string, RJSFSchema>();
detectorSchemas.forEach(({ type, schema }) => {
if (!map.has(type)) {
map.set(type, schema);
}
});
return map;
}, [detectorSchemas]);
const availableTypes = useMemo(
() => detectorSchemas.map((entry) => entry.type),
[detectorSchemas],
);
const orderedTypes = useMemo(() => {
if (!typeOrder.length) {
return availableTypes;
}
const availableSet = new Set(availableTypes);
const ordered = typeOrder.filter((type) => availableSet.has(type));
const orderedSet = new Set(ordered);
const remaining = availableTypes.filter((type) => !orderedSet.has(type));
return [...ordered, ...remaining];
}, [availableTypes, typeOrder]);
const formData = isJsonObject(rawFormData) ? rawFormData : {};
const detectors = formData as JsonObject;
const [addType, setAddType] = useState<string | undefined>(orderedTypes[0]);
const [addError, setAddError] = useState<string | undefined>();
const [renameDrafts, setRenameDrafts] = useState<Record<string, string>>({});
const [renameErrors, setRenameErrors] = useState<Record<string, string>>({});
const [typeErrors, setTypeErrors] = useState<Record<string, string>>({});
const [openKeys, setOpenKeys] = useState<Set<string>>(
() => new Set(Object.keys(detectors)),
);
useEffect(() => {
if (!orderedTypes.length) {
setAddType(undefined);
return;
}
if (!addType || !orderedTypes.includes(addType)) {
setAddType(orderedTypes[0]);
}
}, [orderedTypes, addType]);
useEffect(() => {
setOpenKeys((prev) => {
const next = new Set<string>();
Object.keys(detectors).forEach((key) => {
if (prev.has(key)) {
next.add(key);
}
});
return next;
});
setRenameDrafts((prev) => {
const next: Record<string, string> = {};
Object.keys(detectors).forEach((key) => {
if (prev[key] !== undefined) {
next[key] = prev[key];
}
});
return next;
});
setRenameErrors((prev) => {
const next: Record<string, string> = {};
Object.keys(detectors).forEach((key) => {
if (prev[key] !== undefined) {
next[key] = prev[key];
}
});
return next;
});
setTypeErrors((prev) => {
const next: Record<string, string> = {};
Object.keys(detectors).forEach((key) => {
if (prev[key] !== undefined) {
next[key] = prev[key];
}
});
return next;
});
}, [detectors]);
const updateDetectors = useCallback(
(nextDetectors: JsonObject, path?: FieldPathList) => {
onChange(nextDetectors as unknown, path ?? fieldPathId.path);
},
[fieldPathId.path, onChange],
);
const getTypeLabel = useCallback(
(type: string) =>
t(`${sectionPrefix}.${type}.label`, {
ns: configNamespace,
defaultValue: humanizeKey(type),
}),
[t, sectionPrefix, configNamespace],
);
const getTypeDescription = useCallback(
(type: string) =>
t(`${sectionPrefix}.${type}.description`, {
ns: configNamespace,
defaultValue: "",
}),
[t, sectionPrefix, configNamespace],
);
const shouldShowRestartForPath = useCallback(
(path: Array<string | number>) =>
requiresRestartForFieldPath(
path,
restartRequired,
defaultRequiresRestart,
),
[defaultRequiresRestart, restartRequired],
);
const renderRestartIcon = (isRequired: boolean) => {
if (!isRequired) {
return null;
}
return <RestartRequiredIndicator className="ml-2" />;
};
const isSingleInstanceType = useCallback(
(type: string) => !multiInstanceSet.has(type),
[multiInstanceSet],
);
const getDetectorDefaults = useCallback(
(type: string) => {
const schema = detectorSchemaByType.get(type);
if (!schema) {
return { type };
}
const base = { type } as Record<string, unknown>;
const withDefaults = applySchemaDefaults(schema, base);
return { ...withDefaults, type } as Record<string, unknown>;
},
[detectorSchemaByType],
);
const resolveDuplicateType = useCallback(
(targetType: string, excludeKey?: string) => {
return Object.entries(detectors).some(([key, value]) => {
if (excludeKey && key === excludeKey) {
return false;
}
return getInstanceType(value) === targetType;
});
},
[detectors],
);
const getExistingType = useCallback(
(excludeKey?: string): string | undefined => {
for (const [key, value] of Object.entries(detectors)) {
if (excludeKey && key === excludeKey) continue;
const type = getInstanceType(value);
if (type) return type;
}
return undefined;
},
[detectors],
);
const handleAdd = useCallback(() => {
if (!addType) {
setAddError(
t("selectItem", {
ns: "common",
defaultValue: "Select {{item}}",
item: t("detectors.type.label", {
ns: configNamespace,
defaultValue: "Type",
}),
}),
);
return;
}
if (isSingleInstanceType(addType) && resolveDuplicateType(addType)) {
setAddError(
t("configForm.detectors.singleType", {
ns: "views/settings",
defaultValue: "Only one {{type}} detector is allowed.",
type: getTypeLabel(addType),
}),
);
return;
}
const existingType = getExistingType();
if (existingType && existingType !== addType) {
const canAddExisting =
multiInstanceSet.has(existingType) ||
!resolveDuplicateType(existingType);
setAddError(
canAddExisting
? t("configMessages.detectors.mixedTypesSuggestion", {
ns: "views/settings",
defaultValue:
"All detectors must use the same type. Remove existing detectors or select {{type}}.",
type: getTypeLabel(existingType),
})
: t("configMessages.detectors.mixedTypes", {
ns: "views/settings",
defaultValue:
"All detectors must use the same type. Remove existing detectors to use a different type.",
}),
);
return;
}
const baseKey = addType;
let nextKey = baseKey;
let index = 2;
while (Object.prototype.hasOwnProperty.call(detectors, nextKey)) {
nextKey = `${baseKey}${index}`;
index += 1;
}
const nextDetectors = {
...detectors,
[nextKey]: getDetectorDefaults(addType),
} as JsonObject;
setAddError(undefined);
setOpenKeys((prev) => {
const next = new Set(prev);
next.add(nextKey);
return next;
});
updateDetectors(nextDetectors);
}, [
addType,
t,
configNamespace,
detectors,
getDetectorDefaults,
getExistingType,
getTypeLabel,
isSingleInstanceType,
multiInstanceSet,
resolveDuplicateType,
updateDetectors,
]);
const handleRemove = useCallback(
(key: string) => {
const { [key]: _, ...rest } = detectors;
updateDetectors(rest as JsonObject);
setOpenKeys((prev) => {
const next = new Set(prev);
next.delete(key);
return next;
});
},
[detectors, updateDetectors],
);
const commitRename = useCallback(
(key: string, nextKey: string) => {
const trimmed = nextKey.trim();
if (!trimmed) {
setRenameErrors((prev) => ({
...prev,
[key]: t("configForm.detectors.keyRequired", {
ns: "views/settings",
defaultValue: "Detector name is required.",
}),
}));
return;
}
if (trimmed !== key && detectors[trimmed] !== undefined) {
setRenameErrors((prev) => ({
...prev,
[key]: t("configForm.detectors.keyDuplicate", {
ns: "views/settings",
defaultValue: "Detector name already exists.",
}),
}));
return;
}
setRenameErrors((prev) => {
const { [key]: _, ...rest } = prev;
return rest;
});
setRenameDrafts((prev) => {
const { [key]: _, ...rest } = prev;
return rest;
});
if (trimmed === key) {
return;
}
const { [key]: value, ...rest } = detectors;
const nextDetectors = { ...rest, [trimmed]: value } as JsonObject;
setOpenKeys((prev) => {
const next = new Set(prev);
if (next.delete(key)) {
next.add(trimmed);
}
return next;
});
updateDetectors(nextDetectors);
},
[detectors, t, updateDetectors],
);
const handleTypeChange = useCallback(
(key: string, nextType: string) => {
const currentType = getInstanceType(detectors[key]);
if (!nextType || nextType === currentType) {
return;
}
if (
isSingleInstanceType(nextType) &&
resolveDuplicateType(nextType, key)
) {
setTypeErrors((prev) => ({
...prev,
[key]: t("configForm.detectors.singleType", {
ns: "views/settings",
defaultValue: "Only one {{type}} detector is allowed.",
type: getTypeLabel(nextType),
}),
}));
return;
}
const existingType = getExistingType(key);
if (existingType && existingType !== nextType) {
const canAddExisting =
multiInstanceSet.has(existingType) ||
!resolveDuplicateType(existingType, key);
setTypeErrors((prev) => ({
...prev,
[key]: canAddExisting
? t("configMessages.detectors.mixedTypesSuggestion", {
ns: "views/settings",
defaultValue:
"All detectors must use the same type. Remove existing detectors or select {{type}}.",
type: getTypeLabel(existingType),
})
: t("configMessages.detectors.mixedTypes", {
ns: "views/settings",
defaultValue:
"All detectors must use the same type. Remove existing detectors to use a different type.",
}),
}));
return;
}
setTypeErrors((prev) => {
const { [key]: _, ...rest } = prev;
return rest;
});
const nextDetectors = {
...detectors,
[key]: getDetectorDefaults(nextType),
} as JsonObject;
updateDetectors(nextDetectors);
},
[
detectors,
getDetectorDefaults,
getExistingType,
getTypeLabel,
isSingleInstanceType,
multiInstanceSet,
resolveDuplicateType,
t,
updateDetectors,
],
);
const getInstanceUiSchema = useCallback(
(type: string) => {
const baseUiSchema =
(uiSchema?.additionalProperties as UiSchema | undefined) ?? {};
const globalHidden = buildHiddenUiSchema(globalHiddenFields);
const hiddenOverrides = buildHiddenUiSchema(hiddenByType[type] ?? []);
const typeHidden = { type: { "ui:widget": "hidden" } } as UiSchema;
const nestedOverrides = {
"ui:options": {
disableNestedCard: true,
addButtonText: t("configForm.detectors.addCustomKey", {
ns: "views/settings",
defaultValue: "Add custom key",
}),
},
} as UiSchema;
const withGlobalHidden = mergeUiSchema(baseUiSchema, globalHidden);
const withTypeHidden = mergeUiSchema(withGlobalHidden, hiddenOverrides);
const withTypeHiddenAndOptions = mergeUiSchema(
withTypeHidden,
typeHidden,
);
return mergeUiSchema(withTypeHiddenAndOptions, nestedOverrides);
},
[globalHiddenFields, hiddenByType, t, uiSchema?.additionalProperties],
);
const renderInstanceForm = useCallback(
(key: string, value: unknown) => {
const SchemaField = registry.fields.SchemaField;
const type = getInstanceType(value);
const schema = type ? detectorSchemaByType.get(type) : undefined;
if (!SchemaField || !schema || !type) {
return null;
}
const instanceUiSchema = getInstanceUiSchema(type);
const instanceFieldPathId = toFieldPathId(
key,
registry.globalFormOptions,
fieldPathId.path,
);
const instanceErrorSchema = (
errorSchema as Record<string, ErrorSchema> | undefined
)?.[key];
const handleInstanceChange = (
nextValue: unknown,
path: FieldPathList,
errors?: ErrorSchema,
id?: string,
) => {
onChange(nextValue, path, errors, id);
};
return (
<SchemaField
name={key}
schema={schema}
uiSchema={instanceUiSchema}
fieldPathId={instanceFieldPathId}
formData={value}
errorSchema={instanceErrorSchema}
onChange={handleInstanceChange}
onBlur={onBlur}
onFocus={onFocus}
registry={registry}
disabled={disabled}
readonly={readonly}
hideError={hideError}
/>
);
},
[
detectorSchemaByType,
getInstanceUiSchema,
disabled,
errorSchema,
fieldPathId,
hideError,
onChange,
onBlur,
onFocus,
readonly,
registry,
],
);
if (!availableTypes.length) {
return (
<p className="text-sm text-muted-foreground">
{t("configForm.detectors.noSchema", {
ns: "views/settings",
defaultValue: "No detector schemas are available.",
})}
</p>
);
}
const detectorEntries = Object.entries(detectors);
const isDisabled = Boolean(disabled || readonly);
return (
<div className="space-y-4">
{detectorEntries.length === 0 ? (
<p className="text-sm text-muted-foreground">
{t("configForm.detectors.none", {
ns: "views/settings",
defaultValue: "No detector instances configured.",
})}
</p>
) : (
<div className="space-y-3">
{detectorEntries.map(([key, value]) => {
const type = getInstanceType(value) ?? "";
const typeLabel = type ? getTypeLabel(type) : key;
const typeDescription = type ? getTypeDescription(type) : "";
const isOpen = openKeys.has(key);
const renameDraft = renameDrafts[key] ?? key;
const detectorPath = [...fieldPathId.path, key];
const detectorTypePath = [...detectorPath, "type"];
const detectorTypeRequiresRestart =
shouldShowRestartForPath(detectorTypePath);
return (
<div key={key} className="rounded-lg border bg-card">
<Collapsible
open={isOpen}
onOpenChange={(open) => {
setOpenKeys((prev) => {
const next = new Set(prev);
if (open) {
next.add(key);
} else {
next.delete(key);
}
return next;
});
}}
>
<div className="flex items-start justify-between gap-4 p-4">
<div className="flex items-start gap-3">
<CollapsibleTrigger asChild>
<Button
type="button"
variant="ghost"
size="xs"
className="mt-0.5"
>
{isOpen ? (
<LuChevronDown className="h-4 w-4" />
) : (
<LuChevronRight className="h-4 w-4" />
)}
</Button>
</CollapsibleTrigger>
<div>
<div className="flex items-center text-sm font-medium">
{typeLabel}
{renderRestartIcon(detectorTypeRequiresRestart)}
<span className="ml-2 text-xs text-muted-foreground">
{key}
</span>
</div>
{typeDescription && (
<div className="text-xs text-muted-foreground">
{typeDescription}
</div>
)}
</div>
</div>
<Button
type="button"
variant="ghost"
size="xs"
onClick={() => handleRemove(key)}
disabled={isDisabled}
>
<LuTrash2 className="h-4 w-4" />
</Button>
</div>
<CollapsibleContent>
<div className="space-y-4 border-t p-4">
<div className="grid gap-4 md:grid-cols-4">
<div className="space-y-2">
<Label className="flex items-center">
{t("label.ID", {
ns: "common",
defaultValue: "ID",
})}
</Label>
<Input
value={renameDraft}
disabled={isDisabled}
onChange={(event) => {
setRenameDrafts((prev) => ({
...prev,
[key]: event.target.value,
}));
}}
onBlur={(event) =>
commitRename(key, event.target.value)
}
onKeyDown={(event) => {
if (event.key === "Enter") {
event.preventDefault();
commitRename(key, renameDraft);
}
}}
/>
<p className="text-xs text-muted-foreground">
{t("field.internalID", {
ns: "common",
defaultValue:
"The Internal ID Frigate uses in the configuration and database",
})}
</p>
{renameErrors[key] && (
<p className="text-xs text-danger">
{renameErrors[key]}
</p>
)}
</div>
<div className="col-span-3 space-y-2">
<Label className="flex items-center">
{t("detectors.type.label", {
ns: configNamespace,
defaultValue: "Type",
})}
</Label>
<Select
value={type}
onValueChange={(value) =>
handleTypeChange(key, value)
}
disabled={isDisabled}
>
<SelectTrigger className="w-full">
<SelectValue
placeholder={t("selectItem", {
ns: "common",
defaultValue: "Select {{item}}",
item: t("detectors.type.label", {
ns: configNamespace,
defaultValue: "Type",
}),
})}
/>
</SelectTrigger>
<SelectContent>
{orderedTypes.map((option) => (
<SelectItem key={option} value={option}>
{getTypeLabel(option)}
</SelectItem>
))}
</SelectContent>
</Select>
{typeErrors[key] && (
<p className="text-xs text-danger">
{typeErrors[key]}
</p>
)}
</div>
</div>
<div className={cn(readonly && "opacity-90")}>
{renderInstanceForm(key, value)}
</div>
</div>
</CollapsibleContent>
</Collapsible>
</div>
);
})}
</div>
)}
<div className="flex justify-start pt-5">
<div className="w-full max-w-lg rounded-lg border bg-card p-4">
<div className="text-sm font-medium text-muted-foreground">
{t("configForm.detectors.add", {
ns: "views/settings",
defaultValue: "Add detector",
})}
</div>
<div className="mt-3 flex flex-col gap-3 md:flex-row md:items-end">
<div className="flex-1 space-y-2">
<Label>
{t("detectors.type.label", {
ns: configNamespace,
defaultValue: "Type",
})}
</Label>
<Select
value={addType ?? ""}
onValueChange={(value) => {
setAddError(undefined);
setAddType(value);
}}
disabled={isDisabled}
>
<SelectTrigger className="w-full">
<SelectValue
placeholder={t("selectItem", {
ns: "common",
defaultValue: "Select {{item}}",
item: t("detectors.type.label", {
ns: configNamespace,
defaultValue: "Type",
}),
})}
/>
</SelectTrigger>
<SelectContent>
{orderedTypes.map((type) => (
<SelectItem key={type} value={type}>
{getTypeLabel(type)}
</SelectItem>
))}
</SelectContent>
</Select>
{addError && <p className="text-xs text-danger">{addError}</p>}
</div>
<div>
<Button
type="button"
variant="outline"
onClick={handleAdd}
disabled={isDisabled}
className="gap-2"
>
<LuPlus className="h-4 w-4" />
{t("button.add", {
ns: "common",
defaultValue: "Add",
})}
</Button>
</div>
</div>
</div>
</div>
</div>
);
}
@@ -0,0 +1,266 @@
import { useCallback, useMemo } from "react";
import { useTranslation } from "react-i18next";
import useSWR from "swr";
import { Checkbox } from "@/components/ui/checkbox";
import { Label } from "@/components/ui/label";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { DetectionHardware } from "@/types/hardware";
import {
hardwareForDevices,
MAX_DETECTORS,
recommendedDetectorCount,
} from "@/utils/detectionHardware";
type HardwarePickerProps = {
// scopes the unit checkbox ids, since several models can list the same unit
idPrefix: string;
devices: string[];
// device strings already taken by another model, mapped to that model's scene
claimedElsewhere: Record<string, string>;
cameraCount: number;
disabled?: boolean;
onChange: (devices: string[]) => void;
};
export function HardwarePicker({
idPrefix,
devices,
claimedElsewhere,
cameraCount,
disabled,
onChange,
}: HardwarePickerProps) {
const { t } = useTranslation(["views/settings", "common"]);
const { data: hardware, isLoading } =
useSWR<DetectionHardware[]>("hardware/probe");
const selected = useMemo(
() => hardwareForDevices(hardware ?? [], devices),
[hardware, devices],
);
const recommended = useMemo(
() => recommendedDetectorCount(cameraCount),
[cameraCount],
);
// the units a model is assigned to, in the order the probe reports them. A
// shareable unit repeats in `devices` once per inference process, so the
// distinct entries are what is selected.
const selectedUnits = useMemo(() => {
if (!selected) {
return [];
}
return selected.units
.map((unit) => unit.device)
.filter((device) => devices.includes(device));
}, [selected, devices]);
/** Spread `count` detectors round robin over the selected units. */
const buildDevices = useCallback((units: string[], count: number) => {
if (units.length === 0) {
return [];
}
return Array.from(
{ length: Math.max(count, units.length) },
(_, index) => units[index % units.length],
);
}, []);
const handleHardwareChange = useCallback(
(key: string) => {
const entry = hardware?.find((candidate) => candidate.key === key);
if (!entry) {
return;
}
// start with the first unit no other model has taken
const free = entry.units.find((unit) => !claimedElsewhere[unit.device]);
if (!free) {
onChange([]);
return;
}
onChange(
entry.unlimited
? buildDevices([free.device], recommended)
: [free.device],
);
},
[hardware, claimedElsewhere, recommended, buildDevices, onChange],
);
const handleUnitToggle = useCallback(
(device: string, checked: boolean) => {
if (!selected) {
return;
}
const units = selected.units
.map((unit) => unit.device)
.filter((candidate) =>
candidate === device ? checked : selectedUnits.includes(candidate),
);
if (!selected.unlimited) {
onChange(units);
return;
}
// keep the detector count while the set of units changes
onChange(buildDevices(units, devices.length));
},
[selected, selectedUnits, devices.length, buildDevices, onChange],
);
const handleCountChange = useCallback(
(value: string) => {
onChange(buildDevices(selectedUnits, Number(value)));
},
[selectedUnits, buildDevices, onChange],
);
if (isLoading) {
return (
<p className="text-sm text-muted-foreground">
{t("detectionModels.hardware.loading")}
</p>
);
}
// a hand-written config can name hardware this system does not report
const unrecognized = devices.length > 0 && !selected;
return (
<div className="space-y-6">
<div className="space-y-1">
<Label>{t("detectionModels.hardware.label")}</Label>
<Select
value={selected?.key ?? ""}
onValueChange={handleHardwareChange}
disabled={disabled}
>
<SelectTrigger className="max-w-xs">
<SelectValue
placeholder={t("detectionModels.hardware.placeholder")}
/>
</SelectTrigger>
<SelectContent>
{(hardware ?? []).map((entry) => (
<SelectItem key={entry.key} value={entry.key}>
{entry.name}
{entry.count > 1 ? ` (${entry.count})` : ""}
</SelectItem>
))}
</SelectContent>
</Select>
<p className="text-xs text-muted-foreground">
{t("detectionModels.hardware.description")}
</p>
</div>
{unrecognized ? (
<p className="text-sm text-danger">
{t("detectionModels.hardware.unrecognized", {
devices: devices.join(", "),
})}
</p>
) : null}
{selected && (selected.units.length > 1 || !selected.unlimited) ? (
<div className="space-y-2">
<p className="text-xs text-muted-foreground">
{t("detectionModels.hardware.unitsDescription")}
</p>
{selected.units.map((unit) => {
const claimedBy = claimedElsewhere[unit.device];
return (
<div
key={unit.device}
className="mb-3 flex flex-row items-center space-x-3 space-y-0 last:mb-0"
>
<Checkbox
id={`${idPrefix}-${unit.device}`}
className="size-5 text-white accent-white data-[state=checked]:bg-selected data-[state=checked]:text-white"
checked={devices.includes(unit.device)}
disabled={disabled || Boolean(claimedBy)}
onCheckedChange={(checked) =>
handleUnitToggle(unit.device, checked === true)
}
/>
<Label
htmlFor={`${idPrefix}-${unit.device}`}
className="cursor-pointer font-normal"
>
{unit.label}
{claimedBy ? (
<span className="ml-2 text-xs text-muted-foreground">
{t("detectionModels.hardware.claimedBy", {
scene: claimedBy,
})}
</span>
) : null}
</Label>
</div>
);
})}
</div>
) : null}
{selected?.unlimited ? (
<div className="space-y-1">
<Label htmlFor={`${idPrefix}-detector-count`}>
{t("detectionModels.hardware.detectorCount")}
</Label>
<Select
value={String(devices.length || 1)}
onValueChange={handleCountChange}
disabled={disabled}
>
<SelectTrigger
id={`${idPrefix}-detector-count`}
className="max-w-xs"
>
{String(devices.length || 1)}
</SelectTrigger>
<SelectContent>
{Array.from({ length: MAX_DETECTORS }, (_, index) => index + 1)
.filter((count) => count >= Math.max(selectedUnits.length, 1))
.map((count) => (
<SelectItem key={count} value={String(count)}>
<div className="flex h-max flex-col justify-between">
<div>{count}</div>
{count === recommended ? (
<div className="text-xs text-muted-foreground">
{t("detectionModels.hardware.countRecommended", {
count: cameraCount,
})}
</div>
) : null}
</div>
</SelectItem>
))}
</SelectContent>
</Select>
<p className="text-xs text-muted-foreground">
{t("detectionModels.hardware.detectorCountDescription")}
</p>
</div>
) : null}
</div>
);
}
export default HardwarePicker;
@@ -0,0 +1,175 @@
import { ReactNode, useMemo, useState } from "react";
import { Trans, useTranslation } from "react-i18next";
import useSWR from "swr";
import axios from "axios";
import { Label } from "@/components/ui/label";
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectTrigger,
} from "@/components/ui/select";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import type { FrigateConfig } from "@/types/frigateConfig";
export type FrigatePlusModel = {
id: string;
name: string;
baseModel: string;
trainDate: string;
isBaseModel: boolean;
supportedDetectors: string[];
width: number;
height: number;
};
const PLUS_PREFIX = "plus://";
/** The Frigate+ model id a path refers to, if it is a Frigate+ path at all. */
function plusModelId(path: unknown): string | undefined {
return typeof path === "string" && path.startsWith(PLUS_PREFIX)
? path.slice(PLUS_PREFIX.length)
: undefined;
}
type ModelSourcePickerProps = {
path: unknown;
// Frigate+ metadata the backend attaches to a saved model, and the only
// reliable signal that one is active: it resolves `plus://<id>` to a local
// cache path before serving the config back
plus?: { id: string } | null;
// the detector this model runs on, used to filter incompatible Plus models
detector?: string;
disabled?: boolean;
onPathChange: (path: string | undefined) => void;
// the schema-driven fields for a custom model
customFields: ReactNode;
};
export function ModelSourcePicker({
path,
plus,
detector,
disabled,
onPathChange,
customFields,
}: ModelSourcePickerProps) {
const { t } = useTranslation(["views/settings"]);
const { data: config } = useSWR<FrigateConfig>("config");
const plusEnabled = Boolean(config?.plus?.enabled);
// an unsaved pick still carries the plus:// path, which wins over the
// metadata of whatever model was saved before it
const selectedId = plusModelId(path) ?? plus?.id;
const { data: availableModels, isLoading } = useSWR<
Record<string, FrigatePlusModel>
>(plusEnabled ? "/plus/models" : null, {
fetcher: async (url) => {
const res = await axios.get(url, { withCredentials: true });
return res.data.reduce(
(obj: Record<string, FrigatePlusModel>, model: FrigatePlusModel) => {
obj[model.id] = model;
return obj;
},
{},
);
},
});
const entries = useMemo(
() => Object.entries(availableModels ?? {}),
[availableModels],
);
// the tab cannot be derived from the path alone: switching to Frigate+
// leaves the path untouched until a model is picked
const [tab, setTab] = useState<"plus" | "custom">(
selectedId ? "plus" : "custom",
);
const handleTabChange = (value: string) => {
setTab(value as "plus" | "custom");
// a resolved Frigate+ path is meaningless as a custom path, so drop it
if (value === "custom" && selectedId) {
onPathChange(undefined);
}
};
const isCompatible = (model: FrigatePlusModel) =>
!detector || model.supportedDetectors.includes(detector);
if (!plusEnabled) {
return <div className="space-y-6">{customFields}</div>;
}
const describe = (model: FrigatePlusModel) =>
`${new Date(model.trainDate).toLocaleString()} ${model.baseModel} (${
model.isBaseModel
? t("frigatePlus.modelInfo.plusModelType.baseModel")
: t("frigatePlus.modelInfo.plusModelType.userModel")
}) ${model.name} (${model.width}x${model.height})`;
return (
<Tabs value={tab} onValueChange={handleTabChange}>
<TabsList className="mb-4">
<TabsTrigger value="plus">{t("detectionModels.tabs.plus")}</TabsTrigger>
<TabsTrigger value="custom">
{t("detectionModels.tabs.custom")}
</TabsTrigger>
</TabsList>
<TabsContent value="plus" className="space-y-1">
<Label>{t("frigatePlus.modelInfo.availableModels")}</Label>
<Select
value={selectedId ?? ""}
onValueChange={(id) => onPathChange(`${PLUS_PREFIX}${id}`)}
disabled={disabled}
>
<SelectTrigger className="w-full max-w-2xl">
{selectedId && availableModels?.[selectedId]
? describe(availableModels[selectedId])
: isLoading
? t("frigatePlus.modelInfo.loadingAvailableModels")
: t("detectionModels.plusModel.noModelSelected")}
</SelectTrigger>
<SelectContent>
<SelectGroup>
{entries.length === 0 ? (
<div className="px-4 py-3 text-center text-sm text-muted-foreground">
{t("frigatePlus.modelInfo.noModelsAvailable")}
</div>
) : (
entries.map(([id, model]) => (
<SelectItem
key={id}
className="cursor-pointer"
value={id}
disabled={!isCompatible(model)}
>
<div>{describe(model)}</div>
<div className="text-xs text-muted-foreground">
{t("frigatePlus.modelInfo.supportedDetectors")}:{" "}
{model.supportedDetectors.join(", ")}
</div>
</SelectItem>
))
)}
</SelectGroup>
</SelectContent>
</Select>
<p className="text-xs text-muted-foreground">
<Trans ns="views/settings">frigatePlus.modelInfo.modelSelect</Trans>
</p>
</TabsContent>
<TabsContent value="custom" className="space-y-6">
{customFields}
</TabsContent>
</Tabs>
);
}
export default ModelSourcePicker;
@@ -0,0 +1,460 @@
import type {
ErrorSchema,
FieldProps,
RJSFSchema,
UiSchema,
} from "@rjsf/utils";
import { toFieldPathId } from "@rjsf/utils";
import { cloneDeep } from "lodash";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import {
LuChevronDown,
LuChevronRight,
LuPlus,
LuTrash2,
} from "react-icons/lu";
import { applySchemaDefaults } from "@/lib/config-schema";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger,
} from "@/components/ui/collapsible";
import { Label } from "@/components/ui/label";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from "@/components/ui/tooltip";
import type { ConfigFormContext } from "@/types/configForm";
import useSWR from "swr";
import { DetectionHardware } from "@/types/hardware";
import { summarizeDevices } from "@/utils/detectionHardware";
import { HardwarePicker } from "./HardwarePicker";
import { ModelSourcePicker } from "./ModelSourcePicker";
type DetectionModel = {
scene?: string;
devices?: string[];
[key: string]: unknown;
};
// scene and devices get dedicated controls; everything else is the model itself
const CUSTOM_MODEL_FIELDS = [
"path",
"labelmap_path",
"width",
"height",
"input_pixel_format",
"input_tensor",
"input_dtype",
"model_type",
];
/** The detector a model runs on, which is the prefix of its device strings. */
const detectorForModel = (model: DetectionModel): string | undefined =>
model.devices?.[0]?.split(":")[0];
const asModelList = (formData: unknown): DetectionModel[] => {
if (!Array.isArray(formData)) {
return [];
}
return formData.filter(
(item): item is DetectionModel => typeof item === "object" && item !== null,
);
};
const getItemSchema = (schema: RJSFSchema): RJSFSchema | undefined => {
const items = schema.items;
if (!items || typeof items !== "object" || Array.isArray(items)) {
return undefined;
}
return items as RJSFSchema;
};
const getItemProperties = (
schema: RJSFSchema | undefined,
): Record<string, RJSFSchema> => {
if (!schema || typeof schema.properties !== "object" || !schema.properties) {
return {};
}
return schema.properties as Record<string, RJSFSchema>;
};
const getSceneOptions = (itemSchema: RJSFSchema | undefined): string[] => {
const scene = getItemProperties(itemSchema).scene as
| Record<string, unknown>
| undefined;
const values = scene?.enum;
return Array.isArray(values)
? values.filter((v): v is string => typeof v === "string")
: [];
};
export function ModelsField(props: FieldProps) {
const {
schema,
uiSchema,
formData,
onChange,
fieldPathId,
registry,
idSchema,
errorSchema,
disabled,
readonly,
hideError,
onBlur,
onFocus,
} = props;
const { t } = useTranslation(["views/settings", "common"]);
const formContext = registry?.formContext as ConfigFormContext | undefined;
const models = useMemo(() => asModelList(formData), [formData]);
const itemSchema = useMemo(
() => getItemSchema(schema as RJSFSchema),
[schema],
);
const itemProperties = useMemo(
() => getItemProperties(itemSchema),
[itemSchema],
);
const itemUiSchema = useMemo(
() =>
((uiSchema as { items?: UiSchema } | undefined)?.items ?? {}) as UiSchema,
[uiSchema],
);
const sceneOptions = useMemo(() => getSceneOptions(itemSchema), [itemSchema]);
const SchemaField = registry.fields.SchemaField;
const [openByIndex, setOpenByIndex] = useState<Record<number, boolean>>({});
// shared with HardwarePicker through the SWR cache, so this is not a second
// request
const { data: hardware } = useSWR<DetectionHardware[]>("hardware/probe");
useEffect(() => {
setOpenByIndex((previous) => {
const next: Record<number, boolean> = {};
for (let index = 0; index < models.length; index += 1) {
next[index] = previous[index] ?? true;
}
return next;
});
}, [models.length]);
const cameras = formContext?.fullConfig?.cameras;
const savedModels = formContext?.fullConfig?.models;
// `plus` is a readonly field stripped from the form data, so read it from the
// full config. Match on scene rather than index, which shifts when a model is
// added or removed.
const savedPlusForScene = useCallback(
(scene: string | undefined) =>
savedModels?.find((saved) => saved.scene === scene)?.plus,
[savedModels],
);
// a model serves the cameras naming its scene, plus every camera that names
// no scene at all when it is the "all" model
const cameraCountForScene = useCallback(
(scene: string | undefined): number => {
if (!cameras) {
return 0;
}
return Object.values(cameras).filter((camera) => {
const cameraScene = camera?.detect?.scene;
return cameraScene ? cameraScene === scene : scene === "all";
}).length;
},
[cameras],
);
const claimedByOtherModels = useCallback(
(index: number): Record<string, string> => {
const claimed: Record<string, string> = {};
models.forEach((model, currentIndex) => {
if (currentIndex === index) {
return;
}
(model.devices ?? []).forEach((device) => {
claimed[device] = model.scene ?? String(currentIndex + 1);
});
});
return claimed;
},
[models],
);
const updateModel = useCallback(
(index: number, partial: Partial<DetectionModel>) => {
const next = cloneDeep(models);
next[index] = { ...next[index], ...partial };
onChange(next, fieldPathId.path);
},
[models, onChange, fieldPathId.path],
);
const handleAddModel = useCallback(() => {
const base = itemSchema
? (applySchemaDefaults(itemSchema) as DetectionModel)
: ({} as DetectionModel);
const taken = new Set(models.map((model) => model.scene));
const scene = sceneOptions.find((option) => !taken.has(option));
onChange([...models, { ...base, scene, devices: [] }], fieldPathId.path);
setOpenByIndex((previous) => ({ ...previous, [models.length]: true }));
}, [models, itemSchema, sceneOptions, onChange, fieldPathId.path]);
const handleRemoveModel = useCallback(
(index: number) => {
onChange(
models.filter((_, currentIndex) => currentIndex !== index),
fieldPathId.path,
);
setOpenByIndex((previous) => {
const next: Record<number, boolean> = {};
Object.entries(previous).forEach(([key, value]) => {
const current = Number(key);
if (Number.isNaN(current) || current === index) {
return;
}
next[current > index ? current - 1 : current] = value;
});
return next;
});
},
[models, onChange, fieldPathId.path],
);
const renderField = useCallback(
(index: number, fieldName: string) => {
const fieldSchema = itemProperties[fieldName];
if (!SchemaField || !fieldSchema) {
return null;
}
const itemFieldPathId = toFieldPathId(
fieldName,
registry.globalFormOptions,
[...fieldPathId.path, index],
);
const itemErrors = (
errorSchema as Record<string, ErrorSchema> | undefined
)?.[index] as Record<string, ErrorSchema> | undefined;
return (
<SchemaField
key={fieldName}
name={fieldName}
schema={fieldSchema}
uiSchema={(itemUiSchema[fieldName] as UiSchema | undefined) ?? {}}
fieldPathId={itemFieldPathId}
formData={(models[index] as Record<string, unknown>)?.[fieldName]}
errorSchema={itemErrors?.[fieldName]}
onChange={(nextValue: unknown) =>
updateModel(index, { [fieldName]: nextValue })
}
onBlur={onBlur}
onFocus={onFocus}
registry={registry}
disabled={disabled}
readonly={readonly}
hideError={hideError}
/>
);
},
[
SchemaField,
itemProperties,
itemUiSchema,
models,
registry,
fieldPathId.path,
errorSchema,
updateModel,
onBlur,
onFocus,
disabled,
readonly,
hideError,
],
);
const baseId = idSchema?.$id ?? "models";
return (
<div className="space-y-4">
{models.map((model, index) => {
const open = openByIndex[index] ?? true;
const takenScenes = new Set(
models
.filter((_, currentIndex) => currentIndex !== index)
.map((other) => other.scene),
);
return (
<Card key={`${baseId}-${index}`} className="w-full">
<Collapsible
open={open}
onOpenChange={(nextOpen) =>
setOpenByIndex((previous) => ({
...previous,
[index]: nextOpen,
}))
}
>
<CardHeader className="p-4">
<div className="flex items-center justify-between gap-4">
<CollapsibleTrigger asChild>
<CardTitle className="flex-1 cursor-pointer text-sm">
<span>
{t(`detectionModels.scenes.${model.scene ?? "all"}`)}
</span>
<span className="mt-1 block text-xs font-normal text-muted-foreground">
{summarizeDevices(
hardware ?? [],
model.devices ?? [],
) ?? t("detectionModels.hardware.none")}
{" • "}
{t("detectionModels.cameras", {
count: cameraCountForScene(model.scene),
})}
</span>
</CardTitle>
</CollapsibleTrigger>
<div className="flex shrink-0 items-center gap-1">
{models.length > 1 ? (
<Tooltip>
<TooltipTrigger asChild>
<Button
type="button"
variant="ghost"
size="icon"
onClick={() => handleRemoveModel(index)}
disabled={disabled || readonly}
aria-label={t("button.delete", { ns: "common" })}
>
<LuTrash2 className="h-4 w-4" />
</Button>
</TooltipTrigger>
<TooltipContent>
{t("button.delete", { ns: "common" })}
</TooltipContent>
</Tooltip>
) : null}
<CollapsibleTrigger asChild>
<Button
type="button"
variant="ghost"
size="icon"
aria-label={t(
open ? "button.collapse" : "button.expand",
{ ns: "common" },
)}
>
{open ? (
<LuChevronDown className="h-4 w-4" />
) : (
<LuChevronRight className="h-4 w-4" />
)}
</Button>
</CollapsibleTrigger>
</div>
</div>
</CardHeader>
<CollapsibleContent>
<CardContent className="space-y-6 p-4 pt-0">
<div className="space-y-1">
<Label>{t("detectionModels.scene.label")}</Label>
<Select
value={model.scene ?? ""}
onValueChange={(scene) => updateModel(index, { scene })}
disabled={disabled || readonly}
>
<SelectTrigger className="max-w-xs">
<SelectValue />
</SelectTrigger>
<SelectContent>
{sceneOptions.map((scene) => (
<SelectItem
key={scene}
value={scene}
disabled={takenScenes.has(scene)}
>
{t(`detectionModels.scenes.${scene}`)}
</SelectItem>
))}
</SelectContent>
</Select>
<p className="text-xs text-muted-foreground">
{t("detectionModels.scene.description")}
</p>
</div>
<HardwarePicker
idPrefix={`${baseId}-${index}`}
devices={model.devices ?? []}
claimedElsewhere={claimedByOtherModels(index)}
cameraCount={cameraCountForScene(model.scene)}
disabled={disabled || readonly}
onChange={(devices) => updateModel(index, { devices })}
/>
<ModelSourcePicker
path={model.path}
plus={savedPlusForScene(model.scene)}
detector={detectorForModel(model)}
disabled={disabled || readonly}
onPathChange={(path) => updateModel(index, { path })}
customFields={CUSTOM_MODEL_FIELDS.map((fieldName) =>
renderField(index, fieldName),
)}
/>
</CardContent>
</CollapsibleContent>
</Collapsible>
</Card>
);
})}
{models.length < sceneOptions.length ? (
<Button
type="button"
variant="outline"
size="sm"
onClick={handleAddModel}
disabled={disabled || readonly}
className="gap-2"
>
<LuPlus className="h-4 w-4" />
{t("detectionModels.addModel")}
</Button>
) : null}
</div>
);
}
export default ModelsField;
@@ -1,5 +1,5 @@
// Custom RJSF Fields
export { LayoutGridField } from "./LayoutGridField";
export { DetectorHardwareField } from "./DetectorHardwareField";
export { ModelsField } from "./ModelsField";
export { ReplaceRulesField } from "./ReplaceRulesField";
export { LiveStreamsField } from "./LiveStreamsField";
@@ -49,7 +49,7 @@ import { MultiSchemaFieldTemplate } from "./templates/MultiSchemaFieldTemplate";
import { WrapIfAdditionalTemplate } from "./templates/WrapIfAdditionalTemplate";
import { LayoutGridField } from "./fields/LayoutGridField";
import { DetectorHardwareField } from "./fields/DetectorHardwareField";
import { ModelsField } from "./fields/ModelsField";
import { ReplaceRulesField } from "./fields/ReplaceRulesField";
import { CameraInputsField } from "./fields/CameraInputsField";
import { DictAsYamlField } from "./fields/DictAsYamlField";
@@ -111,7 +111,7 @@ export const frigateTheme: FrigateTheme = {
},
fields: {
LayoutGridField: LayoutGridField,
DetectorHardwareField: DetectorHardwareField,
ModelsField: ModelsField,
ReplaceRulesField: ReplaceRulesField,
CameraInputsField: CameraInputsField,
DictAsYamlField: DictAsYamlField,
@@ -111,11 +111,7 @@ const resolveErrorFieldLabel = ({
? "config/cameras"
: formContext?.i18nNamespace;
const translationPath = buildTranslationPath(
stringSegments,
sectionI18nPrefix,
formContext,
);
const translationPath = buildTranslationPath(stringSegments, formContext);
if (effectiveNamespace && translationPath) {
const translated = resolveConfigTranslation(
@@ -168,11 +168,7 @@ export function FieldTemplate(props: FieldTemplateProps) {
(!isArrayItemInAdditionalProp || showArrayItemDescription) &&
!suppressDescription;
const translationPath = buildTranslationPath(
pathSegments,
sectionI18nPrefix,
formContext,
);
const translationPath = buildTranslationPath(pathSegments, formContext);
const fieldPath = fieldPathId.path;
const overrides = formContext?.overrides;
const baselineFormData = formContext?.baselineFormData;

Some files were not shown because too many files have changed in this diff Show More