恢复Windows相机兼容枚举与采集并保持统一曝光控制
This commit is contained in:
parent
c3aa4a458c
commit
2349ac726d
@ -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 确认且完成概率达标后才推进。步骤说明为空、接口失败或证据不足时不会按规则直接完成。
|
||||
|
||||
@ -17,6 +17,9 @@ public class CameraInfo
|
||||
/// <summary>索引(UVC 设备号)</summary>
|
||||
public int Index { get; set; }
|
||||
|
||||
/// <summary>Windows 内部兼容采集标记;由枚举结果决定,不作为用户设置。</summary>
|
||||
public bool UseCompatibilityBackend { get; set; }
|
||||
|
||||
/// <summary>名称</summary>
|
||||
public string Name { get; set; } = "";
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@ using OpenCvSharp;
|
||||
|
||||
namespace SopHelper.Engine.Camera;
|
||||
|
||||
/// <summary>USB(UVC)相机:Windows 使用 MSMF,Linux 使用 V4L2。</summary>
|
||||
/// <summary>USB(UVC)相机:Windows 自动兼容采集接口,Linux 使用 V4L2。</summary>
|
||||
public class UvcCameraService : ICameraService
|
||||
{
|
||||
/// <summary>串行保护采集后端的设备创建与销毁。</summary>
|
||||
@ -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)
|
||||
{
|
||||
|
||||
@ -2,7 +2,7 @@ using OpenCvSharp;
|
||||
|
||||
namespace SopHelper.Engine.Camera;
|
||||
|
||||
/// <summary>用统一的毫秒设置适配 MSMF 与 V4L2;可选控制失败时静默跳过。</summary>
|
||||
/// <summary>用统一的毫秒设置适配 Windows 采集接口与 V4L2;可选控制失败时静默跳过。</summary>
|
||||
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 "";
|
||||
|
||||
@ -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");
|
||||
|
||||
Loading…
Reference in New Issue
Block a user