windows vs2017 lib和dll打包和工程调用(opencv_libtorch_realsense)

windows vs2017 lib和dll打包和工程调用(opencv_libtorch_realsense)

一、dll文件生成

1、新建dll工程

选择 “新建”->“项目”->Visual C++ -> windows桌面->具有导出项的(DLL)动态链接库

在这里插入图片描述

2、设置

(1)新建项目完成后:
项目->属性->配置属性->常规,
可以看到:
在这里插入图片描述
做如下设置:
(2)项目->属性->c++目录->包含目录:

D:\OpenCV\opencv-3.4.1-vc14_vc15\opencv\build\include
D:\OpenCV\opencv-3.4.1-vc14_vc15\opencv\build\include\opencv
D:\OpenCV\opencv-3.4.1-vc14_vc15\opencv\build\include\opencv2
D:\libtorch-win-shared-with-deps-1.8.1+cpu\libtorch\include\torch\csrc\api\include
D:\libtorch-win-shared-with-deps-1.8.1+cpu\libtorch\include
C:\Program Files %28x86%29\Intel RealSense SDK 2.0\include

(3)项目->属性->c++目录->库目录:

D:\OpenCV\opencv-3.4.1-vc14_vc15\opencv\build\x64\vc14\lib
D:\libtorch-win-shared-with-deps-1.8.1+cpu\libtorch\lib
C:\Program Files %28x86%29\Intel RealSense SDK 2.0\lib\x64

(4)项目->属性->C/C+±>预处理器:
填写填写在这里插入图片描述预处理器定义处添加以下

NDEBUG
DETECTOR_EXPORTS
RS_EXPORTS
_WINDOWS
_USRDLL
NOMINMAX

其中DETECTOR_EXPORTS和RS_EXPORTS是对dll导出设置的,我的头文件分别是detector.h和rs.h
此处必须加“NOMINMAX”;
如果没有加,在加入libtorch头文件

#include <torch/script.h>
#include <torch/torch.h>`

后,
头文件里的以下两行会呈现灰色, 无法生成dll文件

#ifdef DETECTOR_EXPORTS
#define DETECTOR_API __declspec(dllexport)

(5)项目->属性->C/C+±>语言->符合模式:
设置为**“否”**
在这里插入图片描述
如不进行此设置,可能的导致提示“std”问题。

3、导出文件编写格式

以上设置完毕后,头文件格式:
其中detector.h:

# pragma once
#ifdef DETECTOR_EXPORTS
#define DETECTOR_API __declspec(dllexport)
#else
#define DETECTOR_API __declspec(dllimport)
#endif

#include <torch/script.h>
#include <torch/torch.h>

#include <memory>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/dnn/dnn.hpp>

#include "utils.h"

class DETECTOR_API Detector {
public:

    Detector(const std::string& model_path, const torch::DeviceType& device_type);
    std::vector<std::vector<Detection>> Run(const cv::Mat& img);

private:
    static std::vector<float> LetterboxImage(const cv::Mat& src, cv::Mat& dst, const cv::Size& out_size = cv::Size(640, 640));
    torch::jit::script::Module module_;
    torch::Device device_;
    bool half_;
};

其中detector.cpp:内部函数定义前面没有加DETECTOR_API,在类Detector 定义的时候,前面已经加了DETECTOR_API ,因此在类内的成员变量或成员函数的声明前没有加DETECTOR_API,在下面的.cpp文件里,该类的成员函数的定义前面也不用加DETECTOR_API。

#include "pch.h"
#include "detector.h"
#include "torch/csrc/jit/python/update_graph_executor_opt.h"

Detector::Detector(const std::string& model_path, const torch::DeviceType& device_type) : device_(device_type) {
    try {
		torch::jit::setGraphExecutorOptimize(false);
        // Deserialize the ScriptModule from a file using torch::jit::load().
        module_ = torch::jit::load(model_path);
    }
    catch (const c10::Error& e) {
        std::cerr << "Error loading the model!\n";
        std::exit(EXIT_FAILURE);
    }

    half_ = (device_ != torch::kCPU);
    module_.to(device_);

    if (half_) {
        module_.to(torch::kHalf);
    }
    module_.eval();
}

std::vector<std::vector<Detection>> Detector::Run(const cv::Mat& img) 
{
	float conf_threshold = 0.4;
	float iou_threshold = 0.5;
	std::vector<std::vector<Detection>> result;
    return result;
}

std::vector<float> Detector::LetterboxImage(const cv::Mat& src, cv::Mat& dst, const cv::Size& out_size) {
    auto in_h = static_cast<float>(src.rows);
    auto in_w = static_cast<float>(src.cols);
    float out_h = out_size.height;
    float out_w = out_size.width;

    float scale = std::min(out_w / in_w, out_h / in_h);

    int mid_h = static_cast<int>(in_h * scale);
    int mid_w = static_cast<int>(in_w * scale);
    cv::resize(src, dst, cv::Size(mid_w, mid_h));

    int top = (static_cast<int>(out_h) - mid_h) / 2;
    int down = (static_cast<int>(out_h)- mid_h + 1) / 2;
    int left = (static_cast<int>(out_w)- mid_w) / 2;
    int right = (static_cast<int>(out_w)- mid_w + 1) / 2;
    cv::copyMakeBorder(dst, dst, top, down, left, right, cv::BORDER_CONSTANT, cv::Scalar(114, 114, 114));
    std::vector<float> pad_info{static_cast<float>(left), static_cast<float>(top), scale};
    return pad_info;
}

