From 93a59735a011af3785be2344b507ce373494670e Mon Sep 17 00:00:00 2001 From: tfarcraw <123211521@163.com> Date: Sun, 8 Nov 2026 22:22:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9A=E6=8C=89=E8=A1=8C?= =?UTF-8?q?=E7=94=9F=E6=88=90=E6=A8=A1=E5=9E=8B=E8=BE=93=E5=85=A5=E5=BC=A0?= =?UTF-8?q?=E9=87=8F=E4=BB=A5=E5=87=8F=E5=B0=91=E9=80=90=E5=83=8F=E7=B4=A0?= =?UTF-8?q?=E5=8E=9F=E7=94=9F=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Inference/MediaPipeHandDetector.cs | 15 +--------- .../Inference/TensorPreprocessing.cs | 29 ++++++++++++++++++ .../Inference/YoloDetector.cs | 13 +------- tests/TensorPreprocessing.Smoke/Program.cs | 30 +++++++++++++++++++ .../TensorPreprocessing.Smoke.csproj | 4 +++ 5 files changed, 65 insertions(+), 26 deletions(-) create mode 100644 src/SopHelper.CrossPlatform.Engine/Inference/TensorPreprocessing.cs create mode 100644 tests/TensorPreprocessing.Smoke/Program.cs create mode 100644 tests/TensorPreprocessing.Smoke/TensorPreprocessing.Smoke.csproj diff --git a/src/SopHelper.CrossPlatform.Engine/Inference/MediaPipeHandDetector.cs b/src/SopHelper.CrossPlatform.Engine/Inference/MediaPipeHandDetector.cs index 83dfdc4..84db89e 100644 --- a/src/SopHelper.CrossPlatform.Engine/Inference/MediaPipeHandDetector.cs +++ b/src/SopHelper.CrossPlatform.Engine/Inference/MediaPipeHandDetector.cs @@ -397,20 +397,7 @@ public class MediaPipeHandDetector : IDisposable using var resized = new Mat(); Cv2.Resize(rgb, resized, new Size(size, size), 0, 0, InterpolationFlags.Area); - var tensor = new DenseTensor([1, 3, size, size]); - - for (var yy = 0; yy < size; yy++) - { - for (var xx = 0; xx < size; xx++) - { - var px = resized.At(yy, xx); - tensor[0, 0, yy, xx] = px.Item0 / 255f; - tensor[0, 1, yy, xx] = px.Item1 / 255f; - tensor[0, 2, yy, xx] = px.Item2 / 255f; - } - } - - return tensor; + return TensorPreprocessing.FromRgb(resized); } private static float IoU(DetRect a, DetRect b) diff --git a/src/SopHelper.CrossPlatform.Engine/Inference/TensorPreprocessing.cs b/src/SopHelper.CrossPlatform.Engine/Inference/TensorPreprocessing.cs new file mode 100644 index 0000000..fe79ce5 --- /dev/null +++ b/src/SopHelper.CrossPlatform.Engine/Inference/TensorPreprocessing.cs @@ -0,0 +1,29 @@ +using Microsoft.ML.OnnxRuntime.Tensors; +using OpenCvSharp; + +namespace SopHelper.Engine.Inference; + +internal static class TensorPreprocessing +{ + internal static unsafe DenseTensor FromRgb(Mat rgb) + { + if (rgb.Type() != MatType.CV_8UC3) throw new ArgumentException("输入必须为 RGB 8 位三通道图像", nameof(rgb)); + var width = rgb.Width;var height = rgb.Height;var plane = checked(width * height); + var tensor = new DenseTensor([1, 3, height, width]); + var output = tensor.Buffer.Span; + for (var y = 0; y < height; y++) + { + // One native row lookup instead of a Mat.At call and tensor index calculation per pixel. + var row = (byte*)rgb.Ptr(y); + for (var x = 0; x < width; x++) + { + var target = y * width + x;var source = x * 3; + output[target] = row[source] / 255f; + output[plane + target] = row[source + 1] / 255f; + output[2 * plane + target] = row[source + 2] / 255f; + } + } + GC.KeepAlive(rgb); + return tensor; + } +} diff --git a/src/SopHelper.CrossPlatform.Engine/Inference/YoloDetector.cs b/src/SopHelper.CrossPlatform.Engine/Inference/YoloDetector.cs index e032d2c..1c3a2e0 100644 --- a/src/SopHelper.CrossPlatform.Engine/Inference/YoloDetector.cs +++ b/src/SopHelper.CrossPlatform.Engine/Inference/YoloDetector.cs @@ -123,18 +123,7 @@ public class YoloDetector : IDisposable resized.CopyTo(roi); } - var tensor = new DenseTensor([1, 3, inputSize, inputSize]); - - for (var y = 0; y < inputSize; y++) - { - for (var x = 0; x < inputSize; x++) - { - var px = canvas.At(y, x); - tensor[0, 0, y, x] = px.Item0 / 255f; - tensor[0, 1, y, x] = px.Item1 / 255f; - tensor[0, 2, y, x] = px.Item2 / 255f; - } - } + var tensor = TensorPreprocessing.FromRgb(canvas); var inputs = new List { NamedOnnxValue.CreateFromTensor(InputName(), tensor) }; diff --git a/tests/TensorPreprocessing.Smoke/Program.cs b/tests/TensorPreprocessing.Smoke/Program.cs new file mode 100644 index 0000000..f6fb4da --- /dev/null +++ b/tests/TensorPreprocessing.Smoke/Program.cs @@ -0,0 +1,30 @@ +using System.Reflection; +using Microsoft.ML.OnnxRuntime.Tensors; +using OpenCvSharp; +using SopHelper.Engine.Inference; + +var method = typeof(YoloDetector).Assembly.GetType("SopHelper.Engine.Inference.TensorPreprocessing")! + .GetMethod("FromRgb", BindingFlags.NonPublic | BindingFlags.Static)!; +using var parent = new Mat(5, 7, MatType.CV_8UC3, new Scalar(99, 98, 97)); +using var roi = new Mat(parent, new Rect(1, 1, 3, 2)); +var pixels = new[] { new Vec3b(0, 127, 255), new Vec3b(21, 42, 63), new Vec3b(1, 2, 3), + new Vec3b(4, 5, 6), new Vec3b(128, 129, 130), new Vec3b(254, 253, 252) }; +for (var y = 0; y < 2; y++) for (var x = 0; x < 3; x++) roi.Set(y, x, pixels[y * 3 + x]); +if (roi.IsContinuous()) throw new Exception("Test must exercise padded row strides"); +foreach (var contiguous in new[] { false, true }) +{ + using var source = contiguous ? roi.Clone() : new Mat(parent, new Rect(1, 1, 3, 2)); + var tensor = (DenseTensor)method.Invoke(null, new object[] { source })!; + if (!tensor.Dimensions.SequenceEqual(new[] { 1, 3, 2, 3 })) throw new Exception("Incorrect NCHW shape"); + for (var y = 0; y < 2; y++) for (var x = 0; x < 3; x++) + { + var pixel = pixels[y * 3 + x]; + if (tensor[0, 0, y, x] != pixel.Item0 / 255f || tensor[0, 1, y, x] != pixel.Item1 / 255f || tensor[0, 2, y, x] != pixel.Item2 / 255f) + throw new Exception("RGB order, normalization or row stride changed"); + } + source.SetTo(Scalar.Black); + if (tensor[0, 2, 0, 0] != 1) throw new Exception("Tensor must own its normalized data"); + // Restore the ROI because the non-contiguous case shares the parent buffer. + for (var y = 0; y < 2; y++) for (var x = 0; x < 3; x++) roi.Set(y, x, pixels[y * 3 + x]); +} +Console.WriteLine("PASS: RGB-to-NCHW normalization is exact for contiguous and padded Mats, preserves channel order and owns its data"); diff --git a/tests/TensorPreprocessing.Smoke/TensorPreprocessing.Smoke.csproj b/tests/TensorPreprocessing.Smoke/TensorPreprocessing.Smoke.csproj new file mode 100644 index 0000000..5716f4b --- /dev/null +++ b/tests/TensorPreprocessing.Smoke/TensorPreprocessing.Smoke.csproj @@ -0,0 +1,4 @@ + + Exenet8.0enableenable + +