[黄埔学院]paddle在c++编译成功后,封装成.dll时出错
Created by: Tinagao222
根据 https://zhuanlan.zhihu.com/p/145446681?utm_source=wechat_session&utm_medium=social&utm_oi=77894483181568&from=groupmessage&isappinstalled=0&wechatShare=1&s_r=0 ,知乎这篇文章的讲解,将paddle在c++编译成功后,封装成.dll供C#调用,用单张图片时测试成功。但实际应用中需要实时接收相机的图片,然后经过模型,得到的检测结果要输入给C#写的界面,完成计数、显示的工作,因此希望.dll库的接口是有输入输出的函数,而不是void函数。
因此对文中的main.cpp做了如下修改,修改完再生成就报错了 原文程序:
#include <glog/logging.h>
#include <iostream>
#include <string>
#include <vector>
#include "include/object_detector.h"
extern "C" __declspec(dllexport) void Loadmodel();
extern "C" __declspec(dllexport) int add(int a, int b);
void PredictImage(const std::string& image_path,
PaddleDetection::ObjectDetector* det);
int add(int a, int b) {
return a + b;
}
void Loadmodel() {
std::string model_dir = "D:\\0524\\test\\model";
std::string image_path = "D:\\0524\\test\\orange_71.jpg";
std::string video_path = "";
std::string run_mode = "fluid";
bool use_gpu = true;
// Load model and create a object detector
PaddleDetection::ObjectDetector det(model_dir, use_gpu, run_mode);
PredictImage(image_path, &det);
}
void PredictImage(const std::string& image_path,
PaddleDetection::ObjectDetector* det) {
// Open input image as an opencv cv::Mat object
cv::Mat im = cv::imread(image_path, 1);
// Store all detected result
std::vector<PaddleDetection::ObjectResult> result;
det->Predict(im, &result);
for (const auto& item : result) {
printf("class=%d confidence=%.2f rect=[%d %d %d %d]\n",
item.class_id,
item.confidence,
item.rect[0],
item.rect[1],
item.rect[2],
item.rect[3]);
}
// Visualization result
auto labels = det->GetLabelList();
auto colormap = PaddleDetection::GenerateColorMap(labels.size());
cv::Mat vis_img = PaddleDetection::VisualizeResult(
im, result, labels, colormap);
std::vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
compression_params.push_back(95);
cv::imwrite("output.jpeg", vis_img, compression_params);
printf("Visualized output saved as output.jpeg\n");
}
我修改完的程序:
#include <glog/logging.h>
#include <iostream>
#include <string>
#include <vector>
#include "include/object_detector.h"
extern "C" __declspec(dllexport) std::vector<PaddleDetection::ObjectResult> detect(std::string model_dir, cv::Mat im);
std::string model_dir = "D:\\tina\\NN\\BaiduAI\\model";
std::string image_path = "D:\\tina\\NN\\BaiduAI\\2.jpg";
std::string run_mode = "fluid";
bool use_gpu = true;
int gpu_id = 0;
void PredictImage(cv::Mat im, std::vector<PaddleDetection::ObjectResult> &result, PaddleDetection::ObjectDetector* det) {
// Store all detected result
det->Predict(im, &result);
}
std::vector<PaddleDetection::ObjectResult> pubResult;
std::vector<PaddleDetection::ObjectResult> detect(std::string model_dir, cv::Mat im) {
// Load model and create a object detector
PaddleDetection::ObjectDetector det(model_dir, use_gpu=true, run_mode= "fluid", gpu_id=0);
std::vector<PaddleDetection::ObjectResult> result;
PredictImage(im,result, &det);
pubResult = result;
return pubResult;
}