Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I am using haarcascade_frontalface_alt_tree.xml to detect face from video. I get result also but the problem is the frame size of the face of every person is not the whole size of face. I am missing the upper and lower part of the face. I want to increase the size of the frame gets cut from the video.
C#
private void ProcessFrame(object sender, EventArgs e)
      {
          try
         {
              //Step 1: Video Capture
             if (videoCapture != null &&
                 videoCapture.Ptr != IntPtr.Zero)
             {
                 videoCapture.Retrieve(frame, 0);
                 currentFrame = frame.ToImage<Bgr,
                 byte>().Resize(picCapture.Width,
                 picCapture.Height, Emgu.CV.CvEnum.Inter.Cubic);

                  //currentFrame = frame.ToImage<Bgr, byte>().Resize(axWindowsMediaPlayer1.Width, axWindowsMediaPlayer1.Height, Emgu.CV.CvEnum.Inter.Cubic);

                  //Render the video capture into the Picture Box PicCapture
                  ////  picCapture.Image = currentFrame.AsBitmap();
                  ///

                  //Step 2: Face Detection
                  if (facesDetectionEnabled)
                  {
                      //Convert from Bgr to Gray Image
                      Mat grayImage = new Mat();
                      //Size minSize = new Size(30, 30);
                      // Adjust as needed
                      //Size maxSize = new Size(150, 50);
                      // Adjust as needed

                      CvInvoke.CvtColor(currentFrame, grayImage,
                                        ColorConversion.Bgr2Gray);
                      //Enhance the image to get better result
                     CvInvoke.EqualizeHist(grayImage, grayImage);

                      System.Drawing.Rectangle[] faces =
                      faceCasacdeClassifier.DetectMultiScale
                      (grayImage, 1.1, 3);
                      //If faces detected

                      if (faces.Length > 0)
                      {
                          // lbl_FaceCount_Data.Text =
                          // faces.Length.ToString();
                          foreach (var face in faces)
                          {
                              //Draw square around each face
                              CvInvoke.Rectangle(currentFrame, face,
                              new Bgr(System.Drawing.Color.Red).MCvScalar, 2);

                              //Step 3: Add Person
                              //Assign the face to the picture Box
                              //face picDetected
                              Image<Bgr, byte> resultImage =
                              currentFrame.Convert<Bgr, byte>();
                              resultImage.ROI = face;
                              picDetected.SizeMode =
                              PictureBoxSizeMode.StretchImage;
                              picDetected.Image = resultImage.AsBitmap();

                              if (EnableSaveImage)
                              {
                                 //We will create a directory
                                 //if does not exists!
                                 // string path =
                                 //System.IO.Directory.GetCurrentDirectory()
                                 // + @"\TrainedImages";
                                 string path = Value.ValueG._DataFilePath +
                                 "\\Video\\FaceFind\\Video" +
                                 Value.ValueG._CaseVideo_Id;

                                  // string matchingImagesFolderPath =
                                  // "C:\\Path\\To\\MatchingImagesFolder";
                                  string path1 =
                                  System.IO.Directory.GetCurrentDirectory()
                                  + @"\TrainedImages";

                                 System.IO.Directory.CreateDirectory(path1);

                                  if (!System.IO.Directory.Exists(path))
                                 System.IO.Directory.CreateDirectory(path);
                                  //we will save 10 images with delay a
                                  //second for each image
                                  //to avoid hang GUI
                                  //we will create a new task
                                  Task.Factory.StartNew(() =>
                                  {
                                      for (int i = 0; i < 1; i++)
                                      {
                                          //resize the image then saving it
                                          ////  resultImage.Resize(200, 200,
                                          //Inter.Cubic).Save(path + @"\"
                                          //+ txtPersonName.Text +
                                          //ImageCount +
                                          //DateTime.Now.ToString
                                          //("-hh-mm-ss") + ".jpg");
                                          //// ImageCount++;

                                          resultImage.Resize(200, 200,
                                          Inter.Cubic).Save(path + @"\" +
                                          txtPersonName.Text + "_" +
                                          DateTime.Now.ToString
                                          ("hh-mm-ss") + ".jpg");

                                          //resultImage.Resize(200, 200,
                                          //Inter.Cubic).Save(Path.Combine
                                          //(path1, txtPersonName.Text +
                                          // "_" + DateTime.Now.ToString
                                          //("hh-mm-ss") + ".jpg"));

                                          Thread.Sleep(1000);
                                      }
                                  });
                              }
                              EnableSaveImage = true;

                              if (btnAddPerson.InvokeRequired)
                              {
                                  btnAddPerson.Invoke
                                     (new ThreadStart(delegate
                                  {
                                      btnAddPerson.Enabled = true;
                                  }));
                              }
                          }
                      }
                  }
                  //Render the video capture into the
                  //Picture Box picCapture
                  picCapture.Image = currentFrame.AsBitmap();
                  // axWindowsMediaPlayer1. = currentFrame.AsBitmap();
                  //
                  ////string path1 = Value.ValueG._DataFilePath +
                  //"\\Video\\FaceFind\\Video" +
                  //Value.ValueG._CaseVideo_Id;
                  ////int fileCount =
                  ////System.IO.Directory.GetFiles(path1).Length;
                  ////lbl_FaceCount_Data.Text = fileCount.ToString();
              }
              //Dispose the Current Frame after processing it
              //to reduce the memory consumption.
              if (currentFrame != null)
              {
                  currentFrame.Dispose();
              }
              ////string path1 = Value.ValueG._DataFilePath +
              ////"\\Video\\FaceFind\\Video" + Value.ValueG._CaseVideo_Id;
              ////int fileCount =
              //// System.IO.Directory.GetFiles(path1).Length;
              ////lbl_FaceCount_Data.Text = fileCount.ToString();
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
          finally
          {

          }
      }

This is the method I am using for processing the frame. How can I increase the size of the frame to upwards and downwards?

What I have tried:

I tried haarcascade_frontalface_alt_tree.xml to capture the frame and detect the face.
Posted
Updated 13-Sep-23 4:25am
v4

1 solution

You can add a padding variable to control how much extra space is added around each detected face to ensure the entire face is captured. You can adjust the padding value as you need. The expanded ROI (region of interest) is calculated based on the detected face's position and size, and it takes into account the image boundaries to prevent going out of bounds -
C#
//Step 2: Face Detection...
if (facesDetectionEnabled)
{
    //Convert from Bgr to Gray Image...
    Mat grayImage = new Mat();
    CvInvoke.CvtColor(currentFrame, grayImage, ColorConversion.Bgr2Gray);
    //Enhance the image to get better results...
    CvInvoke.EqualizeHist(grayImage, grayImage);

    System.Drawing.Rectangle[] faces = faceCasacdeClassifier.DetectMultiScale(grayImage, 1.1, 3);

    //If faces are detected...
    if (faces.Length > 0)
    {
        foreach (var face in faces)
        {
            //Draw a square around each face...
            CvInvoke.Rectangle(currentFrame, face, new Bgr(System.Drawing.Color.Red).MCvScalar, 2);

            //Expand the region of interest (ROI) to include the entire face...
            int padding = 10; //You can adjust the padding here as needed...
            Rectangle expandedFace = new Rectangle(
                Math.Max(face.Left - padding, 0),
                Math.Max(face.Top - padding, 0),
                Math.Min(face.Width + 2 * padding, currentFrame.Width - face.Left),
                Math.Min(face.Height + 2 * padding, currentFrame.Height - face.Top));

            //Assign the expanded ROI to the picture Box picDetected...
            Image<Bgr, byte> resultImage = currentFrame.Convert<Bgr, byte>();
            resultImage.ROI = expandedFace;
            picDetected.SizeMode = PictureBoxSizeMode.StretchImage;
            picDetected.Image = resultImage.AsBitmap();

            //Rest of your code...
        }
    }
}
 
Share this answer
 

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