Add detector using mesa teflon delegate

Non-EdgeTPU TFLite can use the standard .tflite format
This commit is contained in:
Jimmy Hon 2025-05-19 04:59:16 +00:00
parent a78518bd94
commit 27bfab3646
3 changed files with 79 additions and 1 deletions

View File

@ -506,7 +506,9 @@ class FrigateConfig(FrigateBaseModel):
model_config["path"] = detector_config.model_path
if "path" not in model_config:
if detector_config.type == "cpu":
if detector_config.type == "cpu" or detector_config.type.endswith(
"_tfl"
):
model_config["path"] = "/cpu_model.tflite"
elif detector_config.type == "edgetpu":
model_config["path"] = "/edgetpu_model.tflite"

View File

@ -1,5 +1,16 @@
import logging
import os
import numpy as np
try:
from tflite_runtime.interpreter import Interpreter, load_delegate
except ModuleNotFoundError:
from tensorflow.lite.python.interpreter import Interpreter, load_delegate
logger = logging.getLogger(__name__)
def tflite_init(self, interpreter):
self.interpreter = interpreter
@ -34,3 +45,30 @@ def tflite_detect_raw(self, tensor_input):
]
return detections
def tflite_load_delegate_interpreter(
delegate_library: str, detector_config, device_config
):
try:
logger.info("Attempting to load NPU")
tf_delegate = load_delegate(delegate_library, device_config)
logger.info("NPU found")
interpreter = Interpreter(
model_path=detector_config.model.path,
experimental_delegates=[tf_delegate],
)
return interpreter
except ValueError:
_, ext = os.path.splitext(detector_config.model.path)
if ext and ext != ".tflite":
logger.error(
"Incorrect model used with NPU. Only .tflite models can be used with a TFLite delegate."
)
else:
logger.error(
"No NPU was detected. If you do not have a TFLite device yet, you must configure CPU detectors."
)
raise

View File

@ -0,0 +1,38 @@
import logging
from typing_extensions import Literal
from frigate.detectors.detection_api import DetectionApi
from frigate.detectors.detector_config import BaseDetectorConfig
from ..detector_utils import (
tflite_detect_raw,
tflite_init,
tflite_load_delegate_interpreter,
)
logger = logging.getLogger(__name__)
# Use _tfl suffix to default tflite model
DETECTOR_KEY = "teflon_tfl"
class TeflonDetectorConfig(BaseDetectorConfig):
type: Literal[DETECTOR_KEY]
class TeflonTfl(DetectionApi):
type_key = DETECTOR_KEY
def __init__(self, detector_config: TeflonDetectorConfig):
# Location in Debian's mesa-teflon-delegate
delegate_library = "/usr/lib/teflon/libteflon.so"
device_config = {}
interpreter = tflite_load_delegate_interpreter(
delegate_library, detector_config, device_config
)
tflite_init(self, interpreter)
def detect_raw(self, tensor_input):
return tflite_detect_raw(self, tensor_input)