小转盘基本都能出列

This commit is contained in:
2025-03-10 17:18:45 +08:00
parent 0314f4d403
commit e7736217db
17 changed files with 6712 additions and 299 deletions

View File

@ -17,6 +17,8 @@ using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static AntdUI.Math3D;
@ -209,7 +211,7 @@ namespace DHSoftware
public List<HikVisionCamera> HKCameras { get; } = new List<HikVisionCamera>();
public List<Do3ThinkCamera> Cameras { get; } = new List<Do3ThinkCamera>();
public Dictionary<string, SimboObjectDetection> Dectection { get; } = new Dictionary<string, SimboObjectDetection>();
public XinJEPLCTcpNet PLC { get; } = new XinJEPLCTcpNet();
@ -224,7 +226,10 @@ namespace DHSoftware
{
camera.CameraDisConnect();
}
foreach (var camera in HKCameras)
{
camera.CameraDisConnect();
}
PLC.PLCDisConnect();
}
@ -339,6 +344,7 @@ namespace DHSoftware
detectionList.Add(det3);
detectionList.Add(det4);
Cameras.Clear();
HKCameras.Clear();
Dectection.Clear();
_cameraRelatedDetectionDict = new();
@ -365,7 +371,34 @@ namespace DHSoftware
}
);
});
string inferenceDevice = "CPU";
//for (int i = 1; i <= 8; i++)
//{
// HikVisionCamera camera = new HikVisionCamera();
// camera.CameraName = $"Cam{i}";
// camera.CameraIP = $"192.168.{i}.1";
// camera.ComputerIP = $"192.168.{i}.1";
// camera.CameraConnect();
// camera.OnHImageOutput += OnCameraHImageOutput;
// HKCameras.Add(camera);
// var simbo_1 = new SimboObjectDetection
// {
// };
// MLInit mLInit_1;
// mLInit_1 = new MLInit($"D:\\PROJECTS\\MaodingTest1\\Vision\\cam{i}.onnx", "images", inferenceDevice, 640, 640);
// simbo_1.Load(mLInit_1);
// Dectection.Add(camera.CameraName, simbo_1);
//}
//Add the code for the "启动" button click here
Do3ThinkCamera do3ThinkCamera1 = new Do3ThinkCamera();
@ -383,7 +416,7 @@ namespace DHSoftware
do3ThinkCamera2.OnHImageOutput += OnCameraHImageOutput;
var simbo1 = new SimboObjectDetection();
MLInit mLInit;
string inferenceDevice = "CPU";
mLInit = new MLInit($"D:\\PROJECTS\\MaodingTest1\\Vision\\cam1.onnx", "images", inferenceDevice, 640, 640);
@ -395,9 +428,9 @@ namespace DHSoftware
var simbo2 = new SimboObjectDetection();
MLInit mLInit2;
string inferenceDevice2 = "CPU";
mLInit2 = new MLInit($"D:\\PROJECTS\\MaodingTest1\\Vision\\cam2.onnx", "images", inferenceDevice2, 640, 640);
mLInit2 = new MLInit($"D:\\PROJECTS\\MaodingTest1\\Vision\\cam2.onnx", "images", inferenceDevice, 640, 640);
simbo2.Load(mLInit2);
@ -479,6 +512,7 @@ namespace DHSoftware
{
using (Mat localImageSet = imageSet.Clone()) // 复制 Mat 避免并发问题
{
// imageSet?.Dispose();
// 拍照计数与物件编号一致,查找对应的产品
ProductData product = null;
//内外壁模组多个相机的处理方法
@ -558,190 +592,148 @@ namespace DHSoftware
return;
}
#region 1.
#endregion
#region 2.
var req = new MLRequest();
req.mImage = localImageSet.Clone();
//req.ResizeWidth = detectConfig.ModelWidth;
//req.ResizeHeight = detectConfig.ModelHeight;
//req.confThreshold = detectConfig.ModelconfThreshold;
//req.iouThreshold = 0.3f;
//req.out_node_name = detectConfig.ModeloutNodeName;
//req.in_lable_path = detectConfig.in_lable_path;
req.ResizeWidth = 640;
req.ResizeHeight = 640;
req.confThreshold = 0.5f;
req.iouThreshold = 0.3f;
req.out_node_name = "output0";
req.in_lable_path = "D:\\PROJECTS\\MaodingTest1\\Vision\\cam1.txt";
//req.LabelNames = dc.GetLabelNames();
//HOperatorSet.WriteImage(req.HImage, "png", 0, @"D:\\666.png");
Stopwatch
sw = new Stopwatch();
sw.Start();
var result = Dectection[detectionId].RunInference(req);
sw.Stop();
//LogAsync(DateTime.Now, LogLevel.Information, $"{camera.Name} 推理进度1.1,产品{productNumber},耗时{sw.ElapsedMilliseconds}ms");
#endregion
this.BeginInvoke(new MethodInvoker(delegate ()
// 1. 预处理
using (Mat inferenceImage = localImageSet.Clone()) // 仅在此处克隆,确保推理过程中 Mat 有独立副本
{
if (pictureBox1.Image != null)
#region 1.
#endregion
#region 2.
var req = new MLRequest();
req.mImage = inferenceImage;
req.ResizeWidth = 640;
req.ResizeHeight = 640;
req.confThreshold = 0.5f;
req.iouThreshold = 0.3f;
req.out_node_name = "output0";
req.in_lable_path = "D:\\PROJECTS\\MaodingTest1\\Vision\\cam1.txt";
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");
#endregion
this.BeginInvoke(new MethodInvoker(delegate ()
{
pictureBox1.Image.Dispose(); // 释放旧图像
}
pictureBox1.Image = result.ResultMap;
richTextBox1.AppendText($"推理成功 {productNumber}, {result.IsSuccess} 耗时 {sw.ElapsedMilliseconds}ms\n");
}));
req.mImage.Dispose();
pictureBox1.Image?.Dispose(); // 释放旧图像
pictureBox1.Image = result.ResultMap;
richTextBox1.AppendText($"推理成功 {productNumber}, {result.IsSuccess} 耗时 {sw.ElapsedMilliseconds}ms\n");
}));
req.mImage.Dispose();
#if true
#region 3.
DetectStationResult detectResult = new DetectStationResult();
if (result == null || (result != null && !result.IsSuccess))
{
detectResult.IsMLDetectDone = false;
}
if (result != null && result.IsSuccess)
{
detectResult.DetectDetails = result.ResultDetails;
if (detectResult.DetectDetails != null)
{
}
else
#region 3.
DetectStationResult detectResult = new DetectStationResult();
if (result == null || (result != null && !result.IsSuccess))
{
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)
if (result != null && result.IsSuccess)
{
if (d.LabelName.ToLower() == "ok")
detectResult.DetectDetails = result.ResultDetails;
if (detectResult.DetectDetails != null)
{
d.FinalResult = d.InferenceResult = ResultState.OK;
}
else
{
d.FinalResult = d.InferenceResult = ResultState.DetectNG;
detectResult.IsMLDetectDone = false;
}
}
else
#endregion
#region 3.
#endregion
//根据那些得分大于阈值的推理结果,判断产品是否成功
#region 4.
detectResult.DetectDetails?.ForEach(d =>
{
if (detectConfig.IsMixModel)
//当前检测项的 过滤条件
//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.InferenceResult = ResultState.A_NG;
d.FinalResult = d.LabelName.ToLower() == "ok"
? ResultState.OK
: ResultState.DetectNG;
}
else
{
//将所有已将筛选出来的缺陷进行过滤
d.FinalResult = d.InferenceResult = ResultState.OK;
d.FinalResult = detectConfig.IsMixModel
? ResultState.A_NG
: ResultState.OK;
}
}
foreach (IGrouping<ResultState, DetectionFilter> group in conditionList)
{
bool b = group.ToList().Any(f =>
foreach (IGrouping<ResultState, DetectionFilter> group in conditionList)
{
return f.FilterOperation(d);
});
//bool b = group.ToList().Any(f =>
//{
// return f.FilterOperation(d);
//});
if (b)
{
d.FinalResult = group.Key;
break;
//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;
//}
}
//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;
});
#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:设置优先级
//////根据优先级设置ResultLabel
//if (detectionLabels.Count > 0)
//{
// foreach (var l in detectionLabels)
// {
// var isExist = DetectDetails.Any(o => NormalizeAndClean(o.LabelName) == NormalizeAndClean(l.LabelName) && o.FinalResult == ResultState.DetectNG);
// if (isExist)
// {
// ResultLabelCategoryId = l.LabelCategoryId;
// break;
// }
// }
//}
#endregion
resultStates.Add(detectResult.ResultState);
return;
}
#endregion
resultStates.Add(detectResult.ResultState);
product.ResultCollection.Add(detectResult);
product.ResultCollection.Add(detectResult);
#endif
//DetectStationResult temp;
////LogAsync(DateTime.Now, LogLevel.Information, $"{camera.Name} 推理进度1.4,产品{productNumber}");
//// 检测结果
//if (temp != null)
//{
// // LogAsync(DateTime.Now, LogLevel.Information, $"{camera.Name} 推理进度1.5,产品{productNumber}");
// //var totalElapsed = (temp.EndTime - temp.VisionImageSet.ImageTime).TotalMilliseconds;
// //totalTime += totalElapsed;
// resultStates.Add(temp.ResultState);
// product.ResultCollection.Add(temp);
// // LogAsync(DateTime.Now, LogLevel.Information, $"{camera.Name} 推理进度1.6,产品{productNumber}");
//}
//else
//{
// //LogAsync(DateTime.Now, LogLevel.Information, $"{camera.Name} 检测失败,物料编号:{productNumber},检测项:{d}");
//}
// imageSet.Dispose();
}
}
catch (Exception ex)
{
@ -749,15 +741,9 @@ namespace DHSoftware
}
}
//detectionDict.ForEach(d =>
//{
//});
// LogAsync(DateTime.Now, LogLevel.Information, $"{camera.Name} 推理进度2产品{productNumber}");
imageSet.Dispose();
product.InferenceOne(() =>
{
;
@ -767,7 +753,7 @@ namespace DHSoftware
});
// LogAsync(DateTime.Now, LogLevel.Information, $"{camera.Name} 推理完成,产品{productNumber}");
if (!product.InferenceFinished())
{
@ -786,24 +772,12 @@ namespace DHSoftware
richTextBox1.SelectionStart = richTextBox1.TextLength;
richTextBox1.ScrollToCaret();
}));
#region 6.
if (product.ResultCollection.Any(u => u.ResultState != ResultState.OK))
{
//检测结果TBD
// CurTrigger = TriggerSettings.FirstOrDefault(u => u.TriggerType == TriggerType.B_NG);
product.ProductResult = ResultState.B_NG;
product.ProductLabelCategory = ResultState.B_NG.GetEnumDescription();
product.ProductLabel = ResultState.B_NG.GetEnumDescription();
}
else
{
// CurTrigger = TriggerSettings.FirstOrDefault(u => u.TriggerType == TriggerType.OK);
product.ProductResult = ResultState.OK;
product.ProductLabelCategory = ResultState.OK.GetEnumDescription();
product.ProductLabel = ResultState.OK.GetEnumDescription();
}
#region 6.
product.ProductResult = product.ResultCollection.Any(u => u.ResultState != ResultState.OK)
? ResultState.B_NG
: ResultState.OK;
product.ProductLabelCategory = product.ProductResult.GetEnumDescription();
product.ProductLabel = product.ProductResult.GetEnumDescription();
#endregion
#region 7.
@ -811,110 +785,45 @@ namespace DHSoftware
//LogAsync(DateTime.Now, LogLevel.Information, $"推理完成,产品{product.PieceNumber}获取结果");
// 统计产品结果
//Task resultTask = product.SetProductResult(_totalDetectionNum, X017Config.IsTBDPriority, labelCategoryLists);
//await resultTask.ContinueWith(t =>
//{
// try
// {
// UpdateTriggerCount(DateTime.Now, "获取结果");
// UpdateResult(DateTime.Now, camera, product.ProductResult.GetEnumDescription());
// UpdateResultNew(DateTime.Now, camera, "合计");
// LogAsync(DateTime.Now, LogLevel.Information, $"产品{product.PieceNumber}获取结果:{product.ProductResult} {(product.IsA2B ? "产品IsA2B" : "")}");
// }
// catch (Exception ex)
// {
// LogAsync(DateTime.Now, LogLevel.Information, $"产品{product.PieceNumber}获取结果异常:{product.ProductResult} {(product.IsA2B ? "产品IsA2B" : "")}" +
// $"异常信息:{ex.GetExceptionMessage}");
// }
// // 吹气
// DateTime dtNow = DateTime.Now;
// product.BlowOutTime = dtNow;
// var setting = X017Config.ProductBlowSettings.FirstOrDefault(s => product.ProductResult.Equals(s.ProductResult));
// if (setting != null)
// {
// uint NGNumC = 0;
// uint OKNumC = 0;
// CMCDLL_NET.MCF_Sorting_Get_Lose_Blow_NG_Count_Net(ref NGNumC, 0);
// CMCDLL_NET.MCF_Sorting_Get_Lose_Blow_OK_Count_Net(ref OKNumC, 0);
// _mainMotion.Blow(product.PieceNumber, setting.BindBlow);
// LogAsync(DateTime.Now, LogLevel.Action, $"产品{product.PieceNumber}吹气{NGNumC}+ {OKNumC}");
// //if (product.ProductResult == ResultState.OK)
// //{
// // //OKProcessNum++;
// // OKNum++;
// //}
// }
// //判断超时出队
// foreach (var kvp in tmpDic)
// {
// try
// {
// ProductData dataN = kvp.Value;
// TimeSpan timeDifference = DateTime.Now - dataN.CreateTime;
// if (timeDifference.TotalMinutes >= 1)
// {
// //出队
// bool isremoved = tmpDic.TryRemove(kvp.Key, out _);
// if (isremoved)
// {
// LogAsync(DateTime.Now, LogLevel.Error, $"====产品{kvp.Key}超时出列成功" +
// $"产品结果:{dataN.ProductResult.GetEnumDescription()} {dataN.ProductResult}" +
// $"当前队列产品数量:{tmpDic.Count}====");
// try
// {
// //重新生成实例 销毁之前的实例
// var saveData = dataN.GetProductData();
// var productDefects = dataN.GetDetectDetailData();
// SaveProductDataAsync(saveData, productDefects);
// dataN = null;
// }
// catch (Exception) { }
// dataN?.Dispose();
// }
// else
// {
// LogAsync(DateTime.Now, LogLevel.Error, $"=====产品{kvp.Key}超时出列失败" +
// $"当前队列产品数量:{tmpDic.Count}=====");
// }
// }
// }
// catch (Exception ex)
// {
// LogAsync(DateTime.Now, LogLevel.Error, $"=====产品{kvp.Key}超时出列失败" +
// $"当前队列产品数量:{tmpDic.Count}====={ex.GetExceptionMessage}");
// }
// }
// 出列
ProductData temp = null;
int tryTimes = 5;
int tryTimes = 10;
while (temp == null && tryTimes > 0)
{
var isSuccess = tmpDic.Remove(productNumber, out temp);
if (isSuccess)
if (tmpDic.TryRemove(productNumber, out temp))
{
string logStr = $"{DateTime.Now}产品{productNumber}出列成功:{isSuccess}" +
$"产品结果:{temp.ProductResult.GetEnumDescription()}" +
break;
}
tryTimes--;
Thread.Sleep(5);
}
if (temp == null)
{
string logStr = $"{DateTime.Now}产品{productNumber}出列失败true"+
$"当前队列产品数量:{tmpDic.Count}";
this.BeginInvoke(new MethodInvoker(delegate ()
{
int currentScrollPosition = richTextBox1.GetPositionFromCharIndex(richTextBox1.TextLength).Y;
richTextBox1.AppendText(logStr);
// 设置回原来的滚动位置
richTextBox1.SelectionStart = richTextBox1.TextLength;
richTextBox1.ScrollToCaret();
}));
}
else
{
try
{
string logStr = $"{DateTime.Now}产品{productNumber}出列成功true" +
$"产品结果:{temp.ProductResult.GetEnumDescription()}" +
$"当前队列产品数量:{tmpDic.Count}";
this.BeginInvoke(new MethodInvoker(delegate ()
{
@ -926,22 +835,12 @@ namespace DHSoftware
richTextBox1.SelectionStart = richTextBox1.TextLength;
richTextBox1.ScrollToCaret();
}));
}
tryTimes--;
Thread.Sleep(1);
}
if (temp == null)
{
//LogAsync(DateTime.Now, LogLevel.Assist, $"产品{productNumber}出列失败," +
// $"当前队列产品数量:{tmpDic.Count}");
}
else
{
try
{
//重新生成实例 销毁之前的实例
var saveData = temp.GetProductData();
using(StreamWriter sw=new StreamWriter("D://123log.txt",true,Encoding.UTF8))
{
sw.WriteLine(logStr);
}
}
catch (Exception) { }