升级:将跨平台图像依赖迁移到 OpenCvSharp 5

This commit is contained in:
tfarcraw 2026-11-08 21:41:04 +08:00
parent 02f2e67858
commit cb33b67329
4 changed files with 53 additions and 3 deletions

View File

@ -34,6 +34,9 @@ pwsh -File tools/Publish-Avalonia.ps1
`artifacts/avalonia/linux-x64/SopHelper.Avalonia`。
两个平台使用同一套视图、ViewModel 和业务代码,仅供应商 SDK 绑定及原生运行库不同。
图像依赖统一使用 OpenCvSharp5 `5.0.0.20261107`,Windows 和 Linux 配套原生包使用相同版本,
实际加载 OpenCV 5.0.0。项目保持 .NET 8,现有相机、预览和检测调用通过迁移验证。
Windows Python 环境使用 `tools/Setup-Python.ps1 -Sam2`;Linux 环境和部署见 [LINUX.md](LINUX.md)。
发布脚本会携带 Windows 的仓库 Python 运行时,Linux 需建立对应平台环境。
@ -51,6 +54,7 @@ Windows Python 环境使用 `tools/Setup-Python.ps1 -Sam2`;Linux 环境和部
```powershell
dotnet run --project tests/Avalonia.Smoke -c Release
dotnet run --project tests/OpenCv.Smoke -c Release
dotnet run --project tests/HandednessSmoke -c Release
dotnet run --project tests/Training.Smoke -c Release -- python/python.exe
dotnet run --project tests/YoloModelSmoke -c Release -- models/yolo11n.onnx

View File

@ -13,9 +13,9 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="MvCameraControl.Net"><HintPath>$(MvsManagedSdk)</HintPath><Private>true</Private></Reference>
<PackageReference Include="OpenCvSharp4" Version="4.13.0.20260627" />
<PackageReference Include="OpenCvSharp4.runtime.win" Version="4.13.0.20260627" Condition="'$(NativePlatform)' == 'Windows'" />
<PackageReference Include="OpenCvSharp4.official.runtime.linux-x64" Version="4.13.0.20260627" Condition="'$(NativePlatform)' == 'Linux'" />
<PackageReference Include="OpenCvSharp5" Version="5.0.0.20261107" />
<PackageReference Include="OpenCvSharp5.runtime.win" Version="5.0.0.20261107" Condition="'$(NativePlatform)' == 'Windows'" />
<PackageReference Include="OpenCvSharp5.official.runtime.linux-x64" Version="5.0.0.20261107" Condition="'$(NativePlatform)' == 'Linux'" />
<PackageReference Include="Microsoft.ML.OnnxRuntime.Gpu" Version="1.23.0" Condition="'$(NativePlatform)' == 'Windows'" />
<PackageReference Include="Microsoft.ML.OnnxRuntime" Version="1.23.0" Condition="'$(NativePlatform)' == 'Linux'" />
<PackageReference Include="YamlDotNet" Version="16.3.0" />

View 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.CrossPlatform.Engine/SopHelper.CrossPlatform.Engine.csproj" /></ItemGroup>
</Project>

View File

@ -0,0 +1,42 @@
using OpenCvSharp;
using SopHelper.Engine.Camera;
var version = Cv2.GetVersionString() ?? throw new Exception("OpenCV did not report its native version");
if (!version.StartsWith("5.", StringComparison.Ordinal)) throw new Exception("Loaded an unexpected OpenCV runtime: " + version);
Console.WriteLine("PASS: OpenCV " + version + " native runtime loaded");
var directory = Path.GetFullPath("artifacts/opencv-smoke");
using var recorder = new VideoRecorder();
using var frame = new Mat(240, 320, MatType.CV_8UC3, new Scalar(10, 30, 220));
var path = recorder.Start(directory, frame, 30);
await Task.Delay(80);
frame.SetTo(new Scalar(20, 180, 40));
recorder.Write(frame);
await Task.Delay(80);
var result = recorder.Stop() ?? throw new Exception("Recording did not finalize");
if (result.Frames < 2 || recorder.IsRecording) throw new Exception("Recording frame pacing or stop failed");
using var video = new VideoCapture(path);
if (!video.IsOpened()) throw new Exception("OpenCV could not reopen the recorded MJPEG video");
using var decoded = new Mat();
var frames = 0;
Scalar first = default, last = default;
while (video.Read(decoded))
{
if (decoded.Width != 320 || decoded.Height != 240 || decoded.Type() != MatType.CV_8UC3)
throw new Exception("Video dimensions or pixel type changed");
last = Cv2.Mean(decoded);
if (frames++ == 0) first = last;
}
if (frames != result.Frames) throw new Exception($"Recorded {result.Frames} frames but decoded {frames}");
CheckColor(first, new Scalar(10, 30, 220));
CheckColor(last, new Scalar(20, 180, 40));
Console.WriteLine($"PASS: VideoRecorder MJPEG round trip, {frames} frames, 320x240, BGR colors preserved");
Console.WriteLine("Video: " + path);
static void CheckColor(Scalar actual, Scalar expected)
{
// MJPEG is lossy; verify channel order and retained frames with a small codec tolerance.
if (Math.Abs(actual.Val0 - expected.Val0) > 8 || Math.Abs(actual.Val1 - expected.Val1) > 8 || Math.Abs(actual.Val2 - expected.Val2) > 8)
throw new Exception($"Unexpected decoded color: {actual}; expected approximately {expected}");
}