2023-11-02 13:55:24 +01:00
|
|
|
import logging
|
2023-11-17 02:08:41 +01:00
|
|
|
import os.path
|
|
|
|
import urllib.request
|
2023-11-02 13:55:24 +01:00
|
|
|
from typing import Literal
|
|
|
|
|
|
|
|
import numpy as np
|
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"]
|
|
|
|
|
|
|
|
yolov8_suffix = {
|
2023-11-17 02:08:41 +01:00
|
|
|
"default-yolov8n": "n",
|
|
|
|
"default-yolov8s": "s",
|
|
|
|
"default-yolov8m": "m",
|
|
|
|
"default-yolov8l": "l",
|
|
|
|
"default-yolov8x": "x",
|
|
|
|
}
|
|
|
|
|
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.model_path = config.model.path or "default-yolov8n"
|
|
|
|
self.core_mask = config.core_mask
|
2023-11-02 13:55:24 +01:00
|
|
|
self.height = config.model.height
|
|
|
|
self.width = config.model.width
|
|
|
|
|
2023-11-18 14:53:49 +01:00
|
|
|
if self.model_path in yolov8_suffix:
|
2023-11-17 02:08:41 +01:00
|
|
|
if self.model_path == "default-yolov8n":
|
2023-11-18 14:53:49 +01:00
|
|
|
self.model_path = "/models/rknn/yolov8n-320x320-{soc}.rknn".format(
|
|
|
|
soc=soc
|
|
|
|
)
|
2023-11-17 02:08:41 +01:00
|
|
|
else:
|
2023-11-18 14:53:49 +01:00
|
|
|
model_suffix = yolov8_suffix[self.model_path]
|
2023-11-17 02:08:41 +01:00
|
|
|
self.model_path = (
|
2023-11-18 14:53:49 +01:00
|
|
|
"/config/model_cache/rknn/yolov8{suffix}-320x320-{soc}.rknn".format(
|
|
|
|
suffix=model_suffix, soc=soc
|
2023-11-17 02:08:41 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
os.makedirs("/config/model_cache/rknn", exist_ok=True)
|
|
|
|
if not os.path.isfile(self.model_path):
|
2023-11-18 14:53:49 +01:00
|
|
|
logger.info(
|
|
|
|
"Downloading yolov8{suffix} model.".format(suffix=model_suffix)
|
|
|
|
)
|
2023-11-17 02:08:41 +01:00
|
|
|
urllib.request.urlretrieve(
|
2023-11-18 14:53:49 +01:00
|
|
|
"https://github.com/MarcA711/rknn-models/releases/download/v1.5.2-{soc}/yolov8{suffix}-320x320-{soc}.rknn".format(
|
|
|
|
soc=soc, suffix=model_suffix
|
2023-11-17 02:08:41 +01:00
|
|
|
),
|
|
|
|
self.model_path,
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
self.rknn.release()
|
|
|
|
|
|
|
|
def postprocess(self, results):
|
|
|
|
"""
|
|
|
|
Processes yolov8 output.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
results: array with shape: (1, 84, n, 1) where n depends on yolov8 model size (for 320x320 model n=2100)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
detections: array with shape (20, 6) with 20 rows of (class, confidence, y_min, x_min, y_max, x_max)
|
|
|
|
"""
|
|
|
|
|
|
|
|
results = np.transpose(results[0, :, :, 0]) # array shape (2100, 84)
|
|
|
|
scores = np.max(
|
|
|
|
results[:, 4:], axis=1
|
|
|
|
) # array shape (2100,); max confidence of each row
|
|
|
|
|
2023-11-17 02:08:41 +01:00
|
|
|
# remove lines with score scores < 0.4
|
|
|
|
filtered_arg = np.argwhere(scores > 0.4)
|
|
|
|
results = results[filtered_arg[:, 0]]
|
|
|
|
scores = scores[filtered_arg[:, 0]]
|
|
|
|
|
|
|
|
num_detections = len(scores)
|
|
|
|
|
|
|
|
if num_detections == 0:
|
|
|
|
return np.zeros((20, 6), np.float32)
|
|
|
|
|
|
|
|
if num_detections > 20:
|
|
|
|
top_arg = np.argpartition(scores, -20)[-20:]
|
|
|
|
results = results[top_arg]
|
|
|
|
scores = scores[top_arg]
|
|
|
|
num_detections = 20
|
|
|
|
|
|
|
|
classes = np.argmax(results[:, 4:], axis=1)
|
|
|
|
|
2023-11-02 13:55:24 +01:00
|
|
|
boxes = np.transpose(
|
|
|
|
np.vstack(
|
|
|
|
(
|
2023-11-18 14:53:49 +01:00
|
|
|
(results[:, 1] - 0.5 * results[:, 3]) / self.height,
|
|
|
|
(results[:, 0] - 0.5 * results[:, 2]) / self.width,
|
|
|
|
(results[:, 1] + 0.5 * results[:, 3]) / self.height,
|
|
|
|
(results[:, 0] + 0.5 * results[:, 2]) / self.width,
|
2023-11-02 13:55:24 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
detections = np.zeros((20, 6), np.float32)
|
2023-11-17 02:08:41 +01:00
|
|
|
detections[:num_detections, 0] = classes
|
|
|
|
detections[:num_detections, 1] = scores
|
|
|
|
detections[:num_detections, 2:] = boxes
|
2023-11-02 13:55:24 +01:00
|
|
|
|
|
|
|
return detections
|
|
|
|
|
|
|
|
@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])
|