goo hey

goo hey

  • NA
  • 29
  • 4.6k

Facial recognition issue

Jun 16 2020 5:35 AM
Can some help me with this issue, have a facial recognition app but when my camera records an image of a person who is not in the database yet, the application still displays the name of a person I have added to the database... some code below i think the issue might be
 
 
  1. if (isTrained) {  
  2.  Image < Gray, Byte > grayFaceResult = resultImage.Convert < Gray, Byte > ().Resize(200, 200, Inter.Cubic);  
  3.  CvInvoke.EqualizeHist(grayFaceResult, grayFaceResult);  
  4.  var result = recognizer.Predict(grayFaceResult);  
  5.  pictureBox1.Image = grayFaceResult.Bitmap;  
  6.  pictureBox2.Image = TrainedFaces[result.Label].Bitmap;  
  7.  Debug.WriteLine(result.Label + ". " + result.Distance);  
  8.  //Here results found known faces  
  9.  if (result.Label != -1 && result.Distance < 2000) {  
  10.   CvInvoke.PutText(currentFrame, PersonsNames[result.Label], new Point(face.X - 2, face.Y - 2),  
  11.    FontFace.HersheyComplex, 1.0, new Bgr(Color.Orange).MCvScalar);  
  12.   CvInvoke.Rectangle(currentFrame, face, new Bgr(Color.Green).MCvScalar, 2);  
  13.  }  
  14.  //here results did not found any know faces  
  15.  else {  
  16.   CvInvoke.PutText(currentFrame, "Unknown"new Point(face.X - 2, face.Y - 2),  
  17.    FontFace.HersheyComplex, 1.0, new Bgr(Color.Orange).MCvScalar);  
  18.   CvInvoke.Rectangle(currentFrame, face, new Bgr(Color.Red).MCvScalar, 2);  
  19.   
  20.  }  
  21. }  
  22. private bool TrainImagesFromDir() {  
  23.   int ImagesCount = 0;  
  24.   double Threshold = 2000;  
  25.   TrainedFaces.Clear();  
  26.   PersonsLabes.Clear();  
  27.   PersonsNames.Clear();  
  28.   try {  
  29.    string path = Directory.GetCurrentDirectory() + @ "\TrainedImages";  
  30.    string[] files = Directory.GetFiles(path, "*.jpg", SearchOption.AllDirectories);  
  31.   
  32.    foreach(var file in files) {  
  33.     Image < Gray, byte > trainedImage = new Image < Gray, byte > (file).Resize(200, 200, Inter.Cubic);  
  34.     CvInvoke.EqualizeHist(trainedImage, trainedImage);  
  35.     TrainedFaces.Add(trainedImage);  
  36.     PersonsLabes.Add(ImagesCount);  
  37.     string name = file.Split('\\').Last().Split('_')[0];  
  38.     PersonsNames.Add(name);  
  39.     ImagesCount++;  
  40.     Debug.WriteLine(ImagesCount + ". " + name);  
  41.   
  42.    }  
  43.   
  44.    if (TrainedFaces.Count() > 0) {  
  45.     // recognizer = new EigenFaceRecognizer(ImagesCount,Threshold);  
  46.     recognizer = new EigenFaceRecognizer(ImagesCount, Threshold);  
  47.     recognizer.Train(TrainedFaces.ToArray(), PersonsLabes.ToArray());  
  48.   
  49.     isTrained = true;  
  50.     //Debug.WriteLine(ImagesCount);  
  51.     //Debug.WriteLine(isTrained);  
  52.     return true;  
  53.    } else {  
  54.     isTrained = false;  
  55.     return false;  
  56.    }  
  57.   } catch (Exception ex) {  
  58.    isTrained = false;  
  59.    MessageBox.Show("Error in Train Images: " + ex.Message);  
  60.    return false;  
  61.   }