aisop/tests/Mvs.Smoke/Program.cs

95 lines
5.2 KiB
C#

using MvCameraControl;
using SopHelper.Engine.Camera;
using OpenCvSharp;
try
{
using var camera=new HikvisionCameraService();
var errors=new List<string>();
camera.Error+=message=>{errors.Add(message);Console.Error.WriteLine(message);};
Console.WriteLine("SDK: "+SDKSystem.GetSDKVersion());
var cameras=await camera.EnumerateAsync();
foreach(var device in cameras)Console.WriteLine($"DEVICE: {device.Name}");
if(errors.Count>0)throw new Exception("Camera enumeration reported an error");
Console.WriteLine($"PASS: SDK initialized and enumerated {cameras.Count} camera(s)");
var snapshotIndex = Array.IndexOf(args, "--snapshot");
if (snapshotIndex >= 0)
{
if (snapshotIndex + 1 >= args.Length) throw new ArgumentException("--snapshot requires an output path");
var path = Path.GetFullPath(args[snapshotIndex + 1]);
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
var device = cameras.FirstOrDefault() ?? throw new Exception("No Hikvision camera found");
var ready = new TaskCompletionSource<Mat>(TaskCreationOptions.RunContinuationsAsynchronously);
var frames = 0;
camera.FrameArrived += frame =>
{
if (Interlocked.Increment(ref frames) == 5) ready.TrySetResult(frame.Bgr.Clone());
};
var watch = System.Diagnostics.Stopwatch.StartNew();
if (!await camera.ConnectAsync(device) || !await camera.StartAsync()) throw new Exception("Open/start failed");
using var captured = await ready.Task.WaitAsync(TimeSpan.FromSeconds(15));
watch.Stop();
camera.Disconnect();
if (!Cv2.ImWrite(path, captured)) throw new IOException("Snapshot write failed");
Console.WriteLine($"SNAPSHOT: {path}; {captured.Width}x{captured.Height}; camera={device.Name}; open/start/5 frames={watch.Elapsed.TotalMilliseconds:F1}ms");
return 0;
}
if(args.Contains("--inspect-timing"))
{
var result=DeviceEnumerator.EnumDevices(DeviceTLayerType.MvGigEDevice|DeviceTLayerType.MvUsbDevice,out var infos);
if(result!=MvError.MV_OK||infos.Count==0)throw new Exception("Enumeration failed");
using var device=DeviceFactory.CreateDevice(infos[0]);
if(device.Open()!=MvError.MV_OK)throw new Exception("Open failed");
try
{
foreach(var node in new[]{"ExposureTime","AcquisitionFrameRate","ResultingFrameRate"})
if(device.Parameters.GetFloatValue(node,out var value)==MvError.MV_OK)
Console.WriteLine($"TIMING: {node}={value.CurValue} (max={value.Max})");
foreach(var node in new[]{"PayloadSize","DeviceLinkThroughputLimit","GevSCPSPacketSize","GevSCPD"})
if(device.Parameters.GetIntValue(node,out var value)==MvError.MV_OK)
Console.WriteLine($"TRANSPORT: {node}={value.CurValue}");
}
finally { device.Close(); }
}
if(args.Contains("--inspect-frame"))
{
var result=DeviceEnumerator.EnumDevices(DeviceTLayerType.MvGigEDevice|DeviceTLayerType.MvUsbDevice,out var infos);
if(result!=MvError.MV_OK||infos.Count==0)throw new Exception("Enumeration failed");
using var device=DeviceFactory.CreateDevice(infos[0]);
Console.WriteLine($"OPEN: 0x{device.Open():X8}");
device.Parameters.SetEnumValueByString("TriggerMode","Off");
try
{
Console.WriteLine($"START: 0x{device.StreamGrabber.StartGrabbing():X8}");
result=device.StreamGrabber.GetImageBuffer(3000,out var buffer);
Console.WriteLine($"GET: 0x{result:X8}");
if(result==MvError.MV_OK)
{
try
{
foreach(var p in typeof(IImage).GetProperties().Where(p=>p.Name!="PixelData"))Console.WriteLine($"IMAGE {p.Name}: {p.GetValue(buffer.Image)}");
result=device.PixelTypeConverter.ConvertPixelType(buffer.Image,out var converted,MvGvspPixelType.PixelType_Gvsp_BGR8_Packed);
using(converted)Console.WriteLine($"CONVERT: 0x{result:X8}");
}
finally{device.StreamGrabber.FreeImageBuffer(buffer);}
}
}
finally{device.StreamGrabber.StopGrabbing();device.Close();}
}
if(args.Contains("--hardware"))
{
var device=cameras.FirstOrDefault()??throw new Exception("No camera found");
var ready=new TaskCompletionSource<(int W,int H)>(TaskCreationOptions.RunContinuationsAsynchronously);
var frames=0;
camera.FrameArrived+=frame=>{if(Interlocked.Increment(ref frames)>=5)ready.TrySetResult((frame.Bgr.Width,frame.Bgr.Height));};
if(!await camera.ConnectAsync(device)||!await camera.StartAsync())throw new Exception("Open/start failed");
var frame=await ready.Task.WaitAsync(TimeSpan.FromSeconds(10));
camera.Disconnect();
Console.WriteLine($"PASS: acquired {frames} {frame.W}x{frame.H} BGR frames and released camera");
if(!await camera.ConnectAsync(device))throw new Exception("Camera could not reopen after disconnect");
camera.Disconnect();Console.WriteLine("PASS: camera reopened after release");
}
return 0;
}
catch(Exception error){Console.Error.WriteLine(error);return 1;}