修复旧录像目录配置导致相机无法打开的问题
This commit is contained in:
parent
7555d23465
commit
a2414fd944
@ -20,7 +20,7 @@ pwsh -File tools/Start-HandAiD3.ps1
|
||||
## 操作流程
|
||||
|
||||
1. 在“运行设置”中选择 UVC 或海康相机,新建或选择配方。设置页保留相机、配方和抽帧参数。
|
||||
2. 配方编辑器包含“步骤、区域、全局设置、SOP流程视频、Agent助手”五页。在原始相机画面上绘制区域;相机设置独立于配方,在“运行设置”中点击相机后面的“配置”按钮打开专用窗口。USB 分辨率、目标帧率、视频格式和自动曝光使用下拉菜单选择,关闭自动曝光后可填写以毫秒为单位的手动曝光时间(留空保留当前时间);海康相机可设置曝光时间。USB 采集统一使用 OpenCV:Windows 自动使用设备可用的兼容接口(DirectShow/MSMF),枚举结果记录内部接口标记,保证连接时沿用同一设备索引体系;Linux 使用 V4L2,无需选择采集方式。曝光控制分别适配 Windows 的对数档位及 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-device-settings.json,保存后下次开启采集、取图或录像预览时生效。SOP 日志显示驱动回读采集模式,状态栏分别显示 USB 实测采集和渲染 fps。
|
||||
3. 步骤名描述操作,“说明”填写送检单帧能看到的静态完成状态,AI 用说明判断图片。触发规则支持手在区域内、手进入后离开区域、A→B 移动和持续计时。
|
||||
4. 送检可选整张画面、固定区域或跟随手部;跟随手部可以设置扩展距离、最小范围和位置保留时间。
|
||||
5. 保存、应用后开始采集。规则满足只触发送图;AI 确认且完成概率达标后才推进。步骤说明为空、接口失败或证据不足时不会按规则直接完成。
|
||||
|
||||
@ -21,14 +21,28 @@ public sealed record CameraDeviceSettings
|
||||
/// <summary>按设备保存相机设置,独立于所有配方。</summary>
|
||||
public sealed class CameraSettingsStore(string root)
|
||||
{
|
||||
public string FilePath => Path.Combine(root, "workspace", "camera-settings.json");
|
||||
public string FilePath => Path.Combine(root, "workspace", "camera-device-settings.json");
|
||||
public static CameraSettingsStore Current => new(DesktopPlatform.WorkspaceRoot);
|
||||
private static string Key(CameraInfo camera) => camera.Kind == CameraKind.Hikvision
|
||||
? $"Hikvision:{camera.SerialNumber}" : $"Uvc:{camera.Index}";
|
||||
private Dictionary<string, CameraDeviceSettings> Read() => File.Exists(FilePath)
|
||||
? JsonSerializer.Deserialize<Dictionary<string, CameraDeviceSettings>>(File.ReadAllText(FilePath))
|
||||
?? throw new InvalidDataException("相机设置文件内容为空")
|
||||
: new();
|
||||
private Dictionary<string, CameraDeviceSettings> Read()
|
||||
{
|
||||
// 旧路径也用于录像目录;只迁移设备项,避免把 directory 字符串读成设备设置。
|
||||
var path = File.Exists(FilePath) ? FilePath : Path.Combine(root, "workspace", "camera-settings.json");
|
||||
if (!File.Exists(path)) return new();
|
||||
using var json = JsonDocument.Parse(File.ReadAllText(path));
|
||||
if (json.RootElement.ValueKind != JsonValueKind.Object)
|
||||
throw new InvalidDataException("相机设置文件必须为 JSON 对象");
|
||||
var settings = new Dictionary<string, CameraDeviceSettings>();
|
||||
foreach (var property in json.RootElement.EnumerateObject())
|
||||
{
|
||||
if (!property.Name.StartsWith("Uvc:", StringComparison.Ordinal) &&
|
||||
!property.Name.StartsWith("Hikvision:", StringComparison.Ordinal)) continue;
|
||||
settings[property.Name] = property.Value.Deserialize<CameraDeviceSettings>()
|
||||
?? throw new InvalidDataException("相机设备设置内容为空:" + property.Name);
|
||||
}
|
||||
return settings;
|
||||
}
|
||||
public CameraDeviceSettings Load(CameraInfo camera) => Read().GetValueOrDefault(Key(camera)) ?? new();
|
||||
public void Save(CameraInfo camera, CameraDeviceSettings settings)
|
||||
{
|
||||
|
||||
4
tests/CameraSettings.Smoke/CameraSettings.Smoke.csproj
Normal file
4
tests/CameraSettings.Smoke/CameraSettings.Smoke.csproj
Normal file
@ -0,0 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup><OutputType>Exe</OutputType><TargetFramework>net8.0</TargetFramework><ImplicitUsings>enable</ImplicitUsings><Nullable>enable</Nullable></PropertyGroup>
|
||||
<ItemGroup><ProjectReference Include="../../src/SopHelper.Avalonia/SopHelper.Avalonia.csproj" /></ItemGroup>
|
||||
</Project>
|
||||
46
tests/CameraSettings.Smoke/Program.cs
Normal file
46
tests/CameraSettings.Smoke/Program.cs
Normal file
@ -0,0 +1,46 @@
|
||||
using System.Text.Json;
|
||||
using SopHelper.Models;
|
||||
using SopHelper.Engine.Camera;
|
||||
|
||||
var root=Path.Combine(Path.GetTempPath(),"sophelper-camera-settings-"+Guid.NewGuid().ToString("N"));
|
||||
Directory.CreateDirectory(Path.Combine(root,"workspace"));
|
||||
try
|
||||
{
|
||||
var legacy=Path.Combine(root,"workspace","camera-settings.json");
|
||||
var original="{\"directory\":\"/previous/recordings\"}";
|
||||
File.WriteAllText(legacy,original);
|
||||
var store=new CameraSettingsStore(root);
|
||||
var usb=new CameraInfo {Index=0};
|
||||
if(!store.Load(usb).UsbCapture.IsDefault)throw new Exception("旧录像目录应使用默认相机参数");
|
||||
Console.WriteLine("PASS: 旧录像目录文件不再阻止读取相机参数");
|
||||
File.WriteAllText(legacy,"{\"Uvc:0\":{\"UsbCapture\":{\"Width\":640,\"Height\":480}},\"Uvc:2\":{\"ExposureTimeUs\":1000}}");
|
||||
if(store.Load(usb).UsbCapture.Width!=640)throw new Exception("现有设备参数没有迁移");
|
||||
var settings=new CameraDeviceSettings {UsbCapture=new() {Width=1280,Height=720,Format="MJPG",Fps=30}};
|
||||
store.Save(usb,settings);
|
||||
if(store.Load(usb).UsbCapture.Width!=1280||store.Load(new CameraInfo {Index=2}).ExposureTimeUs!=1000)throw new Exception("保存时丢失设备参数");
|
||||
File.WriteAllText(legacy,original);
|
||||
if(store.Load(usb).UsbCapture.Width!=1280)throw new Exception("录像目录覆盖了设备参数");
|
||||
store.Save(usb,settings);
|
||||
if(File.ReadAllText(legacy)!=original)throw new Exception("保存相机参数改写了录像目录");
|
||||
Console.WriteLine("PASS: 旧设备参数可迁移;录像目录与设备参数互不覆盖");
|
||||
}
|
||||
finally {Directory.Delete(root,true);}
|
||||
|
||||
if(args.Contains("--hardware"))
|
||||
{
|
||||
var deviceRoot="/home/wl/sophelper";
|
||||
var store=new CameraSettingsStore(deviceRoot);
|
||||
using var camera=new CameraService();
|
||||
camera.Error+=Console.Error.WriteLine;
|
||||
var cameras=await camera.EnumerateAsync();
|
||||
var selected=cameras.FirstOrDefault(c=>c.Kind==CameraKind.Uvc)??throw new Exception("未枚举到 USB 相机");
|
||||
if(!await store.ConnectAsync(camera,selected))throw new Exception("相机连接失败");
|
||||
Console.WriteLine(await store.ApplyAsync(camera,selected));
|
||||
var frames=0;var width=0;var height=0;
|
||||
camera.FrameArrived+=frame=>{width=frame.Bgr.Width;height=frame.Bgr.Height;Interlocked.Increment(ref frames);};
|
||||
if(!await camera.StartAsync())throw new Exception("相机采集启动失败");
|
||||
await Task.Delay(5000);
|
||||
camera.Stop();
|
||||
if(frames<10)throw new Exception("相机未持续输出图像,帧数="+frames);
|
||||
Console.WriteLine(JsonSerializer.Serialize(new {passed=true,camera=selected.Name,frames,width,height}));
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user