111
This commit is contained in:
		| @@ -1,5 +1,6 @@ | ||||
| using Check.Main.Common; | ||||
| using Check.Main.Infer; | ||||
| using HalconTemplateMatch; | ||||
| using SkiaSharp; | ||||
| using System; | ||||
| using System.Collections.Concurrent; | ||||
| @@ -10,7 +11,7 @@ using System.Runtime.InteropServices; | ||||
| using System.Text; | ||||
| using System.Threading.Tasks; | ||||
| using YoloDotNet.Extensions; | ||||
|  | ||||
| using OpenCvSharp; | ||||
| namespace Check.Main.Camera | ||||
| { | ||||
|     public class CameraProcessor : IDisposable | ||||
| @@ -52,6 +53,9 @@ namespace Check.Main.Camera | ||||
|             _imageQueue.Add(new ImageData(_imageCounter, _cameraIndex, bmp)); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// 图像处理主循环 | ||||
|         /// </summary> | ||||
|         private void ProcessQueue() | ||||
|         { | ||||
|             // 从模型管理器获取此线程专属的YOLO模型 | ||||
| @@ -61,6 +65,36 @@ namespace Check.Main.Camera | ||||
|                 ThreadSafeLogger.Log($"[错误] 相机 #{_modeId} 无法获取对应的YOLO模型,处理线程已中止。"); | ||||
|                 return; // 如果没有模型,此线程无法工作 | ||||
|             } | ||||
|  | ||||
|             //训练阶段(相机2) | ||||
|             var trainer = new LogoTemplateTrainer(); | ||||
|  | ||||
|             trainer.TrainAndSaveTemplates( | ||||
|                 new List<string> | ||||
|                 { | ||||
|                 @"D:\HalconTemplateMatch\train2\logo1.bmp", | ||||
|                 @"D:\HalconTemplateMatch\train2\logo2.bmp", | ||||
|                 @"D:\HalconTemplateMatch\train2\logo3.bmp" | ||||
|                 }, | ||||
|                 @"D:\HalconTemplateMatch\model_2"); | ||||
|  | ||||
|             //训练阶段(相机3)9.25修改!! | ||||
|             trainer.TrainAndSaveTemplates( | ||||
|                new List<string> | ||||
|                { | ||||
|                 @"D:\HalconTemplateMatch\train3\3C_1.bmp", | ||||
|                 @"D:\HalconTemplateMatch\train3\3C_2.bmp", | ||||
|                 @"D:\HalconTemplateMatch\train3\3C_3.bmp" | ||||
|                }, | ||||
|                @"D:\HalconTemplateMatch\model_3"); | ||||
|  | ||||
|             if (trainer == null) | ||||
|             { | ||||
|                 ThreadSafeLogger.Log($"[错误] 相机 #{_modeId} 未加载模板,处理线程已中止。"); | ||||
|                 return; | ||||
|             } | ||||
|  | ||||
|             | ||||
|             while (_isRunning) | ||||
|             { | ||||
|                 try | ||||
| @@ -69,19 +103,15 @@ namespace Check.Main.Camera | ||||
|                     ImageData data = _imageQueue.Take(); | ||||
|                     using (data) | ||||
|                     { | ||||
|                         //SKImage resultSkImage = null; // 用于存储最终绘制好结果的图像 | ||||
|  | ||||
|                         string result = ""; | ||||
|                         using (var skImage = ConvertBitmapToSKImage(data.Image)) // 转换图像格式并管理其生命周期 | ||||
|                         { | ||||
|                             if (skImage == null) continue; | ||||
|                             var predictions = yoloModel.RunObjectDetection(skImage); | ||||
|                             // 模拟模型处理 | ||||
|                             //Thread.Sleep(50); // 模拟耗时 | ||||
|                             //bool isOk = new Random().NextDouble() > 0.1; | ||||
|                             //string result = isOk ? "OK" : "NG"; | ||||
|                              result = predictions.Any() ? "NG" : "OK"; | ||||
|  | ||||
|                             string result = predictions.Any() ? "NG" : "OK"; | ||||
|                             ThreadSafeLogger.Log($"相机 #{_cameraIndex} 处理产品 #{data.ProductId},检测到 {predictions.Count} 个目标,结果: {result}"); | ||||
|  | ||||
|                             // 将处理结果交给协调器进行组装 | ||||
|                             DetectionCoordinator.AssembleProduct(data, result); | ||||
|  | ||||
| @@ -90,16 +120,111 @@ namespace Check.Main.Camera | ||||
|                                 using (var resultSkImage = skImage.Draw(predictions)) | ||||
|                                 { | ||||
|                                     // 4. 触发事件,将绘制好的 resultSkImage 传递出去 | ||||
|  | ||||
|                                     Bitmap bitmap = CameraManager.ConvertSKImageToBitmap(resultSkImage); | ||||
|                                     // 所有权在这里被转移 | ||||
|                                     OnProcessingCompleted?.Invoke(this, new ProcessingCompletedEventArgs( | ||||
|                                         _cameraIndex, | ||||
|                                         data.ProductId, | ||||
|                                         resultSkImage | ||||
|                                         bitmap | ||||
|                                     )); | ||||
|                                 } | ||||
|                             } | ||||
|                         } | ||||
|  | ||||
|                         //***********************************使用Halcon模板匹配进行检测**************************************************** | ||||
|                         if (data.Image == null) continue; | ||||
|                         // 统一定义预测结果 | ||||
|                         var matcher = new LogoMatcher(); | ||||
|  | ||||
|                         //9.25(增加一根据不同的相机编号调用不同的模型!) | ||||
|                         string filepath = ""; | ||||
|                         if (_cameraIndex == 2) | ||||
|                         { | ||||
|                             matcher.LoadTemplates(@"D:\HalconTemplateMatch\model_2"); | ||||
|                             filepath = "D:\\HalconTemplateMatch\\train2"; | ||||
|                         } | ||||
|                         else if (_cameraIndex == 3) | ||||
|                         { | ||||
|                             matcher.LoadTemplates(@"D:\HalconTemplateMatch\model_3"); | ||||
|                             filepath = "D:\\HalconTemplateMatch\\train3"; | ||||
|                         } | ||||
|  | ||||
|                         ////原bool返回的处理 | ||||
|                         //bool found = matcher.FindLogo(data.Image); | ||||
|                         //string result = found ? "OK":"NG"; | ||||
|  | ||||
|                         //double返回的处理 | ||||
|                         double score = matcher.FindLogo(data.Image); | ||||
|                           | ||||
|  | ||||
|                         //Mat cam = ProcessImg.BitmapToMat(data.Image); | ||||
|                          | ||||
|                         //score= ProcessImg.ProcessImagesInFolder(filepath,cam); | ||||
|  | ||||
|                         //string result = (score > 0.5) ? "OK" : "NG"; | ||||
|  | ||||
|                         ThreadSafeLogger.Log($"相机 #{_cameraIndex} 处理产品 #{data.ProductId},结果: {result},得分: {score}"); | ||||
|  | ||||
|                         // 将处理结果交给协调器进行组装 | ||||
|                         DetectionCoordinator.AssembleProduct(data, result); | ||||
|  | ||||
|  | ||||
|                         //给PLC的M90、M91写值(10.10) | ||||
|                         if (FrmMain.PlcClient != null && FrmMain.PlcClient.IsConnected) | ||||
|                         { | ||||
|                             if (result == "OK") | ||||
|                             { | ||||
|                                 //吹气到合格框 | ||||
|                                 FrmMain.PlcClient.WriteAsync("M90", 1).Wait(); // 写入M90为1 | ||||
|                                 //Thread.Sleep(100); | ||||
|                                 FrmMain.PlcClient.WriteAsync("M90", 0).Wait(); // 写入M90为1 | ||||
|  | ||||
|                                 //var a = FrmMain.PlcClient.ReadAsync("M90"); | ||||
|                                 //Console.WriteLine(a); | ||||
|  | ||||
|                                 //FrmMain.PlcClient.WriteAsync("M91", 0).Wait(); | ||||
|  | ||||
|                                 // 延时复位 | ||||
|                                 Task.Run(async () => | ||||
|                                 { | ||||
|                                     //await Task.Delay(300); // 延时300毫秒,可根据实际气动时间调整 | ||||
|                                     //await FrmMain.PlcClient.WriteAsync("M90", 0); | ||||
|                                 }); | ||||
|                             } | ||||
|                             else | ||||
|                             { | ||||
|                                 //吹气到不合格框 | ||||
|                                 //FrmMain.PlcClient.WriteAsync("M90", 0).Wait(); | ||||
|                                 FrmMain.PlcClient.WriteAsync("M91", 1).Wait();// 写入M91为1 | ||||
|                                 FrmMain.PlcClient.WriteAsync("M91", 0).Wait(); // 写入M90为1 | ||||
|  | ||||
|                                 //var a = FrmMain.PlcClient.ReadAsync("M90"); | ||||
|                                 //Console.WriteLine(a); | ||||
|                                 // 延时复位 | ||||
|                                 Task.Run(async () => | ||||
|                                 { | ||||
|                                     //await Task.Delay(300); | ||||
|                                     //await FrmMain.PlcClient.WriteAsync("M91", 0); | ||||
|                                 }); | ||||
|                             } | ||||
|                         } | ||||
|                         else | ||||
|                         { | ||||
|                             ThreadSafeLogger.Log("[PLC] 未连接,跳过写入。"); | ||||
|                         } | ||||
|  | ||||
|  | ||||
|                         // ③ 外部订阅事件 | ||||
|                         OnProcessingCompleted?.Invoke( | ||||
|                             this, | ||||
|                             new ProcessingCompletedEventArgs | ||||
|                             ( | ||||
|                                 _cameraIndex, | ||||
|                                 data.ProductId, | ||||
|                                 data.Image // 原图传出去 | ||||
|                             ) | ||||
|                         ); | ||||
|                     } | ||||
|                 } | ||||
|                 catch (InvalidOperationException) | ||||
| @@ -107,12 +232,14 @@ namespace Check.Main.Camera | ||||
|                     // 当调用 Stop 时,会 CompleteAdding 队列,Take 会抛出此异常,是正常退出流程 | ||||
|                     break; | ||||
|                 } | ||||
|                 catch (Exception ex) | ||||
|                 catch (Exception ex)  | ||||
|                 { | ||||
|                     ThreadSafeLogger.Log($"[ERROR] 相机 #{_cameraIndex} 处理线程异常: {ex.Message}"); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  | ||||
|  | ||||
|         /// <summary> | ||||
|         /// 将 System.Drawing.Bitmap 安全地转换为 SkiaSharp.SKImage。 | ||||
|         /// </summary> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user