From 2349ac726d498d93c642b072f0e578502b9eeeea Mon Sep 17 00:00:00 2001 From: tfarcraw <123211521@163.com> Date: Wed, 18 Nov 2026 20:53:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=81=A2=E5=A4=8DWindows=E7=9B=B8=E6=9C=BA?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E6=9E=9A=E4=B8=BE=E4=B8=8E=E9=87=87=E9=9B=86?= =?UTF-8?q?=E5=B9=B6=E4=BF=9D=E6=8C=81=E7=BB=9F=E4=B8=80=E6=9B=9D=E5=85=89?= =?UTF-8?q?=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../Camera/ICameraService.cs | 3 ++ .../Camera/UvcCameraService.cs | 30 +++++++++++-------- .../Camera/UvcExposureControl.cs | 9 ++++-- tests/Avalonia.Smoke/Program.cs | 5 ++++ 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 01a3aa6..e13059b 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ pwsh -File tools/Start-HandAiD3.ps1 ## 操作流程 1. 在“运行设置”中选择 UVC 或海康相机,新建或选择配方。设置页保留相机、配方和抽帧参数。 -2. 配方编辑器包含“步骤、区域、全局设置、SOP流程视频、Agent助手”五页。在原始相机画面上绘制区域;相机设置独立于配方,在“运行设置”中点击相机后面的“配置”按钮打开专用窗口。USB 分辨率、目标帧率、视频格式和自动曝光使用下拉菜单选择,关闭自动曝光后可填写以毫秒为单位的手动曝光时间(留空保留当前时间);海康相机可设置曝光时间。USB 采集统一使用 OpenCV:Windows 使用 Media Foundation(MSMF),Linux 使用 V4L2,无需选择采集方式。曝光控制分别适配 MSMF 的对数档位及 V4L2 的原始控制值,Windows 会匹配最接近的曝光档位;设备不支持或控制失败时静默跳过。已移除 Windows 专属驱动属性页,旧设置中的采集方式及补偿字段会被忽略。设置按设备保存在 workspace/camera-settings.json,保存后下次开启采集、取图或录像预览时生效。SOP 日志显示驱动回读采集模式,状态栏分别显示 USB 实测采集和渲染 fps。 +2. 配方编辑器包含“步骤、区域、全局设置、SOP流程视频、Agent助手”五页。在原始相机画面上绘制区域;相机设置独立于配方,在“运行设置”中点击相机后面的“配置”按钮打开专用窗口。USB 分辨率、目标帧率、视频格式和自动曝光使用下拉菜单选择,关闭自动曝光后可填写以毫秒为单位的手动曝光时间(留空保留当前时间);海康相机可设置曝光时间。USB 采集统一使用 OpenCV:Windows 自动使用设备可用的兼容接口(DirectShow/MSMF),枚举结果记录内部接口标记,保证连接时沿用同一设备索引体系;Linux 使用 V4L2,无需选择采集方式。曝光控制分别适配 Windows 的对数档位及 V4L2 的原始控制值,Windows 会匹配最接近的曝光档位;设备不支持或控制失败时静默跳过。已移除 Windows 专属驱动属性页,旧设置中的采集方式及补偿字段会被忽略。设置按设备保存在 workspace/camera-settings.json,保存后下次开启采集、取图或录像预览时生效。SOP 日志显示驱动回读采集模式,状态栏分别显示 USB 实测采集和渲染 fps。 3. 步骤名描述操作,“说明”填写送检单帧能看到的静态完成状态,AI 用说明判断图片。触发规则支持手在区域内、手进入后离开区域、A→B 移动和持续计时。 4. 送检可选整张画面、固定区域或跟随手部;跟随手部可以设置扩展距离、最小范围和位置保留时间。 5. 保存、应用后开始采集。规则满足只触发送图;AI 确认且完成概率达标后才推进。步骤说明为空、接口失败或证据不足时不会按规则直接完成。 diff --git a/src/SopHelper.CrossPlatform.Engine/Camera/ICameraService.cs b/src/SopHelper.CrossPlatform.Engine/Camera/ICameraService.cs index 096c810..bfb8039 100644 --- a/src/SopHelper.CrossPlatform.Engine/Camera/ICameraService.cs +++ b/src/SopHelper.CrossPlatform.Engine/Camera/ICameraService.cs @@ -17,6 +17,9 @@ public class CameraInfo /// 索引(UVC 设备号) public int Index { get; set; } + /// Windows 内部兼容采集标记;由枚举结果决定,不作为用户设置。 + public bool UseCompatibilityBackend { get; set; } + /// 名称 public string Name { get; set; } = ""; } diff --git a/src/SopHelper.CrossPlatform.Engine/Camera/UvcCameraService.cs b/src/SopHelper.CrossPlatform.Engine/Camera/UvcCameraService.cs index 508492c..0381dab 100644 --- a/src/SopHelper.CrossPlatform.Engine/Camera/UvcCameraService.cs +++ b/src/SopHelper.CrossPlatform.Engine/Camera/UvcCameraService.cs @@ -2,7 +2,7 @@ using OpenCvSharp; namespace SopHelper.Engine.Camera; -/// USB(UVC)相机:Windows 使用 MSMF,Linux 使用 V4L2。 +/// USB(UVC)相机:Windows 自动兼容采集接口,Linux 使用 V4L2。 public class UvcCameraService : ICameraService { /// 串行保护采集后端的设备创建与销毁。 @@ -53,7 +53,8 @@ public class UvcCameraService : ICameraService { if (_capture is not null && Current?.Index == index) { - list.Add(new CameraInfo { Index = index, Name = $"Camera {index}(当前已连接)" }); + list.Add(new CameraInfo { Index = index, Name = $"Camera {index}(当前已连接)", + UseCompatibilityBackend = Current.UseCompatibilityBackend }); continue; } } @@ -62,14 +63,16 @@ public class UvcCameraService : ICameraService { try { - using var probe = new VideoCapture(index, OperatingSystem.IsWindows() ? VideoCaptureAPIs.MSMF : VideoCaptureAPIs.V4L2); - if (!probe.IsOpened()) continue; - - var w = probe.Get(VideoCaptureProperties.FrameWidth); - var h = probe.Get(VideoCaptureProperties.FrameHeight); - if (w <= 0 || h <= 0) continue; - - list.Add(new CameraInfo { Index = index, Name = $"Camera {index}" }); + // Windows 的部分 UVC 驱动无法被 MSMF 打开。探测和连接必须使用相同后端,避免索引顺序差异。 + using var probe = OperatingSystem.IsWindows() + ? TryOpen(index, VideoCaptureAPIs.DSHOW) ?? TryOpen(index, VideoCaptureAPIs.MSMF) + : TryOpen(index, VideoCaptureAPIs.V4L2); + if (probe is null) continue; + list.Add(new CameraInfo + { + Index = index, Name = $"Camera {index}", + UseCompatibilityBackend = probe.Get(VideoCaptureProperties.Backend) == (double)VideoCaptureAPIs.DSHOW + }); } catch { @@ -98,8 +101,11 @@ public class UvcCameraService : ICameraService _capture = null; } - _capture = TryOpen(camera.Index, - OperatingSystem.IsWindows() ? VideoCaptureAPIs.MSMF : VideoCaptureAPIs.V4L2); + _capture = OperatingSystem.IsWindows() + ? camera.UseCompatibilityBackend + ? TryOpen(camera.Index, VideoCaptureAPIs.DSHOW) + : TryOpen(camera.Index, VideoCaptureAPIs.MSMF) ?? TryOpen(camera.Index, VideoCaptureAPIs.DSHOW) + : TryOpen(camera.Index, VideoCaptureAPIs.V4L2); if (_capture is null) { diff --git a/src/SopHelper.CrossPlatform.Engine/Camera/UvcExposureControl.cs b/src/SopHelper.CrossPlatform.Engine/Camera/UvcExposureControl.cs index 5d151a7..8618034 100644 --- a/src/SopHelper.CrossPlatform.Engine/Camera/UvcExposureControl.cs +++ b/src/SopHelper.CrossPlatform.Engine/Camera/UvcExposureControl.cs @@ -2,7 +2,7 @@ using OpenCvSharp; namespace SopHelper.Engine.Camera; -/// 用统一的毫秒设置适配 MSMF 与 V4L2;可选控制失败时静默跳过。 +/// 用统一的毫秒设置适配 Windows 采集接口与 V4L2;可选控制失败时静默跳过。 public static class UvcExposureControl { public static string TryApply(UvcCaptureOptions options, VideoCaptureAPIs backend, @@ -11,14 +11,17 @@ public static class UvcExposureControl if (options.AutoExposure is not { } automatic) return ""; try { - if (backend == VideoCaptureAPIs.MSMF) + if (backend is VideoCaptureAPIs.MSMF or VideoCaptureAPIs.DSHOW) { // MSMF 的 AutoExposure=0 同时会把曝光值写成 0(1 秒)。 // 关闭自动曝光应写 Exposure:它会使用 Manual 标志并保留当前时间或设置新时间。 var current = get(VideoCaptureProperties.Exposure); if (!double.IsFinite(current)) return ""; if (automatic) - return set(VideoCaptureProperties.AutoExposure, current == 0 ? 1 : current) ? ";USB 曝光:自动" : ""; + { + var autoValue = backend == VideoCaptureAPIs.DSHOW ? 1 : current == 0 ? 1 : current; + return set(VideoCaptureProperties.AutoExposure, autoValue) ? ";USB 曝光:自动" : ""; + } var target = options.ExposureTimeMs is { } ms ? Math.Round(Math.Log2(ms / 1000), MidpointRounding.AwayFromZero) : current; if (!set(VideoCaptureProperties.Exposure, target)) return ""; diff --git a/tests/Avalonia.Smoke/Program.cs b/tests/Avalonia.Smoke/Program.cs index 7c31cac..25f76d5 100644 --- a/tests/Avalonia.Smoke/Program.cs +++ b/tests/Avalonia.Smoke/Program.cs @@ -592,6 +592,11 @@ internal static class Smoke UvcExposureControl.TryApply(new(){AutoExposure=true},VideoCaptureAPIs.MSMF,Get,Set); Check(writes.Last()==(VideoCaptureProperties.AutoExposure,-6d),"Enabling MSMF automatic exposure uses a nonzero value with the auto flag"); writes.Clear(); + UvcExposureControl.TryApply(new(){AutoExposure=true},VideoCaptureAPIs.DSHOW,Get,Set); + Check(writes.Last()==(VideoCaptureProperties.AutoExposure,1d),"Windows compatibility capture uses its own automatic-exposure flag encoding"); + UvcExposureControl.TryApply(new(){AutoExposure=false,ExposureTimeMs=15.625},VideoCaptureAPIs.DSHOW,Get,Set); + Check(controls[VideoCaptureProperties.Exposure]==-6,"Windows compatibility capture also supports unified manual milliseconds"); + writes.Clear(); var linux=UvcExposureControl.TryApply(new(){AutoExposure=false,ExposureTimeMs=15.6},VideoCaptureAPIs.V4L2,Get,Set); Check(controls[VideoCaptureProperties.AutoExposure]==1&&controls[VideoCaptureProperties.Exposure]==156&&controls[VideoCaptureProperties.Mode]==1&&linux.Contains("15.6"), "Linux exposure uses raw 100-microsecond units and restores OpenCV normalization");