Skip to content

Instantly share code, notes, and snippets.

@techyian
Created August 26, 2018 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save techyian/fa3295304f7e18fc0aba55f422a4a5c7 to your computer and use it in GitHub Desktop.
Save techyian/fa3295304f7e18fc0aba55f422a4a5c7 to your computer and use it in GitHub Desktop.
MMALSharp Accord framework HaarObjectDetector
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using Accord.Vision.Detection;
using MMALSharp.Handlers;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using Image = SixLabors.ImageSharp.Image;
namespace MMALSharpExample
{
public class AccordVideoCaptureHandler : ICaptureHandler
{
public void Dispose()
{
}
public ProcessResult Process(uint allocSize)
{
throw new NotImplementedException();
}
public void Process(byte[] data)
{
Stopwatch stopwatch = new Stopwatch();
var cascade = new Accord.Vision.Detection.Cascades.FaceHaarCascade();
var detector = new HaarObjectDetector(cascade, minSize: 50,
searchMode: ObjectDetectorSearchMode.NoOverlap);
stopwatch.Start();
using (var ms = new MemoryStream())
using (Image<Rgba32> img = Image.Load(data))
{
img.SaveAsBmp(ms);
ms.Seek(0, SeekOrigin.Begin);
stopwatch.Stop();
Console.WriteLine($"Time to save as bmp : {stopwatch.Elapsed}");
stopwatch.Reset();
stopwatch.Start();
Bitmap bmp = new Bitmap(ms);
Rectangle[] rectangles = detector.ProcessFrame(bmp);
stopwatch.Stop();
Console.WriteLine($"Time to detect face : {stopwatch.Elapsed}");
Console.WriteLine($"Detected objects {detector.DetectedObjects.Length}");
using (var file = File.Create("/home/pi/testface.bmp"))
{
ms.Seek(0, SeekOrigin.Begin);
ms.CopyTo(file);
}
}
}
public void PostProcess()
{
}
}
}
public class Program
{
private static MMALCamera MMALCam = MMALCamera.Instance;
public void AccordFaceDetect()
{
MMALCameraConfig.Debug = true;
MMALCameraConfig.VideoResolution = Resolution.As03MPixel;
using (var vidCaptureHandler = new AccordVideoCaptureHandler())
using (var vidEncoder = new MMALVideoEncoder(vidCaptureHandler))
using (var renderer = new MMALVideoRenderer())
{
MMALCam.ConfigureCameraSettings();
vidEncoder.ConfigureOutputPort(0, MMALEncoding.MJPEG, MMALEncoding.RGBA, 90, 25000000);
MMALCam.Camera.VideoPort.ConnectTo(vidEncoder);
MMALCam.Camera.PreviewPort.ConnectTo(renderer);
// Camera warm up time
Task.Delay(2000).GetAwaiter().GetResult();
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(15));
// Take video for 15 seconds.
MMALCam.ProcessAsync(MMALCam.Camera.VideoPort, cts.Token).GetAwaiter().GetResult();
}
}
public static void Main(string[] args)
{
var program = new Program();
program.AccordFaceDetect();
MMALCam.Cleanup();
}
}
@inf1981
Copy link

inf1981 commented May 2, 2019

Hi,
I tried to get this running, but I'm struggling a bit with the dependencies. If I use Accord.Net (3.8) from NuGet it wants CoreCompat.System.Drawing. That is not working, since it's coliding with System.Drawing, that got ported to .NET Core...

So I built the latest develop of Accord.Net from source to .NET Core 2. This works then, because it solved the dependency on CoreCompat. The problem is, that the code gives me an exception while calling detector.ProcessFrame(bmp). Im sure it's related to my build, so the question is: what setup / settings did you use to test this?

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment