## What Replace the stub `FrameAnalyzerService.RunInference` (returning `[]`) with a real ONNX Runtime inference pipeline for YOLOv8 object detection. ## Why The entire detection chain was non-functional — `FrameAnalyzerService` returned empty results, `Microsoft.ML.OnnxRuntime` was not referenced, and no object detection was produced. This is the core AI feature of the Heimdall platform (issue #2, etape-3). ## How - **Packages**: Added `Microsoft.ML.OnnxRuntime` 1.22.0 and `SixLabors.ImageSharp` 4.1.0 to `Heimdall.Agent.csproj` - **ONNX session**: `InferenceSession` loaded in constructor with graceful fallback to passthrough mode when model is absent - **Preprocessing**: JPEG → ImageSharp `Image<Rgb24>` → resize 640×640 → NCHW tensor normalized [0,1] - **Inference**: ONNX Runtime `Session.Run()` with the preprocessed tensor - **Post-processing**: YOLOv8 output parsing (`[1, 4+C, 8400]` tensor → bounding boxes + class scores) with class-aware Non-Maximum Suppression (IoU threshold 0.45, confidence threshold 0.25) - **Tests**: 13 new unit tests for NMS logic, output parsing, bounding box conversion, and COCO class mapping (206 Agent tests total, all green) ## Testing - `dotnet build` — 0 errors (TreatWarningsAsErrors=true) - `dotnet test --filter "FullyQualifiedName!~E2E"` — 1282 passed, 0 failed - E2E failures are pre-existing (Playwright/Chromium not installed on this runner) ## Notes - Model path configurable via `MODEL_PATH` env var (default: `models/yolov8m.onnx`) - Passthrough mode preserved when model file is absent — no crash - Thread-safe: `InferenceSession.Run()` is thread-safe; session created once in constructor Reviewed-on: https://forgejo.tordais.cc/nonobis/Heimdall/pulls/148
194 lines
6.5 KiB
C#
194 lines
6.5 KiB
C#
using Heimdall.Agent.Services;
|
|
using Microsoft.Extensions.Logging;
|
|
using NSubstitute;
|
|
|
|
namespace Heimdall.Agent.Tests;
|
|
|
|
public class FrameAnalyzerServiceTests
|
|
{
|
|
[Fact]
|
|
public void Constructor_WhenModelNotFound_DoesNotThrow()
|
|
{
|
|
using var service = new FrameAnalyzerService(Substitute.For<ILogger<FrameAnalyzerService>>());
|
|
service.ShouldNotBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AnalyzeFrameAsync_WhenPassthrough_ReturnsNull()
|
|
{
|
|
using var service = new FrameAnalyzerService(Substitute.For<ILogger<FrameAnalyzerService>>());
|
|
var result = await service.AnalyzeFrameAsync([1, 2, 3]);
|
|
result.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void AnalyzeFrame_WhenPassthrough_ReturnsNull()
|
|
{
|
|
using var service = new FrameAnalyzerService(Substitute.For<ILogger<FrameAnalyzerService>>());
|
|
var result = service.AnalyzeFrame([1, 2, 3]);
|
|
result.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AnalyzeFrameAsync_EmptyArray_ReturnsNull()
|
|
{
|
|
using var service = new FrameAnalyzerService(Substitute.For<ILogger<FrameAnalyzerService>>());
|
|
var result = await service.AnalyzeFrameAsync([]);
|
|
result.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AnalyzeFrameAsync_LargeBuffer_ReturnsNull()
|
|
{
|
|
using var service = new FrameAnalyzerService(Substitute.For<ILogger<FrameAnalyzerService>>());
|
|
var result = await service.AnalyzeFrameAsync(new byte[1024 * 1024]);
|
|
result.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void Dispose_MultipleTimes_DoesNotThrow()
|
|
{
|
|
var service = new FrameAnalyzerService(Substitute.For<ILogger<FrameAnalyzerService>>());
|
|
service.Dispose();
|
|
service.Dispose();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AnalyzeFrameAsync_AfterDispose_ReturnsNull()
|
|
{
|
|
var service = new FrameAnalyzerService(Substitute.For<ILogger<FrameAnalyzerService>>());
|
|
service.Dispose();
|
|
var result = await service.AnalyzeFrameAsync([1, 2, 3]);
|
|
result.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AnalyzeFrameAsync_ConcurrentCalls_DoNotThrow()
|
|
{
|
|
using var service = new FrameAnalyzerService(Substitute.For<ILogger<FrameAnalyzerService>>());
|
|
var tasks = Enumerable.Range(0, 10)
|
|
.Select(_ => service.AnalyzeFrameAsync([1, 2, 3]))
|
|
.ToArray();
|
|
var results = await Task.WhenAll(tasks);
|
|
results.ShouldAllBe(r => r == null);
|
|
}
|
|
|
|
// ── NMS unit tests ──────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void ApplyNms_EmptyList_ReturnsEmpty()
|
|
{
|
|
var result = FrameAnalyzerService.ApplyNms([]);
|
|
result.ShouldBeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void ApplyNms_SingleDetection_ReturnsItUnchanged()
|
|
{
|
|
var input = new List<FrameAnalyzerService.RawDetection>
|
|
{
|
|
new(10, 10, 50, 50, 0.9f, 0)
|
|
};
|
|
var result = FrameAnalyzerService.ApplyNms(input);
|
|
result.Length.ShouldBe(1);
|
|
result[0].Label.ShouldBe("person");
|
|
result[0].Confidence.ShouldBe(0.9f, 0.001f);
|
|
}
|
|
|
|
[Fact]
|
|
public void ApplyNms_TwoOverlapping_SameClass_KeepsHigherConfidence()
|
|
{
|
|
var input = new List<FrameAnalyzerService.RawDetection>
|
|
{
|
|
new(10, 10, 50, 50, 0.9f, 0), // person, high confidence
|
|
new(12, 12, 52, 52, 0.7f, 0), // person, overlapping, lower
|
|
};
|
|
var result = FrameAnalyzerService.ApplyNms(input);
|
|
result.Length.ShouldBe(1);
|
|
result[0].Confidence.ShouldBe(0.9f, 0.001f);
|
|
}
|
|
|
|
[Fact]
|
|
public void ApplyNms_TwoOverlapping_DifferentClass_KeepsBoth()
|
|
{
|
|
var input = new List<FrameAnalyzerService.RawDetection>
|
|
{
|
|
new(10, 10, 50, 50, 0.9f, 0), // person
|
|
new(12, 12, 52, 52, 0.8f, 2), // car, overlapping but different class
|
|
};
|
|
var result = FrameAnalyzerService.ApplyNms(input);
|
|
result.Length.ShouldBe(2);
|
|
}
|
|
|
|
[Fact]
|
|
public void ApplyNms_TwoNonOverlapping_KeepsBoth()
|
|
{
|
|
var input = new List<FrameAnalyzerService.RawDetection>
|
|
{
|
|
new(10, 10, 50, 50, 0.9f, 0), // top-left
|
|
new(200, 200, 250, 250, 0.8f, 0), // bottom-right
|
|
};
|
|
var result = FrameAnalyzerService.ApplyNms(input);
|
|
result.Length.ShouldBe(2);
|
|
}
|
|
|
|
[Fact]
|
|
public void ApplyNms_ThreeDetections_MixedOverlaps_KeepsBest()
|
|
{
|
|
var input = new List<FrameAnalyzerService.RawDetection>
|
|
{
|
|
new(10, 10, 50, 50, 0.9f, 0), // person, best
|
|
new(11, 11, 51, 51, 0.8f, 0), // person, overlaps with #1 → suppressed
|
|
new(12, 12, 52, 52, 0.7f, 0), // person, overlaps with #1 → suppressed
|
|
};
|
|
var result = FrameAnalyzerService.ApplyNms(input);
|
|
result.Length.ShouldBe(1);
|
|
result[0].Confidence.ShouldBe(0.9f, 0.001f);
|
|
}
|
|
|
|
[Fact]
|
|
public void ApplyNms_BoundingBoxConvertedCorrectly()
|
|
{
|
|
// RawDetection(10, 20, 60, 80) → DetectionResult(X=10, Y=20, Width=50, Height=60)
|
|
var input = new List<FrameAnalyzerService.RawDetection>
|
|
{
|
|
new(10, 20, 60, 80, 0.95f, 0)
|
|
};
|
|
var result = FrameAnalyzerService.ApplyNms(input);
|
|
result.Length.ShouldBe(1);
|
|
result[0].X.ShouldBe(10f, 0.001f);
|
|
result[0].Y.ShouldBe(20f, 0.001f);
|
|
result[0].Width.ShouldBe(50f, 0.001f);
|
|
result[0].Height.ShouldBe(60f, 0.001f);
|
|
}
|
|
|
|
[Fact]
|
|
public void ApplyNms_ClassIndexMappedToLabel()
|
|
{
|
|
var input = new List<FrameAnalyzerService.RawDetection>
|
|
{
|
|
new(0, 0, 10, 10, 0.5f, 2), // class 2 = "car"
|
|
new(0, 0, 10, 10, 0.5f, 15), // class 15 = "cat"
|
|
new(0, 0, 10, 10, 0.5f, 79), // class 79 = "toothbrush"
|
|
};
|
|
var result = FrameAnalyzerService.ApplyNms(input);
|
|
// Different classes → all kept (IoU = 1.0 but different class)
|
|
result.Length.ShouldBe(3);
|
|
result.ShouldContain(d => d.Label == "car");
|
|
result.ShouldContain(d => d.Label == "cat");
|
|
result.ShouldContain(d => d.Label == "toothbrush");
|
|
}
|
|
|
|
[Fact]
|
|
public void ApplyNms_UnknownClassIndex_GeneratesFallbackLabel()
|
|
{
|
|
var input = new List<FrameAnalyzerService.RawDetection>
|
|
{
|
|
new(0, 0, 10, 10, 0.5f, 999)
|
|
};
|
|
var result = FrameAnalyzerService.ApplyNms(input);
|
|
result.Length.ShouldBe(1);
|
|
result[0].Label.ShouldBe("class_999");
|
|
}
|
|
}
|