使用 OpenVINO API 2.0 从 cv::Mat 和 cv::Rect 创建具有单个 ROI 的张量

问题描述 投票:0回答:2

OpenVINO API 2.0 改变了图像预处理的方式。

文档描述了用于从另一个张量构造感兴趣区域张量的重载。

Tensor(const Tensor& other, const Coordinate& begin, const Coordinate& end)

我找到了一个简短的示例,但发现它不够清晰,无法翻译我的用例。

/** input_tensor points to input of a previous network and
    cropROI contains coordinates of output bounding box **/
ov::Tensor input_tensor(ov::element::f32, ov::Shape({1, 3, 20, 20}));
ov::Coordinate begin({0, 0, 0, 0});
ov::Coordinate end({1, 2, 3, 3});
//...

我的目标是拍摄图像 (cv::Mat) 和边界框 (cv::Rect) 并在 ROI 上执行推理,而无需在调用

set_input_tensor
(替换
SetBlob
)之前复制 ROI 内存。我无法找到足够的示例或文档来说明如何实现这一目标。我目前不确定如何将
cv::Rect
翻译为
ov::Coordinate
s。

以前我可以使用 API 1.0 使用以下示例来执行此操作:

const InferenceEngine::ROI roi(0, bounding_box.x, bounding_box.y, bounding_box.width, bounding_box.height);
const InferenceEngine::TensorDesc tensor_desc(InferenceEngine::Precision::U8, { 1, channels, image_height, image_width }, InferenceEngine::Layout::NHWC);
const InferenceEngine::TensorDesc roi_tensor_desc = InferenceEngine::make_roi_desc(tensor_desc, roi, true);
const InferenceEngine::Blob::Ptr image_blob = InferenceEngine::make_shared_blob<uint8_t>(roi_tensor_desc, image.data);
request.SetBlob(input_name, image_blob);
c++ opencv roi openvino
2个回答
0
投票

OpenVINO API 2.0 更改了具有

NHWC
的模型的布局工作。如果您的模型具有
{ 1, channels, image_height, image_width }
布局的输入
NHWC
,则在使用 cmd
'--layout=input_1(NHWC)' '--input_shape=[1, image_height, image_width, channels]'
转换为 IR 后,您将获得输入形状
[1, image_height, image_width, channels]
。因此,您需要相对于这个大小进行投资回报率。

例如,要制作 ROI 张量,您需要:

const auto cv::Rect bounding_box = <>;
const auto shared_tensor = ov::Tensor(<data>); // it's tensor with shared input data
// N, H, W, C
const auto begin = ov::Coordinate({0, bounding_box.y, bounding_box.x, 0}); // the coordinates first bounding box corner
const auto end = ov::Coordinate({1, bounding_box.y + bounding_box.height, bounding_box.x + bounding_box.width, 3}); // the coordinates second bounding box corner
const auto roi_tensor = ov::Tensor(shared_tensor, begin, end); 

ov::Coordinate
是表示形状坐标的类。在这种情况下,这些坐标是边界框的坐标。


0
投票

以前在旧版 API 中,您需要指定 h 和 w 尺寸。

同时,在 OV 2.0 API 中,您需要指定 ROI 张量的坐标起点和终点。

您可以参考这里了解更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.