轮胎外观检测添加思谋语义分割模型检测工具
LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
@@ -2,9 +2,13 @@
using LB_SmartVisionCameraSDK.PHM6000;
using LB_SmartVisionCommon;
using LB_VisionProcesses.Cameras;
using OpenVinoSharp.Extensions.model;
using SharpCompress.Common;
using Sunny.UI.Win32;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Reflection;
@@ -32,12 +36,11 @@
    {
        private IntPtr _cameraHandle = IntPtr.Zero;
        private PHM6000SensorConfig _sensorConfig;
        // 采集回调
        private AcquisitionCallbackZA _acquisitionCallback;
        private AcquisitionCompletedCallback _acquisitionCompletedCallback;
        private bool _isConnected = false;
        public bool _isConnected = false;
        private int _frameCount = 0; // 采集帧计数
        // 图像缓冲
@@ -95,11 +98,11 @@
                {
                    byte[] moduleTypeBytes = new byte[64];
                    byte[] serialNumberBytes = new byte[64];
                    if (PHM6000Profiler.GetCameraInformation(tempHandle, i, moduleTypeBytes, serialNumberBytes) == 0)
                    {
                        string currentSn = Encoding.UTF8.GetString(serialNumberBytes).TrimEnd('\0');
                        // 匹配SN或IP
                        if (currentSn == sn || sn.Contains(currentSn))
                        {
@@ -124,7 +127,7 @@
                    if (System.Net.IPAddress.TryParse(sn, out _))
                    {
                        targetIp = sn;
                        targetPort = 5577;
                        targetPort = 5577;
                    }
                    else
                    {
@@ -139,7 +142,7 @@
                var addr = Encoding.ASCII.GetBytes(targetIp);
                int result = PHM6000Profiler.ConnectToCamera(_cameraHandle, addr, targetPort);
                if (result == 0)
                {
                    _isConnected = true;
@@ -166,8 +169,8 @@
                    AsyncLogHelper.Error($"LBCamera[{SN}]: ConnectToCamera failed, result={result}");
                }
            }
            catch (Exception ex)
            {
            catch (Exception ex)
            {
                AsyncLogHelper.Error($"LBCamera: InitDevice异常 - {ex.Message}");
                if (tempHandle != IntPtr.Zero) PHM6000Profiler.DestroyCameraEntry(tempHandle);
            }
@@ -204,7 +207,7 @@
                        if (PHM6000Profiler.GetCameraInformation(tempHandle, i, moduleTypeBytes, serialNumberBytes) == 0)
                        {
                            string sn = Encoding.UTF8.GetString(serialNumberBytes).TrimEnd('\0');
                            if (!string.IsNullOrEmpty(sn))
                            if (!string.IsNullOrEmpty(sn) && sn.Contains("L"))
                            {
                                cameraList.Add(sn);
                            }
@@ -246,7 +249,7 @@
        public bool StartSingleGrab()
        {
            if (!_isConnected) return false;
            _isContinuous = false;
            InitBuffer();
            AsyncLogHelper.Info($"LBCamera[{SN}]: 开始单次采集");
@@ -288,7 +291,7 @@
            _isContinuous = false;
            if (!_isConnected) return true;
            PHM6000Profiler.StopAcquisition(_cameraHandle);
            // 停止时如果有未显示的缓存数据,将其显示出来(支持显示不完整的帧)
            lock (_bufferLock)
            {
@@ -299,7 +302,7 @@
                    _currentLineCount = 0;
                }
            }
            isGrabbing = false;
            return true;
        }
@@ -354,11 +357,11 @@
        public override void SetCamConfig(CameraConfig config) { }
        public override void GetCamConfig(out CameraConfig config) { config = new CameraConfig(null); }
        public override bool GetImage(out Bitmap bitmap, int outtime = 3000) { bitmap = null; return false; }
        public override bool GetImageWithSoftTrigger(out Bitmap bitmap, int outtime = 3000)
        public override bool GetImageWithSoftTrigger(out Bitmap bitmap, int outtime = 3000)
        {
            // 简单实现:软触发等待
            bitmap = null;
            if(!_isConnected) return false;
            if (!_isConnected) return false;
            // 计算理论最小耗时 (仅当使用固定频率触发时)
            int minTime = 0;
@@ -376,18 +379,20 @@
                actualTimeout = minTime + 3000; // 预留3秒余量
                AsyncLogHelper.Warn($"LBCamera: Provided timeout {outtime}ms is too short for {minTime}ms scan. Extended to {actualTimeout}ms.");
            }
            using (AutoResetEvent waitHandle = new AutoResetEvent(false))
            {
                Bitmap res = null;
                EventHandler<CameraEventArgs> handler = (s, e) => {
                    if(e.Bitmap != null) {
                         res = e.Bitmap.Clone() as Bitmap;
                         waitHandle.Set();
                EventHandler<CameraEventArgs> handler = (s, e) =>
                {
                    if (e.Bitmap != null)
                    {
                        res = e.Bitmap.Clone() as Bitmap;
                        waitHandle.Set();
                    }
                };
                ImageGrabbed += handler;
                if (StartSingleGrab())
                {
                    if (!waitHandle.WaitOne(actualTimeout))
@@ -401,10 +406,10 @@
                }
                ImageGrabbed -= handler;
                // 确保停止采集
                StopGrabbing();
                bitmap = res;
                return bitmap != null;
            }
@@ -418,22 +423,81 @@
        public void UpdateSensorConfig(PHM6000SensorConfig config)
        {
            _sensorConfig = config;
            if (!_isConnected) return;
            SetParam(EnumNameId.ExposureTime, (float)config.ExposureTime);
            SetParam(EnumNameId.AnalogGain, (float)config.AnalogGain);
            PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.ScanLineCount, config.ScanLineCount, 0, 0);
            PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.LineScanTriggerSource, 0, 0, (int)config.LineScanTriggerSource);
            PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.DataAcquisitionTriggerSource, 0, 0, (int)config.DataAcquisitionTriggerSource);
            if (config.LineScanTriggerSource == EnumLineScanTriggerSource.固定频率)
            //_sensorConfig = config;
            //if (!_isConnected) return;
            //SetParam(EnumNameId.ExposureTime, (float)config.ExposureTime);
            //SetParam(EnumNameId.AnalogGain, (float)config.AnalogGain);
            //PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.ScanLineCount, config.ScanLineCount, 0, 0);
            //PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.LineScanTriggerSource, 0, 0, (int)config.LineScanTriggerSource);
            //PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.DataAcquisitionTriggerSource, 0, 0, (int)config.DataAcquisitionTriggerSource);
            //if (config.LineScanTriggerSource == EnumLineScanTriggerSource.固定频率)
            //{
            //    PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.SoftwareTriggerRate, 0, config.SoftwareTriggerRate, 0);
            //}
            int result = 0;
            var type = config.GetType();
            var propLineScan = type.GetProperty(nameof(config.LineScanTriggerSource));
            var val = (EnumLineScanTriggerSource)propLineScan.GetValue(config);
            var props = config.GetType().GetProperties();
            //排除Y轴
            props = props.Where(d => d.Name != nameof(PHM6000SensorConfig.YResolution)).ToArray();
            //排除不需要的项
            if (val == EnumLineScanTriggerSource.固定频率)
            {
                PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.SoftwareTriggerRate, 0, config.SoftwareTriggerRate, 0);
                props = props.Where(d => d.Name != nameof(PHM6000SensorConfig.EncoderTriggerDirection) && d.Name != nameof(PHM6000SensorConfig.EncoderTriggerInterval) && d.Name != nameof(PHM6000SensorConfig.EncoderTriggerSignalCountingMode)).ToArray();
            }
            else
            {
                props = props.Where(d => d.Name != nameof(PHM6000SensorConfig.SoftwareTriggerRate)).ToArray();
            }
            foreach (var p in props)
            {
                //跳过自定义参数
                var iscustomAttr = p.GetCustomAttribute<IsCustomAttribute>();
                if (iscustomAttr != null) continue;
                //判断是6030传感器还是普通传感器
                if (SN.StartsWith("LX030") && p.Name == nameof(config.AnalogGain))
                {
                    continue;
                }
                if (!SN.StartsWith("LX030") && p.Name == nameof(config.AnalogGainFor6030))
                {
                    continue;
                }
                var id = Convert.ToInt32(Enum.Parse(typeof(EnumNameId), p.Name));
                if (p.PropertyType == typeof(int))
                {
                    var value = Convert.ToInt32(p.GetValue(config));
                    result = PHM6000Profiler.SetProfilerParameter(_cameraHandle, id, value, 0, 0);
                }
                else if (p.PropertyType == typeof(float))
                {
                    var value = Convert.ToDouble(p.GetValue(config));
                    result = PHM6000Profiler.SetProfilerParameter(_cameraHandle, id, 0, value, 0);
                }
                else
                {
                    var value = Convert.ToInt32(p.GetValue(config));
                    result = PHM6000Profiler.SetProfilerParameter(_cameraHandle, id, 0, 0, value);
                }
                if (result == -1)
                {
                    var disattr = p.GetCustomAttribute<DisplayNameAttribute>();
                    var name = disattr?.DisplayName ?? p.Name;
                    throw new Exception($"设置参数{name}时不成功!");
                }
            }
            var finalResult = PHM6000Profiler.SaveAllParametersToDevice(_cameraHandle);
            if (finalResult != 0)
            {
            }
            PHM6000Profiler.SaveAllParametersToDevice(_cameraHandle);
        }
        #endregion
        #endregion
        #region Callbacks
@@ -468,7 +532,7 @@
                // 提取灰度(Intensity/Alpha)数据填充到 _rawPixelBuffer
                // LBPointZA结构: float(4) + res(3) + alpha(1). Alpha在偏移7
                int bufferOffset = _currentLineCount * _currentBitmapWidth;
                for (int i = 0; i < points; i++)
                {
                    if (bufferOffset + i < _rawPixelBuffer.Length)
@@ -483,7 +547,7 @@
                if (_currentLineCount >= _currentBitmapHeight)
                {
                    CreateAndFireBitmap();
                    // 重置,准备下一帧 (如果是连续采集)
                    _currentLineCount = 0;
                    // _rawPixelBuffer 可以复用,不需要置空
@@ -502,7 +566,7 @@
                {
                    // 如果在连续模式下收到结束信号,尝试自动重启采集
                    AsyncLogHelper.Info($"LBCamera[{SN}]: Continuous mode frame ended, restarting...");
                    Task.Run(() =>
                    Task.Run(() =>
                    {
                        if (_isContinuous && _isConnected)
                        {
@@ -533,14 +597,23 @@
        private void CreateAndFireBitmap()
        {
            Bitmap bmp = null;
            BitmapData bmpData = null;
            try
            {
                int width = _currentBitmapWidth;
                int height = _currentLineCount; // 使用实际采集到的行数
                int height = _currentLineCount;
                if (width <= 0 || height <= 0 || _rawPixelBuffer == null) return;
                // 基础合法性校验
                if (width <= 0 || height <= 0 || _rawPixelBuffer == null || _rawPixelBuffer.Length < width * height)
                {
                    AsyncLogHelper.Warn($"LBCamera[{SN}]: 图像参数无效,跳过生成");
                    return;
                }
                Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
                // 1. 创建8位灰度位图
                bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
                // 设置灰度调色板
                ColorPalette palette = bmp.Palette;
@@ -550,9 +623,8 @@
                }
                bmp.Palette = palette;
                // 拷贝数据
                BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
                // 2. 高效内存拷贝(支持Stride对齐,整行复制)
                bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
                // 注意:Bitmap Stride 可能不等于 Width,需要逐行拷贝
                int stride = bmpData.Stride;
                IntPtr ptr = bmpData.Scan0;
@@ -567,141 +639,120 @@
                }
                bmp.UnlockBits(bmpData);
                bmpData = null; // 标记已解锁
                _frameCount++;
                AsyncLogHelper.Info($"LBCamera[{SN}]: Frame {_frameCount} generated ({width}x{height})");
                //空值校验:转换失败则直接返回
                if (bmp == null)
                AsyncLogHelper.Info($"LBCamera[{SN}]: 生成第 {_frameCount} 帧 ({width}x{height})");
                // 3. 获取/创建线程安全队列
                var queue = CollectedImages.GetOrAdd(SN, new ConcurrentQueue<Bitmap>());
                // 4. 队列限流,防止内存溢出
                if (queue.Count >= MAX_QUEUE_CAPACITY)
                {
                    AsyncLogHelper.Warn(SN + "帧转换为Bitmap失败,跳过处理");
                    return;
                }
                // 线程安全地将Bitmap添加到CollectedImages字典
                lock (_collectedImagesLock)
                {
                    // 确保当前相机SN对应的列表存在
                    if (!CollectedImages.ContainsKey(SN))
                    if (queue.TryDequeue(out Bitmap old))
                    {
                        CollectedImages[SN] = new List<Bitmap>();
                        old.Dispose(); // 丢弃最旧帧,释放内存
                        AsyncLogHelper.Warn($"LBCamera[{SN}]: 队列已满,自动丢弃最旧帧");
                    }
                    CollectedImages[SN].Add(bmp);
                    AsyncLogHelper.Info(SN + $"图像已加入缓存,当前缓存数量:{CollectedImages[SN].Count}");
                }
                // 处理CollectedImages中的图像:遍历消费列表第一个元素直到为空
                ProcessCollectedImages();
                //// 异步触发事件,避免阻塞SDK回调线程
                //Task.Factory.StartNew(() =>
                // 5. 入队
                queue.Enqueue(bmp);
                AsyncLogHelper.Info($"LBCamera[{SN}]: 图像入队,当前队列:{queue.Count}");
                // 6. 启动队列(单例,避免多线程重复)
                StartConsumeQueue();
                //Task.Factory.StartNew(() =>
                //{
                //    try
                //    CallBackImg = (Bitmap)bitmap.Clone();
                //    if (CallBackImg == null)
                //    {
                //        ImageGrabbed?.Invoke(this, new LBCameraEventArgs(SN, bmp, true));
                //        CallBackImg = (Bitmap)bmp.Clone();
                //        if (CallBackImg == null)
                //        {
                //            return;
                //        }
                //        if (GetTriggerMode(out TriggerMode mode, out TriggerSource source))
                //        {
                //            if (mode == TriggerMode.On && source != TriggerSource.Software)
                //                TriggerRunMessageReceived?.Invoke(SN, source.ToString());  // 触发运行事件
                //        }
                //        bmp.Dispose();
                //        return;
                //    }
                //    catch (Exception ex)
                //    if (GetTriggerMode(out TriggerMode mode, out TriggerSource source))
                //    {
                //        AsyncLogHelper.Error($"LBCamera: Event Invoke error - {ex.Message}");
                //        bmp.Dispose(); // 异常时释放资源
                //        if (mode == TriggerMode.On && source != TriggerSource.Software)
                //            TriggerRunMessageReceived?.Invoke(SN, source.ToString());  // 触发运行事件
                //    }
                //    bitmap.Dispose();
                //});
            }
            catch (Exception ex)
            {
                AsyncLogHelper.Error($"LBCamera: CreateBitmap error - {ex.Message}");
                AsyncLogHelper.Error($"LBCamera[{SN}]: 创建图像失败 - {ex.Message}", ex);
            }
            finally
            {
                // 强制资源释放,绝对杜绝泄漏
                if (bmpData != null)
                {
                    try { bmp?.UnlockBits(bmpData); } catch { }
                }
                // 注意:bmp 已入队,不能在这里释放,由调用者释放
            }
        }
        /// <summary>
        /// 启动队列(保证单线程)
        /// </summary>
        private void StartConsumeQueue()
        {
            // 使用轻量级判断,避免重复启动消费任务
            if (CollectedImages.TryGetValue(SN, out var queue) && !queue.IsEmpty)
            {
                Task.Factory.StartNew(ProcessImageQueue, TaskCreationOptions.LongRunning);
            }
        }
        /// <summary>
        /// 处理CollectedImages中的缓存图像
        /// 核心逻辑:遍历取第一个图像 -> 赋值给CallBackImg -> 触发事件 -> 释放并移除
        /// </summary>
        private void ProcessCollectedImages()
        private void ProcessImageQueue()
        {
            Task.Factory.StartNew(() =>
            try
            {
                // 加锁保证线程安全,防止多线程同时操作列表
                lock (_collectedImagesLock)
                if (!CollectedImages.TryGetValue(SN, out var queue) || queue.IsEmpty)
                    return;
                // 短锁:仅出队,不阻塞生产
                while (queue.TryDequeue(out Bitmap bitmap))
                {
                    // 校验当前相机的图像列表是否存在且有数据
                    if (!CollectedImages.ContainsKey(SN) || CollectedImages[SN].Count == 0)
                    {
                        AsyncLogHelper.Info(SN + "当前无缓存图像,跳过处理");
                        return;
                    }
                    // 循环处理:直到列表为空
                    while (CollectedImages[SN].Count > 0)
                    using (bitmap) // 自动释放:using 是最安全的方式
                    {
                        try
                        {
                            // 1 取列表第一个索引的图像赋值给CallBackImg
                            Bitmap firstBitmap = CollectedImages[SN][0];
                            ImageGrabbed?.Invoke(this, new LBCameraEventArgs(SN, firstBitmap, true));
                            CallBackImg = (Bitmap)firstBitmap.Clone(); // 克隆避免原对象被释放后引用失效
                            // 关键:事件传递克隆对象,绝对安全,不传递原资源
                            using (Bitmap clone = (Bitmap)bitmap.Clone())
                            {
                                // 触发图像事件
                                ImageGrabbed?.Invoke(this, new LBCameraEventArgs(SN, clone, true));
                                CallBackImg = (Bitmap)clone.Clone();
                            }
                            // 2 获取触发模式并判断是否触发运行事件
                            // 触发模式判断
                            if (GetTriggerMode(out TriggerMode mode, out TriggerSource source))
                            {
                                // 硬触发模式下触发运行事件
                                if (mode == TriggerMode.On && source != TriggerSource.Software)
                                {
                                    AsyncLogHelper.Info(SN + $"触发硬触发事件,触发源:{source}");
                                    TriggerRunMessageReceived?.Invoke(SN, source.ToString());
                                    AsyncLogHelper.Info($"LBCamera[{SN}]: 硬触发事件 - {source}");
                                }
                            }
                            else
                            {
                                AsyncLogHelper.Warn(SN + "获取触发模式失败,跳过事件触发");
                            }
                            // 3 释放第一个图像资源并从列表移除
                            // 先释放Bitmap内存,再移除列表元素
                            firstBitmap.Dispose();
                            CollectedImages[SN].RemoveAt(0);
                            AsyncLogHelper.Info(SN + $"已消费缓存图像,剩余缓存数量:{CollectedImages[SN].Count}");
                        }
                        catch (Exception ex)
                        {
                            AsyncLogHelper.Error(SN + $"处理缓存图像异常:{ex.Message}", ex);
                            // 单个图像处理失败时,移除该图像避免阻塞后续处理
                            if (CollectedImages[SN].Count > 0)
                            {
                                try
                                {
                                    CollectedImages[SN][0]?.Dispose(); // 尝试释放
                                    CollectedImages[SN].RemoveAt(0);
                                }
                                catch (Exception innerEx)
                                {
                                    AsyncLogHelper.Error(SN + $"清理异常图像失败:{innerEx.Message}", innerEx);
                                }
                            }
                            // 单个图像处理失败不终止循环,继续处理下一个
                            // 4. 所有图像处理完成后,清空CallBackImg
                            if (CallBackImg != null)
                            {
                                CallBackImg.Dispose();
                                CallBackImg = null;
                            }
                            continue;
                        }
                        // 4. 所有图像处理完成后,清空CallBackImg
                        if (CallBackImg != null)
                        {
                            CallBackImg.Dispose();
                            CallBackImg = null;
                            AsyncLogHelper.Error($"LBCamera[{SN}]: 处理单帧图像异常 - {ex.Message}", ex);
                            continue; // 单帧异常,继续处理下一帧
                        }
                    }
                }
            });
            }
            catch (Exception ex)
            {
                AsyncLogHelper.Error($"LBCamera[{SN}]: 消费队列异常 - {ex.Message}", ex);
            }
        }
@@ -724,7 +775,7 @@
                        int intValue = 0;
                        double doubleValue = 0;
                        int enumValue = 0;
                        if (PHM6000Profiler.GetProfilerParameter(_cameraHandle, (int)nameId, ref intValue, ref doubleValue, ref enumValue) == 0)
                        {
                            if (p.PropertyType == typeof(int))