Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am doing face detection using haar detector from emgu cv open library.
Using the language C#.

C#
public Form1()
{
    InitializeComponent();
    try
    {
        haar = new HaarCascade("C:\\Program Files\\emgucv2.1.0.793\\opencv\\data\\haarcascades\\haarcascade_frontalface_alt_tree.xml");
        
        capture = new Capture(0);
    }
    catch
    {
    }
}

private void timer1_Tick(object sender, EventArgs e)
{
    {
        Image<bgr,> image = capture.QueryFrame();
    Bitmap original = image.ToBitmap(pictureBox1.Width,pictureBox1.Height);


It is giving error at run time.
It does not show image in the picture box when it runs and gives null reference exception in line:
Image<bgr,> image = capture.QueryFrame();

I have used breakpoints to detect errors, it is going to the Haar line but it not entering in capture line capture= new capture(0).

Can anyone tell me the error why image is not been captured in picture box at run time?
I tried this using emgucv liberary in win XP as well as window 7.
Posted
Updated 29-Sep-11 8:05am
v3
Comments
JF2015 4-Jan-11 2:31am    
Edited to fix code formatting.

Empty catch blocks are always wrong.
Catch the error as it happens, otherwise you'll never know the real problem.

Cheers
 
Share this answer
 
Comments
OriginalGriff 4-Jan-11 5:35am    
Not always, sometimes they are your only way to do things. For example, what do you do when there is a problem in your error reporting / logging module? Any attempt to log the problem will likely make the situation worse...
They are an anathema though, and need to be well commented with why they are empty.
I don't know anything about the library you are using on face detection, but I think your problem is with the timer. Asign the tick event handler after the line of code capture = new Capture(0), because probably tick runs before you initialize capture
C#
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);

and remove the above line form designer...

or
set in designer Enabled = false
and in code after capture = new capture set it to enabled = true
 
Share this answer
 
v2
This is an educated guess (given I have not met the EMGUCV library), but from what you say :
"I have used breakpoints to detect errors, it is going to the Haar line but it not entering in capture line capture= new capture(0)."
It would seem that creating a new HaarCascade does not return immediately, so you are not reaching the capture instance creation.

Since you are working in a timer, the way I would handle it is:
1) Remove the capture and cascade creation from the form constructor.
2) On the first timer tick, suspend the timer, display a "please wait" message and create the cascade. Re-enable the timer when the cascade is complete, and remove the message. Then create the capture instance and continue.

I can't check what should happen with teh cascade, because the EMGU CV website is not responding, but it should have samples there anyway. Have a look and see what they do.
 
Share this answer
 
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;

namespace CameraCapture
{
    public partial class CameraCapture : Form
    {
        Image<Bgr, Byte> image;
        private Capture _capture;
        private bool _captureInProgress;

        public CameraCapture()
        {
            InitializeComponent();
        }

        private void ProcessFrame(object sender, EventArgs arg)
        {
            image = _capture.QueryFrame();

            Run();


            captureImageBox.Image = image;

        }
        public void Run()
        {

            Image<Gray, Byte> gray = image.Convert<Gray, Byte>(); //Convert it to Grayscale


            //normalizes brightness and increases contrast of the image
            gray._EqualizeHist();

            //Read the HaarCascade objects
           HaarCascade face = new HaarCascade("haarcascade_frontalface_alt_tree.xml");
             //HaarCascade eye = new HaarCascade("haarcascade_eye.xml");

            //Detect the faces  from the gray scale image and store the locations as rectangle
            //The first dimensional is the channel
            //The second dimension is the index of the rectangle in the specific channel
            MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
               face,
               1.1,
               10,
               Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
               new Size(20, 20));

            foreach (MCvAvgComp f in facesDetected[0])
            {
                //draw the face detected in the 0th (gray) channel with blue color
                image.Draw(f.rect, new Bgr(Color.Blue), 2);




            }


        }
        private void captureButtonClick(object sender, EventArgs e)
        {
            #region if capture is not created, create it now
            if (_capture == null)
            {
                try
                {
                    _capture = new Capture();
                }
                catch (NullReferenceException excpt)
                {
                    MessageBox.Show(excpt.Message);
                }
            }
            #endregion

            if (_capture != 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 ReleaseData()
        {
            if (_capture != null)
                _capture.Dispose();
        }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900