2022-11-04 05:23:09 +03:00
|
|
|
import logging
|
2023-05-29 13:31:17 +03:00
|
|
|
|
|
|
|
|
from pydantic import Field
|
|
|
|
|
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-06-25 16:24:45 +03:00
|
|
|
from frigate.log import redirect_output_to_logger
|
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:
|
|
|
|
|
from tensorflow.lite.python.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):
|
|
|
|
|
type: Literal[DETECTOR_KEY]
|
|
|
|
|
num_threads: int = Field(default=3, title="Number of detection threads")
|
|
|
|
|
|
2022-11-04 05:23:09 +03:00
|
|
|
|
|
|
|
|
class CpuTfl(DetectionApi):
|
2022-12-15 16:12:52 +03:00
|
|
|
type_key = DETECTOR_KEY
|
|
|
|
|
|
2025-06-25 16:24:45 +03:00
|
|
|
@redirect_output_to_logger(logger, logging.DEBUG)
|
2022-12-15 16:12:52 +03:00
|
|
|
def __init__(self, detector_config: CpuDetectorConfig):
|
2025-06-06 22:41:04 +03:00
|
|
|
interpreter = Interpreter(
|
2023-04-24 15:24:28 +03:00
|
|
|
model_path=detector_config.model.path,
|
2022-12-15 16:12:52 +03:00
|
|
|
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)
|