上传界面显示

This commit is contained in:
2025-03-16 13:11:08 +08:00
parent ca6476b87c
commit c49c45d6e4
62 changed files with 9095 additions and 1204 deletions

View File

@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<BaseOutputPath>..\</BaseOutputPath>
<AppendTargetFrameworkToOutputPath>output</AppendTargetFrameworkToOutputPath>
<UseWindowsForms>true</UseWindowsForms>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Platforms>AnyCPU;x64</Platforms>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<BaseOutputPath>..\</BaseOutputPath>
<AppendTargetFrameworkToOutputPath>output</AppendTargetFrameworkToOutputPath>
<UseWindowsForms>true</UseWindowsForms>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Platforms>AnyCPU;x64</Platforms>
</PropertyGroup>
@ -22,4 +22,24 @@
<PackageReference Include="OpenCvSharp4.runtime.win" Version="4.10.0.20241108" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DH.Commons.Devies\DH.Commons.Devies.csproj" />
<ProjectReference Include="..\DH.Commons\DH.Commons.csproj" />
<ProjectReference Include="..\DH.UI.Model.Winform\DH.UI.Model.Winform.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="halcondotnet">
<HintPath>..\x64\Debug\halcondotnet.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

View File

@ -1,689 +0,0 @@
using OpenCvSharp;
using System.ComponentModel;
using System.Drawing;
using static OpenCvSharp.AgastFeatureDetector;
using System.Text.RegularExpressions;
using System.Text;
using System.Drawing.Design;
namespace DH.Devices.Vision
{
public enum MLModelType
{
[Description("图像分类")]
ImageClassification = 1,
[Description("目标检测")]
ObjectDetection = 2,
//[Description("图像分割")]
//ImageSegmentation = 3
[Description("语义分割")]
SemanticSegmentation = 3,
[Description("实例分割")]
InstanceSegmentation = 4,
[Description("目标检测GPU")]
ObjectGPUDetection = 5
}
public class ModelLabel
{
public string LabelId { get; set; }
[Category("模型标签")]
[DisplayName("模型标签索引")]
[Description("模型识别的标签索引")]
public int LabelIndex { get; set; }
[Category("模型标签")]
[DisplayName("模型标签")]
[Description("模型识别的标签名称")]
public string LabelName { get; set; }
//[Category("模型配置")]
//[DisplayName("模型参数配置")]
//[Description("模型参数配置集合")]
//public ModelParamSetting ModelParamSetting { get; set; } = new ModelParamSetting();
public string GetDisplayText()
{
return $"{LabelId}-{LabelName}";
}
}
public class MLRequest
{
public int ImageChannels = 3;
public Mat mImage;
public int ResizeWidth;
public int ResizeHeight;
public float confThreshold;
public float iouThreshold;
//public int ImageResizeCount;
public bool IsCLDetection;
public int ProCount;
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 List<labelStringBase> OkClassTxtList;
public List<ModelLabel> LabelNames;
}
public enum ResultState
{
[Description("检测NG")]
DetectNG = -3,
//[Description("检测不足TBD")]
// ShortageTBD = -2,
[Description("检测结果TBD")]
ResultTBD = -1,
[Description("OK")]
OK = 1,
// [Description("NG")]
// NG = 2,
//统计结果
[Description("A类NG")]
A_NG = 25,
[Description("B类NG")]
B_NG = 26,
[Description("C类NG")]
C_NG = 27,
}
/// <summary>
/// 深度学习 识别结果明细 面向业务detect 面向深度学习Recongnition、Inference
/// </summary>
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>();
}
public class MLInit
{
public string ModelFile;
public string InferenceDevice;
public int InferenceWidth;
public int InferenceHeight;
public string InputNodeName;
public int SizeModel;
public bool bReverse;//尺寸测量正反面
//目标检测Gpu
public bool IsGPU;
public int GPUId;
public float Score_thre;
public MLInit(string modelFile, bool isGPU, int gpuId, float score_thre)
{
ModelFile = modelFile;
IsGPU = isGPU;
GPUId = gpuId;
Score_thre = score_thre;
}
public MLInit(string modelFile, string inputNodeName, string inferenceDevice, int inferenceWidth, int inferenceHeight)
{
ModelFile = modelFile;
InferenceDevice = inferenceDevice;
InferenceWidth = inferenceWidth;
InferenceHeight = inferenceHeight;
InputNodeName = inputNodeName;
}
}
public class DetectStationResult
{
public string Pid { get; set; }
public string TempPid { get; set; }
/// <summary>
/// 检测工位名称
/// </summary>
public string DetectName { get; set; }
/// <summary>
/// 深度学习 检测结果
/// </summary>
public List<DetectionResultDetail> DetectDetails = new List<DetectionResultDetail>();
/// <summary>
/// 工位检测结果
/// </summary>
public ResultState ResultState { get; set; } = ResultState.ResultTBD;
public double FinalResultfScore { get; set; } = 0.0;
public string ResultLabel { get; set; } = "";// 多个ng时根据label优先级设定当前检测项的label
public string ResultLabelCategoryId { get; set; } = "";// 多个ng时根据label优先级设定当前检测项的label
public int PreTreatState { get; set; }
public bool IsPreTreatDone { get; set; } = true;
public bool IsAfterTreatDone { get; set; } = true;
public bool IsMLDetectDone { get; set; } = true;
/// <summary>
/// 预处理阶段已经NG
/// </summary>
public bool IsPreTreatNG { get; set; } = false;
/// <summary>
/// 目标检测NG
/// </summary>
public bool IsObjectDetectNG { get; set; } = false;
public DateTime EndTime { get; set; }
public int StationDetectElapsed { get; set; }
public static string NormalizeAndClean(string input)
{
if (input == null) return null;
// Step 1: 标准化字符编码为 Form C (规范组合)
string normalizedString = input.Normalize(NormalizationForm.FormC);
// Step 2: 移除所有空白字符,包括制表符和换行符
string withoutWhitespace = Regex.Replace(normalizedString, @"\s+", "");
// Step 3: 移除控制字符 (Unicode 控制字符,范围 \u0000 - \u001F 和 \u007F)
string withoutControlChars = Regex.Replace(withoutWhitespace, @"[\u0000-\u001F\u007F]+", "");
// Step 4: 移除特殊的不可见字符(如零宽度空格等)
string cleanedString = Regex.Replace(withoutControlChars, @"[\u200B\u200C\u200D\uFEFF]+", "");
return cleanedString;
}
}
public class RelatedCamera
{
[Category("关联相机")]
[DisplayName("关联相机")]
[Description("关联相机描述")]
//[TypeConverter(typeof(CollectionCountConvert))]
public string CameraSourceId { get; set; } = "";
public RelatedCamera()
{
}
public RelatedCamera(string cameraSourceId)
{
CameraSourceId = cameraSourceId;
}
}
public class DetectionConfig
{
[ReadOnly(true)]
public string Id { get; set; } = Guid.NewGuid().ToString();
[Category("检测配置")]
[DisplayName("检测配置名称")]
[Description("检测配置名称")]
public string Name { get; set; }
[Category("关联相机")]
[DisplayName("关联相机")]
[Description("关联相机描述")]
public string CameraSourceId { get; set; } = "";
[Category("关联相机集合")]
[DisplayName("关联相机集合")]
[Description("关联相机描述")]
//[TypeConverter(typeof(DeviceIdSelectorConverter<CameraBase>))]
public List<RelatedCamera> CameraCollects { get; set; } = new List<RelatedCamera>();
[Category("启用配置")]
[DisplayName("是否启用GPU检测")]
[Description("是否启用GPU检测")]
public bool IsEnableGPU { get; set; } = false;
[Category("启用配置")]
[DisplayName("是否混料模型")]
[Description("是否混料模型")]
public bool IsMixModel { get; set; } = false;
[Category("启用配置")]
[DisplayName("是否启用该检测")]
[Description("是否启用该检测")]
public bool IsEnabled { get; set; }
[Category("启用配置")]
[DisplayName("是否加入检测工位")]
[Description("是否加入检测工位")]
public bool IsAddStation { get; set; } = true;
[Category("1.预处理(视觉算子)")]
[DisplayName("预处理-算法文件路径")]
// [Description("预处理算法文件路径配置")][Editor(typeof(FileDialogEditor), typeof(UITypeEditor))]
public string HalconAlgorithemPath_Pre { get; set; }
// [Category("1.预处理(视觉算子)")]
//[DisplayName("预处理-输出结果的SPEC标准")]
//[Description("预处理输出结果的SPEC标准配置")]
// public List<IndexedSpec> OutputSpec_Pre { get; set; } = new List<IndexedSpec>();
[Category("1.预处理(视觉算子)")]
[DisplayName("预处理-参数列表")]
[Description("预处理-参数列表")]
public List<PreTreatParam> PreTreatParams { get; set; } = new List<PreTreatParam>();
[Category("1.预处理(视觉算子)")]
[DisplayName("预处理-输出参数列表")]
[Description("预处理-输出参数列表")]
public List<PreTreatParam> OUTPreTreatParams { get; set; } = new List<PreTreatParam>();
[Category("2.中检测(深度学习)")]
[DisplayName("中检测-模型类型")]
[Description("模型类型ImageClassification-图片分类ObjectDetection目标检测Segmentation-图像分割")]
//[TypeConverter(typeof(EnumDescriptionConverter<MLModelType>))]
public MLModelType ModelType { get; set; } = MLModelType.ObjectDetection;
//[Category("2.中检测(深度学习)")]
//[DisplayName("中检测-GPU索引")]
//[Description("GPU索引")]
//public int GPUIndex { get; set; } = 0;
[Category("2.中检测(深度学习)")]
[DisplayName("中检测-模型文件路径")]
[Description("中处理 深度学习模型文件路径,路径中不可含有中文字符,一般情况可以只配置中检测模型,当需要先用预检测过滤一次时,请先配置好与预检测相关配置")]
public string ModelPath { get; set; }
[Category("2.中检测(深度学习)")]
[DisplayName("中检测-模型宽度")]
[Description("中处理-模型宽度")]
public int ModelWidth { get; set; } = 640;
[Category("2.中检测(深度学习)")]
[DisplayName("中检测-模型高度")]
[Description("中处理-模型高度")]
public int ModelHeight { get; set; } = 640;
[Category("2.中检测(深度学习)")]
[DisplayName("中检测-模型节点名称")]
[Description("中处理-模型节点名称")]
public string ModeloutNodeName { get; set; } = "output0";
[Category("2.中检测(深度学习)")]
[DisplayName("中检测-模型置信度")]
[Description("中处理-模型置信度")]
public float ModelconfThreshold { get; set; } = 0.5f;
[Category("2.中检测(深度学习)")]
[DisplayName("中检测-模型标签路径")]
[Description("中处理-模型标签路径")]
public string in_lable_path { get; set; }
[Category("4.最终过滤(逻辑过滤)")]
[DisplayName("过滤器集合")]
[Description("最后的逻辑过滤:可根据 识别出对象的 宽度、高度、面积、得分来设置最终检测结果,同一识别目标同一判定,多项过滤器之间为“或”关系")]
public List<DetectionFilter> DetectionFilterList { get; set; } = new List<DetectionFilter>();
//[Category("深度学习配置")]
//[DisplayName("检测配置标签")]
//[Description("检测配置标签关联")]
//public List<DetectConfigLabel> DetectConfigLabelList { get; set; } = new List<DetectConfigLabel>();
public DetectionConfig()
{
}
public DetectionConfig(string name, MLModelType modelType, string modelPath, bool isEnableGPU,string sCameraSourceId)
{
ModelPath = modelPath ?? string.Empty;
Name = name;
ModelType = modelType;
IsEnableGPU = isEnableGPU;
Id = Guid.NewGuid().ToString();
CameraSourceId = sCameraSourceId;
}
}
/// <summary>
/// 识别目标定义 class分类信息 Detection Segmentation要识别的对象
/// </summary>
public class RecongnitionLabel //: IComplexDisplay
{
[Category("检测标签定义")]
[Description("检测标签编码")]
[ReadOnly(true)]
public string Id { get; set; } = Guid.NewGuid().ToString();
[Category("检测标签定义")]
[DisplayName("检测标签名称")]
[Description("检测标签名称")]
public string LabelName { get; set; } = "";
[Category("检测标签定义")]
[DisplayName("检测标签描述")]
[Description("检测标签描述,中文描述")]
public string LabelDescription { get; set; } = "";
[Category("检测标签定义")]
[DisplayName("检测标签分类")]
[Description("检测标签分类id")]
//[TypeConverter(typeof(LabelCategoryConverter))]
public string LabelCategory { get; set; } = "";
}
/// <summary>
/// 检测项识别对象
/// </summary>
public class DetectConfigLabel //: IComplexDisplay
{
[Category("检测项标签")]
[DisplayName("检测项标签")]
[Description("检测标签Id")]
//[TypeConverter(typeof(DetectionLabelConverter))]
public string LabelId { get; set; }
[Browsable(false)]
//public string LabelName { get => GetLabelName(); }
[Category("检测项标签")]
[DisplayName("检测标签优先级")]
[Description("检测标签优先级,值越小,优先级越高")]
public int LabelPriority { get; set; } = 0;
//[Category("检测项标签")]
//[DisplayName("标签BGR值")]
//[Description("检测标签BGR值例如0,128,0")]
//public string LabelBGR { get; set; }
//[Category("模型配置")]
//[DisplayName("模型参数配置")]
//[Description("模型参数配置集合")]
//[TypeConverter(typeof(ComplexObjectConvert))]
//[Editor(typeof(PropertyObjectEditor), typeof(UITypeEditor))]
//public ModelParamSetting ModelParamSetting { get; set; } = new ModelParamSetting();
//public string GetDisplayText()
//{
// string dName = "";
// if (!string.IsNullOrWhiteSpace(LabelId))
// {
// using (var scope = GlobalVar.Container.BeginLifetimeScope())
// {
// IProcessConfig config = scope.Resolve<IProcessConfig>();
// var mlBase = config.DeviceConfigs.FirstOrDefault(c => c is VisionEngineInitialConfigBase) as VisionEngineInitialConfigBase;
// if (mlBase != null)
// {
// var targetLabel = mlBase.RecongnitionLabelList.FirstOrDefault(u => u.Id == LabelId);
// if (targetLabel != null)
// {
// dName = targetLabel.GetDisplayText();
// }
// }
// }
// }
// return dName;
//}
//public string GetLabelName()
//{
// var name = "";
// var mlBase = iConfig.DeviceConfigs.FirstOrDefault(c => c is VisionEngineInitialConfigBase) as VisionEngineInitialConfigBase;
// if (mlBase != null)
// {
// var label = mlBase.RecongnitionLabelList.FirstOrDefault(u => u.Id == LabelId);
// if (label != null)
// {
// name = label.LabelName;
// }
// }
// return name;
//}
}
/// <summary>
/// 识别对象定义分类信息 A类B类
/// </summary>
public class RecongnitionLabelCategory //: IComplexDisplay
{
[Category("检测标签分类")]
[Description("检测标签分类")]
[ReadOnly(true)]
public string Id { get; set; } = Guid.NewGuid().ToString();
[Category("检测标签分类")]
[DisplayName("检测标签分类名称")]
[Description("检测标签分类名称")]
public string CategoryName { get; set; } = "A-NG";
[Category("检测标签分类")]
[DisplayName("检测标签分类优先级")]
[Description("检测标签分类优先级,值越小,优先级越高")]
public int CategoryPriority { get; set; } = 0;
public string GetDisplayText()
{
return CategoryPriority + ":" + CategoryName;
}
}
/// <summary>
/// 检测过滤
/// </summary>
public class DetectionFilter ///: IComplexDisplay
{
[Category("过滤器基础信息")]
[DisplayName("检测标签")]
[Description("检测标签信息")]
//[TypeConverter(typeof(DetectionLabelConverter))]
public string LabelId { get; set; }
// [Browsable(false)]
public string LabelName { get; set; }
[Category("过滤器基础信息")]
[DisplayName("是否启用过滤器")]
[Description("是否启用过滤器")]
public bool IsEnabled { get; set; }
[Category("过滤器判定信息")]
[DisplayName("判定结果")]
[Description("过滤器默认判定结果")]
public ResultState ResultState { get; set; } = ResultState.ResultTBD;
[Category("过滤条件")]
[DisplayName("过滤条件集合")]
[Description("过滤条件集合,集合之间为“且”关系")]
//[TypeConverter(typeof(CollectionCountConvert))]
// [Editor(typeof(ComplexCollectionEditor<FilterConditions>), typeof(UITypeEditor))]
public List<FilterConditions> FilterConditionsCollection { get; set; } = new List<FilterConditions>();
public bool FilterOperation(DetectionResultDetail recongnitionResult)
{
return FilterConditionsCollection.All(u =>
{
return u.FilterConditionCollection.Any(c =>
{
double compareValue = 0;
switch (c.FilterPropperty)
{
case DetectionFilterProperty.Width:
compareValue = recongnitionResult.Rect.Width;
break;
case DetectionFilterProperty.Height:
compareValue = recongnitionResult.Rect.Height;
break;
case DetectionFilterProperty.Area:
compareValue = recongnitionResult.Area;
break;
case DetectionFilterProperty.Score:
compareValue = recongnitionResult.Score;
break;
//case RecongnitionTargetFilterProperty.Uncertainty:
// compareValue = 0;
// //defect.Uncertainty;
// break;
}
return compareValue >= c.MinValue && compareValue <= c.MaxValue;
});
});
}
}
public class FilterConditions //: IComplexDisplay
{
[Category("过滤条件")]
[DisplayName("过滤条件集合")]
[Description("过滤条件集合,集合之间为“或”关系")]
//[TypeConverter(typeof(CollectionCountConvert))]
//[Editor(typeof(ComplexCollectionEditor<FilterCondition>), typeof(UITypeEditor))]
public List<FilterCondition> FilterConditionCollection { get; set; } = new List<FilterCondition>();
//public string GetDisplayText()
//{
// if (FilterConditionCollection.Count == 0)
// {
// return "空";
// }
// else
// {
// var desc = string.Join(" OR ", FilterConditionCollection.Select(u => u.GetDisplayText()));
// if (FilterConditionCollection.Count > 1)
// {
// desc = $"({desc})";
// }
// return desc;
// }
//}
}
public class FilterCondition //: IComplexDisplay
{
[Category("识别目标属性")]
[DisplayName("过滤属性")]
[Description("识别目标过滤针对的属性")]
//[TypeConverter(typeof(EnumDescriptionConverter<DetectionFilterProperty>))]
public DetectionFilterProperty FilterPropperty { get; set; } = DetectionFilterProperty.Width;
[Category("过滤值")]
[DisplayName("最小值")]
[Description("最小值")]
public double MinValue { get; set; } = 1;
[Category("过滤值")]
[DisplayName("最大值")]
[Description("最大值")]
public double MaxValue { get; set; } = 99999999;
//public string GetDisplayText()
//{
// return $"{FilterPropperty.GetEnumDescription()}:{MinValue}-{MaxValue}";
//}
}
public enum DetectionFilterProperty
{
[Description("宽度")]
Width = 1,
[Description("高度")]
Height = 2,
[Description("面积")]
Area = 3,
[Description("得分")]
Score = 4,
//[Description("不确定性")]
//Uncertainty = 5,
}
}