rs.h如下:

# pragma once
#ifdef RS_EXPORTS
#define RS_API__declspec(dllexport)
#else
#define RS_API__declspec(dllimport)
#endif

#include <iostream>
#include <memory>
#include <chrono> 
#include <sstream>
#include <fstream>
#include <algorithm>
#include <cstring>
 
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>

#include "librealsense2/rs.hpp"
#include "librealsense2/rsutil.h"

using namespace std;
using namespace cv;
struct REALSENSE_ALIGN_DEPTH_API realsenseData{
	rs2::pipeline pipe;
	rs2::colorizer c;
	rs2::pipeline_profile profile;
	rs2_intrinsics intrinDepth;
	rs2_intrinsics intrinColor;
	rs2_extrinsics  extrinDepth2Color;
};

RS_API void rsInit(realsenseData &rsData);
RS_API float get_depth(rs2::device dev);

rs.cpp如下:结构体内的成员变量没有加****RS_API, 结构体外的变量或函数的声明或定义前面需要加RS_API

#include "pch.h"
#include "rs.h"

//realsense初始化
RS_API void rsInit(realsenseData &rsData)
{
	//创建数据管道
	rs2::config pipe_config;
	pipe_config.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 30);
	pipe_config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_BGR8, 30);

	//start()函数返回数据管道的profile
	rs2::pipeline_profile profile = rsData.pipe.start(pipe_config);
	rsData.profile = profile;
}

//获取深度像素对应长度单位(米)的换算比例
RS_API float get_depth(rs2::device dev)
{
    // Go over the device's sensors
    for (rs2::sensor& sensor : dev.query_sensors())
    {
        // Check if the sensor if a depth sensor
        if (rs2::depth_sensor dpt = sensor.as<rs2::depth_sensor>())
        {
            return dpt.get_depth_scale();
        }
    }
    throw std::runtime_error("Device does not have a depth sensor");
}

按照以上格式生成后,点击运行,
工程文件夹下“X64”下“Release”下,生成.lib和.dll文件。
比如我的工程名称为:DetectRs,则生成为DetectRs.dll和DetectRs.lib。

注:在对应的detector.cpp和rs.cpp的开头必须包含头文件"pch.h",其中"pch.h"头文件是在新建“具有导出项的动态链接库DLL项目”时,框架自动生成的头文件。
不过不包含,则会提示错误。如“是否忘记了向源中添加“#include “pch.h”?”
在这里插入图片描述

二、新建工程调用生成的dll和lib文件

2.1、新建调用工程

新建->项目->控制台,建立新项目,设置名称为“DetectRsPro”
在这里插入图片描述

2.2、拷贝生成的.lib和.dll文件

在新建的工程目录下,新建lib文件夹,将生成的DetectRs.dll和DetectRs.lib文件拷贝到lib文件夹下。
在这里插入图片描述
在这里插入图片描述

2.3、配置工程

在项目名称位置右击->属性,
同上,设置包含目录,库目录
在这里插入图片描述
项目->属性->c++目录->包含目录:

D:\OpenCV\opencv-3.4.1-vc14_vc15\opencv\build\include
D:\OpenCV\opencv-3.4.1-vc14_vc15\opencv\build\include\opencv
D:\OpenCV\opencv-3.4.1-vc14_vc15\opencv\build\include\opencv2
D:\libtorch-win-shared-with-deps-1.8.1+cpu\libtorch\include\torch\csrc\api\include
D:\libtorch-win-shared-with-deps-1.8.1+cpu\libtorch\include
C:\Program Files %28x86%29\Intel RealSense SDK 2.0\include

项目->属性->c++目录->库目录:

D:\OpenCV\opencv-3.4.1-vc14_vc15\opencv\build\x64\vc14\lib
D:\libtorch-win-shared-with-deps-1.8.1+cpu\libtorch\lib
C:\Program Files %28x86%29\Intel RealSense SDK 2.0\lib\x64
E:\DetectRsPro\lib

**项目->属性->配置属性->调试:**设置
在这里插入图片描述
**项目->属性->C/C+±>优化:**设置
在这里插入图片描述
**项目->属性->C/C+±>预处理器:**设置
在这里插入图片描述
预处理器定义处加入以下:

NDEBUG
_CONSOLE
NOMINMAX
_CRT_SECURE_NO_WARNINGS

**项目->属性->C/C+±>语言->符合模式:**设置为“否”,如:
在这里插入图片描述
**项目->属性->链接器->输入->附加依赖项:**设置
在这里插入图片描述
加入以下:

c10.lib
torch.lib
torch_cpu.lib
opencv_world341.lib
realsense2.lib
DetectRs.lib

设置完以上,还需要在项目资源文件中加入DetectRs.lib
在这里插入图片描述
以上配置完成。
可以根据自己的工程更改路径,更改配置,运行即可。
其中的工程代码,只是贴出部分示意。记录具体配置方法,生成dll和lib方法,调用dll和lib方法。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值