ncnn和opencv在vs2022上创建工程推理示例

序言

因为之前C++代码一直是在ubuntu的CLion上写的,对Visual Studio的相关配置并不是很熟悉,最近需要在win上开发,所以不得不用Visual Studio来编写代码,在做项目的同时,简单记录下Visual Studio的相关配置,以调用ncnn和opencv为例。

一、准备ncnn和opencv

1.1 windows下编译ncnn

首先需要安装Visual Studio 2022社区版,这个不用多说了吧,安装结束后打开vs2022的x64命令行终端
在这里插入图片描述
在windows下编译ncnn前需要先下载编译protobuf-3.4.0,下载后解压,编译命令过程如下:

cd <protobuf-root-dir>
mkdir build-2022
cd build-2022
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

编译完后再编译ncnn,编译命令过程如下:

git clone https://github.com/Tencent/ncnn.git
cd ncnn
git submodule update --init         # 如果这一步一直更新不了的话,需要把cmake编译时候的-DNCNN_VULKAN设置为OFF,不然编译不通过
 
mkdir build && cd build
cmake -G"NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%cd%/install -DProtobuf_INCLUDE_DIR=<protobuf-root-dir>/build-2022/install/include -DProtobuf_LIBRARIES=<protobuf-root-dir>/build-2022/install/lib/libprotobuf.lib -DProtobuf_PROTOC_EXECUTABLE=<protobuf-root-dir>/build-2022/install/bin/protoc.exe -DNCNN_VULKAN=OFF ..
nmake
nmake install

protobuf-root-dir 是刚才编译的protobuf的目录,根据自己的来修改。

1.2 安装opencv

opencv的安装比较简单,去opencv官网下载windows的文件下来安装即可,会得到一个opencv的文件夹
在这里插入图片描述

二、项目配置

打开vs2022,新建一个空项目:
在这里插入图片描述
得到空项目结构如下:
在这里插入图片描述
然后依次点击视图=>其他窗口=>属性管理器,在Release | x64(与上面编译过程中的参数对应)处右击进入属性界面。点击VC++ 目录,在包含目录中依次添加如下内容:

<opencv-root-dir>/build/include 
<opencv-root-dir>/build/include/opencv 
<opencv-root-dir>/build/include/opencv2 
<ncnn-root-dir>/build/install/include/ncnn
<protobuf-root-dir>/build/install/include 

在这里插入图片描述
库目录中依次添加如下内容:

<opencv-root-dir>/build/x64/vc15/lib
<ncnn-root-dir>/build/install/lib
<protobuf-root-dir>/build/install/lib

在这里插入图片描述
然后在属性界面选择链接器=>输入,在附加依赖项中依次添加如下内容:

ncnn.lib
libprotobuf.lib
opencv_world455.lib

在这里插入图片描述

注意:网上很多教程没有这一步,没配置的话可能会报那种找不到dll文件的错误,是因为没有将opencv里面的相关文件复制到C盘中的文件夹里面

操作方法:将opencv/build/x64/vc15/bin目录下面的opencv_world455.dll和opencv_world455d.dll文件复制到C:\Windows\System32这个文件夹里面(详细看图)
在这里插入图片描述
拷贝到C:\Windows\System32中:
在这里插入图片描述
如果是opencv其他的版本,把对应的dll文件移动到上述两个C盘文件夹即可!

至此,项目的配置环境完成。在VS2022的运行窗口栏处依次选择Release和x64,与上面的选择对应。
在这里插入图片描述
然后就可以编写代码了,这里以DBNet文字检测模型代码运行为示例,创建了两个头文件和三个源文件结构如下:
在这里插入图片描述
demo.cpp为主程序入口,代码示例如下:

//
// Created by cai on 2022/2/13.
//

#include "include/DbNet.h"
#include <iostream>

using namespace cv;
using namespace std;

int main(){
    
    
    DbNet dbNet;                                                // 初始化对象

    bool retDbNet = dbNet.initModel("D:\\project_code\\OcrDet_ncnn\\model\\det_int8");       //模型初始化

    if (!retDbNet){
    
    
        printf("DBNet load model fail!");
    }

    const char*imagepath = "D:\\project_code\\OcrDet_ncnn\\test_img";
    vector<String> imagesPath;
    cv::glob(imagepath,imagesPath);

    for (int i =0;i<imagesPath.size();i++) {
    
    
        //载入图像
        cout << imagesPath[i] << endl;
        Mat image = imread(imagesPath[i]);

        if (image.empty()) {
    
    
            cout << "Error: Could not load image" << endl;
            return -1;
        }

        vector<Rect> rect;
        rect = dbNet.getTextImages(image);         // 检测接口

        for(auto &xywh : rect){
    
    
            cv::rectangle(image, xywh, Scalar(0, 0, 255),1, LINE_8,0);        // 画矩形框

        }
        cv::imshow("img0",image);
        cv::waitKey();
    }
    return 0;
}

在这里插入图片描述
这里用的DBNet的模型经过量化后不到600k,直接运行代码后得到检测输出结果如下,检测图和掩码图效果都还不错:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_39056987/article/details/122937169