Face Recognation in C#








Emgu CV is a cross platform .Net wrapper to the Intel OpenCV image processing library. Allowing OpenCV functions to be called from .NET compatible languages such as C#, VB, VC++, IronPython etc. The wrapper can be compiled in Mono and run on Linux / Mac OS X. Unlike other wrappers such as OpenCVDotNet, SharperCV which use unsafe code, Emgu CV is written entirely in C#. The benefit is that it can be compiled in Mono and therefore is able to run on any platform Mono supports, including Linux, Solaris and Mac OS X. A lot of efforts has been spend to have a pure C# implementation since the headers have to be ported, compared with managed C++ implementation where header files can simply be included. But it is well worth it if you see Emgu CV running on Fedora 10! Plus it always gives you the comfort knowing that your code is cross-platform.

Face Recognation

1- Create a Windows Form Application
2- Add a PictureBox and a Timer (and Enable it)
3- Run it on a x86 system
4- Be sure you have the OpenCV relevant dlls (included with the Emgu CV download) in the folder where you code executes.
5- 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()

        private void timer1_Tick(object sender, EventArgs e)
            using (Image<Bgr, byte> nextFrame = cap.QueryFrame())
                if (nextFrame != null)
                    // There is only one channel (gray scale), 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,
                    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 web cam
            cap = new Capture(0);
            // adjust path to find your xml
            haar = new HaarCascade(“..\\..\\..\\..\\lib\\haarcascade_frontalface_alt2.xml”);

Copyright © All Rights Reserved - C# Learners