Click here to Skip to main content
15,889,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
public string FindFace()
        {
           
            string ex = Path.GetExtension(lbFileName.Text);
            var uniqueName = Guid.NewGuid();
            
            var myUniqueFileName = string.Format(@"{0}{1}", uniqueName, ex);
            Console.WriteLine("Answer ",myUniqueFileName);
            Bitmap InputImage = new Bitmap(lbFileName.Text);
            InputImage.Save(path + myUniqueFileName);


            //System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter(myUniqueFileName);
            //Image img = (Image)converter.ConvertFrom(byteArrayIn);

            
            
            using (Image<Bgr, Byte> image = new Image<Bgr, byte>(myUniqueFileName))
            {
                HaarCascade haar = new HaarCascade("haarcascade_frontalface_alt2.xml");
                // there's only one channel (greyscale), hence the zero index
                //var faces = nextFrame.DetectHaarCascade(haar)[0];
                Image<Gray, byte> grayframe = image.Convert<Gray, byte>();
                var faces =
                        grayframe.DetectHaarCascade(
                                haar, 1.4, 4,
                                HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                new Size(image.Width / 25, image.Height / 25)
                                )[0];

                if (faces.Length != 0)
                {

                   
                    //foreach (var face in faces)
                    //{
                        var FaceFileName = string.Format(@"{0}{1}", uniqueName, ex);
                        // image.Draw(face.rect, new Bgr(0, 255, 0), 3);
                        image.ROI = faces[0].rect;

                        string FaceImage = path + "Temp\\" + FaceFileName;
                        image.Save(FaceImage);

                        ImgFace.Image = Image.FromFile(FaceImage);


                        MessageBox.Show("Face Detected", "Success", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                   

                    //}
                   
                }
                else
                {

                    MessageBox.Show("No Face In this Image", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    lbFileName.Text = "No File Choosen";
                    this.pictureBox1.BackgroundImage = global::FaceAnnotation.Properties.Resources.no_image_selected;
                }
                File.Delete(path + myUniqueFileName);

                return uniqueName.ToString();
               
            }

        }
Posted
Updated 13-Feb-15 20:33pm
v3
Comments
Member 11166005 14-Feb-15 1:50am    
i am getting error in using (Image<bgr, byte=""> image = new Image<bgr, byte="">(myUniqueFileName))
PIEBALDconsult 14-Feb-15 2:07am    
Then supply a valid parameter, we have no idea what that might be.
phil.o 14-Feb-15 4:17am    
Test the value of path + myUniqueFileName in your debugger.
Member 11166005 14-Feb-15 4:53am    
i am getting error in next line and the error is
filenotfoundexception was unhandled in the line
HaarCascade haar = new HaarCascade("haarcascade_frontalface_alt2.xml");

1 solution

The best practice when it comes to build file system paths is to use a dedicated method:
C#
string result = Path.Combine(path, myUniqueFileName);

This will take care that you get a valid path specification.
More here: MSDN: Path Class[^]
 
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