增加日志
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
using DH.Commons.Base;
|
||||
using DH.Commons.Enums;
|
||||
using DH.Commons.Helper;
|
||||
using DH.UI.Model.Winform;
|
||||
using HalconDotNet;
|
||||
using OpenCvSharp;
|
||||
@ -15,6 +16,7 @@ using System.Windows.Forms;
|
||||
using System.Xml.Linq;
|
||||
using XKRS.UI.Model.Winform;
|
||||
using static DH.Commons.Enums.EnumHelper;
|
||||
using LogLevel = DH.Commons.Enums.EnumHelper.LogLevel;
|
||||
using ResultState = DH.Commons.Base.ResultState;
|
||||
|
||||
|
||||
@ -70,16 +72,21 @@ namespace DH.Devices.Vision
|
||||
|
||||
if (detectConfig == null)
|
||||
{
|
||||
|
||||
LogAsync(DateTime.Now, LogLevel.Exception, $"异常:未能获取检测配置");
|
||||
//未能获得检测配置
|
||||
return detectResult;
|
||||
}
|
||||
Stopwatch sw = new Stopwatch();
|
||||
#region 1.预处理
|
||||
|
||||
sw.Start();
|
||||
using (Mat PreTMat = originImgSet.Clone())
|
||||
{
|
||||
PreTreated(detectConfig, detectResult, PreTMat);
|
||||
PreTreated2(detectConfig, detectResult, PreTMat);
|
||||
}
|
||||
sw.Stop();
|
||||
LogAsync(DateTime.Now, LogLevel.Information, $"产品:{detectResult.Pid} {detectConfig.Name}预处理耗时:{sw.ElapsedMilliseconds}ms。SpecsResults:{string.Join(";", detectResult.realSpecs.Select(u => $"{u.Code} {u.ActualValue}"))}");
|
||||
// 工位2尺寸测量
|
||||
|
||||
|
||||
|
||||
@ -111,7 +118,7 @@ namespace DH.Devices.Vision
|
||||
}
|
||||
|
||||
#region 2.深度学习推理
|
||||
//LogAsync(DateTime.Now, LogLevel.Information, $"{detectConfig.Name} 产品{detectResult.TempPid} 模型检测执行");
|
||||
LogAsync(DateTime.Now, LogLevel.Information, $"{detectConfig.Name} 产品{detectResult.TempPid} 模型检测执行");
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(detectConfig.ModelPath))
|
||||
{
|
||||
@ -193,6 +200,8 @@ namespace DH.Devices.Vision
|
||||
|
||||
if (result == null || (result != null && !result.IsSuccess))
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Exception, $"CoreInx:{mlSet.GPUNo} 产品:{detectResult.Pid} {detectConfig.Name} 模型检测异常,{result?.ResultMessage}");
|
||||
|
||||
detectResult.IsMLDetectDone = false;
|
||||
}
|
||||
if (result != null && result.IsSuccess)
|
||||
@ -200,9 +209,13 @@ namespace DH.Devices.Vision
|
||||
detectResult.DetectDetails = result.ResultDetails;
|
||||
if (detectResult.DetectDetails != null)
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Information, $"CoreInx:{mlSet.GPUNo} 产品:{detectResult.Pid} {detectResult.DetectName} 模型检测总耗时:{mlWatch.ElapsedMilliseconds} ms {result.ResultMessage},{string.Join(";", detectResult.DetectDetails.Select(u => $"{u.LabelName} X:{u.Rect.X} Y:{u.Rect.Y} Area:{u.Area.ToString("f2")} W:{u.Rect.Width} H:{u.Rect.Height}"))}");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Exception, $"CoreInx:{mlSet.GPUNo} 产品:{detectResult.Pid} {detectConfig.Name} 模型检测异常返回 null");
|
||||
|
||||
detectResult.IsMLDetectDone = false;
|
||||
}
|
||||
}
|
||||
@ -280,6 +293,7 @@ namespace DH.Devices.Vision
|
||||
#endregion
|
||||
|
||||
|
||||
LogAsync(DateTime.Now, LogLevel.Information, $"{detectConfig.Name} 检测结果:{detectResult.ResultState.GetEnumDescription()}");
|
||||
|
||||
|
||||
|
||||
@ -430,11 +444,13 @@ namespace DH.Devices.Vision
|
||||
{
|
||||
// throw new ProcessException("异常:模型加载异常", null);
|
||||
}
|
||||
//LogAsync(DateTime.Now, LogLevel.Information, $"模型加载成功;是否GPU:{isGPU} CoreInx:{coreInx} - {dc.Name}" + $" {dc.ModelType.GetEnumDescription()}:{dc.ModelPath}");
|
||||
LogAsync(DateTime.Now, LogLevel.Information, $"模型加载成功;是否GPU:{isGPU} CoreInx:{coreInx} - {dc.Name}" + $" {dc.ModelType.GetEnumDescription()}:{dc.ModelPath}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Error, $"模型加载成功;是否GPU:{isGPU} CoreInx:{coreInx} - {dc.Name}" + $" {dc.ModelType.GetEnumDescription()}:{dc.ModelPath}");
|
||||
|
||||
//throw new ProcessException($"异常:是否GPU:{isGPU} CoreInx:{coreInx} - {dc.Name}模型加载异常:{ex.GetExceptionMessage()}");
|
||||
}
|
||||
return mLEngineSet;
|
||||
@ -519,7 +535,7 @@ namespace DH.Devices.Vision
|
||||
if (!tool.RunProcedure(out string errorMsg, out _))
|
||||
{
|
||||
// detectResult.PreTreatedFlag = false;
|
||||
|
||||
LogAsync(DateTime.Now, LogLevel.Exception, $"{detectConfig.Name}预处理异常,{errorMsg}");
|
||||
detectResult.IsPreTreatDone = false;
|
||||
|
||||
|
||||
@ -534,12 +550,82 @@ namespace DH.Devices.Vision
|
||||
// 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++)
|
||||
switch (preTreatRet)
|
||||
{
|
||||
var param = detectConfig.OUTPreTreatParams[i];
|
||||
tool.InputTupleDic[param.Name] = double.Parse(param.Value);
|
||||
case 0: // 预处理算法无异常
|
||||
{
|
||||
|
||||
for (int i = 0; i < detectConfig.OUTPreTreatParams.Count; i++)
|
||||
{
|
||||
|
||||
var param = detectConfig.OUTPreTreatParams[i];
|
||||
|
||||
var Value = tool.GetResultTuple(param.Name);
|
||||
|
||||
// 显示结果
|
||||
IndexedSpec specRCricularity = new()
|
||||
{
|
||||
Code = param.Name,
|
||||
ActualValue = Value
|
||||
};
|
||||
detectResult.realSpecs.Add(specRCricularity);
|
||||
}
|
||||
detectResult.IsPreTreatNG = false;
|
||||
detectResult.IsPreTreatDone = true;
|
||||
}
|
||||
break;
|
||||
case -111: // 检测结果为NG
|
||||
{
|
||||
/// detectResult.VisionImageSet.DetectionResultImage =
|
||||
// tool.GetResultObject("OUTPUT_PreTreatedImage").ConvertHImageToBitmap();
|
||||
for (int i = 0; i < detectConfig.OUTPreTreatParams.Count; i++)
|
||||
{
|
||||
|
||||
var param = detectConfig.OUTPreTreatParams[i];
|
||||
|
||||
var Value = tool.GetResultTuple(param.Name);
|
||||
|
||||
// 显示结果
|
||||
IndexedSpec specRCricularity = new()
|
||||
{
|
||||
Code = param.Name,
|
||||
ActualValue = Value
|
||||
};
|
||||
detectResult.realSpecs.Add(specRCricularity);
|
||||
}
|
||||
// 结果为NG
|
||||
detectResult.ResultState = ResultState.DetectNG;
|
||||
detectResult.IsPreTreatNG = true;
|
||||
detectResult.IsPreTreatDone = true;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
case -999: // 算法异常
|
||||
{
|
||||
// 算法异常时,结果图
|
||||
// detectResult.VisionImageSet.DetectionResultImage =
|
||||
// tool.GetResultObject("OUTPUT_PreTreatedImage").ConvertHImageToBitmap();
|
||||
|
||||
for (int i = 0; i < detectConfig.OUTPreTreatParams.Count; i++)
|
||||
{
|
||||
|
||||
|
||||
var param = detectConfig.OUTPreTreatParams[i];
|
||||
// 显示结果
|
||||
IndexedSpec specRCricularity = new()
|
||||
{
|
||||
Code = param.Name,
|
||||
ActualValue = -1,
|
||||
};
|
||||
detectResult.realSpecs.Add(specRCricularity);
|
||||
}
|
||||
|
||||
// 结果保持TBD
|
||||
detectResult.IsPreTreatDone = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ -571,7 +657,7 @@ namespace DH.Devices.Vision
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
LogAsync(DateTime.Now, LogLevel.Exception, $"{detectConfig.Name}预处理异常:{ex.GetExceptionMessage()}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
@ -582,6 +668,266 @@ namespace DH.Devices.Vision
|
||||
}
|
||||
|
||||
}
|
||||
public void PreTreated2(DetectionConfig detectConfig, DetectStationResult detectResult,Mat MhImage)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
if (detectConfig.SizeTreatParamList != null && detectConfig.SizeTreatParamList.Count > 0)
|
||||
{
|
||||
foreach (var preTreat in detectConfig.SizeTreatParamList)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(preTreat.PrePath))
|
||||
{
|
||||
string toolKey = preTreat.PrePath;
|
||||
if (!HalconToolDict.ContainsKey(toolKey))
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Exception, $"{detectConfig.Name}未获取预处理{preTreat.PreName}算法");
|
||||
return;
|
||||
}
|
||||
//Mean_Thre Deviation_Thre Mean_standard Deviation_standard
|
||||
var tool = HalconToolDict[toolKey];
|
||||
|
||||
//tool.InputTupleDic["Mean_Thre"] = 123;
|
||||
List<PreTreatParam> PreParams = new List<PreTreatParam>();
|
||||
preoutparms(PreParams, preTreat.ResultShow);
|
||||
for (int i = 0; i < PreParams.Count(); i++)
|
||||
{
|
||||
var param = PreParams[i];
|
||||
if (param.Value.Contains(","))
|
||||
{
|
||||
string[] strings = param.Value.Split(",");
|
||||
float[] array = strings.Select(s => float.Parse(s)).ToArray();
|
||||
HTuple hTupleArray = new HTuple(array);
|
||||
|
||||
tool.InputTupleDic[param.Name] = hTupleArray;
|
||||
}
|
||||
else
|
||||
{
|
||||
tool.InputTupleDic[param.Name] = double.Parse(param.Value);// param.Value.ToInt();
|
||||
}
|
||||
|
||||
IndexedSpec spec1 = new IndexedSpec();
|
||||
|
||||
switch (preTreat.PreType)
|
||||
{
|
||||
case SizeEnum.线线测量:
|
||||
|
||||
break;
|
||||
case SizeEnum.线圆测量:
|
||||
|
||||
break;
|
||||
case SizeEnum.圆形测量:
|
||||
break;
|
||||
case SizeEnum.高度测量:
|
||||
spec1.Code = $"in-{param.Name}";
|
||||
spec1.ActualValue = double.Parse(param.Value);
|
||||
break;
|
||||
case SizeEnum.直线测量:
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
|
||||
detectResult.realSpecs.Add(spec1);
|
||||
}
|
||||
//// 指定保存路径
|
||||
//string filePath = @"D:\saved_image.jpg"; // 你可以根据需要更改路径和文件名
|
||||
|
||||
//// 使用WriteImage保存图像
|
||||
//detectResult.VisionImageSet.HImage.WriteImage("jpeg", 0, filePath); // "jpeg" 表示图像格式,0表示不使用压缩
|
||||
HObject obj = OpenCVHelper.MatToHImage(MhImage);
|
||||
HImage hImage = HalconHelper.ConvertHObjectToHImage(obj);
|
||||
tool.InputImageDic["INPUT_Image"] = hImage;
|
||||
if (!tool.RunProcedure(out string errorMsg, out _))
|
||||
{
|
||||
// detectResult.VisionImageSet.PreTreatedFlag = false;
|
||||
LogAsync(DateTime.Now, LogLevel.Exception, $"{detectConfig.Name}预处理异常,{errorMsg}");
|
||||
detectResult.IsPreTreatDone = false;
|
||||
|
||||
// HandleDetectDone(detectResult, detectConfig);
|
||||
return;
|
||||
}
|
||||
|
||||
var preTreatRet = tool.GetResultTuple("OUTPUT_Flag").I;
|
||||
//double MatchScore = 1;
|
||||
//MatchScore = tool.GetResultTuple("MatchScore");
|
||||
|
||||
|
||||
detectResult.IsPreTreatDone = preTreatRet == 1;
|
||||
// detectResult.IsPreTreatDone = detectResult.VisionImageSet.PreTreatedFlag = true;
|
||||
// detectResult.VisionImageSet.PreTreatedTime = DateTime.Now;
|
||||
|
||||
|
||||
//IndexedSpec spec1 = new IndexedSpec();
|
||||
//spec1.Code = "score";
|
||||
//spec1.ActualValue = MatchScore;
|
||||
|
||||
|
||||
//detectResult.realSpecs.Add(spec1);
|
||||
;
|
||||
|
||||
|
||||
// 2023/10/16 新增预处理结果反馈,如果预处理结果为NG,直接返回
|
||||
if (preTreatRet != 0)
|
||||
{
|
||||
detectResult.ResultState = ResultState.DetectNG;
|
||||
|
||||
detectResult.IsPreTreatNG = true;
|
||||
// detectResult.VisionImageSet.DetectionResultImage = detectResult.VisionImageSet.MLImage.ConvertHImageToBitmap();
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
switch (preTreat.PreType)
|
||||
{
|
||||
case SizeEnum.线线测量:
|
||||
isPreOutparams(ref detectResult, preTreat, tool, ref preTreatRet);
|
||||
break;
|
||||
case SizeEnum.线圆测量:
|
||||
isPreOutparams(ref detectResult, preTreat, tool, ref preTreatRet);
|
||||
break;
|
||||
case SizeEnum.圆形测量:
|
||||
break;
|
||||
case SizeEnum.高度测量:
|
||||
{
|
||||
|
||||
isPreOutparams(ref detectResult, preTreat, tool, ref preTreatRet);
|
||||
}
|
||||
break;
|
||||
case SizeEnum.直线测量:
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogAsync(DateTime.Now, LogLevel.Exception, $"{detectConfig.Name}尺寸预处理异常:{ex.GetExceptionMessage()}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
//detectResult.VisionImageSet.HImage?.Dispose();
|
||||
//detectResult.VisionImageSet.HImage = null;
|
||||
}
|
||||
}
|
||||
public void isPreOutparams(ref DetectStationResult detectResult, SizeTreatParam preTreat, HDevEngineTool tool, ref int preTreatRet)
|
||||
{
|
||||
List<PreTreatParam> PreParams = new List<PreTreatParam>();
|
||||
preoutparms(PreParams, preTreat.OutResultShow);
|
||||
|
||||
for (int i = 0; i < PreParams.Count; i++)
|
||||
{
|
||||
var param = PreParams[i];
|
||||
double dParam = double.Parse(param.Value);
|
||||
double heights = tool.GetResultTuple(param.Name).D;
|
||||
switch (preTreat.PreType)
|
||||
{
|
||||
case SizeEnum.高度测量:
|
||||
|
||||
|
||||
IndexedSpec spec2 = new IndexedSpec();
|
||||
spec2.Code = $"out-{param.Name}";
|
||||
spec2.ActualValue = Convert.ToDouble(param.Value);
|
||||
|
||||
|
||||
detectResult.realSpecs.Add(spec2);
|
||||
|
||||
|
||||
IndexedSpec spec1 = new IndexedSpec();
|
||||
spec1.Code = $"actual-{param.Name}";
|
||||
spec1.ActualValue = heights;
|
||||
|
||||
|
||||
detectResult.realSpecs.Add(spec1);
|
||||
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if ((heights > dParam - preTreat.PrePix) && (heights < dParam + preTreat.PrePix))
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
preTreatRet = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (preTreatRet != 0)
|
||||
{
|
||||
detectResult.ResultState = ResultState.DetectNG;
|
||||
|
||||
detectResult.IsPreTreatNG = true;
|
||||
//detectResult.VisionImageSet.DetectionResultImage = detectResult.VisionImageSet.MLImage.ConvertHImageToBitmap();
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
// detectResult.VisionImageSet.DetectionResultImage = detectResult.VisionImageSet.MLImage.ConvertHImageToBitmap();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void preoutparms(List<PreTreatParam> preTreatParams, string defectRows)
|
||||
{
|
||||
// 解析 ReslutShow 字符串,构建 PreTreatParam 列表
|
||||
if (!string.IsNullOrEmpty(defectRows))
|
||||
{
|
||||
var keyValuePairs = defectRows.Split(';');
|
||||
|
||||
foreach (var pair in keyValuePairs)
|
||||
{
|
||||
var parts = pair.Split(':');
|
||||
if (parts.Length == 2)
|
||||
{
|
||||
PreTreatParam param;
|
||||
if (parts[1].Trim().Contains(","))
|
||||
{
|
||||
param = new PreTreatParam
|
||||
{
|
||||
Name = parts[0].Trim(), // 去除多余的空格
|
||||
Value = parts[1].Trim() // 转换为 double,失败则为0
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
double dvalue = double.TryParse(parts[1].Trim(), out double value) ? value : 0;
|
||||
|
||||
param = new PreTreatParam
|
||||
{
|
||||
Name = parts[0].Trim(), // 去除多余的空格
|
||||
Value = dvalue.ToString() // 转换为 double,失败则为0
|
||||
};
|
||||
}
|
||||
|
||||
preTreatParams.Add(param);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示检测结果
|
||||
@ -654,8 +1000,8 @@ namespace DH.Devices.Vision
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// LogAsync(DateTime.Now, LogLevel.Exception,
|
||||
// $"{Name}显示{detectResult.DetectName}检测结果异常,{ex.GetExceptionMessage()}");
|
||||
LogAsync(DateTime.Now, LogLevel.Exception,
|
||||
$"{Name}显示{detectResult.DetectName}检测结果异常,{ex.GetExceptionMessage()}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
Reference in New Issue
Block a user