HaarCascade.Detect Method stucking

Ask questions on using Emgu CV with C++
Post Reply
Lnear
Posts: 6
Joined: Fri Jul 06, 2012 8:22 am

HaarCascade.Detect Method stucking

Post by Lnear » Fri Jul 13, 2012 10:16 am

Hi,
I am currently working on a WPF face detection application using kinect and Emgu. I am also working on x64 OS.
When I debug my application, it start loading and just freezes at a given point. Since I couldn't even have error messages from the debug operation for the application stucks, step by step I've removed parts of the code, and the HaarCascade.Detect Method seems to be the problem. here is the part of the code code:

Code: Select all

using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
            {
                if (colorFrame == null)
                {
                    return;
                }

                Bitmap bitmap = colorFrame.ToBitmap();
                Image<Bgr, byte> imageFrame = new Image<Bgr, byte>(bitmap);
                Image<Gray, byte> grayFrame = imageFrame.Convert<Gray, byte>();

                //var faces = haar.Detect(grayFrame, 1.4, 4,
                //                         HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                //                         new System.Drawing.Size(20, 20),
                //                         new System.Drawing.Size(400, 400));

                //foreach (var face in faces)
                //{
                //    imageFrame.Draw(face.rect, new Bgr(System.Drawing.Color.Blue), 3);
                //}

                image1.Source = ToBitmpSrc(imageFrame);
            }
has you can notice by commenting the HaarCascade.Detect part it works very well, even by replacing "imageFrame" by "grayFrame" i get my "gray Image" in the application window.
Does anyone has an idea of the problem ?

Thank you in advance.

Chris_Johnson
Posts: 573
Joined: Tue Sep 20, 2011 3:29 pm

Re: HaarCascade.Detect Method stucking

Post by Chris_Johnson » Mon Jul 16, 2012 4:36 am

Hi,

I expect that it's because your using an outdated call to the method the method should now be this:

Where:
HaarCascade _face = new HaarCascade("haarcascade_frontalface_alt2.xml");

Code: Select all

using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
            {
                if (colorFrame == null)
                {
                    return;
                }

                Bitmap bitmap = colorFrame.ToBitmap();
                Image<Bgr, byte> imageFrame = new Image<Bgr, byte>(bitmap);
                Image<Gray, byte> grayFrame = imageFrame.Convert<Gray, byte>();

                grayFrame._EqualizeHist(); //allows for better detection due to equalised lighting effect

               MCvAvgComp[] faces = grayFrame.DetectHaarCascade(_face)[0];
                if (faces.Length > 0)
                {
                  foreach (var face in faces)
                  {
                    imageFrame.Draw(face.rect, new Bgr(System.Drawing.Color.Blue), 3);
                  }
                }
                image1.Source = ToBitmpSrc(imageFrame);
            }
Hope that helps,
Cheers,
Chris

Lnear
Posts: 6
Joined: Fri Jul 06, 2012 8:22 am

Re: HaarCascade.Detect Method stucking

Post by Lnear » Tue Jul 17, 2012 4:43 am

Hi,
Actually that's one which is the old way to do it. and I've also tried but the same problem occurred.
I've tried this using a loaded image, and it works.

Code: Select all

void kinectSensorChooser_KinectChanged(object sender, KinectChangedEventArgs e)
        {
            KinectSensor oldSensor = (KinectSensor)e.OldSensor;
            StopKinect(oldSensor);
            newSensor = (KinectSensor)e.NewSensor;

            if (newSensor == null)
            {
                return;
            }

            newSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
           // newSensor.AllFramesReady += new EventHandler<AllFramesReadyEventArgs>(newSensor_AllFramesReady);
           // try
           // {
              //  newSensor.Start();
           // }
            //catch (System.IO.IOException)
           // {
             //  kinectSensorChooser.TryResolveConflict();
           // }
            
            System.Drawing.Image inputImg = System.Drawing.Image.FromFile(@"D:\pic3.jpg");
            Image<Bgr, Byte> imageFrame = new Image<Bgr, byte>(new Bitmap(inputImg));
            Image<Gray, Byte> grayFrame = imageFrame.Convert<Gray, Byte>();
            //normalizes brightness and increases contrast of the image
            grayFrame._EqualizeHist();

            Detect the faces  from the gray scale image and store the locations as rectangle
            MCvAvgComp[] faces = haar.Detect(grayFrame, 1.1, 5,
                                     HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                     new System.Drawing.Size(24, 24),
                                     System.Drawing.Size.Empty);

            foreach (MCvAvgComp face in faces)
            {
                imageFrame.Draw(face.rect, new Bgr(System.Drawing.Color.Blue), 2);
            }

            image1.Source = ToBitmpSrc(imageFrame);

        }
but when I tried to use it in live using the "AllFramesReady event (obviously after removing changing the commented parts of the code above), I get stuck ( same code as in my first post ).
Is it something related to thread ? the video stream speed (30Fps)?

Hugues

Crap04
Posts: 1
Joined: Sat May 18, 2013 6:02 am

Re: HaarCascade.Detect Method stucking

Post by Crap04 » Sat May 18, 2013 6:15 am

I have done the same code sir...but still no image is being displayed on the screen I can't seem to identify the problem...

I have created a helper method...

Code: Select all

public static System.Drawing.Bitmap ToBitmap(this BitmapSource bitmapsource)
        {
            System.Drawing.Bitmap bitmap;
            using (var outStream = new MemoryStream())
            {
                // from System.Media.BitmapImage to System.Drawing.Bitmap
                BitmapEncoder enc = new BmpBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create(bitmapsource));
                enc.Save(outStream);
                bitmap = new System.Drawing.Bitmap(outStream);
                return bitmap;
            }
        }
and used it on

Code: Select all

 Image<Bgr, Byte> imageFrame = new Image<Bgr, byte>(ImageHelpers.ToBitmap(colorFrame.ToBitmapSource()));
converted it to gray scale

Code: Select all

Image<Gray, Byte> grayFrame = imageFrame.Convert<Gray, Byte>();
and tried to detect the haar with

Code: Select all

MCvAvgComp[] faces = _face.Detect(grayFrame, 1.1, 5,
                                             HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                             new System.Drawing.Size(24, 24),
                                             System.Drawing.Size.Empty);

                    foreach (MCvAvgComp face in faces)
                    {
                        imageFrame.Draw(face.rect, new Bgr(System.Drawing.Color.Blue), 2);
                    }

                    Image1.Source = ImageHelpers.ToBitmapSource(imageFrame);
running the program, no image is being displayed on the screen...just pure black then it hangs...

Roman77
Posts: 1
Joined: Fri Mar 27, 2015 11:39 pm

Re: HaarCascade.Detect Method stucking

Post by Roman77 » Fri Mar 27, 2015 11:40 pm

This is extremely decent and not too bad post....You shook posting it....Thanks a great deal for posting it....!!!

Michaelgrawl
Posts: 13
Joined: Sun Jun 10, 2018 1:52 am
Location: sportzfuel.com
Contact:

HaarCascade Detect Method stucking

Post by Michaelgrawl » Sat Jul 14, 2018 1:01 am

Hi everyone, I want to detect the human right and left eyes separately using the Haar Cascade. However, from the xml files I have, both the haarcascade_lefteye_2splits and haarcascade_righteye_2splits detect the same eye which is the left eye from human side. I want to do this because I want to get the coordinates of both eyes. Or is there any method that I can use to get both eye coordinates by only using haarcascade_eye_tree_eyeglasses xml file? Please guide me thanks
Sport nutrition here: https://www.sportzfuel.com/

Post Reply