From 0febc4d4564ee0ddb6aa3334391b8d72d3e330ed Mon Sep 17 00:00:00 2001 From: ChirayuRai Date: Tue, 26 Aug 2025 15:38:34 -0700 Subject: [PATCH] DeGirum Detector for Frigate (#19111) * Added degirum plugin, updated documentation for degirum detector usage, updated requirements with degirum_headless * Fixed broken link * Made it so openvino prioritizes using GPU and NPU over CPU * Version that detects model and can begin using @local * Updating requirements to build dev container * Added optimized version of degirum plugin + updated docs * Added guard clause for empty inference reponse * Updated DeGirum's docs * Moved DeGirum section to 'Community' detectors, fixed formatting of headers to be more consistent with the rest of the page, and removed uneeded 'models' folder * Moved DeGirum section to correct place in community models * Update ROCm to 6.4.0 (#18264) * Update to rocm 6.4.0 * Update URL * Remove old env var * Dynamic Config Updates (#18353) * Create classes to handle publishing and subscribing config updates * Cleanup * Use config updater * Update handling for enabled config * Cleanup * Recording config updates * Birdseye config updates * Handle notifications * handle review * Update motion * Dynamically update masks and zones for cameras (#18359) * Include config publisher in api * Call update topic for passed topics * Update zones dynamically * Update zones internally * Support zone and mask reset * Handle updating objects config * Don't put status for needing to restart Frigate * Cleanup http tests * Fix tests * Initial custom classification model config support (#18362) * Add basic config for defining a teachable machine model * Add model type * Add basic config for teachable machine models * Adjust config for state and object * Use config to process * Correctly check for objects * Remove debug * Rename to not be teachable machine specific * Cleanup * Implement support for no recordings indicator on timeline (#18363) * Indicate no recordings on the history timeline with gray hash marks This commit includes a new backend API endpoint and the frontend changes needed to support this functionality * don't show slashes for now * Update ROCm to 6.4.1 (#18364) * Update rocm to 6.4.1 * Quick fix * Add ability to configure when custom classification models run (#18380) * Add config to control when classification models are run * Cleanup * Add basic config editor when Frigate can't startup (#18383) * Start Frigate in safe mode when config does not validate * Add safe mode page that is just the config editor * Adjust Frigate config editor when in safe mode * Cleanup * Improve log message * Fix incorrectly running lpr (#18390) * Audio transcription support (#18398) * install new packages for transcription support * add config options * audio maintainer modifications to support transcription * pass main config to audio process * embeddings support * api and transcription post processor * embeddings maintainer support for post processor * live audio transcription with sherpa and faster-whisper * update dispatcher with live transcription topic * frontend websocket * frontend live transcription * frontend changes for speech events * i18n changes * docs * mqtt docs * fix linter * use float16 and small model on gpu for real-time * fix return value and use requestor to embed description instead of passing embeddings * run real-time transcription in its own thread * tweaks * publish live transcriptions on their own topic instead of tracked_object_update * config validator and docs * clarify docs * Implement API to train classification models (#18475) * Intel updates (#18493) * Update openvino and onnxruntime * Install icd and level-zero-gpu deps from intel directly * Install * Add dep * Fix package install * Tiered recordings (#18492) * Implement tiered recording * Add migration for record config * Update docs * Update reference docs * Fix preview query * Fix incorrect accesses * Fix * Fix * Fix * Fix * Upgrade PaddleOCR models to v4 (rec) and v5 (det) (#18505) The PP_OCRv5 text detection models have greatly improved over v3. The v5 recognition model makes improvements to challenging handwriting and uncommon characters, which are not necessary for LPR, so using v4 seemed like a better choice to continue to keep inference time as low as possible. Also included is the full dictionary for Chinese character support. * Audio transcription tweaks (#18540) * use model runner * unload whisper model when live transcription is complete * Classification Model UI (#18571) * Setup basic training structure * Build out route * Handle model configs * Add image fetch APIs * Implement model training screen with dataset selection * Implement viewing of training images * Adjust directories * Implement viewing of images * Add support for deleting images * Implement full deletion * Implement classification model training * Improve naming * More renaming * Improve layout * Reduce logging * Cleanup * Live classification model training (#18583) * Implement model training via ZMQ and add model states to represent training * Get model updates working * Improve toasts and model state * Clean up logging * Add back in * Classification Model Metrics (#18595) * Add speed and rate metrics for custom classification models * Use metrics for classification models * Use keys * Cast to list * Add Mesa Teflon as a TFLite detector (#18310) * Refactor common functions for tflite detector implementations * Add detector using mesa teflon delegate Non-EdgeTPU TFLite can use the standard .tflite format * Add mesa-teflon-delegate from bookworm-backports to arm64 images * feat: enable using GenAI for cameras with GenAI disabled from the API (#18616) * fix: Initialize GenAI client if GenAI is enabled globally (#18623) * Make Birdseye clickable (#18628) * keep track of layout changes and publish on change * websocket hook * clickable overlay div to navigate to full camera view * Refactor TensorRT (#18643) * Combine base and arm trt detectors * Remove unused deps for amd64 build * Add missing packages and cleanup ldconfig * Expand packages for tensorflow model training * Cleanup * Refactor training to not reserve memory * Dynamic Management of Cameras (#18671) * Add base class for global config updates * Add or remove camera states * Move camera process management to separate thread * Move camera management fully to separate class * Cleanup * Stop camera processes when stop command is sent * Start processes dynamically when needed * Adjust * Leave extra room in tracked object queue for two cameras * Dynamically set extra config pieces * Add some TODOs * Fix type check * Simplify config updates * Improve typing * Correctly handle indexed entries * Cleanup * Create out SHM * Use ZMQ for signaling object detectoin is completed * Get camera correctly created * Cleanup for updating the cameras config * Cleanup * Don't enable audio if no cameras have audio transcription * Use exact string so similar camera names don't interfere * Add ability to update config via json body to config/set endpoint Additionally, update the config in a single rather than multiple calls for each updated key * fix autotracking calibration to support new config updater function --------- Co-authored-by: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> * Use Fork-Server As Spawn Method (#18682) * Set runtime * Use count correctly * Don't assume camera sizes * Use separate zmq proxy for object detection * Correct order * Use forkserver * Only store PID instead of entire process reference * Cleanup * Catch correct errors * Fix typing * Remove before_run from process util The before_run never actually ran because: You're right to suspect an issue with before_run not being called and a potential deadlock. The way you've implemented the run_wrapper using __getattribute__ for the run method of BaseProcess is a common pitfall in Python's multiprocessing, especially when combined with how multiprocessing.Process works internally. Here's a breakdown of why before_run isn't being called and why you might be experiencing a deadlock: The Problem: __getattribute__ and Process Serialization When you create a multiprocessing.Process object and call start(), the multiprocessing module needs to serialize the process object (or at least enough of it to re-create the process in the new interpreter). It then pickles this serialized object and sends it to the newly spawned process. The issue with your __getattribute__ implementation for run is that: run is retrieved during serialization: When multiprocessing tries to pickle your Process object to send to the new process, it will likely access the run attribute. This triggers your __getattribute__ wrapper, which then tries to bind run_wrapper to self. run_wrapper is bound to the parent process's self: The run_wrapper closure, when created in the parent process, captures the self (the Process instance) from the parent's memory space. Deserialization creates a new object: In the child process, a new Process object is created by deserializing the pickled data. However, the run_wrapper method that was pickled still holds a reference to the self from the parent process. This is a subtle but critical distinction. The child's run is not your wrapped run: When the child process starts, it internally calls its own run method. Because of the serialization and deserialization process, the run method that's ultimately executed in the child process is the original multiprocessing.Process.run or the Process.run if you had directly overridden it. Your __getattribute__ magic, which wraps run, isn't correctly applied to the Process object within the child's context. * Cleanup * Logging bugfix (#18465) * use mp Manager to handle logging queues A Python bug (https://github.com/python/cpython/issues/91555) was preventing logs from the embeddings maintainer process from printing. The bug is fixed in Python 3.14, but a viable workaround is to use the multiprocessing Manager, which better manages mp queues and causes the logging to work correctly. * consolidate * fix typing * Fix typing * Use global log queue * Move to using process for logging * Convert camera tracking to process * Add more processes * Finalize process * Cleanup * Cleanup typing * Formatting * Remove daemon --------- Co-authored-by: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> * Add basic camera settings to UI for testing (#18690) * add basic camera add/edit pane to the UI for testing * only init model runner if transcription is enabled globally * fix role checkboxes * Ensure logging config is propagated to forked processes (#18704) * Move log level initialization to log * Use logger config * Formatting * Fix config order * Set process names --------- Co-authored-by: Nicolas Mowen * Fix go2rtc init (#18708) * Cleanup process handling * Adjust process name * Reduce tf initialization * Don't use staticmethod * Don't fail on unicode debug for config updates * Catch unpickling error * Fix birdseye crash when dynamically adding a camera (#18821) * Catch invalid character index in lpr CTC decoder (#18825) * Classification model cover images (#18843) * Move to separate component * Add cover images for clssification models * Fix process name * Handle SIGINT with forkserver (#18860) * Pass stopevent from main start * Share stop event across processes * preload modules * remove explicit os._exit call --------- Co-authored-by: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> * Don't try to close or join mp manager queues (#18866) Multiprocessing Manager queues don't have a close() or join_thread() method, and the Manager will clean it up appropriately after we empty it. This prevents an infinite loop when an AttributeError exception fires for Manager AutoProxy queue objects. * Improve logging (#18867) * Ignore numpy get limits warning * Add function wrapper to redirect stdout and stderr to logpipe * Save stderr too * Add more to catch * run logpipe * Use other logging redirect class * Use other logging redirect class * add decorator for redirecting c/c++ level output to logger * fix typing --------- Co-authored-by: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> * Add ONVIF focus support (#18883) * backend * frontend and i18n * 0.17 tweaks (#18892) * Set version * Cleanup more logs * Don't log matplotlib * Improve object classification (#18908) * Ui improvements * Improve image cropping and model saving * Improve naming * Add logs for training * Improve model labeling * Don't set sub label for none object classification * Cleanup * Remove TFLite init logs * Improve classification UI (#18910) * Move threhsold to base model config * Improve score handling * Add back button * Classification improvements (#19020) * Move classification training to full process * Sort class images * Semantic Search Triggers (#18969) * semantic trigger test * database and model * config * embeddings maintainer and trigger post-processor * api to create, edit, delete triggers * frontend and i18n keys * use thumbnail and description for trigger types * image picker tweaks * initial sync * thumbnail file management * clean up logs and use saved thumbnail on frontend * publish mqtt messages * webpush changes to enable trigger notifications * add enabled switch * add triggers from explore * renaming and deletion fixes * fix typing * UI updates and add last triggering event time and link * log exception instead of return in endpoint * highlight entry in UI when triggered * save and delete thumbnails directly * remove alert action for now and add descriptions * tweaks * clean up * fix types * docs * docs tweaks * docs * reuse enum * Optionally show tracked object paths in debug view (#19025) * Dynamically enable/disable GenAI (#19139) * config * dispatcher and mqtt * docs * use config updater * add switch to frontend * Classification train updates (#19173) * Improve model train button * Add filters for classification * Cleanup * Don't run classification on false positives * Cleanup filter * Fix icon color * Object attribute classification (#19205) * Add enum for type of classification for objects * Update recognized license plate topic to be used as attribute updater * Update attribute for attribute type object classification * Cleanup * Require setting process priority for FrigateProcess (#19207) * Add bookworm-backports to the rocm images and upgrade mesa/vaapi to support RDNA4 GPUs (#19312) * Improve the tablet layout (#19320) * Improve the tablet layout * Update imports sort * Fix more imports * Implement start for review item description processor (#19352) * Add review item data transmission * Publish review updates * Add review item subscriber * Basic implementation for testing review processor * Formatting * Cleanup * Improve comms typing (#18599) * Enable mypy for comms * Make zmq data types consistent * Cleanup inter process typing issues * Cleanup embeddings typing * Cleanup config updater * Cleanup recordings updator * Make publisher have a generic type * Cleanup event metadata updater * Cleanup event metadata updater * Cleanup detections updater * Cleanup websocket * Cleanup mqtt * Cleanup webpush * Cleanup dispatcher * Formatting * Remove unused * Add return type * Fix tests * Fix semantic triggers config typing * Cleanup * Ensure alertVideos persistence is loaded before displaying thumb or preview (#19432) The default value of true would cause previews to be loaded in the background even if the local storage value was false * Adjust loitering behavior based on object type (#19433) * Adjust loitering behavior based on object * Update docs * Grammar * Enable mypy for DB and fix types (#19434) * Install peewee type hints * Models now have proper types * Fix iterator type * Enable debug builds with dev reqs installed * Install as wheel * Fix cast type * Migrate object genai configuration (#19437) * Move genAI object to objects section * Adjust config propogation behavior * Refactor genai config usage * Automatic migration * Always start the embeddings process * Always init embeddings * Config fixes * Adjust reference config * Adjust docs * Formatting * Fix * Review Item GenAI metadata (#19442) * Rename existing function * Keep track of thumbnial updates * Tinkering with genai prompt * Adjust input format * Create model for review description output * testing prompt changes * Prompt improvements and image saving * Add config for review items genai * Use genai review config * Actual config usage * Adjust debug image saving * Fix * Fix review creation * Adjust prompt * Prompt adjustment * Run genai in thread * Fix detections block * Adjust prompt * Prompt changes * Save genai response to metadata model * Handle metadata * Send review update to dispatcher * Save review metadata to DB * Send review notification updates * Quick fix * Fix name * Fix update type * Correctly dump model * Add card * Add card * Remove message * Cleanup typing and UI * Adjust prompt * Formatting * Add log * Formatting * Add inference speed and keep alive * Review genai updates (#19448) * Include extra level for normal activity * Add dynamic toggling * Update docs * Add different threshold for genai * Adjust webUI for object and review description feature * Adjust config * Send on startup * Cleanup config setting * Set config * Fix config name * Use preview frames for Review Descriptions (#19450) * Use preview frames for genai * Cleanup * Adjust * Add config for users to define additional concerns that GenAI should make note of in review summary (#19463) * Don't default to openai * Improve UI * Allow configuring additional concerns that users may want the AI to note * Formatting * Add preferred language config * Remove unused * Added total camera fps, total processed fps, and total skipped fps to stats api (#19469) Co-authored-by: Mark Francis * Genai review summaries (#19473) * Generate review item summaries with requests * Adjust logic to only send important items * Don't mention ladder * Adjust prompt to be more specific * Add more relaxed nature for normal activity * Cleanup summary * Update ollama client * Add more directions to analyze the frames in order * Remove environment from prompt * Add ability to pass additional args to Ollama (#19484) * Call out recognized objects more specifically * Cleanup * Make keep_alive and options configurable * Generalize * Use for other providers * Update GenAI docs for new review summaries feature (#19493) * Remove old genai docs * Separate existing genai docs to separate sections * Add docs for genai features * Update reference config * Update link * Move to bottom * Improve natural language of prompt (#19515) * Make sequence details human-readable so they are used in natural language response * Cleanup * Improve prompt and image selection * Adjust * Adjust sligtly * Format time * Adjust frame selection logic * Debug save response * Ignore extra fields * Adjust docs * Cleanup filename sanitization * Added degirum plugin, updated documentation for degirum detector usage, updated requirements with degirum_headless * Fixed broken link * Made it so openvino prioritizes using GPU and NPU over CPU * Version that detects model and can begin using @local * Added optimized version of degirum plugin + updated docs * Updating requirements to build dev container * Added guard clause for empty inference reponse * Updated DeGirum's docs * Moved DeGirum section to 'Community' detectors, fixed formatting of headers to be more consistent with the rest of the page, and removed uneeded 'models' folder * Moved DeGirum section to correct place in community models * Added degirum plugin, updated documentation for degirum detector usage, updated requirements with degirum_headless * Fixed broken link * Made it so openvino prioritizes using GPU and NPU over CPU * Version that detects model and can begin using @local * Added optimized version of degirum plugin + updated docs * Updating requirements to build dev container * Added guard clause for empty inference reponse * Updated DeGirum's docs * Moved DeGirum section to 'Community' detectors, fixed formatting of headers to be more consistent with the rest of the page, and removed uneeded 'models' folder * Moved DeGirum section to correct place in community models * Added degirum plugin, updated documentation for degirum detector usage, updated requirements with degirum_headless * Fixed broken link * Made it so openvino prioritizes using GPU and NPU over CPU * Version that detects model and can begin using @local * Added optimized version of degirum plugin + updated docs * Updating requirements to build dev container * Added guard clause for empty inference reponse * Updated DeGirum's docs * Moved DeGirum section to 'Community' detectors, fixed formatting of headers to be more consistent with the rest of the page, and removed uneeded 'models' folder * Moved DeGirum section to correct place in community models * Reverted changes to classification and audio --------- Co-authored-by: Nicolas Mowen Co-authored-by: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Co-authored-by: Jimmy Co-authored-by: FL42 <46161216+fl42@users.noreply.github.com> Co-authored-by: Steve Smith Co-authored-by: markfrancisonly <12145270+markfrancisonly@users.noreply.github.com> Co-authored-by: Mark Francis --- docker/main/requirements-wheels.txt | 4 +- docs/docs/configuration/object_detectors.md | 98 +++++++++++++- frigate/detectors/plugins/degirum.py | 135 ++++++++++++++++++++ 3 files changed, 234 insertions(+), 3 deletions(-) create mode 100644 frigate/detectors/plugins/degirum.py diff --git a/docker/main/requirements-wheels.txt b/docker/main/requirements-wheels.txt index e5f5e6eec..c8c9662ce 100644 --- a/docker/main/requirements-wheels.txt +++ b/docker/main/requirements-wheels.txt @@ -79,4 +79,6 @@ tflite_runtime @ https://github.com/feranick/TFlite-builds/releases/download/v2. sherpa-onnx==1.12.* faster-whisper==1.1.* librosa==0.11.* -soundfile==0.13.* \ No newline at end of file +soundfile==0.13.* +# DeGirum detector +degirum == 0.16.* diff --git a/docs/docs/configuration/object_detectors.md b/docs/docs/configuration/object_detectors.md index fbbeb296e..bed12ffbd 100644 --- a/docs/docs/configuration/object_detectors.md +++ b/docs/docs/configuration/object_detectors.md @@ -14,6 +14,7 @@ Frigate supports multiple different detectors that work on different types of ha - [Coral EdgeTPU](#edge-tpu-detector): The Google Coral EdgeTPU is available in USB and m.2 format allowing for a wide range of compatibility with devices. - [Hailo](#hailo-8): The Hailo8 and Hailo8L AI Acceleration module is available in m.2 format with a HAT for RPi devices, offering a wide range of compatibility with devices. - [MemryX](#memryx-mx3): The MX3 Acceleration module is available in m.2 format, offering broad compatibility across various platforms. +- [DeGirum](#degirum): Service for using hardware devices in the cloud or locally. Hardware and models provided on the cloud on [their website](https://hub.degirum.com). **AMD** @@ -245,8 +246,6 @@ 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 VPU hardware. To configure an OpenVINO detector, set the `"type"` attribute to `"openvino"`. @@ -1200,6 +1199,101 @@ Explanation of the paramters: - **example**: Specifying `output_name = "frigate-{quant}-{input_basename}-{soc}-v{tk_version}"` could result in a model called `frigate-i8-my_model-rk3588-v2.3.0.rknn`. - `config`: Configuration passed to `rknn-toolkit2` for model conversion. For an explanation of all available parameters have a look at section "2.2. Model configuration" of [this manual](https://github.com/MarcA711/rknn-toolkit2/releases/download/v2.3.2/03_Rockchip_RKNPU_API_Reference_RKNN_Toolkit2_V2.3.2_EN.pdf). +## DeGirum + +DeGirum is a detector that can use any type of hardware listed on [their website](https://hub.degirum.com). DeGirum can be used with local hardware through a DeGirum AI Server, or through the use of `@local`. You can also connect directly to DeGirum's AI Hub to run inferences. **Please Note:** This detector *cannot* be used for commercial purposes. + +### Configuration + +#### AI Server Inference + +Before starting with the config file for this section, you must first launch an AI server. DeGirum has an AI server ready to use as a docker container. Add this to your `docker-compose.yml` to get started: +```yaml +degirum_detector: + container_name: degirum + image: degirum/aiserver:latest + privileged: true + ports: + - "8778:8778" +``` +All supported hardware will automatically be found on your AI server host as long as relevant runtimes and drivers are properly installed on your machine. Refer to [DeGirum's docs site](https://docs.degirum.com/pysdk/runtimes-and-drivers) if you have any trouble. + +Once completed, changing the `config.yml` file is simple. +```yaml +degirum_detector: + type: degirum + location: degirum # Set to service name (degirum_detector), container_name (degirum), or a host:port (192.168.29.4:8778) + zoo: degirum/public # DeGirum's public model zoo. Zoo name should be in format "workspace/zoo_name". degirum/public is available to everyone, so feel free to use it if you don't know where to start. If you aren't pulling a model from the AI Hub, leave this and 'token' blank. + token: dg_example_token # For authentication with the AI Hub. Get this token through the "tokens" section on the main page of the [AI Hub](https://hub.degirum.com). This can be left blank if you're pulling a model from the public zoo and running inferences on your local hardware using @local or a local DeGirum AI Server +``` +Setting up a model in the `config.yml` is similar to setting up an AI server. +You can set it to: +- A model listed on the [AI Hub](https://hub.degirum.com), given that the correct zoo name is listed in your detector + - If this is what you choose to do, the correct model will be downloaded onto your machine before running. +- A local directory acting as a zoo. See DeGirum's docs site [for more information](https://docs.degirum.com/pysdk/user-guide-pysdk/organizing-models#model-zoo-directory-structure). +- A path to some model.json. +```yaml +model: + path: ./mobilenet_v2_ssd_coco--300x300_quant_n2x_orca1_1 # directory to model .json and file + width: 300 # width is in the model name as the first number in the "int"x"int" section + height: 300 # height is in the model name as the second number in the "int"x"int" section + input_pixel_format: rgb/bgr # look at the model.json to figure out which to put here +``` + + +#### Local Inference + +It is also possible to eliminate the need for an AI server and run the hardware directly. The benefit of this approach is that you eliminate any bottlenecks that occur when transferring prediction results from the AI server docker container to the frigate one. However, the method of implementing local inference is different for every device and hardware combination, so it's usually more trouble than it's worth. A general guideline to achieve this would be: +1. Ensuring that the frigate docker container has the runtime you want to use. So for instance, running `@local` for Hailo means making sure the container you're using has the Hailo runtime installed. +2. To double check the runtime is detected by the DeGirum detector, make sure the `degirum sys-info` command properly shows whatever runtimes you mean to install. +3. Create a DeGirum detector in your `config.yml` file. + +```yaml +degirum_detector: + type: degirum + location: "@local" # For accessing AI Hub devices and models + zoo: degirum/public # DeGirum's public model zoo. Zoo name should be in format "workspace/zoo_name". degirum/public is available to everyone, so feel free to use it if you don't know where to start. + token: dg_example_token # For authentication with the AI Hub. Get this token through the "tokens" section on the main page of the [AI Hub](https://hub.degirum.com). This can be left blank if you're pulling a model from the public zoo and running inferences on your local hardware using @local or a local DeGirum AI Server + +``` + +Once `degirum_detector` is setup, you can choose a model through 'model' section in the `config.yml` file. + +```yaml +model: + path: mobilenet_v2_ssd_coco--300x300_quant_n2x_orca1_1 + width: 300 # width is in the model name as the first number in the "int"x"int" section + height: 300 # height is in the model name as the second number in the "int"x"int" section + input_pixel_format: rgb/bgr # look at the model.json to figure out which to put here +``` + + +#### AI Hub Cloud Inference + +If you do not possess whatever hardware you want to run, there's also the option to run cloud inferences. Do note that your detection fps might need to be lowered as network latency does significantly slow down this method of detection. For use with Frigate, we highly recommend using a local AI server as described above. To set up cloud inferences, +1. Sign up at [DeGirum's AI Hub](https://hub.degirum.com). +2. Get an access token. +3. Create a DeGirum detector in your `config.yml` file. + +```yaml +degirum_detector: + type: degirum + location: "@cloud" # For accessing AI Hub devices and models + zoo: degirum/public # DeGirum's public model zoo. Zoo name should be in format "workspace/zoo_name". degirum/public is available to everyone, so feel free to use it if you don't know where to start. + token: dg_example_token # For authentication with the AI Hub. Get this token through the "tokens" section on the main page of the (AI Hub)[https://hub.degirum.com). + +``` + +Once `degirum_detector` is setup, you can choose a model through 'model' section in the `config.yml` file. + +```yaml +model: + path: mobilenet_v2_ssd_coco--300x300_quant_n2x_orca1_1 + width: 300 # width is in the model name as the first number in the "int"x"int" section + height: 300 # height is in the model name as the second number in the "int"x"int" section + input_pixel_format: rgb/bgr # look at the model.json to figure out which to put here +``` + # Models Some model types are not included in Frigate by default. diff --git a/frigate/detectors/plugins/degirum.py b/frigate/detectors/plugins/degirum.py new file mode 100644 index 000000000..07f81e74d --- /dev/null +++ b/frigate/detectors/plugins/degirum.py @@ -0,0 +1,135 @@ +import logging +import queue + +import degirum as dg +import numpy as np +from pydantic import Field +from typing_extensions import Literal + +from frigate.detectors.detection_api import DetectionApi +from frigate.detectors.detector_config import BaseDetectorConfig + +logger = logging.getLogger(__name__) +DETECTOR_KEY = "degirum" + + +### DETECTOR CONFIG ### +class DGDetectorConfig(BaseDetectorConfig): + type: Literal[DETECTOR_KEY] + location: str = Field(default=None, title="Inference Location") + zoo: str = Field(default=None, title="Model Zoo") + token: str = Field(default=None, title="DeGirum Cloud Token") + + +### ACTUAL DETECTOR ### +class DGDetector(DetectionApi): + type_key = DETECTOR_KEY + + def __init__(self, detector_config: DGDetectorConfig): + self._queue = queue.Queue() + self._zoo = dg.connect( + detector_config.location, detector_config.zoo, detector_config.token + ) + + logger.debug(f"Models in zoo: {self._zoo.list_models()}") + + self.dg_model = self._zoo.load_model( + detector_config.model.path, + ) + + # Setting input image format to raw reduces preprocessing time + self.dg_model.input_image_format = "RAW" + + # Prioritize the most powerful hardware available + self.select_best_device_type() + # Frigate handles pre processing as long as these are all set + input_shape = self.dg_model.input_shape[0] + self.model_height = input_shape[1] + self.model_width = input_shape[2] + + # Passing in dummy frame so initial connection latency happens in + # init function and not during actual prediction + frame = np.zeros( + (detector_config.model.width, detector_config.model.height, 3), + dtype=np.uint8, + ) + # Pass in frame to overcome first frame latency + self.dg_model(frame) + self.prediction = self.prediction_generator() + + def select_best_device_type(self): + """ + Helper function that selects fastest hardware available per model runtime + """ + types = self.dg_model.supported_device_types + + device_map = { + "OPENVINO": ["GPU", "NPU", "CPU"], + "HAILORT": ["HAILO8L", "HAILO8"], + "N2X": ["ORCA1", "CPU"], + "ONNX": ["VITIS_NPU", "CPU"], + "RKNN": ["RK3566", "RK3568", "RK3588"], + "TENSORRT": ["DLA", "GPU", "DLA_ONLY"], + "TFLITE": ["ARMNN", "EDGETPU", "CPU"], + } + + runtime = types[0].split("/")[0] + # Just create an array of format {runtime}/{hardware} for every hardware + # in the value for appropriate key in device_map + self.dg_model.device_type = [ + f"{runtime}/{hardware}" for hardware in device_map[runtime] + ] + + def prediction_generator(self): + """ + Generator for all incoming frames. By using this generator, we don't have to keep + reconnecting our websocket on every "predict" call. + """ + logger.debug("Prediction generator was called") + with self.dg_model as model: + while 1: + logger.info(f"q size before calling get: {self._queue.qsize()}") + data = self._queue.get(block=True) + logger.info(f"q size after calling get: {self._queue.qsize()}") + logger.debug( + f"Data we're passing into model predict: {data}, shape of data: {data.shape}" + ) + result = model.predict(data) + logger.debug(f"Prediction result: {result}") + yield result + + def detect_raw(self, tensor_input): + # Reshaping tensor to work with pysdk + truncated_input = tensor_input.reshape(tensor_input.shape[1:]) + logger.debug(f"Detect raw was called for tensor input: {tensor_input}") + + # add tensor_input to input queue + self._queue.put(truncated_input) + logger.debug(f"Queue size after adding truncated input: {self._queue.qsize()}") + + # define empty detection result + detections = np.zeros((20, 6), np.float32) + # grab prediction + res = next(self.prediction) + + # If we have an empty prediction, return immediately + if len(res.results) == 0 or len(res.results[0]) == 0: + return detections + + i = 0 + for result in res.results: + if i >= 20: + break + + detections[i] = [ + result["category_id"], + float(result["score"]), + result["bbox"][1] / self.model_height, + result["bbox"][0] / self.model_width, + result["bbox"][3] / self.model_height, + result["bbox"][2] / self.model_width, + ] + i += 1 + + logger.debug(f"Detections output: {detections}") + return detections