Extract faces from live webcam video by c#

My target is make a program which take a webcam live video and save only extracted image of faces from that video.

So i use a very strong dotnet library name emguCV . what u can get from here. in short emgucv is

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

So first step is download the emgucv dll files because we are going to use it in our code. After download just add the dll in reference of ur project solution.

Now we need a very important file which is used as template to detect face we called it face_template, It is a xml file, u can get it from here . secondary link is here.

After download the xml file saved in “bin” folder and named it “face_template.xml” .

Now prepare a simple gui, A windows from, two picture box and a timer which is enabled at interval not less than 100.

first step is declare the library just add following

using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;

second step is initialize the web cam and load my detection template. U can just write this simple code for that

private Capture webcamvideo;
private HaarCascade facedetection;

 public Form1()
 {InitializeComponent();
 webcamvideo = new Capture(0); 
 facedetection = new HaarCascade("face_template.xml");
 }

Now timerclick event which will take the webcam captured image one after one. first see the full function code then analyze it.

private void timer1_Tick(object sender, EventArgs e)
{
 using (Image<Bgr, byte> Frame = webcamvideo.QueryFrame())
 {
  if (nextFrame != null)
{
 Image<Gray, byte> grayframe = Frame.Convert<Gray, byte>();
var faces =grayframe.DetectHaarCascade(facedetection , 1.4, 4,HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,new Size(Frame.Width/8, Frame.Height/8))[0];
foreach (var face in faces)
{
 Frame.Draw(face.rect, new Bgr(Color.Red), 3);
 Bitmap c = Frame.ToBitmap();
 Bitmap bmp = new Bitmap(face.rect.Width, face.rect.Height);
 Graphics g = Graphics.FromImage(bmp);
 g.DrawImage(c, 0, 0, face.rect, GraphicsUnit.Pixel);
 pictureBox2.Image = bmp;
 bmp.Save("myface"+DateTime.Now.Second.ToString() + ".jpeg"); 
 }
 pictureBox1.Image = nextFrame.ToBitmap();
}
 }
}

Now see what code does

using (Image<Bgr, byte> Frame = webcamvideo.QueryFrame())

it takes a frame from current web cam video stream. after .1 seconds it gets another frame because of timer function interval set to 100, if we want to fastened it we have to use threading.

if (Frame != null)

by this code we certain that we get a picture and can start analysis.

This analysis can divide in two steps.

  • detact face
  • extract the face

In detect face step we first make the image in greyscale using the following code

Image<Gray, byte> grayframe = Frame.Convert<Gray, byte>();

Now we take a special variable ‘var’ which a special type of Emgu.cv.ml. it can takes all the face location . and we use grayframe.DetectHaarCascade() function which take our template file , edggedetection algorithom type, scale factor and size of the part to except . So the code is like

var faces =grayframe.DetectHaarCascade( facedetection , 1.4, 4,

HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,

new Size(Frame.Width/8, Frame.Height/8)

)[0];

now we have to create extracted image of face. “faces” has all of faces information if multiple face have. To extract we use

foreach (var face in faces)
{
Frame.Draw(face.rect, new Bgr(Color.Red), 3);
Bitmap c = Frame.ToBitmap();
Bitmap bmp = new Bitmap(face.rect.Width, face.rect.Height);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(c, 0, 0, face.rect, GraphicsUnit.Pixel);
pictureBox2.Image = bmp;
bmp.Save("myface"+DateTime.Now.Second.ToString() + ".jpeg"); 
 }

here frame.draw() draw the rectangle in the main frame.Graphics g is used to draw the extracted face in new image file which is bmp.

So using this way we can extract the face from live video like this image

hope, u find it cool. this is the most easiest way to detect and extract faces using web-cam or cc-cam

About these ads

About kishordgupta

A software developer
This entry was posted in C#, Image Processing, Winform app and tagged , , , , , , , , . Bookmark the permalink.

