diff --git a/DH.Commons/Base/CameraBase.cs b/DH.Commons/Base/CameraBase.cs index c1ac599..12e5ab8 100644 --- a/DH.Commons/Base/CameraBase.cs +++ b/DH.Commons/Base/CameraBase.cs @@ -343,7 +343,18 @@ namespace DH.Commons.Base } // 其他方法保持原有逻辑 - public Action OnHImageOutput { get; set; } + public MatSet CopyImageSet(MatSet srcSet) + { + MatSet imageSet = new MatSet + { + Id = srcSet.Id, + _mat = srcSet._mat.Clone(), + // ImageSaveOption = srcSet.ImageSaveOption.Copy(), + ImageTime = srcSet.ImageTime + }; + return imageSet; + } + public Action OnHImageOutput { get; set; } public virtual bool CameraConnect() { return false; } diff --git a/DH.Commons/Base/VisionEngineBase.cs b/DH.Commons/Base/VisionEngineBase.cs index f713dfb..3addef4 100644 --- a/DH.Commons/Base/VisionEngineBase.cs +++ b/DH.Commons/Base/VisionEngineBase.cs @@ -36,7 +36,7 @@ namespace DH.Commons.Base public HTuple hv_ModelID; - public abstract DetectStationResult RunInference(Mat originImgSet, string detectionId = null); + public abstract DetectStationResult RunInference(MatSet originImgSet, string detectionId = null); //public abstract void SaveDetectResultAsync(DetectStationResult detectResult); diff --git a/DH.Devices.Camera/Do3ThinkCamera.cs b/DH.Devices.Camera/Do3ThinkCamera.cs index bc3d963..696012d 100644 --- a/DH.Devices.Camera/Do3ThinkCamera.cs +++ b/DH.Devices.Camera/Do3ThinkCamera.cs @@ -1,4 +1,5 @@ using System.Collections.Concurrent; +using System.Collections.Generic; using System.Diagnostics; using System.Reflection.Metadata; using System.Xml.Linq; @@ -372,7 +373,9 @@ namespace DH.Devices.Camera }; InitialImageSet(imageSet); - OnHImageOutput?.Invoke(DateTime.Now, this, smat); + + var outImgSet = CopyImageSet(imageSet); + OnHImageOutput?.Invoke(DateTime.Now, this, outImgSet); //存图 DisplayAndSaveOriginImage(imageSet.Id,SnapshotCount); diff --git a/DH.Devices.Camera/HikVisionCamera.cs b/DH.Devices.Camera/HikVisionCamera.cs index daedb5b..5b2eb93 100644 --- a/DH.Devices.Camera/HikVisionCamera.cs +++ b/DH.Devices.Camera/HikVisionCamera.cs @@ -368,7 +368,7 @@ namespace DH.Devices.Camera throw new NotSupportedException($"Unsupported pixel type: {pFrameInfo.enPixelType}"); } - OnHImageOutput?.Invoke(DateTime.Now, this, cvImage); + //OnHImageOutput?.Invoke(DateTime.Now, this, cvImage); } catch (Exception ex) diff --git a/DH.Devices.Vision/SimboVisionDriver.cs b/DH.Devices.Vision/SimboVisionDriver.cs index 25e2a39..a1cb4d5 100644 --- a/DH.Devices.Vision/SimboVisionDriver.cs +++ b/DH.Devices.Vision/SimboVisionDriver.cs @@ -58,7 +58,7 @@ namespace DH.Devices.Vision //{ // LogAsync(new LogMsg(dt, LogLevel.Error, msg)); //} - public override DetectStationResult RunInference(Mat originImgSet, string detectionId = null) + public override DetectStationResult RunInference(MatSet originImgSet, string detectionId = null) { DetectStationResult detectResult = new DetectStationResult(); DetectionConfig detectConfig = null; @@ -78,19 +78,20 @@ namespace DH.Devices.Vision //未能获得检测配置 return detectResult; } + detectResult.Id = originImgSet.Id; detectResult.DetectName = detectConfig.Name; detectResult.ImageSaveDirectory=detectConfig.ImageSaveDirectory; detectResult.SaveNGDetect=detectConfig.SaveNGDetect; detectResult.SaveNGOriginal=detectConfig.SaveNGOriginal; detectResult.SaveOKDetect=detectConfig.SaveOKDetect; detectResult.SaveOKOriginal=detectConfig.SaveOKOriginal; - Mat OriginImage = originImgSet.Clone(); + Mat OriginImage = originImgSet._mat.Clone(); detectResult.DetectionOriginImage = CopyBitmapWithLockBits(OriginImage.ToBitmap()); //detectResult.DetectionOriginImage = originImgSet.Clone().ToBitmap(); Stopwatch sw = new Stopwatch(); #region 1.预处理 sw.Start(); - using (Mat PreTMat = originImgSet.Clone()) + using (Mat PreTMat = originImgSet._mat.Clone()) { PreTreated(detectConfig, detectResult, PreTMat); PreTreated2(detectConfig, detectResult, PreTMat); @@ -142,7 +143,7 @@ namespace DH.Devices.Vision req.ResizeHeight = (int)detectConfig.ModelHeight; // req.LabelNames = detectConfig.GetLabelNames(); // req.Score = IIConfig.Score; - req.mImage = originImgSet.Clone(); + req.mImage = originImgSet._mat.Clone(); req.in_lable_path = detectConfig.In_lable_path; @@ -308,7 +309,7 @@ namespace DH.Devices.Vision - DisplayDetectionResult(detectResult, originImgSet.Clone(), detectionId); + DisplayDetectionResult(detectResult, originImgSet._mat.Clone(), detectionId); diff --git a/DHSoftware/MainWindow.cs b/DHSoftware/MainWindow.cs index fabae99..da71414 100644 --- a/DHSoftware/MainWindow.cs +++ b/DHSoftware/MainWindow.cs @@ -610,7 +610,7 @@ namespace DHSoftware cam.IsEnabled = cameraBase.IsEnabled; HKCameras.Add(cam); // cam.CameraConnect(); - cam.OnHImageOutput += OnCameraHImageOutput; + //cam.OnHImageOutput += OnCameraHImageOutput; } } } @@ -1199,7 +1199,7 @@ namespace DHSoftware /// /// /// - private void OnCameraHImageOutput(DateTime dt, CameraBase camera, Mat imageSet) + private void OnCameraHImageOutput(DateTime dt, CameraBase camera, MatSet imageSet) { //if (camera.CameraName.Equals("cam1", StringComparison.OrdinalIgnoreCase)) //{ @@ -1215,7 +1215,7 @@ namespace DHSoftware Task.Run(async () => { - using (Mat localImageSet = imageSet.Clone()) // 复制 Mat 避免并发问题 + //using (Mat localImageSet = imageSet._mat.Clone()) // 复制 Mat 避免并发问题 { // imageSet?.Dispose(); // 拍照计数与物件编号一致,查找对应的产品 @@ -1254,7 +1254,7 @@ namespace DHSoftware } //LogAsync(DateTime.Now, LogLevel.Error, $"{camera.Name} 未找到产品,编号:{productNumber},队列{index}数量:{tmpDic.Count},列表:{pnStr}"); - localImageSet.Dispose(); + imageSet.Dispose(); return; } @@ -1263,7 +1263,7 @@ namespace DHSoftware if (!_cameraRelatedDetectionDict.ContainsKey(camera.CameraName)) { - localImageSet.Dispose(); + imageSet.Dispose(); @@ -1280,11 +1280,11 @@ namespace DHSoftware for (int i = 0; i < detectionDict.Count; i++) { string detectionId = detectionDict[i]; - - - using (Mat inferenceImage = localImageSet.Clone()) // 仅在此处克隆,确保推理过程中 Mat 有独立副本 + var tmpImgSet = camera.CopyImageSet(imageSet as MatSet); + //imageSet + // using (Mat inferenceImage = localImageSet.Clone()) // 仅在此处克隆,确保推理过程中 Mat 有独立副本 { - DetectStationResult temp1 = _visionEngine.RunInference(inferenceImage, detectionId); + DetectStationResult temp1 = _visionEngine.RunInference(tmpImgSet, detectionId); resultStates.Add(temp1.ResultState);