C # - Face image and crop detection

I am writing HttpHandlerin C # which serves modified images and blah blah blah ... No problem, we have millions of handlers to be used as references.

The problem is that I have photos of my users taken with "traditional" sizes, like 4: 3 and 16: 9. But this processor will have to serve the image in the size of the photo ID (4 cm by 3 cm) and, obviously, Needs cropping around the user's face . The positions of the faces vary greatly (they are not always in the center of the image).

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

+5
source share
4 answers

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

Notes to run this example:

  • Creating a Windows Form Application
  • Add a PictureBox and a Timer (and turn it on). Run it on an x86 system.
  • Make sure you have the appropriate OpenCV DLL files (included in the Emgu CV download) in the folder where the code is executed.
  • Adjust the path to find Haarcascade xml (last line of 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 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");
        }
    }
}

Here is the sample I wrote http://www.overroot.com/blog/wp-content/uploads/2011/03/FaceRecognition.zip

+8
source
+1

, Microsoft Cognitive Service Face API, , JSON, , Rectangle, , .

: FaceAPI

+1

All Articles