34 Responses to Extract faces from live webcam video by c#

  1. This article is very good, but may I ask for code …??? thanks a lot …..

  2. hi!,I like your writing very much! share we communicate more about your article on AOL? I require a specialist on this area to solve my problem. May be that’s you! Looking forward to see you.

  3. hassam ullah sheikh says:

    can u plzz e mail the code???

  4. hassam ullah sheikh says:

    isnt there any way to convert ur code to aforge library??? m trying to implement facial machine learning using pixel by pixel mapping..(not very much efficient method)
    the only this i left with is face detection…i want to do exactly the same thing as u did..bt nt able to do so..can u plz help

    • kishordgupta says:

      before using emgu, i made skin color tracing by aforge. not good as emgu but worked well.
      just use pixellete filter then a euclidean color(skin) filtering. u can try that.
      a certain range it gives good result.

      my current thesis depending on that

  5. Pingback: Recognize face orientation using a new approach based on eye position and size by c# | Kishordgupta's Blog

  6. Anonymous says:

    thanks for your source code..it’s very useful for me…

  7. Milos says:

    can you send me source code??
    thanks

  8. Iqbal says:

    Sir, could u pls convrt the code into vb.net. If, send to my mailbox pls..
    Avdvancd thanx!

  9. Iqbal says:

    Itz oK.

    Could u pls send to me the c# code?

  10. Gajendran says:

    pls send how do extract facial image from video

  11. Prakash says:

    Sir pls send the report for “Detect and Track Objects in Live Webcam Video based on Color and Size by C#”

  12. Remya says:

    Sir,
    Will you please send me the source code.

  13. Jack says:

    Sir, the download link is not available, do you mind upload it in another link ?

  14. Hi Kishore Can I get a copy of your code? I want to detect two color objects instead of face.

  15. Mangala says:

    can u send me c# code. given link is not working. e-mail : mangalauwu@gmail.com

  16. Suraj G. Shete says:

    Thank You very Much Sir……Its very Useful for me

  17. Anonymous says:

    Sir the link is invalid. May your send the source code to me? Your help can greatly improve my understanding.

    My email is smcivan@hotmail.com thx !!

  18. Moiz Adamjee says:

    SIr plz send the code at moizaj@yahoo.com ..
    i need help ..

  19. Chan ken says:

    May you send me a source code? Thank for your help.

  20. I’m so happy to read this. This is the kind of manual that needs to be given and not the

    accidental misinformation that is at the other blogs. Appreciate your sharing this best doc.

  21. Desmond Hew says:

    Hi Kishordgupta
    Can you please send me the code at ahhew2006@gmail.com. I had tried for your download link and it is not available, and also lot advertising here and there.

    However, i would give a big thanks for your sharing. Lot people are looking for it and hope to learn OpenCV like me.

    Thanks
    Desmond

  22. Leoma says:

    Good site you have got here.. It’s hard to find good quality writing like yours these days. I honestly appreciate people like you! Take care!!

  23. Antje says:

    Hmm is anyone else encountering problems with the images on this blog
    loading? I’m trying to determine if its a problem on my end or if it’s the blog.
    Any suggestions would be greatly appreciated.

  24. My spouse and I absolutely love your blog and find the majority of your post’s to be exactly I’m
    looking for. Do you offer guest writers to write content to suit your needs?
    I wouldn’t mind publishing a post or elaborating on most of the subjects you write regarding here. Again, awesome blog!

  25. Jere says:

    I would like to show thanks to this writer for rescuing me from such a challenge.

    Just after scouting throughout the internet and coming across recommendations which were not productive, I assumed my entire life was over.
    Living minus the approaches to the problems you’ve fixed by means of the report is a crucial case, and the ones which could have in a wrong way damaged my entire career if I had not come across the website. Your own personal mastery and kindness in handling a lot of stuff was crucial. I’m not sure what I would’ve done if I had not come across such a thing like this. I can at this moment relish my future. Thanks a lot very much for this impressive and effective help. I won’t hesitate to suggest
    the blog to anyone who would like care on this subject.

  26. Anonymous says:

    please send this code to my email id pavan.srs4@gmail.com

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s