diff --git a/README.md b/README.md index fa012f0..3e29a61 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ pwsh -File tools/Start-HandAiD3.ps1 ## 操作流程 1. 在“运行设置”中选择 UVC 或海康相机,新建或选择配方。设置页保留相机、配方和抽帧参数。 -2. 配方编辑器包含“步骤、区域、全局设置、SOP流程视频、Agent助手”五页。在原始相机画面上绘制区域;相机设置独立于配方,在“运行设置”中点击相机后面的“配置”按钮打开专用窗口。USB 分辨率、目标帧率、视频格式、曝光补偿和低亮度补偿使用下拉菜单选择,列表提供常用配置,不代表设备均支持;海康相机可设置曝光时间。设置按设备保存在 workspace/camera-settings.json,保存后下次开启采集、取图或录像预览时生效。SOP 日志显示驱动回读模式,状态栏分别显示 USB 实测采集和渲染 fps。曝光补偿以 EV 为单位,负值压低自动曝光;选择“不修改”保留当前值,0 EV 清除补偿。当前通过 Windows MSMF 对应设备的 KS 扩展接口写入并回读确认,不支持的设备、步长或平台,以及曝光补偿设置失败时,均静默跳过,不提示、不阻断采集。低亮度补偿提供“不修改/关闭/开启”,对应 UVC 属性页同名开关(自动曝光优先级),按设备保存,并在采集、取图、录像预览启动前应用;不支持或设置失败时同样静默跳过,成功回读后记录运行日志。 +2. 配方编辑器包含“步骤、区域、全局设置、SOP流程视频、Agent助手”五页。在原始相机画面上绘制区域;相机设置独立于配方,在“运行设置”中点击相机后面的“配置”按钮打开专用窗口。USB 分辨率、目标帧率、视频格式和采集方式使用下拉菜单选择;海康相机可设置曝光时间。设置按设备保存在 workspace/camera-settings.json,保存后下次开启采集、取图或录像预览时生效。要尝试驱动原生图像控制,选择“DirectShow(兼容 VideoCap)”并保存,开启采集后点击“驱动属性”,通过当前 DirectShow 后端打开所选相机驱动提供的原生页面,可调整其中的曝光、亮度或其他受支持开关。原生页面的项目和保存行为由驱动决定,程序不再模拟 EV 补偿或低亮度补偿开关,也不应用旧配置中的这两个字段。原生属性页不可用时静默跳过;SOP 日志显示驱动回读采集模式,状态栏分别显示 USB 实测采集和渲染 fps。 3. 步骤名描述操作,“说明”填写送检单帧能看到的静态完成状态,AI 用说明判断图片。触发规则支持手在区域内、手进入后离开区域、A→B 移动和持续计时。 4. 送检可选整张画面、固定区域或跟随手部;跟随手部可以设置扩展距离、最小范围和位置保留时间。 5. 保存、应用后开始采集。规则满足只触发送图;AI 确认且完成概率达标后才推进。步骤说明为空、接口失败或证据不足时不会按规则直接完成。 diff --git a/src/SopHelper.Avalonia/Models/CameraSettingsStore.cs b/src/SopHelper.Avalonia/Models/CameraSettingsStore.cs index bcc942d..c75159d 100644 --- a/src/SopHelper.Avalonia/Models/CameraSettingsStore.cs +++ b/src/SopHelper.Avalonia/Models/CameraSettingsStore.cs @@ -44,6 +44,12 @@ public sealed class CameraSettingsStore(string root) } finally { if (File.Exists(temporary)) File.Delete(temporary); } } + public Task ConnectAsync(ICameraService service, CameraInfo camera) + { + var settings = Load(camera); + settings.Validate(); + return service.ConnectAsync(camera, settings.UsbCapture); + } public async Task ApplyAsync(ICameraService service, CameraInfo camera) { var settings = Load(camera); diff --git a/src/SopHelper.Avalonia/Training/CameraViewModel.cs b/src/SopHelper.Avalonia/Training/CameraViewModel.cs index 59b359c..d1fca5d 100644 --- a/src/SopHelper.Avalonia/Training/CameraViewModel.cs +++ b/src/SopHelper.Avalonia/Training/CameraViewModel.cs @@ -178,7 +178,7 @@ public sealed partial class CameraViewModel : BindableBase } var selected = _selectedCameraProvider() ?? throw new InvalidOperationException("请先在运行设置中选择相机"); Status = "正在连接 " + selected.Name; - if (!await _camera.ConnectAsync(selected)) throw new IOException("相机连接失败,请检查设备是否被其他程序占用"); + if (!await SopHelper.Models.CameraSettingsStore.Current.ConnectAsync(_camera, selected)) throw new IOException("相机连接失败,请检查设备是否被其他程序占用"); if (_closing) return; try { await SopHelper.Models.CameraSettingsStore.Current.ApplyAsync(_camera, selected); } catch { await Task.Run(_camera.Disconnect); throw; } diff --git a/src/SopHelper.Avalonia/VVMs/CameraSettings/CameraSettingsViewModel.cs b/src/SopHelper.Avalonia/VVMs/CameraSettings/CameraSettingsViewModel.cs index 2055682..f2021d3 100644 --- a/src/SopHelper.Avalonia/VVMs/CameraSettings/CameraSettingsViewModel.cs +++ b/src/SopHelper.Avalonia/VVMs/CameraSettings/CameraSettingsViewModel.cs @@ -13,13 +13,9 @@ public sealed record FrameRatePreset(double? Fps) { public override string ToString() => Fps is null ? "驱动默认" : $"{Fps:0.##} fps"; } -public sealed record ExposureCompensationPreset(double? Ev) +public sealed record CaptureBackendPreset(UvcCaptureBackend Backend, string Label) { - public override string ToString() => Ev is null ? "不修改" : $"{Ev:+0.##;-0.##;0} EV"; -} -public sealed record LowLightCompensationPreset(bool? Enabled) -{ - public override string ToString() => Enabled switch { true => "开启", false => "关闭", null => "不修改" }; + public override string ToString() => Label; } public sealed class CameraSettingsViewModel : BindableBase { @@ -35,12 +31,11 @@ public sealed class CameraSettingsViewModel : BindableBase new(3264,2448), new(3840,2160), new(4096,2160), new(4608,3456), new(4656,3496)]); public ObservableCollection FrameRates { get; } = new([ new(null), new(5), new(10), new(15), new(20), new(24), new(25), new(30), new(50), new(60), new(120)]); - public ObservableCollection ExposureCompensations { get; } = new([ - new(null), new(-3), new(-2), new(-5d/3), new(-4d/3), new(-1), new(-2d/3), new(-1d/3), - new(0), new(1d/3), new(2d/3), new(1), new(2), new(3)]); - public ExposureCompensationPreset SelectedExposureCompensation { get; set; } - public LowLightCompensationPreset[] LowLightCompensations { get; } = [new(null), new(false), new(true)]; - public LowLightCompensationPreset SelectedLowLightCompensation { get; set; } + public CaptureBackendPreset[] Backends { get; } = [ + new(UvcCaptureBackend.Default, "自动选择"), + new(UvcCaptureBackend.DirectShow, "DirectShow(兼容 VideoCap)"), + new(UvcCaptureBackend.MediaFoundation, "Media Foundation")]; + public CaptureBackendPreset SelectedBackend { get; set; } public string[] Formats { get; } = ["驱动默认", "MJPG", "YUY2"]; public ResolutionPreset SelectedResolution { get; set; } public FrameRatePreset SelectedFrameRate { get; set; } @@ -53,8 +48,7 @@ public sealed class CameraSettingsViewModel : BindableBase { _camera = camera; _store = store; SelectedResolution = Resolutions[0]; SelectedFrameRate = FrameRates[0]; - SelectedExposureCompensation = ExposureCompensations[0]; - SelectedLowLightCompensation = LowLightCompensations[0]; + SelectedBackend = Backends[0]; try { var settings = store.Load(camera); @@ -66,10 +60,7 @@ public sealed class CameraSettingsViewModel : BindableBase if (!FrameRates.Contains(rate)) FrameRates.Add(rate); SelectedFrameRate = FrameRates.First(r => r == rate); SelectedFormat = string.IsNullOrWhiteSpace(settings.UsbCapture.Format) ? Formats[0] : settings.UsbCapture.Format.Trim().ToUpperInvariant(); - var compensation = new ExposureCompensationPreset(settings.UsbCapture.ExposureCompensationEv); - if (!ExposureCompensations.Contains(compensation)) ExposureCompensations.Add(compensation); - SelectedExposureCompensation = ExposureCompensations.First(c => c == compensation); - SelectedLowLightCompensation = LowLightCompensations.Single(p => p.Enabled == settings.UsbCapture.LowLightCompensation); + SelectedBackend = Backends.Single(p => p.Backend == settings.UsbCapture.Backend); ExposureTimeUs = settings.ExposureTimeUs; } catch (Exception ex) { Error = "读取相机设置失败:" + ex.Message; CanSave = false; } @@ -85,8 +76,7 @@ public sealed class CameraSettingsViewModel : BindableBase { Width = SelectedResolution.Width, Height = SelectedResolution.Height, Fps = SelectedFrameRate.Fps, Format = SelectedFormat == Formats[0] ? null : SelectedFormat, - ExposureCompensationEv = SelectedExposureCompensation.Ev, - LowLightCompensation = SelectedLowLightCompensation.Enabled + Backend = SelectedBackend.Backend } : new(), ExposureTimeUs = IsIndustrial ? ExposureTimeUs : null }); diff --git a/src/SopHelper.Avalonia/VVMs/CameraSettings/CameraSettingsWindow.axaml b/src/SopHelper.Avalonia/VVMs/CameraSettings/CameraSettingsWindow.axaml index 5554b61..0cca21f 100644 --- a/src/SopHelper.Avalonia/VVMs/CameraSettings/CameraSettingsWindow.axaml +++ b/src/SopHelper.Avalonia/VVMs/CameraSettings/CameraSettingsWindow.axaml @@ -1,6 +1,6 @@ + Width="520" Height="520" MinWidth="440" MinHeight="400" Background="{StaticResource Bg}" WindowStartupLocation="CenterOwner"> @@ -22,15 +22,10 @@ - - + + - - - - - - + diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditorViewModel.cs b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditorViewModel.cs index c69a63f..3875826 100644 --- a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditorViewModel.cs +++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditorViewModel.cs @@ -234,7 +234,7 @@ public partial class RecipeEditorViewModel : BindableBase, IDialogAware } try { - if (!await camera.ConnectAsync(_cameraInfo)) throw new InvalidOperationException("连接相机失败,请查看 SOP 页日志"); + if (!await SopHelper.Models.CameraSettingsStore.Current.ConnectAsync(camera, _cameraInfo)) throw new InvalidOperationException("连接相机失败,请查看 SOP 页日志"); await SopHelper.Models.CameraSettingsStore.Current.ApplyAsync(camera, _cameraInfo); camera.FrameArrived += Receive; if (!await camera.StartAsync()) throw new InvalidOperationException("启动采集失败"); diff --git a/src/SopHelper.Avalonia/VVMs/RuntimeSettings/RuntimeSettings.axaml b/src/SopHelper.Avalonia/VVMs/RuntimeSettings/RuntimeSettings.axaml index 1b68c41..405788f 100644 --- a/src/SopHelper.Avalonia/VVMs/RuntimeSettings/RuntimeSettings.axaml +++ b/src/SopHelper.Avalonia/VVMs/RuntimeSettings/RuntimeSettings.axaml @@ -2,7 +2,7 @@ - + @@ -12,6 +12,7 @@ + @@ -22,6 +23,7 @@