Code Monkey home page Code Monkey logo

Comments (2)

wangyaxin1998 avatar wangyaxin1998 commented on July 22, 2024

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.

wangyaxin1998 avatar wangyaxin1998 commented on July 22, 2024

我只用了推理部分 而且我把PaddleXCsharp相关dll文件放到了项目的bin\debug文件夹里面,但在调用dll接口CreatePaddlexModel出现了System.DllNotFoundException:“无法加载 DLL“paddlex_inference.dll”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。”

from paddlexcsharp.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.