Webcam face detection in C# using Emgu CV

This tutorial has already been posted on the other sites, since maybe few have only knew about that site then I'll have to post it here. This tutorial has also been explain in the Emgu CV website that refers in this link: http://www.emgu.com/wiki/index.php/Face_detection in which that link refers also in this link http://friism.com/webcam-face-detection-in-c-using-emgu-cv 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) 1.)You don’t have to install OpenCV, but instead have to copy the relevant dlls (included with the Emgu CV download) to the folder where you code executes. 2.)Open CV and X64 are not friends. If you’re running X64 Windows (and unless you are up to recompiling OpenCV) you have to make sure your app is compiled to X86, instead of the usual “Any CPU”. 3.)Remember to add PictureBox as per the original tutorial. Here’s sample code:
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4. using Emgu.CV;
  5. using Emgu.Util;
  6. using Emgu.CV.Structure;
  7. using Emgu.CV.CvEnum;
  8.  
  9. namespace opencvtut
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.                 private Capture cap;
  14.                 private HaarCascade haar;
  15.  
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         private void timer1_Tick(object sender, EventArgs e)
  22.         {
  23.                 using (Image nextFrame = cap.QueryFrame())
  24.                 {
  25.                         if (nextFrame != null)
  26.                         {
  27.                                 // there's only one channel (greyscale), hence the zero index
  28.                                 //var faces = nextFrame.DetectHaarCascade(haar)[0];
  29.                                 Image grayframe = nextFrame.Convert();
  30.                                 var faces =
  31.                                         grayframe.DetectHaarCascade(
  32.                                                 haar, 1.4, 4,
  33.                                                 HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
  34.                                                 new Size(nextFrame.Width/8, nextFrame.Height/8)
  35.                                                 )[0];
  36.  
  37.                                 foreach (var face in faces)
  38.                                 {
  39.                                         nextFrame.Draw(face.rect, new Bgr(0,double.MaxValue,0), 3);
  40.                                 }
  41.                                 pictureBox1.Image = nextFrame.ToBitmap();
  42.                         }
  43.                 }
  44.         }
  45.  
  46.         private void Form1_Load(object sender, EventArgs e)
  47.         {
  48.             // passing 0 gets zeroth webcam
  49.                         cap = new Capture(0);
  50.             // adjust path to find your xml
  51.                         haar = new HaarCascade(
  52.                 "..\\..\\..\\..\\lib\\haarcascade_frontalface_alt2.xml");
  53.         }
  54.     }
  55. }
Good luck my fellow programmers..

Comments

hello sir , pwede ba akong magpaturo sa inyu about sa thesis nmin... kahit about sa concept nyu po... tnx po....this is my # para updated kaagad ako.. 09264529746 tnx and godblesz!!! then pwede nyu rin po aq e-mail... ung nilalaman ng subject q..tnx

I'm lester, can I have your add you in facebook or yahoo messenger to have conversation about C# and Visual Basic. I have lots of question in programming. Mr. Itachi add me [email protected] Thx

there is an error at the "var face= ..." the haar == null so it throws an error just saying. help would be appreciated on how to fix it.

when i write this code

var faces = grayframe.DetectHaarCascade(haar, 1.4, 4,
HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
new Size(25, 25))[0];

then they gives error for null exception

Add new comment