VS2015搭建NCNN开发环境

 一、简介

 NCNN的编译依赖OpenCV和Protobuf,所以需要总共需要安装OpenCV、Protobuf和NCNN三个东西。

二、安装OpenCV

1、下载OpenCV  Releases - OpenCV  一般选择最新版,这里以3.4.16为例。

2、选择喜欢的目录安装,这里以E:/soft-setup为例,安装之后生成下面几个文件。

 3、添加path(vs2015对应:E:\soft-setup\opencv\build\x64\vc14\bin;vs2017对应:E:\soft-setup\opencv\build\x64\vc15\bin)到环境变量中,重启电脑生效。

4、打开VS2015,新建一个C++项目,点击属性管理器。

 右键Release | x64属性,因为下面的ncnn,Debug | x64可能会有问题。

 下图中的“包含目录”填写:

E:\soft-setup\opencv\build\include
E:\soft-setup\opencv\build\include\opencv
E:\soft-setup\opencv\build\include\opencv2

“库目录”填写:

E:\soft-setup\opencv\build\x64\vc14\lib

扫描二维码关注公众号,回复: 14136229 查看本文章

 下图中的“附加依赖项”填写:

opencv_world3416.lib

 4、写个demo,运行一下,OpenCV安装成功。

#include "stdafx.h"
#include <opencv.hpp>
using namespace cv;

int main()
{
	Mat srcImage;
	srcImage = imread("1.jpg");
	imshow("显示图像", srcImage);
	waitKey();
	return 0;
}

三、编译Protobuf

1、安装CMake

  下载 Download | CMake

2、下载并解压:protobuf-3.4.0 https://github.com/google/protobuf/archive/v3.4.0.zip
打开CMake-GUI:cmd执行cmake-gui
设置 源文件目录:E:/soft-setup/protobuf-3.4.0/cmake
设置 目标文件目录:E:\soft-setup\protobuf-3.4.0\buildvs1
点击 Configure:选择VS2015 Win64->Finish
不勾选:protobuf_BUILD_TESTS和protobuf_MSVC_STATIC_RUNTIME
点击 Add Entry:CMAKE_BUILD_TYPE=Release
修改(可选):CMAKE_CONFIGURATION_TYPES=Release
修改:CMAKE_INSTALL_PREFIX=./install
再点2次 Configure
点击 Generate

3、打开VS2015:
打开 目标文件目录(E:\soft-setup\protobuf-3.4.0\buildvs1)中的工程文件(protobuf.sln),编译其中的INSTALL项目。解决方案-INSTALL-生成。

四、编译NCNN

1、下载并解压:ncnn https://github.com/Tencent/ncnn/archive/master.zip
2、打开CMake-GUI:cmd执行cmake-gui
        设置 源文件目录:E:\soft-setup/ncnn-master
        设置 目标文件目录:E:\soft-setup/ncnn-master/build/
        点击 Configure:选择VS2015 Win64->Finish
        点击 Add Entry:Protobuf_LIBRARIES=E:/soft-setup/protobuf-3.4.0\buildvs1\install\lib\libprotobuf.lib
        点击 Add Entry:Protobuf_INCLUDE_DIR=E:/soft-setup/protobuf-3.4.0\buildvs1\install\include
        点击 Add Entry:Protobuf_PROTOC_EXECUTABLE=E:/soft-setup/protobuf-3.4.0\buildvs1\install\install/bin/protoc.exe
        修改(可选):CMAKE_CONFIGURATION_TYPES=Release
        修改:Protobuf_SRC_ROOT_FOLDER=E:/soft-setup/protobuf-3.4.0/src
        再次点击 Configure
        点击 Generate


3、打开VS2015:
打开 目标文件目录(E:\soft-setup/ncnn-master/build/)中的工程文件(ncnn.sln),编译其中的INSTALL项目即可。
编译结束后,在目标文件目录的install和tools目录下,即可看到编译好的文件。至此,ncnn编译完成。

4、打开VS2015,点击属性管理器, 右键Release | x64属性。

 下图中的“包含目录”添加:

E:\soft-setup\ncnn-master\build\install\include\ncnn

“库目录”添加:

E:\soft-setup\ncnn-master\build\install\lib

“Windows运行库目录”添加:

E:\soft-setup\ncnn-master\build\install\bin

 下图中的“附加依赖项”添加:

ncnn.lib

5、写个demo,运行一下,大功告成。

#include "stdafx.h"
#include <opencv2/highgui/highgui.hpp>
#include <vector>
#include "net.h"
#include <stdio.h>

using namespace std;

 vector<float> get_output(const ncnn::Mat& m)
 {
 	vector<float> res;
 	for (int q = 0; q<m.c; q++)
 	{
 		const float* ptr = m.channel(q);
 		for (int y = 0; y<m.h; y++)
 		{
 			for (int x = 0; x<m.w; x++)
 			{
 				res.push_back(ptr[x]);
 			}
 			ptr += m.w;
 		}
 	}
 	return res;
 }

int main()
{
	cv::Mat img = cv::imread("1.jpg");
	 int w = img.cols;
	 int h = img.rows;

	 ncnn::Mat in = ncnn::Mat::from_pixels_resize(img.data, ncnn::Mat::PIXEL_BGR, w, h, 224, 224);

	 ncnn::Net net;
	 net.load_param("mobilenetv2.param");
	 net.load_model("mobilenetv2.bin");
	 ncnn::Extractor ex = net.create_extractor();
	 ex.set_light_mode(true);
	 ex.set_num_threads(4);

	 ex.input("x", in);
	 ncnn::Mat feat;
	 ex.extract("y", feat);
	 vector<float> res = get_output(feat);
	 vector<float>::iterator max_id = max_element(res.begin(), res.end());
	 printf("predicted class: %d, predicted value: %f", max_id - res.begin(), res[max_id - res.begin()]);
	 net.clear();
	 return 0;
}

猜你喜欢

转载自blog.csdn.net/xian0710830114/article/details/121306680
今日推荐