DHDHSoftware/DH.Devices.Vision/SimboVisionMLBase.cs
2025-03-18 14:20:11 +08:00

236 lines
7.9 KiB
C#

using AntdUI;
using DH.Commons.Enums;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Xml.Linq;
using static System.ComponentModel.Design.ObjectSelectorEditor;
namespace DH.Devices.Vision
{
public abstract class SimboVisionMLBase
{
public Mat ColorLut { get; set; }
public byte[] ColorMap { get; set; }
public MLModelType ModelType { get; set; }
public IntPtr Model { get; set; }
public abstract bool Load(MLInit mLInit);
public abstract MLResult RunInference(MLRequest req);
public void Dispose()
{
try
{
MLGPUEngine.FreePredictor(Model);
}
catch (Exception e) { }
// MLEngine.FreePredictor(Model);
}
public void Dispose2()
{
try
{
MLEngine.FreePredictor(Model);
}
catch (Exception e) { }
// MLEngine.FreePredictor(Model);
}
public SimboVisionMLBase()
{
ColorMap = OpenCVHelper.GetColorMap(256);//使用3个通道
// ColorLut = new Mat(1, 256, MatType.CV_8UC3, ColorMap);
}
}
public class HYoloResult
{
//{
// "HYolo": [{
// "fScore": "0.687012",
// "classId": 0,
// "classname": "quejiao",
// "rect": [421, 823, 6, 8]
// }]
//}
public List<Result> HYolo;
public class Result
{
public double fScore;
public int classId;
public string classname;
//public double area;
public List<int> rect;
}
}
public class SegResult
{
public List<Result> SegmentResult;
public class Result
{
public double fScore;
public int classId;
public string classname;
public double area;
public List<int> rect;
}
}
public static class MLGPUEngine
{
// private const string sPath = @"D:\XHM\XHM\M018_NET7.0speed - 副本 - 副本\src\x64\Debug\HYolo.dll";
[DllImport("HYolo.dll", EntryPoint = "InitModel")]
//public static extern IntPtr InitModel(string model_path, int batch_size, float score_thre, int device_id, int number_of_warmup_runs);
public static extern IntPtr InitModel(string model_path, int batch_size, float score_thre, int device_id, int number_of_warmup_runs,int request_infer);
[DllImport("HYolo.dll", EntryPoint = "PreHot")]
public static extern bool PreHot(IntPtr model, byte[] img, int W, int H, int C);
[DllImport("HYolo.dll", EntryPoint = "Inference")]
public static extern bool Inference(IntPtr model, byte[] img, int W, int H, int C,
string labelText, ref byte Mask_output, ref byte label);
[DllImport("HYolo.dll", EntryPoint = "Inference2")]
public static extern bool Inference2(IntPtr model, byte[] img, int W, int H, int C,
string labelText, ref byte label);
[DllImport("HYolo.dll", EntryPoint = "FreePredictor")]
public static extern void FreePredictor(IntPtr model);
}
public static class MLEngine
{
//private const string sPath = @"D:\\C#\磁环项目\\OpenVinoYolo\\openvino_Yolov5_v7_v2.0\\openvino_Yolov5_v7\\Program\ConsoleProject\\x64\\Release\\QuickSegmentDynamic.dll";
[DllImport("QuickSegmentDynamic.dll", EntryPoint = "InitModel")]
public static extern IntPtr InitModel(string model_filename, string inferenceDevice, string input_node_name, int bacth, int inferenceChannels, int InferenceWidth, int InferenceHeight,int request_infer);
/// <summary>
/// 分割
/// </summary>
/// <param name="model"></param>
/// <param name="img"></param>
/// <param name="W"></param>
/// <param name="H"></param>
/// <param name="C"></param>
/// <param name="labelText"></param>
/// <param name="conf_threshold"></param>
/// <param name="IOU_THRESHOLD"></param>
/// <param name="fScoreThre"></param>
/// <param name="segmentWidth"></param>
/// <param name="Mask_output"></param>
/// <param name="label"></param>
/// <returns></returns>
[DllImport("QuickSegmentDynamic.dll", EntryPoint = "seg_ModelPredict")]
public static extern bool seg_ModelPredict(IntPtr model, byte[] img, int W, int H, int C,
string labelText, float conf_threshold, float IOU_THRESHOLD, float fScoreThre, int segmentWidth,
ref byte Mask_output, ref byte label);
/// <summary>
/// 目标检测
/// </summary>
/// <param name="model"></param>
/// <param name="img"></param>
/// <param name="W"></param>
/// <param name="H"></param>
/// <param name="C"></param>
/// <param name="nodes"></param>
/// <param name="labelText"></param>
/// <param name="conf_threshold"></param>
/// <param name="IOU_THRESHOLD"></param>
/// <param name="Mask_output"></param>
/// <param name="label"></param>
[DllImport("QuickSegmentDynamic.dll", EntryPoint = "det_ModelPredict")]
public static extern bool det_ModelPredict(IntPtr model, byte[] img, int W, int H, int C,
string nodes,// ++++++++++++++++++++++++++++++++++++
string labelText, float conf_threshold, float IOU_THRESHOLD,
ref byte Mask_output, ref byte label);
[DllImport("QuickSegmentDynamic.dll", EntryPoint = "FreePredictor")]
public static extern void FreePredictor(IntPtr model);
}
public static class MLEngine1
{
/**********************************************************************/
/***************** 1.推理DLL导入实现 ****************/
/**********************************************************************/
//private const string sPath = @"D:\M018_NET7.0\src\Debug\model_infer.dll";
// 加载推理相关方法
[DllImport("model_infer.dll", EntryPoint = "InitModel")] // 模型统一初始化方法: 需要yml、pdmodel、pdiparams
//[DllImport(sPath, EntryPoint = "InitModel")] // 模型统一初始化方法: 需要yml、pdmodel、pdiparams
public static extern IntPtr InitModel(string model_type, string model_filename, string params_filename, string cfg_file, bool use_gpu, int gpu_id, ref byte paddlex_model_type);
[DllImport("model_infer.dll", EntryPoint = "Det_ModelPredict")] // PaddleDetection模型推理方法
public static extern bool Det_ModelPredict(IntPtr model, byte[] img, int W, int H, int C, IntPtr output, int[] BoxesNum, ref byte label);
[DllImport("model_infer.dll", EntryPoint = "Seg_ModelPredict")] // PaddleSeg模型推理方法
public static extern bool Seg_ModelPredict(IntPtr model, byte[] img, int W, int H, int C, ref byte output);
[DllImport("model_infer.dll", EntryPoint = "Cls_ModelPredict")] // PaddleClas模型推理方法
public static extern bool Cls_ModelPredict(IntPtr model, byte[] img, int W, int H, int C, ref float score, ref byte category, ref int category_id);
[DllImport("model_infer.dll", EntryPoint = "Mask_ModelPredict")] // Paddlex的MaskRCNN模型推理方法
public static extern bool Mask_ModelPredict(IntPtr model, byte[] img, int W, int H, int C, IntPtr output, ref byte Mask_output, int[] BoxesNum, ref byte label);
//public static extern bool Mask_ModelPredict(IntPtr model, IntPtr img, int W, int H, int C, IntPtr output, ref byte Mask_output, int[] BoxesNum, ref byte label);
[DllImport("model_infer.dll", EntryPoint = "DestructModel")] // 分割、检测、识别模型销毁方法
public static extern void DestructModel(IntPtr model);
}
}