Tuesday, February 7, 2012

Face Capture and Face Detection in c# using webcam


Hi Friends your friend vivek is here.The past month was very tough lots of emotional issues.Each days with .NET giving me new knowledge.
This time i was planning to develop an image based authentication system.My plan was to implement this system with in one day.

    Vivek’s Targets

1. Capture your face from your web cam.
2. Store that image and your information to a database.
3. Next time when you came to system,the system will check your face against the image stored in database.

In this article i just wanna implement the first step,that is how to capture image using a web cam.A small image of the application is given below.Hi hi i haven’t put my face on the application
because some of my friends always complain that i am writing blog for impressing girls.I don’t want that impression went to waste box.So put a match box in my hand.

OpenCV

    OpenCV is a tool developed by Intel in C++.OpenCV can do almost all image processing operations.Initially i planned to use OpenCV.Then i changed my mind because OpenCV
    is C++ oriented.After some Google search i found EMGU CV wrapper for .NET and c#.

    1. Capture your face from your web cam.

a. Download EmguCv and install it.
b. The installation folders bin folder contain all DLL’s for our development(C:\Emgu\emgucv-windows-x86 2.2.1.1150\bin).
c. Create a windows form project and add reference to Emgu.CV.dll,Emgu.CV.UI.dll,Emgu.Util.dll
d. Copy all OpenCV dll’s from Emgu’s bin folder(C:\Emgu\emgucv-windows-x86 2.2.1.1150\bin) to your applications Bin folder
e. Add Emgu controls to your visual studio toolbox from Emgu.CV.dll
f. Put an Emgu ImageBox in your form,in which we show captured image.
g. Put 2 buttons one to start capture image and next one to store that image to a database.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.UI;
using Emgu.Util;
namespace WebCamCapture
{
public partial class Form1 : Form
{
private Capture _VivekCapTure;
private bool _captureInProgress;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
#region if capture is not created, create it now
if (_VivekCapTure == null)
{
try
{
_VivekCapTure = new Capture();
}
catch (NullReferenceException excpt)
{
MessageBox.Show(excpt.Message);
}
}
#endregion
if (_VivekCapTure != null)
{
if (_captureInProgress)
{ //stop the capture
//captureButton.Text = "Start Capture";
Application.Idle -= ProcessFrame;
}
else
{
//start the capture
//captureButton.Text = "Stop";
Application.Idle += ProcessFrame;
}

_captureInProgress = !_captureInProgress;
}
}
private void ProcessFrame(object sender, EventArgs arg)
{
Image frame = _VivekCapTure.QueryFrame();
captureImageBox.Image = frame;

}
}
}