43 lines
2.1 KiB
C#
43 lines
2.1 KiB
C#
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}");
|
|
}
|