完整的安装配置过程可以参考这个博客
https://blog.csdn.net/weixin_43051346/article/details/122414506
因为我在进行protobuf和ncnn框架编译的时候,总会出现报错,无法解决。
最后发现,换了台电脑重新配置就没问题了。
配置流程
然后我直接把另外一台电脑编译好的文件拿来用,流程如下:
1.下载好百度云里面的文件并解压:
链接:https://pan.baidu.com/s/1_rLFVW_4p9I8YO1F3Im-Aw
提取码:rjh9
–来自百度网盘超级会员V1的分享
2.配置opencv(配置好的可以跳过)
opencv配置过程
这是我已经编译好的文件:
链接:https://pan.baidu.com/s/1937PkY6WBKYhhbr_y7LGvA
提取码:187x
–来自百度网盘超级会员V1的分享
导入vs2017的是:D:\biancheng\opencv\build\x64\vc15\lib
3.打开vs2017
新建vs2017项目,添加以下包含目录,第一个目录为opencv的include路径,第二个为编译生成的ncnn里的include路径,第三个为编译生成的protobuf里的include路径。

继续添加库目录,第一个目录为opencv的lib路径,第二个为编译生成的ncnn里的lib路径,第三个为编译生成的protobuf里的lib路径。

继续添加windows运行库目录,该目录为protobuf的bin路径

在链接器-输入中添加附加依赖项,分别是

测试
测试用的图片自己找,使用的模型文件在ncnn下的examples文件夹内
#include <opencv2/opencv.hpp>
#include <map>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <net.h>
static int detect_squeezenet(const cv::Mat& bgr, std::vector<float>& cls_scores)
{
ncnn::Net squeezenet;
squeezenet.load_param("./squeezenet_v1.1.param"); // ncnn加速所需的转换后的模型文件之一
squeezenet.load_model("./squeezenet_v1.1.bin"); // ncnn加速所需的转换后的模型文件之二
ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, 227, 227);
const float mean_vals[3] = { 104.f, 117.f, 123.f };
in.substract_mean_normalize(mean_vals, 0);
ncnn::Extractor ex = squeezenet.create_extractor();
ex.input("data", in);
ncnn::Mat out;
ex.extract("prob", out);
cls_scores.resize(out.w);
for (int j = 0; j < out.w; j++)
{
cls_scores[j] = out[j];
}
return 0;
}
static int print_topk(const std::vector<float>& cls_scores, int topk)
{
// partial sort topk with index
int size = cls_scores.size();
std::vector< std::pair<float, int> > vec;
vec.resize(size);
for (int i = 0; i < size; i++)
{
vec[i] = std::make_pair(cls_scores[i], i);
}
std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(),
std::greater< std::pair<float, int> >());
// print topk and score
for (int i = 0; i < topk; i++)
{
float score = vec[i].first;
int index = vec[i].second;
fprintf(stderr, "%d = %f\n", index, score);
}
return 0;
}
int main()
{
std::string imagepath = "./duck.jpg";
cv::Mat m = cv::imread(imagepath, CV_LOAD_IMAGE_COLOR);
if (m.empty())
{
std::cout << "cv::imread " << imagepath << " failed\n" << std::endl;
return -1;
}
std::vector<float> cls_scores;
detect_squeezenet(m, cls_scores);
print_topk(cls_scores, 3);
return 0;
}
结果

