2023-11-02 13:55:24 +01:00
|
|
|
import logging
|
2023-11-17 02:08:41 +01:00
|
|
|
import os.path
|
2023-11-02 13:55:24 +01:00
|
|
|
from typing import Literal
|
|
|
|
|
2023-11-04 13:56:35 +01:00
|
|
|
try:
|
|
|
|
from hide_warnings import hide_warnings
|
|
|
|
except: # noqa: E722
|
|
|
|
|
|
|
|
def hide_warnings(func):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-11-02 13:55:24 +01:00
|
|
|
from pydantic import Field
|
|
|
|
|
|
|
|
from frigate.detectors.detection_api import DetectionApi
|
|
|
|
from frigate.detectors.detector_config import BaseDetectorConfig
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DETECTOR_KEY = "rknn"
|
|
|
|
|
2023-11-18 14:53:49 +01:00
|
|
|
supported_socs = ["rk3562", "rk3566", "rk3568", "rk3588"]
|
|
|
|
|
2023-11-02 13:55:24 +01:00
|
|
|
|
|
|
|
class RknnDetectorConfig(BaseDetectorConfig):
|
|
|
|
type: Literal[DETECTOR_KEY]
|
2023-11-17 02:08:41 +01:00
|
|
|
core_mask: int = Field(default=0, ge=0, le=7, title="Core mask for NPU.")
|
2023-11-02 13:55:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Rknn(DetectionApi):
|
|
|
|
type_key = DETECTOR_KEY
|
|
|
|
|
|
|
|
def __init__(self, config: RknnDetectorConfig):
|
2023-11-28 01:13:12 +01:00
|
|
|
# create symlink for Home Assistant add on
|
|
|
|
if not os.path.isfile("/proc/device-tree/compatible"):
|
|
|
|
if os.path.isfile("/device-tree/compatible"):
|
|
|
|
os.symlink("/device-tree/compatible", "/proc/device-tree/compatible")
|
|
|
|
|
2023-11-18 14:53:49 +01:00
|
|
|
# find out SoC
|
|
|
|
try:
|
|
|
|
with open("/proc/device-tree/compatible") as file:
|
|
|
|
soc = file.read().split(",")[-1].strip("\x00")
|
|
|
|
except FileNotFoundError:
|
|
|
|
logger.error("Make sure to run docker in privileged mode.")
|
|
|
|
raise Exception("Make sure to run docker in privileged mode.")
|
|
|
|
|
|
|
|
if soc not in supported_socs:
|
|
|
|
logger.error(
|
|
|
|
"Your SoC is not supported. Your SoC is: {}. Currently these SoCs are supported: {}.".format(
|
|
|
|
soc, supported_socs
|
|
|
|
)
|
|
|
|
)
|
|
|
|
raise Exception(
|
|
|
|
"Your SoC is not supported. Your SoC is: {}. Currently these SoCs are supported: {}.".format(
|
|
|
|
soc, supported_socs
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2023-11-18 22:36:24 +01:00
|
|
|
if not os.path.isfile("/usr/lib/librknnrt.so"):
|
|
|
|
if "rk356" in soc:
|
|
|
|
os.rename("/usr/lib/librknnrt_rk356x.so", "/usr/lib/librknnrt.so")
|
|
|
|
elif "rk3588" in soc:
|
|
|
|
os.rename("/usr/lib/librknnrt_rk3588.so", "/usr/lib/librknnrt.so")
|
2023-11-18 14:53:49 +01:00
|
|
|
|
2023-11-17 02:08:41 +01:00
|
|
|
self.core_mask = config.core_mask
|
2023-11-02 13:55:24 +01:00
|
|
|
self.height = config.model.height
|
|
|
|
self.width = config.model.width
|
|
|
|
|
2024-03-30 11:44:44 +01:00
|
|
|
if True:
|
|
|
|
os.makedirs("/config/model_cache/rknn", exist_ok=True)
|
2023-11-17 02:08:41 +01:00
|
|
|
|
|
|
|
if (config.model.width != 320) or (config.model.height != 320):
|
|
|
|
logger.error(
|
|
|
|
"Make sure to set the model width and heigth to 320 in your config.yml."
|
|
|
|
)
|
|
|
|
raise Exception(
|
|
|
|
"Make sure to set the model width and heigth to 320 in your config.yml."
|
|
|
|
)
|
|
|
|
|
|
|
|
if config.model.input_pixel_format != "bgr":
|
|
|
|
logger.error(
|
|
|
|
'Make sure to set the model input_pixel_format to "bgr" in your config.yml.'
|
|
|
|
)
|
|
|
|
raise Exception(
|
|
|
|
'Make sure to set the model input_pixel_format to "bgr" in your config.yml.'
|
|
|
|
)
|
|
|
|
|
|
|
|
if config.model.input_tensor != "nhwc":
|
|
|
|
logger.error(
|
|
|
|
'Make sure to set the model input_tensor to "nhwc" in your config.yml.'
|
|
|
|
)
|
|
|
|
raise Exception(
|
|
|
|
'Make sure to set the model input_tensor to "nhwc" in your config.yml.'
|
|
|
|
)
|
2023-11-02 13:55:24 +01:00
|
|
|
|
2023-11-03 01:12:54 +01:00
|
|
|
from rknnlite.api import RKNNLite
|
|
|
|
|
2023-11-02 13:55:24 +01:00
|
|
|
self.rknn = RKNNLite(verbose=False)
|
|
|
|
if self.rknn.load_rknn(self.model_path) != 0:
|
|
|
|
logger.error("Error initializing rknn model.")
|
2023-11-17 02:08:41 +01:00
|
|
|
if self.rknn.init_runtime(core_mask=self.core_mask) != 0:
|
|
|
|
logger.error(
|
|
|
|
"Error initializing rknn runtime. Do you run docker in privileged mode?"
|
|
|
|
)
|
2023-11-02 13:55:24 +01:00
|
|
|
|
2024-03-30 11:44:44 +01:00
|
|
|
raise Exception(
|
|
|
|
"RKNN does not currently support any models. Please see the docs for more info."
|
2023-11-02 13:55:24 +01:00
|
|
|
)
|
|
|
|
|
2024-03-30 11:44:44 +01:00
|
|
|
def __del__(self):
|
|
|
|
self.rknn.release()
|
2023-11-02 13:55:24 +01:00
|
|
|
|
|
|
|
@hide_warnings
|
|
|
|
def inference(self, tensor_input):
|
|
|
|
return self.rknn.inference(inputs=tensor_input)
|
|
|
|
|
|
|
|
def detect_raw(self, tensor_input):
|
|
|
|
output = self.inference(
|
|
|
|
[
|
|
|
|
tensor_input,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
return self.postprocess(output[0])
|