Search code examples
c#kinectemgucvface-detection

Faces not getting detected from Kinect Feed


Below is the code for a method which I am using for detecting faces from Kinect Feed and then setting the pixels from the face into a new image. . It is triggered by a Gesture which is done by GestureFlag . The Detect method which I am calling from the FaceDetection class is taken from the EMGU CV sample.

 public string Detect(WriteableBitmap colorBitmap, int GestureFlag)
    {
        if(GestureFlag!=0)
        {
            List<Rectangle> faces = new List<Rectangle>();               
            Bitmap bitface = BitmapFromSource(colorBitmap);
            Image<Bgr, Byte> image = new Image<Bgr, Byte>(bitface);
            FaceDetection.Detect(image, "haarcascade_frontalface_default.xml",faces);
             Bitmap img = new Bitmap(@"C:\Users\rama\Downloads\kinect-2-background-removal-master\KinectBackgroundRemoval\Assets\emptyimage.png");
            img = ResizeImage(img, 1540, 1980);
            int high = image.Height;
            int width = image.Width;                
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < high; j++)
                {
                    Bgr pixel = image[j,i];
                    System.Drawing.Point p = new System.Drawing.Point(j,i);                    
                        if (faces[0].Contains(p) && i<1540 && j<1980)
                        {
                            img.SetPixel(i, j, System.Drawing.Color.FromArgb(255, (int)pixel.Blue, (int)pixel.Green,(int) pixel.Red));
                        }                   
                }
            }
            count++;
            key = count.ToString() + "rich.jpg";
            image.Save(@"C:\Users\rama\Downloads\kinect-2-background-removal-master\KinectBackgroundRemoval\Assets\"+key);
            img.Save(@"C:\Users\rama\Downloads\kinect-2-background-removal-master\KinectBackgroundRemoval\"+key);
            bool status = UploadToS3("nitish2", key, @"C:\Users\rama\Downloads\kinect-2-background-removal-master\KinectBackgroundRemoval\"+key);
            var FBClient = new FacebookClient();
            var UploadToFacebook = new FacebookUpload(FBClient, key);
            UploadToFacebook.Show();
            GestureFlag = 0;                      
         }
        return key;
    }

The problem I'm running into is that an entirely different set of pixels gets printed on the new image which I'm saving.

Basically i think that the problem is here:

FaceDetection.Detect(image, "haarcascade_frontalface_default.xml",faces);
for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < high; j++)
                {
                    Bgr pixel = image[j,i];
                    System.Drawing.Point p = new System.Drawing.Point(j,i);
                    if (faces[0].Contains(p) && i<1540 && j<1980)
                        {
                            img.SetPixel(i, j, System.Drawing.Color.FromArgb(255, (int)pixel.Blue, (int)pixel.Green,(int) pixel.Red));
                        }

                 }
            }

So can someone please point out where I'm going wrong? Thank you very much.

EDITS : I've tried putting flags like faces[0]!=null which should take care of whether FaceDetection.Detect is actually returning anything but still I'm getting the same result.

I've also tried saving the ColorBitmap and testing it against the EMGU CV sample and it detects the faces in the image easily.

EDIT 2: So I've cross checked the co-ordinates of the rectangle which is being printed with the co-ordinates of the face being detected and the values of the Rectangle() being populated. They turn out to be almost same as far as I can see. So no luck there.

I don't know what else I can try for debugging.

If someone could point that out that would be great.

Thanks!


Solution

  • Ok I got it to work finally.

    The problem was the Point[j,i] which should have been Point[i,j] . Also my FaceDetection method proved to be a little erratic and I had to fix that too to finally properly debug my code.