2022-11-03 22:23:09 -04:00
|
|
|
import logging
|
2026-07-06 09:28:02 -08:00
|
|
|
from typing import Literal
|
2023-05-29 12:31:17 +02:00
|
|
|
|
2026-02-27 09:55:36 -06:00
|
|
|
from pydantic import ConfigDict, Field
|
2022-11-03 22:23:09 -04:00
|
|
|
|
|
|
|
|
from frigate.detectors.detection_api import DetectionApi
|
2022-12-15 07:12:52 -06:00
|
|
|
from frigate.detectors.detector_config import BaseDetectorConfig
|
2025-12-18 15:12:10 -07:00
|
|
|
from frigate.log import suppress_stderr_during
|
2023-03-03 23:44:17 +00:00
|
|
|
|
2025-06-06 14:41:04 -05:00
|
|
|
from ..detector_utils import tflite_detect_raw, tflite_init
|
|
|
|
|
|
2023-03-03 23:44:17 +00:00
|
|
|
try:
|
|
|
|
|
from tflite_runtime.interpreter import Interpreter
|
|
|
|
|
except ModuleNotFoundError:
|
2026-02-27 05:55:29 +01:00
|
|
|
from ai_edge_litert.interpreter import Interpreter
|
2022-11-03 22:23:09 -04:00
|
|
|
|
2022-12-15 07:12:52 -06:00
|
|
|
|
2022-11-03 22:23:09 -04:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2022-12-15 07:12:52 -06:00
|
|
|
DETECTOR_KEY = "cpu"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CpuDetectorConfig(BaseDetectorConfig):
|
2026-02-27 09:55:36 -06: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 07:12:52 -06:00
|
|
|
type: Literal[DETECTOR_KEY]
|
2026-02-27 09:55:36 -06: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 07:12:52 -06:00
|
|
|
|
2022-11-03 22:23:09 -04:00
|
|
|
|
|
|
|
|
class CpuTfl(DetectionApi):
|
2022-12-15 07:12:52 -06:00
|
|
|
type_key = DETECTOR_KEY
|
|
|
|
|
|
|
|
|
|
def __init__(self, detector_config: CpuDetectorConfig):
|
2025-12-18 15:12:10 -07: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-03 22:23:09 -04:00
|
|
|
|
2025-06-06 14:41:04 -05:00
|
|
|
tflite_init(self, interpreter)
|
2022-11-03 22:23:09 -04:00
|
|
|
|
|
|
|
|
def detect_raw(self, tensor_input):
|
2025-06-06 14:41:04 -05:00
|
|
|
return tflite_detect_raw(self, tensor_input)
|