aisop/tests/HandCrop.Smoke/Program.cs

48 lines
3.2 KiB
C#

using System.Diagnostics;
using System.Reflection;
using OpenCvSharp;
using SopHelper.Engine.Inference;
var method = typeof(MediaPipeHandDetector).GetMethod("CropRotatedSquare", BindingFlags.NonPublic | BindingFlags.Static)!;
var palmType = typeof(MediaPipeHandDetector).GetNestedType("PalmRect", BindingFlags.NonPublic)!;
using var image = new Mat(240, 320, MatType.CV_8UC3);
Cv2.Randu(image, Scalar.All(0), Scalar.All(255));
foreach (var angle in new[] { 0.0, 23.5, -75.0, 179.0 })
foreach (var center in new[] { (160.2f, 119.4f), (10f, 5f), (-12f, 90f), (300f, 210f) })
foreach (var side in new[] { 96, 221 }) Check(image, center.Item1, center.Item2, side, angle);
using var large = new Mat(2048, 2448, MatType.CV_8UC3, new Scalar(31, 62, 93));
Check(large, 400.3f, 300.7f, 640, 35);
Console.WriteLine("PASS: 33 crops preserve full-canvas pixels and inverse coordinates, including rotations, borders, off-image centers and 5MP input");
void Check(Mat source, float centerX, float centerY, int side, double angle)
{
var pad = (int)Math.Ceiling(Math.Sqrt((double)source.Width * source.Width + (double)source.Height * source.Height));
var cx = centerX + pad;var cy = centerY + pad;
var watch = Stopwatch.StartNew();
// Independent reference: the previous full padded-canvas rotation followed by ROI extraction.
using var canvas = new Mat(source.Height + 2 * pad, source.Width + 2 * pad, MatType.CV_8UC3, Scalar.Black);
using (var roi = new Mat(canvas, new Rect(pad, pad, source.Width, source.Height))) source.CopyTo(roi);
using var rotation = Cv2.GetRotationMatrix2D(new Point2f(cx, cy), angle, 1);
using var rotated = new Mat();
Cv2.WarpAffine(canvas, rotated, rotation, canvas.Size());
var x = Math.Max(0, Math.Min((int)Math.Round(cx - side / 2.0), rotated.Width - side));
var y = Math.Max(0, Math.Min((int)Math.Round(cy - side / 2.0), rotated.Height - side));
using var expected = new Mat(rotated, new Rect(x, y, side, side));
var referenceMs = watch.Elapsed.TotalMilliseconds;
var palm = Activator.CreateInstance(palmType, centerX, centerY, (float)side, angle, 0.9f, side / 2.5f);
var args = new object?[] { source, palm, null };
watch.Restart();
using var actual = (Mat?)method.Invoke(null, args) ?? throw new Exception("Crop unexpectedly rejected");
var directMs = watch.Elapsed.TotalMilliseconds;
using var difference = new Mat();Cv2.Absdiff(expected, actual, difference);
var mean = Cv2.Mean(difference);
var max = Cv2.Norm(expected, actual, NormTypes.INF);
if (max > 8 || Math.Max(mean.Val0, Math.Max(mean.Val1, mean.Val2)) > 0.05)
throw new Exception($"Crop pixels changed: angle={angle}, center={centerX},{centerY}, side={side}, max={max}, mean={mean}");
var map = args[2] ?? throw new Exception("Missing inverse map");
var fields = new Dictionary<string, double> { ["Pad"] = pad, ["Cx"] = cx, ["Cy"] = cy, ["Degree"] = angle, ["X"] = x, ["Y"] = y, ["Side"] = side };
foreach (var (name, value) in fields)
if (Convert.ToDouble(map.GetType().GetProperty(name)!.GetValue(map)) != value) throw new Exception("Inverse coordinate changed: " + name);
if (source.Width == 2448) Console.WriteLine($"5MP crop: reference={referenceMs:F2}ms direct={directMs:F2}ms");
}