上传视觉检测模块

This commit is contained in:
2025-03-07 16:29:38 +08:00
parent af2e65dd58
commit 4df6b668bf
14 changed files with 1570 additions and 400 deletions

View File

@ -25,7 +25,7 @@
<ItemGroup>
<Reference Include="DVPCameraCS64">
<HintPath>..\X64\Debug\DVPCameraCS64.dll</HintPath>
<HintPath>..\x64\Debug\DVPCameraCS64.dll</HintPath>
</Reference>
</ItemGroup>

View File

@ -13,6 +13,7 @@ using System;
using System.CodeDom;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
@ -258,41 +259,111 @@ namespace DHSoftware
public volatile int ProductNum_Total = 0;
public volatile int ProductNum_OK = 0;
private readonly object _cameraSummaryLock = new object();
List<DetectionConfig> detectionList = new List<DetectionConfig>();
public List<RecongnitionLabel> RecongnitionLabelList { get; set; } = new List<RecongnitionLabel>();
public DateTime sraerttime;
private void HandleStartButton()
{
CurrentMachine = true;
List<VisionEngine> detectionList = new List<VisionEngine>();
detectionList.Add(new VisionEngine("相机1", MLModelType.ObjectDetection, @"D:\DHSoftware\DHSoftware\Models\yolov3.cfg", false, "Cam1"));
detectionList.Add(new VisionEngine("相机2", MLModelType.ObjectDetection, @"D:\DHSoftware\DHSoftware\Models\yolov3.cfg", false, "Cam2"));
detectionList.Add(new VisionEngine("相机3", MLModelType.ObjectDetection, @"D:\DHSoftware\DHSoftware\Models\yolov3.cfg", false, "Cam3"));
detectionList.Add(new VisionEngine("相机4", MLModelType.ObjectDetection, @"D:\DHSoftware\DHSoftware\Models\yolov3.cfg", false, "Cam4"));
//[Category("深度学习检测配置")]
//[DisplayName("检测标签定义集合")]
//[Description("定义检测标签的集合例如Seg/Detection模式断裂、油污、划伤...Class模式ok、ng、上面、下面、套环、正常...")]
//[TypeConverter(typeof(CollectionCountConvert))]
//[Editor(typeof(ComplexCollectionEditor<RecongnitionLabel>), typeof(UITypeEditor))]
RecongnitionLabel recongnition=new RecongnitionLabel
{
LabelName="youwu",
LabelDescription="油污",
LabelCategory="A_NG"
};
RecongnitionLabel recongnition2 = new RecongnitionLabel
{
LabelName = "youwu",
LabelDescription = "油污",
LabelCategory = "A_NG"
};
RecongnitionLabel recongnition3 = new RecongnitionLabel
{
LabelName = "youwu",
LabelDescription = "油污",
LabelCategory = "A_NG"
};
RecongnitionLabelList.Add(recongnition);
RecongnitionLabelList.Add(recongnition2);
RecongnitionLabelList.Add(recongnition3);
var det1 = new DetectionConfig("相机1", MLModelType.ObjectDetection, @"D:\DHSoftware\DHSoftware\Models\yolov3.cfg", false, "Cam1");
var det2 = new DetectionConfig("相机2", MLModelType.ObjectDetection, @"D:\DHSoftware\DHSoftware\Models\yolov3.cfg", false, "Cam2");
var det3 = new DetectionConfig("相机3", MLModelType.ObjectDetection, @"D:\DHSoftware\DHSoftware\Models\yolov3.cfg", false, "Cam3");
var det4 = new DetectionConfig("相机4", MLModelType.ObjectDetection, @"D:\DHSoftware\DHSoftware\Models\yolov3.cfg", false, "Cam4");
List<RelatedCamera> CameraCollects=new List<RelatedCamera>();
CameraCollects.Add(new RelatedCamera("Cam1"));
List<RelatedCamera> CameraCollects2 = new List<RelatedCamera>();
CameraCollects2.Add(new RelatedCamera("Cam2"));
List<RelatedCamera> CameraCollects3 = new List<RelatedCamera>();
CameraCollects3.Add(new RelatedCamera("Cam3"));
List<RelatedCamera> CameraCollects4 = new List<RelatedCamera>();
CameraCollects4.Add(new RelatedCamera("Cam4"));
List<RelatedCamera> CameraCollects5 = new List<RelatedCamera>();
CameraCollects5.Add(new RelatedCamera("Cam5"));
float Conf = 0.5f;
det1.CameraCollects = CameraCollects;
det1.ModelconfThreshold = Conf;
det1.ModelWidth = 640;
det1.ModelHeight = 640;
det1.in_lable_path = " D:\\PROJECTS\\MaodingTest1\\Vision\\cam1.txt";
det2.CameraCollects = CameraCollects2;
det1.ModelconfThreshold = Conf;
det1.ModelWidth = 640;
det1.ModelHeight = 640;
det1.in_lable_path = " D:\\PROJECTS\\MaodingTest1\\Vision\\cam2.txt";
det3.CameraCollects = CameraCollects3;
det1.ModelconfThreshold = Conf;
det1.ModelWidth = 640;
det1.ModelHeight = 640;
det1.in_lable_path = " D:\\PROJECTS\\MaodingTest1\\Vision\\cam3.txt";
det4.CameraCollects = CameraCollects4;
det1.ModelconfThreshold = Conf;
det1.ModelWidth = 640;
det1.ModelHeight = 640;
det1.in_lable_path = " D:\\PROJECTS\\MaodingTest1\\Vision\\cam4.txt";
detectionList.Add(det1);
detectionList.Add(det2);
detectionList.Add(det3);
detectionList.Add(det4);
Cameras.Clear();
Dectection.Clear();
_cameraRelatedDetectionDict = new();
detectionList.ForEach(detection =>
{
// detection.CameraCollects.ForEach(cam =>
detection.CameraCollects.ForEach(cam =>
{
List<string> Dets = new List<string>
{
detection.Id
};
if (!_cameraRelatedDetectionDict.ContainsKey(detection.CameraSourceId))
if (!_cameraRelatedDetectionDict.ContainsKey(cam.CameraSourceId))
{
_cameraRelatedDetectionDict.Add(detection.CameraSourceId, Dets);
_cameraRelatedDetectionDict.Add(cam.CameraSourceId, Dets);
}
else
{
_cameraRelatedDetectionDict[detection.CameraSourceId].Add(detection.Id);
_cameraRelatedDetectionDict[cam.CameraSourceId].Add(detection.Id);
}
}
//);
);
});
//Add the code for the "启动" button click here
@ -310,10 +381,7 @@ namespace DHSoftware
do3ThinkCamera2.CameraConnect();
do3ThinkCamera1.OnHImageOutput += OnCameraHImageOutput;
do3ThinkCamera2.OnHImageOutput += OnCameraHImageOutput;
var simbo1 = new SimboObjectDetection
{
};
var simbo1 = new SimboObjectDetection();
MLInit mLInit;
string inferenceDevice = "CPU";
@ -321,12 +389,11 @@ namespace DHSoftware
simbo1.Load(mLInit);
Dectection.Add(do3ThinkCamera1.CameraName, simbo1);
var simbo2 = new SimboObjectDetection
{
Dectection.Add(det1.Id, simbo1);
};
var simbo2 = new SimboObjectDetection();
MLInit mLInit2;
string inferenceDevice2 = "CPU";
@ -334,7 +401,11 @@ namespace DHSoftware
simbo2.Load(mLInit2);
Dectection.Add(do3ThinkCamera2.CameraName, simbo2);
for(int i = 0;i<Dectection.Count;i++)
{
}
Dectection.Add(det1.Id, simbo2);
PLC.IP = "192.168.6.6";
PLC.Port = 502;
@ -375,10 +446,7 @@ namespace DHSoftware
PieceCount++;
Task.Run(() => {
this.BeginInvoke(new MethodInvoker(delegate () { richTextBox1.AppendText("入料成功" + PieceCount); }));
});
int index = PieceNumberToIndex(pieceNumber);
// productDatas.Add(pData);
//转盘2 的物料是不是重新覆盖之前的pDta
@ -387,6 +455,11 @@ namespace DHSoftware
ProductData pData = new ProductData("", pieceNumber, ProductBaseCount);
_productLists[index][pieceNumber] = pData;
}
string logStr = $"时间:{DateTime.Now} 轴{axisIndex}新产品{pieceNumber}加入队列{index}----入料计数{PieceCount}\n";
Task.Run(() => {
this.BeginInvoke(new MethodInvoker(delegate () { richTextBox1.AppendText(logStr); }));
});
DateTime dtNow = DateTime.Now;
UpdateCT(null, (float)(dtNow - _ctTime).TotalSeconds);
_ctTime = dtNow;
@ -472,28 +545,173 @@ namespace DHSoftware
for (int i = 0; i < detectionDict.Count; i++)
{
string d = detectionDict[i];
string detectionId = detectionDict[i];
try
{
DetectionConfig detectConfig = null;
//找到对应的配置
if (!string.IsNullOrWhiteSpace(detectionId))
{
detectConfig = detectionList.FirstOrDefault(u => u.Id == detectionId);
}
else
{
detectConfig = detectionList.FirstOrDefault(u => u.CameraSourceId == camera.CameraName);
}
// LogAsync(DateTime.Now, LogLevel.Information, $"{camera.Name} 推理进度1.3,产品{productNumber}");
if (detectConfig == null)
{
//未能获得检测配置
return ;
}
#region 1.
#endregion
#region 2.
var req = new MLRequest();
req.mImage = imageSet.Clone();
req.ResizeWidth = 640;
req.ResizeHeight = 640;
req.confThreshold = 0.5f;
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 = "D:\\PROJECTS\\MaodingTest1\\Vision\\cam1.txt";
req.out_node_name = detectConfig.ModeloutNodeName;
req.in_lable_path = detectConfig.in_lable_path;
//req.LabelNames = dc.GetLabelNames();
req.Score = 0.5f;
//HOperatorSet.WriteImage(req.HImage, "png", 0, @"D:\\666.png");
var result = Dectection[camera.CameraName].RunInference(req);
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
#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
{
detectResult.IsMLDetectDone = false;
}
}
#endregion
#region 3.
#endregion
//根据那些得分大于阈值的推理结果,判断产品是否成功
#region 4.
detectResult.DetectDetails?.ForEach(d =>
{
this.BeginInvoke(new MethodInvoker(delegate () {
pictureBox1.Image = result.ResultMap; richTextBox1.AppendText("推理成功" + productNumber+ result.IsSuccess+ "\n"); }));
//当前检测项的 过滤条件
//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 (d.LabelName.ToLower() == "ok")
{
d.FinalResult = d.InferenceResult = ResultState.OK;
}
else
{
d.FinalResult = d.InferenceResult = ResultState.DetectNG;
}
}
else
{
if (detectConfig.IsMixModel)
{
d.FinalResult = d.InferenceResult = ResultState.A_NG;
}
else
{
//将所有已将筛选出来的缺陷进行过滤
d.FinalResult = d.InferenceResult = 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;
}
//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:设置优先级
//////根据优先级设置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;
// }
// }
//}
return;
}
#endregion
resultStates.Add(detectResult.ResultState);
product.ResultCollection.Add(detectResult);
this.BeginInvoke(new MethodInvoker(delegate ()
{
pictureBox1.Image = result.ResultMap; richTextBox1.AppendText($"推理成功{productNumber}{result.IsSuccess} 推理耗时{sw.ElapsedMilliseconds}ms总推理耗时\n");
}));
//DetectStationResult temp;
////LogAsync(DateTime.Now, LogLevel.Information, $"{camera.Name} 推理进度1.4,产品{productNumber}");
//// 检测结果
@ -541,10 +759,7 @@ namespace DHSoftware
if (!product.InferenceFinished())
{
//if (!(camera.Name == "Cam8"))
//{
// return;
//}
return;
}
ProductNum_Total++;
@ -559,6 +774,30 @@ 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();
}
#endregion
#region 7.
#endregion
//LogAsync(DateTime.Now, LogLevel.Information, $"推理完成,产品{product.PieceNumber}获取结果");
@ -661,9 +900,19 @@ namespace DHSoftware
if (isSuccess)
{
// LogAsync(DateTime.Now, LogLevel.Assist, $"产品{productNumber}出列成功:{isSuccess}" +
//$"产品结果:{temp.ProductResult.GetEnumDescription()}" +
//$"当前队列产品数量:{tmpDic.Count}");
string logStr =$"{DateTime.Now}产品{productNumber}出列成功:{isSuccess}" +
$"产品结果:{temp.ProductResult.GetEnumDescription()}" +
$"当前队列产品数量:{tmpDic.Count}";
this.BeginInvoke(new MethodInvoker(delegate () {
int currentScrollPosition = richTextBox1.GetPositionFromCharIndex(richTextBox1.TextLength).Y;
richTextBox1.AppendText(logStr);
// 设置回原来的滚动位置
richTextBox1.SelectionStart = richTextBox1.TextLength;
richTextBox1.ScrollToCaret();
}));
}
tryTimes--;
Thread.Sleep(1);
@ -703,6 +952,29 @@ namespace DHSoftware
}
});
}
public void SetResult()
{
//// detectResult.IsPreTreatDone = detectResult.VisionImageSet.PreTreatedFlag
////2024-02-29 目标检测不能全是NG
//if (IsPreTreatNG || IsObjectDetectNG)
//{
// return;
//}
//if (IsPreTreatDone && IsMLDetectDone && IsAfterTreatDone)
//{
// ResultState = ResultState.OK;
// ResultLabel = ResultState.OK.GetEnumDescription();
//}
}
private void HandleStopButton()
{
Cameras.Clear();