C# - Detect face and crop image
Asked Answered
C

4

8

I'm writing a HttpHandler in C# which serves resized images and blah blah blah... No troubles, we have millions of handlers to use as reference.

The problem is that I have pictures of my users taken with "traditional" sizes, as 4:3 and 16:9. But this handler will need to serve the picture in a Photo ID size (4cm by 3cm) and obviously has need of cropping around the user face. The faces positions vary a lot (aren't always at the picture center).

So, what kind of algorithm I could use to detect the face center and then crop the image around this point?

Chromatology answered 10/9, 2011 at 0:12 Comment(0)
O
10

You can use HaarCascade class in EmguCV (DotNet port of OpenCV) http://www.emgu.com/wiki/index.php/Face_detection

Notes in order to run this example:

  • Create a Windows Form Application
  • Add a PictureBox and a Timer (and Enable it) - Run it on a x86 system
  • Be sure you have the OpenCV relevant dlls (included with the Emgu CV download) in the folder where you code executes.
  • Adjust the path to find the Haarcascade xml (last line of the code)
using System;
using System.Windows.Forms;
using System.Drawing;
using Emgu.CV;
using Emgu.Util;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
 
namespace opencvtut
{
    public partial class Form1 : Form
    {
                private Capture cap;
                private HaarCascade haar;
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void timer1_Tick(object sender, EventArgs e)
        {
                using (Image<Bgr, byte> nextFrame = cap.QueryFrame())
                {
                        if (nextFrame != null)
                        {
                                // there's only one channel (greyscale), hence the zero index
                                //var faces = nextFrame.DetectHaarCascade(haar)[0];
                                Image<Gray, byte> grayframe = nextFrame.Convert<Gray, byte>();
                                var faces =
                                        grayframe.DetectHaarCascade(
                                                haar, 1.4, 4,
                                                HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                                new Size(nextFrame.Width/8, nextFrame.Height/8)
                                                )[0];
 
                                foreach (var face in faces)
                                {
                                        nextFrame.Draw(face.rect, new Bgr(0,double.MaxValue,0), 3);
                                }
                                pictureBox1.Image = nextFrame.ToBitmap();
                        }
                }
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            // passing 0 gets zeroth webcam
                        cap = new Capture(0);
            // adjust path to find your xml
                        haar = new HaarCascade(
                "..\\..\\..\\..\\lib\\haarcascade_frontalface_alt2.xml");
        }
    }
}
Orthotropic answered 10/9, 2011 at 0:20 Comment(2)
Hi the zip file is no longer in that path. Is it possible to get it?Melinite
Sorry, I don't have the zip anymore. I removed the link from the answer now.Orthotropic
C
3

If you are looking for cropping your image, you could use the Microsoft Cognitive Service named Face API which delimiters the face of all persons on your photo, it gives you back a JSON which has the elements to return you a Rectangle struct, then you can Crop and Resize your image as you want.

Here you can see more informationa about it: FaceAPI

Chaffin answered 12/9, 2016 at 6:5 Comment(0)
C
2

You can see an example of face detection and cropping software at http://deteksiwajah.blogspot.com/. It is open source and using OpenCV library.

Communist answered 16/11, 2011 at 23:8 Comment(0)
E
2

In the meantime there are more approaches and libraries available. Some of them are easier to use and/or more accurate than those that were available in the early 201X years. Using the library FaceAiSharp (NuGet), creating a complete working example could be done like this:

  1. dotnet new console

  2. dotnet add package FaceAiSharp.Bundle
    dotnet add package Microsoft.ML.OnnxRuntime
    
  3. Edit Program.cs:

using FaceAiSharp;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;

// adjust these lines to load your own image
using var hc = new HttpClient();
var groupPhoto = await hc.GetByteArrayAsync(
    "https://raw.githubusercontent.com/georg-jung/FaceAiSharp/master/examples/obama_family.jpg");
var img = Image.Load<Rgb24>(groupPhoto);

var det = FaceAiSharpBundleFactory.CreateFaceDetector();
FaceAiSharp.Applications.CropProfilePicture(det, img);
img.Save("obama_family_profilepic.jpg");
  1. dotnet run

Disclaimer: I'm the author of FaceAiSharp. It's MIT-licensed.

Eli answered 26/9, 2023 at 13:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.