提交代码
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using HardwareControl.Core.HardwareServiceImpl.HardwareService;
|
||||
using HardwareControl.Core.SampleAnalysis;
|
||||
@@ -8,16 +9,20 @@ using Microsoft.Extensions.Logging;
|
||||
namespace HardwareControl.Core.HardwareServiceImpl
|
||||
{
|
||||
/// <summary>
|
||||
/// 采集基准画面 / 靶样画面并调用 ONNX 模型分析。
|
||||
/// 作为单例使用,保存本次检测的画面。
|
||||
/// 抓取相机当前画面并调用 ONNX 模型分析。
|
||||
/// 作为单例使用,保存最近一次分析结果供扫描流程复用。
|
||||
/// </summary>
|
||||
public sealed class SampleAnalysisService : ISampleAnalysisService
|
||||
{
|
||||
private const string SamplingPointConfigName = "camera_sampling_point";
|
||||
private const string SamplingRadiusConfigName = "camera_sampling_radius";
|
||||
private const string CameraScaleConfigName = "camera_scale";
|
||||
private const string IdentificationTimeoutConfigName = "camera_Identification_outtime";
|
||||
private const int DefaultSamplingPointCount = 5;
|
||||
private const int MaxSamplingPointCount = 100;
|
||||
private const int DefaultIdentificationTimeoutSeconds = 60;
|
||||
private const int MinIdentificationTimeoutSeconds = 5;
|
||||
private const int MaxIdentificationTimeoutSeconds = 300;
|
||||
|
||||
private readonly IUsbCameraService _camera;
|
||||
private readonly ISystemConfigService _systemConfig;
|
||||
@@ -25,8 +30,6 @@ namespace HardwareControl.Core.HardwareServiceImpl
|
||||
private readonly ILogger<SampleAnalysisService> _logger;
|
||||
|
||||
private readonly object _gate = new();
|
||||
private IReadOnlyList<CameraFrameSnapshot>? _baseline;
|
||||
private CameraFrameSnapshot? _target;
|
||||
private SampleAnalysisResult? _lastAnalysis;
|
||||
|
||||
public SampleAnalysisService(
|
||||
@@ -41,75 +44,9 @@ namespace HardwareControl.Core.HardwareServiceImpl
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task<SampleBaselineInfo> CaptureBaselineAsync(
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
|
||||
var frame = CaptureLatestFrame();
|
||||
|
||||
lock (_gate)
|
||||
{
|
||||
_baseline = new[] { frame };
|
||||
_target = null;
|
||||
_lastAnalysis = null;
|
||||
}
|
||||
|
||||
_logger.LogInformation(
|
||||
"基准画面已采集:{Width}x{Height}", frame.Width, frame.Height);
|
||||
|
||||
return Task.FromResult(
|
||||
new SampleBaselineInfo(frame.Width, frame.Height, frame.CapturedAt));
|
||||
}
|
||||
|
||||
public Task<SampleTargetInfo> CaptureTargetAsync(
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
|
||||
var frame = CaptureLatestFrame();
|
||||
|
||||
lock (_gate)
|
||||
{
|
||||
if (_baseline is null || _baseline.Count == 0)
|
||||
{
|
||||
throw new HardwareException(
|
||||
HardwareErrorKind.InvalidState,
|
||||
"尚未采集基准画面,请等待相机画面稳定后再试");
|
||||
}
|
||||
|
||||
_target = frame;
|
||||
_lastAnalysis = null;
|
||||
}
|
||||
|
||||
_logger.LogInformation(
|
||||
"靶样画面已采集:{Width}x{Height}", frame.Width, frame.Height);
|
||||
|
||||
return Task.FromResult(
|
||||
new SampleTargetInfo(frame.Width, frame.Height, frame.CapturedAt));
|
||||
}
|
||||
|
||||
public SampleAnalysisResult Analyze()
|
||||
{
|
||||
IReadOnlyList<CameraFrameSnapshot> baseline;
|
||||
CameraFrameSnapshot target;
|
||||
|
||||
lock (_gate)
|
||||
{
|
||||
baseline = _baseline ?? throw new HardwareException(
|
||||
HardwareErrorKind.InvalidState,
|
||||
"尚未采集基准画面,请返回样品放置界面重新检测");
|
||||
|
||||
target = _target ?? throw new HardwareException(
|
||||
HardwareErrorKind.InvalidState,
|
||||
"尚未采集靶样画面,请返回样品放置界面重新检测");
|
||||
|
||||
// 同一组画面重复调用 Analyze 时直接返回缓存,避免重复执行 ONNX 推理。
|
||||
if (_lastAnalysis is not null)
|
||||
{
|
||||
return _lastAnalysis;
|
||||
}
|
||||
}
|
||||
var target = CaptureLatestFrame();
|
||||
|
||||
var pointCount = GetSamplingPointCount();
|
||||
var samplingRadiusPixels = GetSamplingRadiusPixels();
|
||||
@@ -121,32 +58,83 @@ namespace HardwareControl.Core.HardwareServiceImpl
|
||||
? $"{samplingRadiusPixels.Value:F1}px"
|
||||
: "不限制");
|
||||
|
||||
var watch = Stopwatch.StartNew();
|
||||
var result = _analyzer.Analyze(
|
||||
baseline, target, pointCount, samplingRadiusPixels);
|
||||
target, pointCount, samplingRadiusPixels);
|
||||
watch.Stop();
|
||||
|
||||
_logger.LogInformation(
|
||||
"靶样轮廓分析完成:{Width}x{Height},采样点 {PointCount} 个",
|
||||
"靶样轮廓分析完成:{Width}x{Height},采样点 {PointCount} 个,总耗时 {Elapsed} ms",
|
||||
result.Width,
|
||||
result.Height,
|
||||
result.SamplingPointCount);
|
||||
result.SamplingPointCount,
|
||||
watch.ElapsedMilliseconds);
|
||||
|
||||
lock (_gate)
|
||||
{
|
||||
// 分析期间如果靶样画面已被重新采集,则不缓存本次结果。
|
||||
if (ReferenceEquals(_target, target))
|
||||
{
|
||||
_lastAnalysis = result;
|
||||
}
|
||||
_lastAnalysis = result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public SampleAnalysisResult GetLatestAnalysis()
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
if (_lastAnalysis is not null)
|
||||
{
|
||||
return _lastAnalysis;
|
||||
}
|
||||
}
|
||||
|
||||
return Analyze();
|
||||
}
|
||||
|
||||
private CameraFrameSnapshot CaptureLatestFrame() =>
|
||||
_camera.GetLatestFrame() ?? throw new HardwareException(
|
||||
HardwareErrorKind.NotConnected,
|
||||
"相机尚未产生画面,请确认相机已连接");
|
||||
|
||||
/// <summary>
|
||||
/// 读取系统参数 camera_Identification_outtime(秒),
|
||||
/// 作为前端等待轮廓识别结果的超时时间;未配置或无效时使用默认值。
|
||||
/// </summary>
|
||||
public int GetIdentificationTimeoutSeconds()
|
||||
{
|
||||
try
|
||||
{
|
||||
var config = _systemConfig
|
||||
.GetAll()
|
||||
.FirstOrDefault(c => string.Equals(
|
||||
c.ConfigName,
|
||||
IdentificationTimeoutConfigName,
|
||||
StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (double.TryParse(
|
||||
config?.ConfigValue,
|
||||
NumberStyles.Float,
|
||||
CultureInfo.InvariantCulture,
|
||||
out var seconds) &&
|
||||
seconds > 0)
|
||||
{
|
||||
return (int)Math.Clamp(
|
||||
Math.Round(seconds),
|
||||
MinIdentificationTimeoutSeconds,
|
||||
MaxIdentificationTimeoutSeconds);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
ex,
|
||||
"读取系统参数 {ConfigName} 失败,使用默认识别超时时间",
|
||||
IdentificationTimeoutConfigName);
|
||||
}
|
||||
|
||||
return DefaultIdentificationTimeoutSeconds;
|
||||
}
|
||||
|
||||
private int GetSamplingPointCount()
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user