1、 将官方C++预测代码在Visual Studio下生成解决方案(.sln)
2、 将C++预测代码进行生成dll
4、 使用C#调用生成的dll
Opencv:选择3.4.6版本
https://sourceforge.net/projects/opencvlibrary/files/3.4.6/opencv-3.4.6-vc14_vc15.exe/download
Paddle预测库:选择win10下的cuda10版本。
https://www.paddlepaddle.org.cn/documentation/docs/zh/advanced_guide/inference_deployment/inference/windows_cpp_inference.html
这里我们选择使用cuda10.0 的,一定要和本地的cuda版本一致。之前使用本地的cudan10.2 编译,开启不了GPU预测。
一次点击按钮
编译需要的dll,这里我已经修改好了文件,并生成了dll文件。
改造了detector.ccp文件中代码,并在头文件中添加了接口文件。
namespace PaddleX {
//图片标签
char res[50];
PaddleX::Model* det;
void* Loadmodel(char * model_dir_c) {
std::string model_dir = model_dir_c;
std::string key = "";
int thread_num = 2;
int gpu_id = 0;
bool use_trt = 0;
bool use_gpu = 0;
bool use_ir_optim = 0;
// bool use_trt = 0;
bool use_mkl = 1;
// PaddleX:Model model;
(det = new PaddleX::Model) ->Init(model_dir,
use_gpu,
use_trt,
use_mkl,
thread_num,
gpu_id,
key,
use_ir_optim);
// PredictImage(image, result );
}
static PaddleX::DetResult result;
char* PredictImage(char* input, int width, int height, int threshold) {
cv::Mat im(height, width, CV_8UC3, input);
// cv::Mat im = cv::imread(image_path, 1);
det->predict(im, &result);
for (int i = 0; i < result.boxes.size(); ++i) {
strcpy_s(res, result.boxes[i].category.c_str());
}
return res;
result.clear();
}
}
到这里基本上已经生成了dll,现在使用C#调用dll
#加载模型文件
[DllImport("detector.dll", EntryPoint = "Loadmodel", CharSet = CharSet.Ansi)]
public static extern void Loadmodel([MarshalAs(UnmanagedType.LPStr)] string modelPath );
#加载图像(bitmap)
[DllImport("detector.dll", EntryPoint = "PredictImage", CharSet = CharSet.Ansi)]
public static extern IntPtr PredictImage( byte[] input, int width, int height, double,threshold);
private void button4_Click(object sender, EventArgs e)
{
#初始化模型文件
string models = @"F:\test\PaddleXdevelop\deploy\cpp\out\paddlex_inference\Release\inference";
Loadmodel(models);
}
private void button2_Click(object sender, EventArgs e) {
#开始预测,显示图像预测结果标签
string image_path = @"C:\Users\admin\Desktop\yuan\12345.jpg";
Bitmap bmp = new Bitmap(image_path);
int stride;
double threshold = 0.7;
byte[] source = GetBGRValues(bmp, out stride);
IntPtr seg_img = PredictImage(source, bmp.Width, bmp.Height, threshold);
string result2 = Marshal.PtrToStringAnsi(seg_img);
MessageBox.Show(result2);
}
public static byte[] GetBGRValues(Bitmap bmp, out int stride)
{
var rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
var bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
stride = bmpData.Stride;
var rowBytes = bmpData.Width * Image.GetPixelFormatSize(bmp.PixelFormat) / 8;
var imgBytes = bmp.Height * rowBytes;
byte[] rgbValues = new byte[imgBytes];
IntPtr ptr = bmpData.Scan0;
for (var i = 0; i < bmp.Height; i++)
{
Marshal.Copy(ptr, rgbValues, i * rowBytes, rowBytes);
ptr += bmpData.Stride;
}
bmp.UnlockBits(bmpData);
return rgbValues;
}
1、dll 缺失,可以根据官网文档生成exe,去看缺失。
一般都是缺少 kldnn.dll,libiomp5md.dll,mkldnn.dll,mklml.dll, opencv_world346.dll
有问题,欢迎咨询,作者微信wxid_yzao1o7wi0wl22