0 down vote favorite
I was working on Face Recognition code. After training the database and calling EigenObjectRecognizer, the result is a black image with unrecognized label.While the code running it looks like the following http://www.mediafire.com/view/?ewns4iqvd51adsc .And as shown in the picture the detected and supposed to be recognized and extracted face in the image box is totally black. And the input image for recognition is exactly the same as the one the database is trained with.So why it has kept giving Unknown or Unrecognized result. Part of the code looks
Images from the training set loaded as
public FaceRecognizer()
{
InitializeComponent();
try
{
ContTrain = ContTrain + 1;
string NameLabelsinfo = File.ReadAllText(Application.StartupPath +
"/TrainedFaces/TrainedNameLables.txt");
string[] NameLabels = NameLabelsinfo.Split('%');
NumNameLabels = Convert.ToInt16(NameLabels[0]);
string IDLabelsinfo = File.ReadAllText(Application.StartupPath +
"/TrainedFaces/TrainedNameLables.txt");
string[] IDLables = IDLabelsinfo.Split('%');
NumIDLabels = Convert.ToInt16(IDLables[0]);
if (NumNameLabels == NumIDLabels)
{
ContTrain = NumNameLabels;
string LoadFaces;
for (int tf = 1; tf < NumNameLabels + 1; tf++)
{
LoadFaces = String.Format("face{0}.bmp", tf);
trainingImages.Add(new Image<Gray, byte>(String.Format("
{0}/TrainedFaces/{1}", Application.StartupPath, LoadFaces)));
IDLabless.Add(IDLables[tf]);
NameLabless.Add(NameLabels[tf]);
}
}
}
catch (Exception e)
{
MessageBox.Show("Nothing in binary database, please add at least a
face(Simply train the prototype with the Add Face Button).", "Triained
faces load",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
The face recognizer method looks like
private void RecognizeFaces()
{
Image<Gray, byte> grayframe = GetGrayframe();
stringOutput.Add("");
MinNeighbors = int.Parse(comboBoxMinNeigh.Text);
WindowsSize = int.Parse(textBoxWinSiz.Text);
ScaleIncreaseRate = Double.Parse(comboBoxScIncRte.Text);
var faces = grayframe.DetectHaarCascade(haar, ScaleIncreaseRate,
MinNeighbors,
HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
new Size(WindowsSize, WindowsSize))[0];
if (faces.Length > 0 && trainingImages.ToArray().Length != 0)
{
Bitmap ExtractedFace;
ExtFaces = new Image<Gray, byte>[faces.Length];
faceNo = 0;
foreach (var face in faces)
{
ExtractedFace = new Bitmap(face.rect.Width, face.rect.Height);
ExtFaces[faceNo] = new Image<Gray, byte>(ExtractedFace);
ExtFaces[faceNo] = ExtFaces[faceNo].Resize(100, 100,
Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
MCvTermCriteria termCrit = new MCvTermCriteria(ContTrain, 0.001);
EigenObjectRecognizer recognizer = new EigenObjectRecognizer(
trainingImages.ToArray(),
NameLabless.ToArray(),
700,
ref termCrit);
stringOutput[faceNo] = recognizer.Recognize(ExtFaces[faceNo]);
stringOutput.Add("");
faceNo++;
}
pbExtractedFaces.Image = ExtFaces[0].ToBitmap();
if (stringOutput[0] == "")
{
label1.Text = "Unknown";
label9.Text = "";
}
else
{
label1.Text = "Known";
label9.Text = stringOutput[0];
}
}
if (faceNo == 0)
{
MessageBox.Show("No face detected");
}
else
{
btnNextRec.Enabled = true;
btnPreviousRec.Enabled = true;
}
}
The training set is trained with detected faces as follows
private void saveFaceToDB_Click(object sender, EventArgs e)
{
abd = (Bitmap) pbExtractedFaces.Image;
TrainedFaces = new Image<Gray, byte>(abd);
trainingImages.Add(TrainedFaces);
NameLabless.Add(StudentName.Text);
IDLabless.Add(StudentID.Text);
File.WriteAllText(Application.StartupPath + "/TrainedFaces
/TrainedNameLables.txt", trainingImages.ToArray().Length + "%");
File.WriteAllText(Application.StartupPath + "/TrainedFaces
/TrainedIDLables.txt", trainingImages.ToArray().Length + "%");
for (int i = 1; i < trainingImages.ToArray().Length + 1; i++)
{
trainingImages.ToArray()[i - 1].Save(String.Format("{0}/TrainedFaces
/face{1}.bmp", Application.StartupPath, i));
File.AppendAllText(Application.StartupPath + "/TrainedFaces
/TrainedIDLables.txt", NameLabless.ToArray()[i - 1] + "%");
File.AppendAllText(Application.StartupPath + "/TrainedFaces
/TrainedNameLables.txt", IDLabless.ToArray()[i - 1] + "%");
}
MessageBox.Show(StudentName.Text + "´s face detected and added :)", "Training
OK", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Thank