View File

@ -0,0 +1,10 @@
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.
using System.Diagnostics.CodeAnalysis;
[assembly: SuppressMessage("Usage", "CA2200:再次引发以保留堆栈详细信息", Justification = "<挂起>", Scope = "member", Target = "~M:DH.Devices.Vision.SimboDetection.Load(DH.Devices.Vision.MLInit)~System.Boolean")]
[assembly: SuppressMessage("Usage", "CA2200:再次引发以保留堆栈详细信息", Justification = "<挂起>", Scope = "member", Target = "~M:DH.Devices.Vision.SimboInstanceSegmentation.Load(DH.Devices.Vision.MLInit)~System.Boolean")]
[assembly: SuppressMessage("Usage", "CA2200:再次引发以保留堆栈详细信息", Justification = "<挂起>", Scope = "member", Target = "~M:DH.Devices.Vision.SimboObjectDetection.Load(DH.Devices.Vision.MLInit)~System.Boolean")]

View File

@ -1,4 +1,4 @@
#define USE_MULTI_THREAD
//#define USE_MULTI_THREAD
using OpenCvSharp;
using OpenCvSharp.Extensions;
@ -13,6 +13,7 @@ using System.Threading.Tasks;
using System.Security.Cryptography.Xml;
using System.Runtime.InteropServices;
using Newtonsoft.Json;
using DH.Commons.Enums;
@ -103,14 +104,18 @@ namespace DH.Devices.Vision
// json = "{\"FastDetResult\":[{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.654843,\"rect\":[175,99,110,594]},{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.654589,\"rect\":[2608,19,104,661]},{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.654285,\"rect\":[1275,19,104,662]},{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.620762,\"rect\":[1510,95,107,600]},{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.617812,\"rect\":[2844,93,106,602]}]}";
//
Console.WriteLine("检测结果JSON" + json);
#pragma warning disable CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
HYoloResult detResult = JsonConvert.DeserializeObject<HYoloResult>(json);
#pragma warning restore CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
if (detResult == null)
{
return;
}
int iNum = detResult.HYolo.Count;
#pragma warning disable CS0219 // 变量已被赋值,但从未使用过它的值
int IokNum = 0;
#pragma warning restore CS0219 // 变量已被赋值,但从未使用过它的值
for (int ix = 0; ix < iNum; ix++)
{
var det = detResult.HYolo[ix];
@ -140,6 +145,7 @@ namespace DH.Devices.Vision
Mat originMat = new Mat();
Mat detectMat = new Mat();
#pragma warning disable CS0168 // 声明了变量,但从未使用过
try
{
if (req.mImage == null)
@ -228,15 +234,20 @@ namespace DH.Devices.Vision
{
originMat?.Dispose();
#pragma warning disable CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
originMat = null;
#pragma warning restore CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
//maskMat?.Dispose();
// maskMat = null;
detectMat?.Dispose();
#pragma warning disable CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
detectMat = null;
#pragma warning restore CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
// maskWeighted?.Dispose();
// maskWeighted = null;
// GC.Collect();
}
#pragma warning restore CS0168 // 声明了变量,但从未使用过
}

View File

@ -12,6 +12,7 @@ using System.Threading;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using Newtonsoft.Json;
using DH.Commons.Enums;
namespace DH.Devices.Vision
@ -126,14 +127,18 @@ namespace DH.Devices.Vision
// json = "{\"FastDetResult\":[{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.654843,\"rect\":[175,99,110,594]},{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.654589,\"rect\":[2608,19,104,661]},{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.654285,\"rect\":[1275,19,104,662]},{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.620762,\"rect\":[1510,95,107,600]},{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.617812,\"rect\":[2844,93,106,602]}]}";
//
Console.WriteLine("检测结果JSON" + json);
#pragma warning disable CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
SegResult detResult = JsonConvert.DeserializeObject<SegResult>(json);
#pragma warning restore CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
if (detResult == null)
{
return;
}
int iNum = detResult.SegmentResult.Count;
#pragma warning disable CS0219 // 变量已被赋值,但从未使用过它的值
int IokNum = 0;
#pragma warning restore CS0219 // 变量已被赋值,但从未使用过它的值
for (int ix = 0; ix < iNum; ix++)
{
var det = detResult.SegmentResult[ix];
@ -166,6 +171,7 @@ namespace DH.Devices.Vision
Mat originMat = new Mat();
Mat detectMat = new Mat();
#pragma warning disable CS0168 // 声明了变量,但从未使用过
try
{
if (req.mImage == null)
@ -253,11 +259,14 @@ namespace DH.Devices.Vision
{
originMat?.Dispose();
#pragma warning disable CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
originMat = null;
#pragma warning restore CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
// GC.Collect();
}
#pragma warning restore CS0168 // 声明了变量,但从未使用过
}
}

View File

@ -13,6 +13,7 @@ using System.Threading.Tasks;
using System.Runtime.InteropServices;
using Newtonsoft.Json;
using System.Xml;
using DH.Commons.Enums;
namespace DH.Devices.Vision
@ -135,7 +136,9 @@ namespace DH.Devices.Vision
// json = "{\"FastDetResult\":[{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.654843,\"rect\":[175,99,110,594]},{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.654589,\"rect\":[2608,19,104,661]},{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.654285,\"rect\":[1275,19,104,662]},{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.620762,\"rect\":[1510,95,107,600]},{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.617812,\"rect\":[2844,93,106,602]}]}";
//
Console.WriteLine("检测结果JSON" + json);
#pragma warning disable CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
SegResult detResult = JsonConvert.DeserializeObject<SegResult>(json);
#pragma warning restore CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
if (detResult == null)
{
return;
@ -173,6 +176,7 @@ namespace DH.Devices.Vision
MLResult mlResult = new MLResult();
Mat originMat=new Mat() ;
Mat detectMat= new Mat();
#pragma warning disable CS0168 // 声明了变量,但从未使用过
try
{
if (req.mImage == null)
@ -263,19 +267,24 @@ namespace DH.Devices.Vision
if (detectMat != null)
{
detectMat.Dispose();
#pragma warning disable CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
detectMat = null;
#pragma warning restore CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
}
if (originMat != null)
{
originMat.Dispose();
#pragma warning disable CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
originMat = null;
#pragma warning restore CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
}
// GC.Collect();
}
#pragma warning restore CS0168 // 声明了变量,但从未使用过
}

View File

@ -1,4 +1,9 @@
using OpenCvSharp;
using DH.Commons.Enums;
using DH.Devices.Devices;
using DH.UI.Model.Winform;
using HalconDotNet;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@ -6,13 +11,659 @@ using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using XKRS.UI.Model.Winform;
using static DH.Commons.Enums.EnumHelper;
using ResultState = DH.Commons.Enums.ResultState;
namespace DH.Devices.Vision
{
public class SimboVisionDriver
public class SimboVisionDriver : VisionEngineBase
{
public Dictionary<string, HDevEngineTool> HalconToolDict = new Dictionary<string, HDevEngineTool>();
public List<SimboStationMLEngineSet> SimboStationMLEngineList = new List<SimboStationMLEngineSet>();
public void Init()
{
//InitialQueue();
InitialHalconTools();
InitialSimboMLEnginesAsync();
// ImageSaveHelper.OnImageSaveExceptionRaised -= ImageSaveHelper_OnImageSaveExceptionRaised;
// ImageSaveHelper.OnImageSaveExceptionRaised += ImageSaveHelper_OnImageSaveExceptionRaised;
// base.Init();
}
//private void ImageSaveHelper_OnImageSaveExceptionRaised(DateTime dt, string msg)
//{
// LogAsync(new LogMsg(dt, LogLevel.Error, msg));
//}
public override DetectStationResult RunInference(Mat originImgSet, string detectionId = null)
{
DetectStationResult detectResult = new DetectStationResult();
DetectionConfig detectConfig = null;
//找到对应的配置
if (!string.IsNullOrWhiteSpace(detectionId))
{
detectConfig = DetectionConfigs.FirstOrDefault(u => u.Id == detectionId);
}
else
{
//detectConfig = DetectionConfigs.FirstOrDefault(u => u.CameraSourceId == camera.CameraName);
}
if (detectConfig == null)
{
//未能获得检测配置
return detectResult;
}
#region 1.
using (Mat PreTMat = originImgSet.Clone())
{
PreTreated(detectConfig, detectResult, PreTMat);
}
#endregion
if (detectResult.IsPreTreatNG)
{
detectResult.ResultState = ResultState.DetectNG;
detectResult.IsPreTreatDone = true;
detectResult.IsMLDetectDone = false;
return detectResult;
}
if (!string.IsNullOrWhiteSpace(detectConfig.ModelPath) && detectConfig.IsEnabled)
{
SimboStationMLEngineSet mlSet = null;
mlSet = SimboStationMLEngineList.FirstOrDefault(t => t.DetectionId == detectConfig.Id);
if (mlSet == null)
{
// LogAsync(DateTime.Now, LogLevel.Exception, $"异常:{detectConfig.Name}未能获取对应配置的模型检测工具");
detectResult.IsMLDetectDone = false;
//HandleDetectDone(detectResult, detectConfig);
return detectResult;
}
#region 2.
//LogAsync(DateTime.Now, LogLevel.Information, $"{detectConfig.Name} 产品{detectResult.TempPid} 模型检测执行");
if (!string.IsNullOrWhiteSpace(detectConfig.ModelPath))
{
Stopwatch mlWatch = new Stopwatch();
var req = new MLRequest();
//之前的检测图片都是相机存储成HImage
req.ResizeWidth = (int)detectConfig.ModelWidth;
req.ResizeHeight = (int)detectConfig.ModelHeight;
// req.LabelNames = detectConfig.GetLabelNames();
// req.Score = IIConfig.Score;
req.mImage = originImgSet.Clone();
req.in_lable_path = detectConfig.in_lable_path;
req.confThreshold = detectConfig.ModelconfThreshold;
req.iouThreshold = 0.3f;
req.segmentWidth = 320;
req.out_node_name = "output0";
switch (detectConfig.ModelType)
{
case MLModelType.ImageClassification:
break;
case MLModelType.ObjectDetection:
break;
case MLModelType.SemanticSegmentation:
break;
case MLModelType.InstanceSegmentation:
break;
case MLModelType.ObjectGPUDetection:
break;
default:
break;
}
// LogAsync(DateTime.Now, LogLevel.Information, $"{detectConfig.Name} 产品{detectResult.TempPid} RunInference BEGIN");
mlWatch.Start();
//20230802改成多线程推理 RunInferenceFixed
var result = mlSet.StationMLEngine.RunInference(req);
// var result = mlSet.StationMLEngine.RunInferenceFixed(req);
mlWatch.Stop();
// LogAsync(DateTime.Now, LogLevel.Information, $"{detectConfig.Name} 产品{detectResult.TempPid} RunInference END");
// var req = new MLRequest();
//req.mImage = inferenceImage;
//req.ResizeWidth = detectConfig.ModelWidth;
//req.ResizeHeight = detectConfig.ModelHeight;
//req.confThreshold = detectConfig.ModelconfThreshold;
//req.iouThreshold = 0.3f;
//req.out_node_name = "output0";
//req.in_lable_path = detectConfig.in_lable_path;
//Stopwatch sw = Stopwatch.StartNew();
//var result = Dectection[detectionId].RunInference(req);
//sw.Stop();
//LogAsync(DateTime.Now, LogLevel.Information, $"{camera.Name} 推理进度1.1,产品{productNumber},耗时{sw.ElapsedMilliseconds}ms");
//this.BeginInvoke(new MethodInvoker(delegate ()
//{
// // pictureBox1.Image?.Dispose(); // 释放旧图像
// // pictureBox1.Image = result.ResultMap;
// richTextBox1.AppendText($"推理成功 {productNumber}, {result.IsSuccess}相机名字{camera.CameraName} 耗时 {mlWatch.ElapsedMilliseconds}ms\n");
//}));
//req.mImage?.Dispose();
if (result == null || (result != null && !result.IsSuccess))
{
detectResult.IsMLDetectDone = false;
}
if (result != null && result.IsSuccess)
{
detectResult.DetectDetails = result.ResultDetails;
if (detectResult.DetectDetails != null)
{
}
else
{
detectResult.IsMLDetectDone = false;
}
}
}
#endregion
#region 3.
#endregion
//根据那些得分大于阈值的推理结果,判断产品是否成功
#region 4.
detectResult.DetectDetails?.ForEach(d =>
{
//当前检测项的 过滤条件
//var conditionList = detectConfig.DetectionFilterList
// .Where(u => u.IsEnabled && u.LabelName == d.LabelName)
// .GroupBy(u => u.ResultState)
// .OrderBy(u => u.Key)
// .ToList();
//当前检测项的 过滤条件
var conditionList = detectConfig.DetectionFilterList
.Where(u => u.IsEnabled && u.LabelName == d.LabelName)
.GroupBy(u => u.ResultState)
.OrderBy(u => u.Key)
.ToList();
if (conditionList.Count == 0)
{
d.FinalResult = d.LabelName.ToLower() == "ok"
? ResultState.OK
: ResultState.DetectNG;
}
else
{
d.FinalResult = detectConfig.IsMixModel
? ResultState.A_NG
: ResultState.OK;
}
foreach (IGrouping<ResultState, DetectionFilter> group in conditionList)
{
//bool b = group.ToList().Any(f =>
//{
// return f.FilterOperation(d);
//});
//if (b)
//{
// d.FinalResult = group.Key;
// break;
//}
if (group.Any(f => f.FilterOperation(d)))
{
d.FinalResult = group.Key;
break;
}
//else
//{
// d.FinalResult = d.InferenceResult = ResultState.OK;
//}
}
});
#endregion
#region 5.NG
//if (detectResult.DetectDetails?.Count > 0)
//{
// detectResult.ResultState = detectResult.DetectDetails.GroupBy(u => u.FinalResult).OrderBy(u => u.Key).First().First().FinalResult;
// detectResult.ResultLabel = detectResult.ResultLabel;
// detectResult.ResultLabelCategoryId = detectResult.ResultLabel;//TODO:设置优先级
//}
detectResult.ResultState = detectResult.DetectDetails?
.GroupBy(u => u.FinalResult)
.OrderBy(u => u.Key)
.FirstOrDefault()?.Key ?? ResultState.OK;
detectResult.ResultLabel = detectResult.ResultLabel;
detectResult.ResultLabelCategoryId = detectResult.ResultLabel;//TODO:设置优先级
#endregion
DisplayDetectionResult(detectResult, originImgSet.Clone(), detectionId);
}
return detectResult;
}
/// <summary>
/// 初始化深度学习工具
/// </summary>
private bool InitialSimboMLEnginesAsync()
{
//深度学习 模型加载
var resultOK = MLLoadModel();
return resultOK;
}
/// <summary>
/// 深度学习 模型加载
/// </summary>
/// <returns></returns>
private bool MLLoadModel()
{
bool resultOK = false;
try
{
// SimboStationMLEngineList = new List<SimboStationMLEngineSet>();
// _cameraRelatedDetectionDict = IConfig.DetectionConfigs.Select(t => t.ModelPath).Distinct().ToList();
DetectionConfigs.ForEach(dc =>
//_cameraRelatedDetectionDict.ForEach(dc =>
{
if (dc.IsEnabled && !string.IsNullOrWhiteSpace(dc.ModelPath))
{
if (dc.IsEnableGPU)
{
//if (IIConfig.IsLockGPU)
//{
//foreach (var validGPU in ValidGPUList2)
//{
// if (validGPU.DetectionIds.Contains(dc.Id))
// {
var engine = SingleMLLoadModel(dc, true, 0);
SimboStationMLEngineList.Add(engine);
// }
//}
//}
//else
//{
// foreach (var validGPU in ValidGPUList)
// {
// //var validGPU = ValidGPUList.FirstOrDefault(u => u.DetectionIds.Contains(dc.Id));
// if (validGPU.DetectionId == dc.Id)
// {
// var engine = SingleMLLoadModel(dc, true, validGPU.GPUNo);
// SimboStationMLEngineList.Add(engine);
// }
// }
//}
}
else
{
//for (int i = 0; i < IConfig.CPUNums; i++)
for (int i = 0; i < 1; i++)
{
//var engine = SingleMLLoadModel(dc, false, i);
var engine = SingleMLLoadModel(dc, false, i);
SimboStationMLEngineList.Add(engine);
}
}
}
});
resultOK = true;
}
catch (Exception ex)
{
// LogAsync(DateTime.Now, LogLevel.Exception, $"异常:模型并发加载异常:{ex.GetExceptionMessage()}");
resultOK = false;
}
return resultOK;
}
/// <summary>
/// 单个模型加载
/// </summary>
/// <param name="dc"></param>
/// <param name="gpuNum"></param>
/// <returns></returns>
private SimboStationMLEngineSet SingleMLLoadModel(DetectionConfig dc, bool isGPU, int coreInx)
{
SimboStationMLEngineSet mLEngineSet = new SimboStationMLEngineSet();
try
{
mLEngineSet.IsUseGPU = isGPU;
if (isGPU)
{
mLEngineSet.GPUNo = coreInx;
}
else
{
mLEngineSet.CPUNo = coreInx;
}
mLEngineSet.DetectionId = dc.Id;
mLEngineSet.DetectionName = dc.Name;
if (!string.IsNullOrWhiteSpace(dc.ModelPath))
{
// 根据算法类型创建不同的实例
switch (dc.ModelType)
{
case MLModelType.ImageClassification:
break;
case MLModelType.ObjectDetection:
mLEngineSet.StationMLEngine = new SimboObjectDetection();
break;
case MLModelType.SemanticSegmentation:
break;
case MLModelType.InstanceSegmentation:
mLEngineSet.StationMLEngine = new SimboInstanceSegmentation();
break;
case MLModelType.ObjectGPUDetection:
mLEngineSet.StationMLEngine = new SimboDetection();
break;
default:
break;
}
MLInit mLInit;
string inferenceDevice = "CPU";
if (dc.IsEnableGPU)
{
inferenceDevice = "GPU";
mLInit = new MLInit(dc.ModelPath, isGPU, coreInx, dc.ModelconfThreshold);
}
else
{
mLInit = new MLInit(dc.ModelPath, "images", inferenceDevice, (int)dc.ModelWidth, (int)dc.ModelHeight);
}
bool isSuccess = mLEngineSet.StationMLEngine.Load(mLInit);
if (!isSuccess)
{
// throw new ProcessException("异常:模型加载异常", null);
}
//LogAsync(DateTime.Now, LogLevel.Information, $"模型加载成功是否GPU:{isGPU} CoreInx:{coreInx} - {dc.Name}" + $" {dc.ModelType.GetEnumDescription()}:{dc.ModelPath}");
}
}
catch (Exception ex)
{
//throw new ProcessException($"异常是否GPU:{isGPU} CoreInx:{coreInx} - {dc.Name}模型加载异常:{ex.GetExceptionMessage()}");
}
return mLEngineSet;
}
private void InitialHalconTools()
{
HOperatorSet.SetSystem("parallelize_operators", "true");
HOperatorSet.SetSystem("reentrant", "true");
HOperatorSet.SetSystem("global_mem_cache", "exclusive");
HalconToolDict = new Dictionary<string, HDevEngineTool>();
DetectionConfigs.ForEach(c =>
{
if (!c.IsEnabled)
return;
if (c.HalconAlgorithemPath_Pre != null)
LoadHalconTool(c.HalconAlgorithemPath_Pre);
});
}
private void LoadHalconTool(string path)
{
if (!HalconToolDict.ContainsKey(path))
{
string algorithemPath = path;
if (string.IsNullOrWhiteSpace(algorithemPath))
return;
string directoryPath = Path.GetDirectoryName(algorithemPath);
string fileName = Path.GetFileNameWithoutExtension(algorithemPath);
HDevEngineTool tool = new HDevEngineTool(directoryPath);
tool.LoadProcedure(fileName);
HalconToolDict[path] = tool;
}
}
/// <summary>
/// 预处理
/// </summary>
/// <param name="detectConfig"></param>
/// <param name="detectResult"></param>
public void PreTreated(DetectionConfig detectConfig, DetectStationResult detectResult, Mat MhImage)
{
try
{
// detectResult.VisionImageSet.DetectionOriginImage = detectResult.VisionImageSet.HImage.ConvertHImageToBitmap();
//detectResult.VisionImageSet.PreTreatedBitmap = detectResult.VisionImageSet.HImage.ConvertHImageToBitmap();
//detectResult.VisionImageSet.DetectionResultImage = detectResult.VisionImageSet.PreTreatedBitmap?.CopyBitmap();
if (!string.IsNullOrWhiteSpace(detectConfig.HalconAlgorithemPath_Pre))
{
HObject obj = OpenCVHelper.MatToHImage(MhImage);
HImage hImage = HalconHelper.ConvertHObjectToHImage(obj);
string toolKey = detectConfig.HalconAlgorithemPath_Pre;
if (!HalconToolDict.ContainsKey(toolKey))
{
// LogAsync(DateTime.Now, LogLevel.Exception, $"{detectConfig.Name}未获取预处理算法");
return;
}
//Mean_Thre Deviation_Thre Mean_standard Deviation_standard
var tool = HalconToolDict[toolKey];
////tool.InputTupleDic["Mean_Thre"] = 123;
for (int i = 0; i < detectConfig.PreTreatParams.Count; i++)
{
var param = detectConfig.PreTreatParams[i];
tool.InputTupleDic[param.Name] = double.Parse(param.Value);
}
// tool.InputTupleDic["fCricularity"] = 200;
tool.InputImageDic["INPUT_Image"] = hImage;
if (!tool.RunProcedure(out string errorMsg, out _))
{
// detectResult.PreTreatedFlag = false;
detectResult.IsPreTreatDone = false;
return;
}
var preTreatRet = tool.GetResultTuple("OUTPUT_Flag").I;
//var fRCricularity = tool.GetResultTuple("fRCricularity");
// detectResult.IsPreTreatDone = detectResult.VisionImageSet.PreTreatedFlag = preTreatRet == 1;
//detectResult.IsPreTreatDone = detectResult.VisionImageSet.PreTreatedFlag = true;
// detectResult.VisionImageSet.PreTreatedTime = DateTime.Now;
for (int i = 0; i < detectConfig.OUTPreTreatParams.Count; i++)
{
var param = detectConfig.OUTPreTreatParams[i];
tool.InputTupleDic[param.Name] = double.Parse(param.Value);
}
// 2023/10/16 新增预处理结果反馈如果预处理结果为NG直接返回
if (preTreatRet != 0)
{
detectResult.ResultState = ResultState.DetectNG;
detectResult.IsPreTreatNG = true;
// if (detectResult.VisionImageSet.PreTreatedFlag)
{
//detectResult.VisionImageSet.MLImage = tool.GetResultObject("OUTPUT_PreTreatedImage");
//DetectionResultImage
// detectResult.VisionImageSet.DetectionResultImage = detectResult.VisionImageSet.MLImage.ConvertHImageToBitmap();
}
}
else
{
// detectResult.VisionImageSet.DetectionResultImage = detectResult.VisionImageSet.MLImage.ConvertHImageToBitmap();
}
}
}
catch (Exception ex)
{
}
finally
{
//detectResult.VisionImageSet.HImage?.Dispose();
//detectResult.VisionImageSet.HImage = null;
// MhImage?.Dispose();
//MhImage = null;
}
}
/// <summary>
/// 显示检测结果
/// </summary>
/// <param name="detectResult"></param>
private void DisplayDetectionResult(DetectStationResult detectResult,Mat result,string DetectionId)
{
//结果显示上传
Task.Run(() =>
{
try
{
string displayTxt = detectResult.ResultState.ToString() + "\r\n";
if (detectResult.DetectDetails != null && detectResult.DetectDetails?.Count > 0)
{
detectResult.DetectDetails.ForEach(d =>
{
displayTxt +=
$"{d.LabelName} score:{d.Score.ToString("f2")} area:{d.Area.ToString("f2")}\r\n";
});
}
//if (detectResult.realSpecs != null && detectResult.realSpecs?.Count > 0)
//{
// detectResult.realSpecs.ForEach(d =>
// {
// displayTxt +=
// $"{d.Code} :{d.ActualValue} \r\n";
// });
//}
Bitmap resultMask=result.ToBitmap();
//if (detectResult.VisionImageSet.DetectionResultImage == null && detectResult.VisionImageSet.SizeResultImage == null)
//{
// return;
//}
//else if (detectResult.VisionImageSet.DetectionResultImage == null && detectResult.VisionImageSet.SizeResultImage != null)
//{
// detectResult.VisionImageSet.DetectionResultImage = detectResult.VisionImageSet.SizeResultImage.CopyBitmap();
// resultMask = detectResult.VisionImageSet.DetectionResultImage.CopyBitmap();
//}
//else if (detectResult.VisionImageSet.DetectionResultImage != null && detectResult.VisionImageSet.SizeResultImage != null)
//{
// Mat img1 = ConvertBitmapToMat(detectResult.VisionImageSet.SizeResultImage.CopyBitmap()); // 第一张图片,已经带框
// Mat img2 = ConvertBitmapToMat(detectResult.VisionImageSet.DetectionResultImage.CopyBitmap()); // 第二张图片,已经带框
// // 合成两张图像:可以选择叠加或拼接
// Mat resultImg = new Mat();
// Cv2.AddWeighted(img1, 0.5, img2, 0.5, 0, resultImg); // 使用加权平均法合成图像
// resultMask = resultImg.ToBitmap();
//}
//else
//{
// resultMask = detectResult.VisionImageSet.DetectionResultImage.CopyBitmap();
//}
List<IShapeElement> detectionResultShapes =
new List<IShapeElement>(detectResult.DetectionResultShapes);
DetectResultDisplay resultDisplay = new DetectResultDisplay(detectResult, resultMask, displayTxt);
detectionResultShapes.Add(resultDisplay);
List<IShapeElement> detectionResultShapesClone = new List<IShapeElement>(detectionResultShapes);
DetectionDone(DetectionId, resultMask, detectionResultShapes);
//SaveDetectResultImageAsync(detectResult);
// SaveDetectResultCSVAsync(detectResult);
}
catch (Exception ex)
{
// LogAsync(DateTime.Now, LogLevel.Exception,
// $"{Name}显示{detectResult.DetectName}检测结果异常,{ex.GetExceptionMessage()}");
}
finally
{
}
});
}
}
}

View File

@ -1,4 +1,5 @@

using DH.Commons.Enums;
using OpenCvSharp;
using System;
using System.Collections.Generic;
@ -28,7 +29,7 @@ namespace DH.Devices.Vision
MLGPUEngine.FreePredictor(Model);
}
catch (Exception e) { }
// MLEngine.FreePredictor(Model);
// MLEngine.FreePredictor(Model);
}
public void Dispose2()
{
@ -41,7 +42,7 @@ namespace DH.Devices.Vision
}
public SimboVisionMLBase()
{
// ColorMap = OpenCVHelper.GetColorMap(256);//使用3个通道
ColorMap = OpenCVHelper.GetColorMap(256);//使用3个通道
// ColorLut = new Mat(1, 256, MatType.CV_8UC3, ColorMap);
}
}
@ -55,16 +56,22 @@ namespace DH.Devices.Vision
// "rect": [421, 823, 6, 8]
// }]
//}
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public List<Result> HYolo;
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public class Result
{
public double fScore;
public int classId;
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public string classname;
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
//public double area;
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public List<int> rect;
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
}
@ -72,45 +79,28 @@ namespace DH.Devices.Vision
}
public class SegResult
{
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public List<Result> SegmentResult;
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public class Result
{
public double fScore;
public int classId;
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public string classname;
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public double area;
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public List<int> rect;
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
}
}
public class PreTreatParam
{
/// <summary>
/// 参数名称
/// </summary>
///
[Category("预处理参数")]
[DisplayName("参数名称")]
[Description("参数名称")]
public string Name { get; set; }
/// <summary>
/// 参数值
/// </summary>
///
[Category("预处理参数")]
[DisplayName("参数值")]
[Description("参数值")]
public string Value { get; set; }
}
public static class MLGPUEngine
{

View File

@ -22,14 +22,20 @@ namespace DH.Devices.Vision
/// <summary>
/// 检测配置ID
/// </summary>
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public string DetectionId { get; set; }
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public string DetectionName { get; set; }
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
/// <summary>
/// 深度学习模型
/// </summary>
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
public SimboVisionMLBase StationMLEngine { get; set; }
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
}
}