For the complete installation and configuration process, please refer to this blog
https://blog.csdn.net/weixin_43051346/article/details/122414506
because when I compile the protobuf and ncnn framework, there will always be errors and cannot be solved.
In the end, it was found that it was no problem to change the computer and reconfigure.
configuration process
Then I directly used the files compiled by another computer, the process is as follows:
1. Download the files in Baidu Cloud and unzip them:
2. Configure opencv (configured can be skipped)
Opencv configuration process
This is the file I have compiled:
Link: https://pan.baidu.com/s/1937PkY6WBKYhhbr_y7LGvA
Extraction code: 187x
– Shared from Baidu Netdisk super member V1
Import vs2017 is: D:\biancheng\opencv\build\x64\vc15\lib
3. Open vs2017
Create a new vs2017 project and add the following include directories. The first directory is the include path of opencv, the second is the include path in the compiled ncnn, and the third is the include path in the compiled protobuf.
Continue to add library directories, the first directory is the lib path of opencv, the second is the lib path in ncnn generated by compilation, and the third is the lib path in protobuf generated by compilation.
Continue to add the windows runtime library directory, which is the bin path of protobuf
Add additional dependencies in the linker-input, respectively
test
Find the pictures for the test by yourself, and the model files used are in the examples folder under ncnn
#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;
}
Results
! [insert picture description here](https://img-blog.csdnimg.cn/e166d2ad1d8b499c913508a62c11ed0d.png