HuaRui/SimboVisionMLBase.cs

267 lines
8.9 KiB
C#
Raw Permalink Normal View History

2024-08-17 14:06:39 +08:00

using HZH_Controls.Controls;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Drawing;
using System.Runtime.InteropServices;
//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()
// {
// MLEngine.FreePredictor(Model);
// }
// public SimboVisionMLBase()
// {
// ColorMap = OpenCVHelper.GetColorMap(256);//使用3个通道
// ColorLut = new Mat(1, 256, MatType.CV_8UC3, ColorMap);
// }
//}
public class MLInit
{
public string ModelFile;
public string InferenceDevice;
public int InferenceWidth;
public int InferenceHeight;
public string InputNodeName;
public MLInit(string modelFile, string inputNodeName, string inferenceDevice, int inferenceWidth, int inferenceHeight)
{
ModelFile = modelFile;
InferenceDevice = inferenceDevice;
InferenceWidth = inferenceWidth;
InferenceHeight = inferenceHeight;
InputNodeName = inputNodeName;
}
}
public class MLResult
{
public bool IsSuccess = false;
public string ResultMessage;
public Bitmap ResultMap;
public List<DetectionResultDetail> ResultDetails = new List<DetectionResultDetail>();
}
public class MLRequest
{
public int ImageChannels = 3;
public Mat currentMat
;
public int ResizeWidth;
public int ResizeHeight;
public float confThreshold;
public float iouThreshold;
//public int ImageResizeCount;
public string in_node_name;
public string out_node_name;
public string in_lable_path;
public int ResizeImageSize;
public int segmentWidth;
public int ImageWidth;
public int Snapshot;
public string SnapshotName;
//public List<labelStringBase> OkClassTxtList;
// public List<ModelLabel> LabelNames;
public float Score;
}
public class DetectionResultDetail
{
public string LabelBGR { get; set; }//识别到对象的标签BGR
public int LabelNo { get; set; } // 识别到对象的标签索引
public string LabelName { get; set; }//识别到对象的标签名称
public double Score { get; set; }//识别目标结果的可能性、得分
public string LabelDisplay { get; set; }//识别到对象的 显示信息
public double Area { get; set; }//识别目标的区域面积
public Rectangle Rect { get; set; }//识别目标的外接矩形
public RotatedRect MinRect { get; set; }//识别目标的最小外接矩形(带角度)
//public ResultState InferenceResult { get; set; }//只是模型推理 label的结果
public double DistanceToImageCenter { get; set; } //计算矩形框到图像中心的距离
// public ResultState FinalResult { get; set; }//模型推理+其他视觉、逻辑判断后 label结果
}
public class XKOCROfficeWord
{
[StringLength(50)]
public int ID { get; set; }
[StringLength(50)]
public string Name { get; set; } = "";
public int PageNum { get; set; } = 0;
[StringLength(100)]
public string filePath { get; set; } = "";
public string jsonpath { get; set; } = "";
public int LeftTopX { get; set; } = 0;
public int LeftTopY { get; set; } = 0;
public int RightBottmX { get; set; } = 0;
public int RightBottmY { get; set; } = 0;
public string ResultStr { get; set; } = "";
}
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);
/// <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 OcrEngine
{
// private const string sPath = @"F:\OOOCR\PaddleOCRsourcecodeGPU\PROJECTS\OcrDetForm\bin\Release\net7.0-windows\ocrInference.dll";
[DllImport("ocrInference.dll", EntryPoint = "InitModel")]
public static extern IntPtr InitModel(string model_ParaPath, string device_id);
[DllImport("ocrInference.dll", EntryPoint = "Inference")]
public static extern bool Inference(IntPtr model, byte[] img, int W, int H, int C,
ref byte Mask_output, ref byte label);
[DllImport("ocrInference.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);
}