Comments (2)
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Threading;
using OpenCvSharp;
using OpenCvSharp.Extensions;
namespace paddlex_inference
{
class paddlex_inference
{
/* ================================= inference ================================= */
#region 接口定义及参数
int modelType = 1; // 模型的类型 0:分类模型;1:检测模型;2:分割模型
string modelPath = @"C:\Users\10592\Desktop\xiaoduxiong_epoch_12"; // 模型目录路径
bool useGPU = true; // 是否使用GPU
bool useTrt = false; // 是否使用TensorRT
bool useMkl = false; // 是否使用MKLDNN加速模型在CPU上的预测性能
int mklThreadNum = 8; // 使用MKLDNN时,线程数量
int gpuID = 0; // 使用GPU的ID号
string key = ""; //模型解密密钥,此参数用于加载加密的PaddleX模型时使用
bool useIrOptim = false; // 是否加速模型后进行图优化
bool visualize = false;
bool isInference = false; // 是否进行推理
IntPtr model; // 模型
// 目标物种类,需根据实际情况修改!
//string[] category = { "bocai", "changqiezi", "hongxiancai", "huluobo", "xihongshi", "xilanhua"};
string[] category = { "meter" };
// 定义CreatePaddlexModel接口
[DllImport("paddlex_inference.dll", EntryPoint = "CreatePaddlexModel", CharSet = CharSet.Ansi)]
static extern IntPtr CreatePaddlexModel(ref int modelType,
string modelPath,
bool useGPU,
bool useTrt,
bool useMkl,
int mklThreadNum,
int gpuID,
string key,
bool useIrOptim);
// 定义检测接口
[DllImport("paddlex_inference.dll", EntryPoint = "PaddlexDetPredict", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
static extern bool PaddlexDetPredict(IntPtr model, byte[] image, int height, int width, int channels, int max_box, float[] result, bool visualize);
#endregion
public static byte[] GetbyteData(Bitmap bmp)
{
BitmapData bmpData = null;
bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);
int numbytes = bmpData.Stride * bmpData.Height;
byte[] byteData = new byte[numbytes];
IntPtr ptr = bmpData.Scan0;
Marshal.Copy(ptr, byteData, 0, numbytes);
return byteData;
}
public Bitmap Inference(Bitmap bmp)
{
isInference = true;
model = CreatePaddlexModel(ref modelType, modelPath, useGPU, useTrt, useMkl, mklThreadNum, gpuID, key, useIrOptim);
Bitmap bmpNew = bmp.Clone(new Rectangle(0, 0, bmp.Width, bmp.Height), bmp.PixelFormat);
Bitmap resultShow = null;
Mat img = BitmapConverter.ToMat(bmpNew);
int channel = Image.GetPixelFormatSize(bmp.PixelFormat) / 8;
byte[] source = GetbyteData(bmp);
int max_box = 10;
float[] result = new float[max_box * 6 + 1];
bool res = PaddlexDetPredict(model, source, bmp.Height, bmp.Width, channel, max_box, result, visualize);
if (res)
{
Scalar color = new Scalar(255, 0, 0);
for (int i = 0; i < result[0]; i++)
{
Rect rect = new Rect((int)result[6 * i + 3], (int)result[6 * i + 4], (int)result[6 * i + 5], (int)result[6 * i + 6]);
Cv2.Rectangle(img, rect, color, 2, LineTypes.AntiAlias);
string text = category[(int)result[6 * i + 1]] + ": " + result[6 * i + 2].ToString("f2");
Cv2.PutText(img, text, new OpenCvSharp.Point((int)result[6 * i + 3], (int)result[6 * i + 4] + 25), HersheyFonts.HersheyPlain, 2, Scalar.White);
}
}
resultShow = new Bitmap(img.Cols, img.Rows, (int)img.Step(), PixelFormat.Format24bppRgb, img.Data);
System.GC.Collect();
return resultShow;
}
}
}
from paddlexcsharp.
我只用了推理部分 而且我把PaddleXCsharp相关dll文件放到了项目的bin\debug文件夹里面,但在调用dll接口CreatePaddlexModel出现了System.DllNotFoundException:“无法加载 DLL“paddlex_inference.dll”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。”
from paddlexcsharp.
Related Issues (15)
- 可以提供预备调试的机械手的资料供参考吗
- 用CPU训练出来的模型,可以用GPU做预测吗?测试下来两者耗时差不多 HOT 5
- 会更新PaddleX2.0吗
- 模型加载
- PixelDataConverter pxConvert = new PixelDataConverter()出报错 HOT 1
- 加载模型报错
- 无法加载DLL,"paddlex_inference.dll"找不到指定模块
- 报错,System.Runtime.InteropServices.SEHException:“外部组件发生异常。” HOT 1
- 可以调用用于何种任务的模型
- C#调用C++动态库之间的数据类型转换 HOT 2
- 在C#端无法正常使用GPU做推理 HOT 2
- 能留一个qq或者微信吗 HOT 2
- 如何获取实例分割模型的目标region
- 可以提供C#调用的完整API吗 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from paddlexcsharp.