c 使用onnxruntime/opencv对onnx模型进行推理(附代码)-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 3574024
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

(365)

  • (365)
文章存档

(8)

(130)

(155)

(50)

(22)

我的朋友
相关博文
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·

分类: python/ruby

2022-03-04 17:31:34

#include

#include

#include

#include

mat cv::dnn::blobfromimage(

inputarray image,

double scalefactor = 1.0,

const size & size = size(),

const scalar & mean = scalar(),

bool swaprb = false,

bool crop = false,

int ddepth = cv_32f

)

mat cv::dnn::blobfromimages(

inputarrayofarrays images,

double scalefactor = 1.0,

size size = size(),

const scalar & mean = scalar(),

bool swaprb = false,

bool crop = false,

int ddepth = cv_32f

)

参数解释

images表示多张图像,image表示单张图像

scalefactor表示放缩

size表示图像大小

mean表示均值

swaprb是否交换通道

crop是否剪切

ddepth 输出的类型,默认是浮点数格式

using namespace cv;

using namespace cv::dnn;

using namespace std;

// 图像处理  标准化处理

void preprocess(const mat& image, mat& image_blob)

{

mat input;

image.copyto(input);

//数据处理 标准化

std::vector channels, channel_p;

split(input, channels);

mat r, g, b;

b = channels.at(0);

g = channels.at(1);

r = channels.at(2);

b = (b / 255. - 0.406) / 0.225;

g = (g / 255. - 0.456) / 0.224;

r = (r / 255. - 0.485) / 0.229;

channel_p.push_back(r);

channel_p.push_back(g);

channel_p.push_back(b);

mat outt;

merge(channel_p, outt);

image_blob = outt;

}

string bin_model = "f:\\pycharm\\pycharm_study\\others\\c _learning\\c _master\\onnx\\classification\\vgg16.onnx";

string labels_txt_file = "f:\\pycharm\\pycharm_study\\others\\c _learning\\c _master\\onnx\\classification\\classification_classes_ilsvrc2012.txt";

vector readclassnames();                  // string对象作为vector对象

int main(int argc, char** argv) {

mat image1 = imread("f:\\pycharm\\pycharm_study\\others\\c _learning\\c _master\\onnx\\classification\\dog.jpg");

mat image2 = imread("f:\\pycharm\\pycharm_study\\others\\c _learning\\c _master\\onnx\\classification\\rabbit.jpg");

//用于显示

vectorshowimages;

showimages.push_back(image1);

showimages.push_back(image2);

//处理image1

resize(image1, image1, size(256, 256), inter_area);

image1.convertto(image1, cv_32fc3);

preprocess(image1, image1);         //标准化处理

//处理image2

resize(image2, image2, size(256, 256), inter_area);

image2.convertto(image2, cv_32fc3);

preprocess(image2, image2);         //标准化处理

//image1image2合并到images

vector images;

images.push_back(image1);

images.push_back(image2);

vector labels = readclassnames();

int w = 224;

int h = 224;

// 加载网络

cv::dnn::net net = cv::dnn::readnetfromonnx(bin_model);  // 加载训练好的识别模型

if (net.empty()) {

printf("read onnx model data failure...\n");

return -1;

}

mat inputblob = blobfromimages(images, 1.0, size(w, h), scalar(0, 0, 0), false, true);

// 执行图像分类

net.setinput(inputblob);

cv::mat prob = net.forward();     // 推理出结果

cout << prob.cols<< endl;

vector times;

double time = net.getperfprofile(times);

float ms = (time * 1000) / gettickfrequency();

printf("current inference time : %.2f ms \n", ms);

// 得到最可能分类输出

for (int n = 0; n < prob.rows; n ) {

point classnumber;

double classprob;

mat probmat = prob(rect(0, n, 1000, 1)).clone();

mat result 外汇跟单gendan5.com= probmat.reshape(1, 1);

minmaxloc(result, null, &classprob, null, &classnumber);

int classidx = classnumber.x;

printf("\n current image classification : %s, possible : %.2f\n", labels.at(classidx).c_str(), classprob);

// 显示文本

puttext(showimages[n], labels.at(classidx), point(10, 20), font_hershey_simplex, 0.6, scalar(0, 0, 255), 1, 1);

imshow("image classification", showimages[n]);

waitkey(0);

}

return 0;

}

std::vector readclassnames()

{

std::vector classnames;

std::ifstream fp(labels_txt_file);

if (!fp.is_open())

{

printf("could not open file...\n");

exit(-1);

}

std::string name;

while (!fp.eof())

{

std::getline(fp, name);

if (name.length())

classnames.push_back(name);

}

fp.close();

return classnames;

}

阅读(5880) | 评论(0) | 转发(0) |
0

上一篇:

下一篇:

给主人留下些什么吧!~~
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图