1.下载vs2019,地址:https://visualstudio.microsoft.com/zh-hans/vs/
2.下载后安装工作负载(安装以下三个即可)
3.如果发现忘记下载某一个工作负载,可以再安装完成之后在重新安装(VS导航栏的工具->获取工具和功能)
1.下载地址:https://pan.baidu.com/s/19q6OcG-7KfNScNr4-dKj-A 提取码:nxv1
2.下载后解压
3.添加环境变量,将解压后cmake-3.16.5-win64-x64文件夹点开进入bin将该路径添加至环境变量,我这里路径为 D:\build_tools\cmake-3.16.5-win64-x64\bin,下图所示:
1.下载地址:https://pan.baidu.com/s/1fRV1OpJsUXscUNlC4uvUMA 提取码:bt4q
2.下载后进行解压
3.安装protobuf-3.4.0,先打开VS2019的X64命令行
4.在VS2019的X64命令行下执行以下命令
注:
cd
mkdir build-vs2019
cd build-vs2019
cmake -G"NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%cd%/install -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_MSVC_STATIC_RUNTIME=OFF ../cmake
nmake
nmake install
5.成功后会产生build-vs2019文件夹以及该文件夹下的若干文件夹及文件
1.ncnn下载地址:https://github.com/Tencent/ncnn/releases,这里我使用的release版本,直接下载源码就可以了,新的release版本要下载full-source字样的代码包
2.下载好之后解压
3.打开VS2019的X64命令,进入到ncnn根目录下,然后执行以下语句
注意:cmake -G...这条命令有三个
cd
mkdir -p build-vs2019
cd build-vs2019
cmake -G"NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%cd%/install -DProtobuf_INCLUDE_DIR=/build-vs2019/install/include -DProtobuf_LIBRARIES=/build-vs2019/install/lib/libprotobuf.lib -DProtobuf_PROTOC_EXECUTABLE=/build-vs2019/install/bin/protoc.exe -DNCNN_VULKAN=OFF ..
nmake
nmake install
4.编译成功后会产生build-vs2019文件夹以及该文件夹下的若干文件夹及文件
1.下载地址:https://github.com/nihui/opencv-mobile/releases,这里使用opencv-mobile是opencv的mini简化版本,属于比原版的opencv库更小。这里我直接下载编译好的 opencv-mobile-4.6.0-windows-vs2019.zip
2.下载后解压,就可以调用相关的接口了(下面配置项目属性的时候要配置这个路径)。
1.打开VS2019并新建一个空项目,新建一个yolo-fastest.cpp文件,解决方案改为Release和x64,文件内容如下(来自这里):
#include "cpu.h"
#include "net.h"
#include "gpu.h"
#include "benchmark.h"
#include "datareader.h"
#include
#include
#include
#include
#include
#include
int demo(cv::Mat& image, ncnn::Net& detector, int detector_size_with, int detector_size_height) {
// 检测类别
static const char* class_names[] = {
"background","aeroplane","bicycle","bird","boat",
"bottle","bus","car","cat","chair","cow","diningtable",
"dog","horse","motorbike","person","pottedplant","sheep",
"sofa","train","tvmonitor"
};
cv::Mat bgr = image.clone();
int image_width = bgr.cols;
int image_height = bgr.rows;
ncnn::Mat input = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR2RGB,
bgr.cols, bgr.rows, detector_size_with, detector_size_height);
// 数据预处理
const float mean_vals[3] = { 0.f, 0.f, 0.f };
const float norm_vals[3] = { 1 / 255.f,1 / 255.f,1 / 255.f };
input.substract_mean_normalize(mean_vals, norm_vals);
ncnn::Extractor ex = detector.create_extractor();
ex.set_num_threads(8);
ex.input("data", input);
ncnn::Mat out;
ex.extract("output", out);
for (int i = 0; i < out.h; ++i) {
int label;
float x1, y1, x2, y2, score;
const float* values = out.row(i);
label = values[0];
score = values[1];
x1 = values[2] * image_width;
y1 = values[3] * image_height;
x2 = values[4] * image_width;
y2 = values[5] * image_height;
// 处理越界坐标
if (x1 < 0)x1 = 0;
if (y1 < 0)y1 = 0;
if (x2 < 0)x2 = 0;
if (y2 < 0)y2 = 0;
if (x1 > image_width)x1 = image_width;
if (y1 > image_height)y1 = image_height;
if (x2 > image_width)x2 = image_width;
if (y2 > image_height)y2 = image_height;
cv::rectangle(image, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(255, 255, 0), 1, 1, 0);
char text[256];
sprintf_s(text, "%s %.1f%%", class_names[label], score * 100);
int baseLine = 0;
cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
cv::putText(image, text, cv::Point(x1, y1 + label_size.height),
cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
}
return 0;
}
int test() {
// 定义yolo-fatest检测器
ncnn::Net detector;
detector.load_param("models/yolo-fastest/yolo-fastest.param");
detector.load_model("models/yolo-fastest/yolo-fastest.bin");
// 定义输入图像尺寸
int detector_size_width = 300;
int detector_size_height = 300;
// 测试图像
cv::Mat image = cv::imread("images/demo_1.jpg");
// 调用函数开始检测
demo(image, detector, detector_size_width, detector_size_height);
// 显示检测结果
cv::imshow("demo", image);
cv::waitKey(0);
return 0;
}
int main() {
test();
return 0;
}
2.项目属性配置
(1)项目的属性->配置属性->VC++目录->包含目录,输入下面的路径:
D:\build_tools\ncnn-20230223\build-vs2019\install\include\ncnn
D:\build_tools\opencv-mobile-4.6.0-windows-vs2019\x64\include
(2)项目的属性->链接器->常规->附加库目录,输入下面的路径:
D:\build_tools\ncnn-20230223\build-vs2019\install\lib
D:\build_tools\opencv-mobile-4.6.0-windows-vs2019\x64\x64\vc16\staticlib
(3)项目的属性->链接器->输入->附加依赖项,输入下面的内容:
ncnn.lib
opencv_core460.lib
opencv_highgui460.lib
opencv_imgproc460.lib
3.项目运行,在V S 2019窗口的解决方案中选择Release和x64,然后直接调试执行就可以看到模型的推理结果了。
参考:
Windows下NCNN环境配置(VS2019)_ncnn windows-CSDN博客
【ncnn开发记录】ncnn & opencv-mobile & VS2019的Windows编译 - 知乎
(一)ncnn | Windows10 + VS2019环境配置_windows下ncnn环境配置和模型推理-CSDN博客