2022-11-03 22:23:09 -04:00
|
|
|
import logging
|
2023-05-29 12:31:17 +02:00
|
|
|
|
|
|
|
|
from pydantic import Field
|
|
|
|
|
from typing_extensions import Literal
|
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:
|
|
|
|
|
from tensorflow.lite.python.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):
|
|
|
|
|
type: Literal[DETECTOR_KEY]
|
|
|
|
|
num_threads: int = Field(default=3, title="Number of detection threads")
|
|
|
|
|
|
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)
|