2022-11-04 05:23:09 +03:00
|
|
|
import logging
|
2023-05-29 13:31:17 +03:00
|
|
|
|
2026-02-27 18:55:36 +03:00
|
|
|
from pydantic import ConfigDict, Field
|
2023-05-29 13:31:17 +03:00
|
|
|
from typing_extensions import Literal
|
2022-11-04 05:23:09 +03:00
|
|
|
|
|
|
|
|
from frigate.detectors.detection_api import DetectionApi
|
2022-12-15 16:12:52 +03:00
|
|
|
from frigate.detectors.detector_config import BaseDetectorConfig
|
2025-12-19 01:12:10 +03:00
|
|
|
from frigate.log import suppress_stderr_during
|
2023-03-04 02:44:17 +03:00
|
|
|
|
2025-06-06 22:41:04 +03:00
|
|
|
from ..detector_utils import tflite_detect_raw, tflite_init
|
|
|
|
|
|
2023-03-04 02:44:17 +03:00
|
|
|
try:
|
|
|
|
|
from tflite_runtime.interpreter import Interpreter
|
|
|
|
|
except ModuleNotFoundError:
|
2026-02-27 07:55:29 +03:00
|
|
|
from ai_edge_litert.interpreter import Interpreter
|
2022-11-04 05:23:09 +03:00
|
|
|
|
2022-12-15 16:12:52 +03:00
|
|
|
|
2022-11-04 05:23:09 +03:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2022-12-15 16:12:52 +03:00
|
|
|
DETECTOR_KEY = "cpu"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CpuDetectorConfig(BaseDetectorConfig):
|
2026-02-27 18:55:36 +03:00
|
|
|
"""CPU TFLite detector that runs TensorFlow Lite models on the host CPU without hardware acceleration. Not recommended."""
|
|
|
|
|
|
|
|
|
|
model_config = ConfigDict(
|
|
|
|
|
title="CPU",
|
|
|
|
|
)
|
|
|
|
|
|
2022-12-15 16:12:52 +03:00
|
|
|
type: Literal[DETECTOR_KEY]
|
2026-02-27 18:55:36 +03:00
|
|
|
num_threads: int = Field(
|
|
|
|
|
default=3,
|
|
|
|
|
title="Number of detection threads",
|
|
|
|
|
description="The number of threads used for CPU-based inference.",
|
|
|
|
|
)
|
2022-12-15 16:12:52 +03:00
|
|
|
|
2022-11-04 05:23:09 +03:00
|
|
|
|
|
|
|
|
class CpuTfl(DetectionApi):
|
2022-12-15 16:12:52 +03:00
|
|
|
type_key = DETECTOR_KEY
|
|
|
|
|
|
|
|
|
|
def __init__(self, detector_config: CpuDetectorConfig):
|
2025-12-19 01:12:10 +03:00
|
|
|
# Suppress TFLite delegate creation messages that bypass Python logging
|
|
|
|
|
with suppress_stderr_during("tflite_interpreter_init"):
|
|
|
|
|
interpreter = Interpreter(
|
|
|
|
|
model_path=detector_config.model.path,
|
|
|
|
|
num_threads=detector_config.num_threads or 3,
|
|
|
|
|
)
|
2022-11-04 05:23:09 +03:00
|
|
|
|
2025-06-06 22:41:04 +03:00
|
|
|
tflite_init(self, interpreter)
|
2022-11-04 05:23:09 +03:00
|
|
|
|
|
|
|
|
def detect_raw(self, tensor_input):
|
2025-06-06 22:41:04 +03:00
|
|
|
return tflite_detect_raw(self, tensor_input)
|