forked from Github/frigate
upgrade to latest openvino version (#11563)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import logging
|
||||
|
||||
import numpy as np
|
||||
import openvino.runtime as ov
|
||||
import openvino as ov
|
||||
from pydantic import Field
|
||||
from typing_extensions import Literal
|
||||
|
||||
@@ -23,28 +23,56 @@ class OvDetector(DetectionApi):
|
||||
|
||||
def __init__(self, detector_config: OvDetectorConfig):
|
||||
self.ov_core = ov.Core()
|
||||
self.ov_model = self.ov_core.read_model(detector_config.model.path)
|
||||
self.ov_model_type = detector_config.model.model_type
|
||||
|
||||
self.h = detector_config.model.height
|
||||
self.w = detector_config.model.width
|
||||
|
||||
self.interpreter = self.ov_core.compile_model(
|
||||
model=self.ov_model, device_name=detector_config.device
|
||||
model=detector_config.model.path, device_name=detector_config.device
|
||||
)
|
||||
|
||||
logger.info(f"Model Input Shape: {self.interpreter.input(0).shape}")
|
||||
self.output_indexes = 0
|
||||
self.model_invalid = False
|
||||
|
||||
# Ensure the SSD model has the right input and output shapes
|
||||
if self.ov_model_type == ModelTypeEnum.ssd:
|
||||
model_inputs = self.interpreter.inputs
|
||||
model_outputs = self.interpreter.outputs
|
||||
|
||||
if len(model_inputs) != 1:
|
||||
logger.error(
|
||||
f"SSD models must only have 1 input. Found {len(model_inputs)}."
|
||||
)
|
||||
self.model_invalid = True
|
||||
if len(model_outputs) != 1:
|
||||
logger.error(
|
||||
f"SSD models must only have 1 output. Found {len(model_outputs)}."
|
||||
)
|
||||
self.model_invalid = True
|
||||
|
||||
if model_inputs[0].get_shape() != ov.Shape([1, self.w, self.h, 3]):
|
||||
logger.error(
|
||||
f"SSD model input doesn't match. Found {model_inputs[0].get_shape()}."
|
||||
)
|
||||
self.model_invalid = True
|
||||
|
||||
output_shape = model_outputs[0].get_shape()
|
||||
if output_shape[0] != 1 or output_shape[1] != 1 or output_shape[3] != 7:
|
||||
logger.error(f"SSD model output doesn't match. Found {output_shape}.")
|
||||
self.model_invalid = True
|
||||
|
||||
while True:
|
||||
try:
|
||||
tensor_shape = self.interpreter.output(self.output_indexes).shape
|
||||
logger.info(f"Model Output-{self.output_indexes} Shape: {tensor_shape}")
|
||||
self.output_indexes += 1
|
||||
except Exception:
|
||||
logger.info(f"Model has {self.output_indexes} Output Tensors")
|
||||
break
|
||||
if self.ov_model_type == ModelTypeEnum.yolox:
|
||||
self.output_indexes = 0
|
||||
while True:
|
||||
try:
|
||||
tensor_shape = self.interpreter.output(self.output_indexes).shape
|
||||
logger.info(
|
||||
f"Model Output-{self.output_indexes} Shape: {tensor_shape}"
|
||||
)
|
||||
self.output_indexes += 1
|
||||
except Exception:
|
||||
logger.info(f"Model has {self.output_indexes} Output Tensors")
|
||||
break
|
||||
self.num_classes = tensor_shape[2] - 5
|
||||
logger.info(f"YOLOX model has {self.num_classes} classes")
|
||||
self.set_strides_grids()
|
||||
@@ -81,29 +109,32 @@ class OvDetector(DetectionApi):
|
||||
|
||||
def detect_raw(self, tensor_input):
|
||||
infer_request = self.interpreter.create_infer_request()
|
||||
infer_request.infer([tensor_input])
|
||||
# TODO: see if we can use shared_memory=True
|
||||
input_tensor = ov.Tensor(array=tensor_input)
|
||||
infer_request.infer(input_tensor)
|
||||
|
||||
if self.ov_model_type == ModelTypeEnum.ssd:
|
||||
results = infer_request.get_output_tensor()
|
||||
|
||||
detections = np.zeros((20, 6), np.float32)
|
||||
i = 0
|
||||
for object_detected in results.data[0, 0, :]:
|
||||
if object_detected[0] != -1:
|
||||
logger.debug(object_detected)
|
||||
if object_detected[2] < 0.1 or i == 20:
|
||||
|
||||
if self.model_invalid:
|
||||
return detections
|
||||
|
||||
results = infer_request.get_output_tensor(0).data[0][0]
|
||||
|
||||
for i, (_, class_id, score, xmin, ymin, xmax, ymax) in enumerate(results):
|
||||
if i == 20:
|
||||
break
|
||||
detections[i] = [
|
||||
object_detected[1], # Label ID
|
||||
float(object_detected[2]), # Confidence
|
||||
object_detected[4], # y_min
|
||||
object_detected[3], # x_min
|
||||
object_detected[6], # y_max
|
||||
object_detected[5], # x_max
|
||||
class_id,
|
||||
float(score),
|
||||
ymin,
|
||||
xmin,
|
||||
ymax,
|
||||
xmax,
|
||||
]
|
||||
i += 1
|
||||
return detections
|
||||
elif self.ov_model_type == ModelTypeEnum.yolox:
|
||||
|
||||
if self.ov_model_type == ModelTypeEnum.yolox:
|
||||
out_tensor = infer_request.get_output_tensor()
|
||||
# [x, y, h, w, box_score, class_no_1, ..., class_no_80],
|
||||
results = out_tensor.data
|
||||
|
||||
Reference in New Issue
Block a user