1111
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
using HardwareControl.Core;
|
||||
using HardwareControl.Core.HardwareServiceImpl.HardwareService;
|
||||
using HardwareWeb.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HardwareWeb.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// 靶样轮廓识别与采样点规划接口。
|
||||
/// 流程:采集基准画面 -> 采集靶样画面 -> 分析并返回处理后的图像。
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/sample")]
|
||||
public class SampleAnalysisController : ControllerBase
|
||||
{
|
||||
private readonly ISampleAnalysisService _service;
|
||||
private readonly ILogger<SampleAnalysisController> _logger;
|
||||
|
||||
public SampleAnalysisController(
|
||||
ISampleAnalysisService service,
|
||||
ILogger<SampleAnalysisController> logger)
|
||||
{
|
||||
_service = service;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>连续抓取多帧并取中值作为基准背景画面。</summary>
|
||||
[HttpPost("baseline")]
|
||||
[ProducesResponseType(typeof(SampleBaselinePayload), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> CaptureBaseline(CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(SampleBaselinePayload.From(
|
||||
await _service.CaptureBaselineAsync(ct)));
|
||||
}
|
||||
catch (HardwareException ex)
|
||||
{
|
||||
return HardwareError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>抓取当前最新画面作为靶样画面。</summary>
|
||||
[HttpPost("target")]
|
||||
[ProducesResponseType(typeof(SampleTargetPayload), StatusCodes.Status200OK)]
|
||||
public IActionResult CaptureTarget()
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(SampleTargetPayload.From(_service.CaptureTarget()));
|
||||
}
|
||||
catch (HardwareException ex)
|
||||
{
|
||||
return HardwareError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>提取靶样轮廓、绘制采样点并返回处理后的 JPEG 图像。</summary>
|
||||
[HttpPost("analyze")]
|
||||
[ProducesResponseType(typeof(SampleAnalysisPayload), StatusCodes.Status200OK)]
|
||||
public IActionResult Analyze()
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(SampleAnalysisPayload.From(_service.Analyze()));
|
||||
}
|
||||
catch (HardwareException ex)
|
||||
{
|
||||
return HardwareError(ex);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "靶样轮廓分析失败");
|
||||
return StatusCode(
|
||||
StatusCodes.Status500InternalServerError,
|
||||
new { error = "AnalyzeFailed", message = "靶样轮廓分析失败,请重试" });
|
||||
}
|
||||
}
|
||||
|
||||
private IActionResult HardwareError(HardwareException ex)
|
||||
{
|
||||
var statusCode = ex.Kind switch
|
||||
{
|
||||
HardwareErrorKind.InvalidArgument => StatusCodes.Status400BadRequest,
|
||||
HardwareErrorKind.NotConnected => StatusCodes.Status409Conflict,
|
||||
HardwareErrorKind.InvalidState => StatusCodes.Status409Conflict,
|
||||
HardwareErrorKind.Timeout => StatusCodes.Status504GatewayTimeout,
|
||||
_ => StatusCodes.Status500InternalServerError,
|
||||
};
|
||||
|
||||
if (statusCode >= 500)
|
||||
{
|
||||
_logger.LogError(ex, "靶样分析接口失败:{Message}", ex.Message);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("靶样分析接口失败:{Message}", ex.Message);
|
||||
}
|
||||
|
||||
return new ObjectResult(new
|
||||
{
|
||||
error = ex.Kind.ToString(),
|
||||
message = ex.Message
|
||||
})
|
||||
{
|
||||
StatusCode = statusCode
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,19 @@
|
||||
<ProjectReference Include="..\HardwareControl.Core\HardwareControl.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<!--
|
||||
ONNX 分割模型放在 Models 目录下,构建时自动复制到输出目录。
|
||||
默认加载路径为 Models 目录(见 appsettings.json)。
|
||||
-->
|
||||
<ItemGroup>
|
||||
<Content Include="Models\**\*.onnx" CopyToOutputDirectory="PreserveNewest" />
|
||||
<Content Include="..\HardwareControl.Core\Models\**\*.onnx" Link="Models\%(RecursiveDir)%(Filename)%(Extension)" CopyToOutputDirectory="PreserveNewest" CopyToPublishDirectory="PreserveNewest" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Remove="..\HardwareControl.Core\Models\3.medicinals_expert_logits.onnx" />
|
||||
<Content Remove="..\HardwareControl.Core\Models\4.highvalue_expert_logits.onnx" />
|
||||
</ItemGroup>
|
||||
|
||||
<!--
|
||||
HardwareControl.Core 通过裸 <Reference> 引用 ArisSharp.dll。
|
||||
这里再显式引用一次,确保 ArisSharp 出现在 HardwareWeb.deps.json 中;
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
using HardwareControl.Core.SampleAnalysis;
|
||||
|
||||
namespace HardwareWeb.Models
|
||||
{
|
||||
/// <summary>基准画面采集结果。</summary>
|
||||
public sealed record SampleBaselinePayload(
|
||||
int Width,
|
||||
int Height,
|
||||
DateTimeOffset CapturedAt)
|
||||
{
|
||||
public static SampleBaselinePayload From(SampleBaselineInfo info) =>
|
||||
new(info.Width, info.Height, info.CapturedAt);
|
||||
}
|
||||
|
||||
/// <summary>靶样画面采集结果。</summary>
|
||||
public sealed record SampleTargetPayload(
|
||||
int Width,
|
||||
int Height,
|
||||
DateTimeOffset CapturedAt)
|
||||
{
|
||||
public static SampleTargetPayload From(SampleTargetInfo info) =>
|
||||
new(info.Width, info.Height, info.CapturedAt);
|
||||
}
|
||||
|
||||
/// <summary>采样点坐标(图像像素)。</summary>
|
||||
public sealed record SamplePointPayload(int X, int Y);
|
||||
|
||||
/// <summary>靶样轮廓与采样点分析结果。</summary>
|
||||
public sealed record SampleAnalysisPayload(
|
||||
string ImageBase64,
|
||||
string MimeType,
|
||||
int Width,
|
||||
int Height,
|
||||
int SamplingPointCount,
|
||||
IReadOnlyList<SamplePointPayload> SamplingPoints)
|
||||
{
|
||||
public static SampleAnalysisPayload From(SampleAnalysisResult result) =>
|
||||
new(
|
||||
Convert.ToBase64String(result.ImageData),
|
||||
result.MimeType,
|
||||
result.Width,
|
||||
result.Height,
|
||||
result.SamplingPointCount,
|
||||
result.SamplingPoints
|
||||
.Select(point => new SamplePointPayload(point.X, point.Y))
|
||||
.ToList());
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using HardwareControl.Core.HardwareServiceImpl;
|
||||
using HardwareControl.Core.HardwareServiceImpl.HardwareService;
|
||||
using HardwareControl.Core.SampleAnalysis;
|
||||
using HardwareControl.Core.Spectrometer;
|
||||
using HardwareControl.Core.SystemSetting;
|
||||
using HardwareControl.Core.UsbCamera;
|
||||
@@ -48,6 +49,13 @@ builder.Services.AddSingleton(_ => new SystemConfigRepository(systemConfigDbPath
|
||||
builder.Services.AddSingleton<ISystemConfigService, SystemConfigService>();
|
||||
builder.Services.AddSingleton<AdminAccessService>();
|
||||
|
||||
// 8. 靶样轮廓识别与采样点规划(ONNX 模型分割)
|
||||
builder.Services.Configure<OnnxSegmentationOptions>(
|
||||
builder.Configuration.GetSection(OnnxSegmentationOptions.SectionName));
|
||||
builder.Services.AddSingleton<BackgroundDiffSegmenter>();
|
||||
builder.Services.AddSingleton<TargetOutlineAnalyzer>();
|
||||
builder.Services.AddSingleton<ISampleAnalysisService, SampleAnalysisService>();
|
||||
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
|
||||
@@ -36,5 +36,16 @@
|
||||
"SystemSetting": {
|
||||
"DatabasePath": null,
|
||||
"AdminPassword": "flzx3QC!1"
|
||||
},
|
||||
"SampleAnalysis": {
|
||||
"Onnx": {
|
||||
"ModelDirectory": "Models",
|
||||
"InputWidth": 640,
|
||||
"InputHeight": 640,
|
||||
"ConfidenceThreshold": 0.25,
|
||||
"FallbackConfidenceThreshold": 0.6,
|
||||
"MaskThreshold": 0.5,
|
||||
"SamplingMarginRatio": 0.1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user