OpenVINO已经有自己的预处理方式,代码如下:
ov::preprocess::PrePostProcessor ppp(model);
ov::preprocess::InputInfo& input = ppp.input(tensor_name);
// we only need to know where is C dimension
input.model().set_layout("...C");
// specify scale and mean values, order of operations is important
input.preprocess().mean(116.78f).scale({ 57.21f, 57.45f, 57.73f });
// insert preprocessing operations to the 'model'
model = ppp.build();
同时你还可以使用OpenCV的blobfromImage函数来完成图像预处理:
// 预处理
cv::Mat blob_image;
resize(image, blob_image, cv::Size(input_w, input_h));
blob_image.convertTo(blob_image, CV_32F);
blob_image = blob_image / 255.0;
或者
cv::Mat blob = cv::dnn::blobFromImage(image, 1 / 255.0, cv::Size(640, 640), cv::Scalar(0, 0, 0), true, false);
从Mat创建Tensor对象,这个时候我就喜欢模型的输入格式是NHWC的方式,这样创建Tensor,设置输入数据只要一行代码即可,示例如下:
bgr.convertTo(bgr, CV_32FC3);
gray.convertTo(gray, CV_32F, 1.0/255);
ov::Tensor blob1(input_tensor_1.get_element_type(), input_tensor_1.get_shape(), (float *)bgr.data);
ov::Tensor blob2(input_tensor_2.get_element_type(), input_tensor_2.get_shape(), (float *)gray.data);
推理预测结果Tensor到OpenCV Mat对象,也很简单明了,如果输出数据是NHWC四维,可以直接用下面的代码:
const float* prob = (float*)output.data();
const ov::Shape outputDims = output.get_shape();
size_t numRows = outputDims[1];
size_t numCols = outputDims[2];
// 通道数为1 用这行
cv::Mat detOut(numRows, numCols, CV_32F, (float*)prob);
// 通道数为3 用这行
cv::Mat detOut(numRows, numCols, CV_32FC3, (float*)prob);
如果输出是1xHW的三维张量,直接用下面这样: