hisence-yolo-detection/HisenceYoloDetection/SimboVisionMLBase.cs

307 lines
11 KiB
C#
Raw Permalink Normal View History

2024-05-31 10:14:57 +08:00

using OpenCvSharp;
using System;
using System.Collections.Generic;
2024-06-27 09:22:31 +08:00
using System.ComponentModel;
2024-05-31 10:14:57 +08:00
using System.Drawing;
using System.Runtime.InteropServices;
2024-06-11 11:40:47 +08:00
public class XK_HisenceWord
{
public string? OcrBar;
public string? OneblockPath;
public string? OneblockMainWord;
public string? OneblockText;
public string? TwoRect;
public bool? TwoIFWhile;
public string? TwoblockPath;
public string? TwoblockMainWord;
public string? TwoblockText;
public string? ThreeblockPath;
public string? ThreeblockMainWord;
public string? ThreeblockText;
public string? FourblockPath;
public string? FourblockMainWord;
public string? FourblockText;
public string? FiveblockPath;
public string? FiveblockMainWord;
public string? FiveblockText;
public string? SixblockPath;
public string? SixblockMainWord;
public string? SixblockText;
public string? SevenblockPath;
public string? SevenblockMainWord;
public string? SevenblockText;
public string? EightblockPath;
public string? EightblockMainWord;
public string? EightblockText;
public XK_HisenceWord()
{
}
public XK_HisenceWord(string? ocrBar, string? oneblockPath, string? oneblockMainWord, string? oneblockText, string? twoRect, bool? twoIFWhile, string? twoblockPath, string? twoblockMainWord, string? twoblockText, string? threeblockPath, string? threeblockMainWord, string? threeblockText, string? fourblockPath, string? fourblockMainWord, string? fourblockText, string? fiveblockPath, string? fiveblockMainWord, string? fiveblockText, string? sixblockPath, string? sixblockMainWord, string? sixblockText, string? sevenblockPath, string? sevenblockMainWord, string? sevenblockText, string? eightblockPath, string? eightblockMainWord, string? eightblockText)
{
OcrBar = ocrBar;
OneblockPath = oneblockPath;
OneblockMainWord = oneblockMainWord;
OneblockText = oneblockText;
TwoRect = twoRect;
TwoIFWhile = twoIFWhile;
TwoblockPath = twoblockPath;
TwoblockMainWord = twoblockMainWord;
TwoblockText = twoblockText;
ThreeblockPath = threeblockPath;
ThreeblockMainWord = threeblockMainWord;
ThreeblockText = threeblockText;
FourblockPath = fourblockPath;
FourblockMainWord = fourblockMainWord;
FourblockText = fourblockText;
FiveblockPath = fiveblockPath;
FiveblockMainWord = fiveblockMainWord;
FiveblockText = fiveblockText;
SixblockPath = sixblockPath;
SixblockMainWord = sixblockMainWord;
SixblockText = sixblockText;
SevenblockPath = sevenblockPath;
SevenblockMainWord = sevenblockMainWord;
SevenblockText = sevenblockText;
EightblockPath = eightblockPath;
EightblockMainWord = eightblockMainWord;
EightblockText = eightblockText;
}
}
2024-05-31 10:14:57 +08:00
public class XKHisence
{
public int Number;
public string ?Type;
public string ?OcrBar;
public int MoveX;
public int MoveY;
public int MoveZ;
public string ?Detect;
public string ?OcrText;
public int MoveTwoX;
public int MoveTwoY;
public int MoveTwoZ;
public string ?OcrParm;
public string ?Language;
2024-06-11 11:40:47 +08:00
public string? FuzzyOcrText;
2024-05-31 10:14:57 +08:00
public XKHisence()
{
}
2024-06-11 11:40:47 +08:00
public XKHisence(string type,string ocrBar,int MoveX,int MoveY,int MoveZ,string Detect,string ocrText,int MoveTwoX,int MoveTwoY,int MoveTwoZ,string OcrParm,string Language,string FuzzyOcrText)
2024-05-31 10:14:57 +08:00
{
this.Type = type;
this.OcrBar = ocrBar;
this.MoveX = MoveX;
this.MoveY = MoveY;
this.MoveZ= MoveZ;
this.Detect = Detect;
this.OcrText= ocrText;
this.MoveTwoX = MoveTwoX;
this.MoveTwoY = MoveTwoY;
this.MoveTwoZ = MoveTwoZ;
this.OcrParm = OcrParm;
this.Language = Language;
2024-06-11 11:40:47 +08:00
this.FuzzyOcrText = FuzzyOcrText;
2024-05-31 10:14:57 +08:00
}
}
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;
2024-06-11 11:40:47 +08:00
public float Score;
2024-05-31 10:14:57 +08:00
2024-06-11 11:40:47 +08:00
public MLRequest()
{
2024-05-31 10:14:57 +08:00
2024-06-11 11:40:47 +08:00
}
2024-05-31 10:14:57 +08:00
}
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 MLResult
{
public bool IsSuccess = false;
public string ResultMessage;
public Bitmap ResultMap;
public List<DetectionResultDetail> ResultDetails = new List<DetectionResultDetail>();
2024-06-27 09:22:31 +08:00
}
public enum ResultState
{
[Description("检测NG")]
DetectNG = 0,
[Description("OK")]
OK = 1,
2024-05-31 10:14:57 +08:00
}
2024-06-11 11:40:47 +08:00
2024-06-27 09:22:31 +08:00
2024-05-31 10:14:57 +08:00
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);
}