Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I WANT TO DETECT ONLY UNIQUE FACES FROM A VIDEO USING FACE DETECTION FROM EACH FRAME OF THE VIDEO BUT ONLY WANT TO SAVE UNIQUE FACES TO MY FOLDER FROM ALL THE DETECTED FILES this is the code i am using for processing frame


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();
                       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
           {

           }
       }


What I have tried:

I HAVE TRIED MANY WAYS BUT THE PROBLEM COMING IS IT IS DETECTING THE SAME FACE AGAIN AND AGAIN WITH FRAME AND SAVING THEM TO THE FOLDER WHERE I WANT IT TO SAVE ONLY ONE UNIQUE FACE OF A PARTICULAR PERSON TO THE FOLDER IAM USING Emgu.CV,FaceRecognitionDotNet,DlibDotNet .iam doing it in c# windows forms

IS THERE ANY WAY TO SAVE THE UNIQUE FACES ONLY FROM THE VIDEO AND REMOVE THE FACES WHICHN ARE REPEATING.THIS PROCESS SHOULD WORK WHILE PROCESSING OF THE VIDEO
Posted
Updated 5-Sep-23 22:18pm
v3
Comments
Richard MacCutchan 4-Sep-23 7:26am    
Please do not shout. Typin in ALL CAPITALS is considered shouting, and rude, on the internet.
[no name] 4-Sep-23 10:17am    
Well ... compare the image you're saving with all the others you've saved, and test for "duplicates".
Richard Deeming 6-Sep-23 4:23am    
Start by fixing your keyboard - C# is a case-sensitive language, and it seems that your CAPS-LOCK key is stuck.

Either that, or you're just being a troll, and ignoring the previous suggestion to STOP SHOUTING!
Graeme_Grant 6-Sep-23 4:50am    
Screaming, demanding ... you're off to a good start...

1 solution

STOP YELLING AT PEOPLE!

Your code only does face detection. It has no clue it's looking at a new frame of video and will return every face it sees in each frame. There is nothing at all in the code to tell the library you're using to skip faces it has already detected nor is there any code that compares the returned faces to what you have stored already.

How do you do that? I have no idea.
 
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