使用emgu cv和C#的人脸识别代码,它始终返回黑色图像和未知/无法识别的标签
我在做人脸识别项目。在训练数据库并调用EigenObjectRecognitzer后,结果是一个带有无法识别标签的黑色图像。当代码运行时,它看起来如下所示。如图所示,图像框中检测到的、应该识别和提取的人脸是完全黑色的。而且用于识别的输入图像与用于训练数据库的图像完全相同。因此,为什么它一直给出未知或无法识别的结果呢。 部分代码看起来 来自作为加载的训练集的图像使用emgu cv和C#的人脸识别代码,它始终返回黑色图像和未知/无法识别的标签,c#,opencv,image-processing,emgucv,face-recognition,C#,Opencv,Image Processing,Emgucv,Face Recognition,我在做人脸识别项目。在训练数据库并调用EigenObjectRecognitzer后,结果是一个带有无法识别标签的黑色图像。当代码运行时,它看起来如下所示。如图所示,图像框中检测到的、应该识别和提取的人脸是完全黑色的。而且用于识别的输入图像与用于训练数据库的图像完全相同。因此,为什么它一直给出未知或无法识别的结果呢。 部分代码看起来 来自作为加载的训练集的图像 public FaceRecognizer() { InitializeComponent();
public FaceRecognizer()
{
InitializeComponent();
//Load faces from the dataset
try
{
ContTrain = ContTrain + 1;
//Load previous trained and labels for each image from the database Here
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;
// Converting the master image to a bitmap
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)
{
//Returns the following message if nothing saved in the training set
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);
}
}
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);
//Write the number of trained faces in a file text for further load
File.WriteAllText(Application.StartupPath + "/TrainedFaces
/TrainedNameLables.txt", trainingImages.ToArray().Length + "%");
File.WriteAllText(Application.StartupPath + "/TrainedFaces
/TrainedIDLables.txt", trainingImages.ToArray().Length + "%");
//Write the labels of trained faces in a file text for further load
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);
}
公共人脸识别器()
{
初始化组件();
//从数据集中加载面
尝试
{
ContTrain=ContTrain+1;
//在此处为数据库中的每个图像加载以前训练过的图像和标签
字符串NameLabelsinfo=File.ReadAllText(Application.StartupPath+
“/TrainedFaces/TrainedNameLables.txt”);
字符串[]NameLabels=NameLabelsinfo.Split('%');
NumNameLabels=Convert.ToInt16(NameLabels[0]);
字符串IDLabelInfo=File.ReadAllText(Application.StartupPath+
“/TrainedFaces/TrainedNameLables.txt”);
字符串[]IDLables=idlabelinfo.Split('%');
NumIDLabels=Convert.ToInt16(IDLables[0]);
if(NumNameLabels==NumIDLabels)
{
CONTRAIN=NumNameLabels;
字符串加载面;
//将主图像转换为位图
对于(int-tf=1;tf
人脸识别器方法看起来像
private void RecognizeFaces()
{
//detect faces from the gray-scale image and store into an array of type
// 'var',i.e 'MCvAvgComp[]'
Image<Gray, byte> grayframe = GetGrayframe();
stringOutput.Add("");
//Assign user-defined Values to parameter variables:
MinNeighbors = int.Parse(comboBoxMinNeigh.Text); // the 3rd parameter
WindowsSize = int.Parse(textBoxWinSiz.Text); // the 5th parameter
ScaleIncreaseRate = Double.Parse(comboBoxScIncRte.Text); //the 2nd
//parameter
//Detect faces from an image and save it to var i.t MCvAcgComp[][]
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; //empty
ExtFaces = new Image<Gray, byte>[faces.Length];
faceNo = 0;
foreach (var face in faces)
{
// ImageFrame.Draw(face.rect, new Bgr(Color.Green), 3);
//set the size of the empty box(ExtractedFace) which will later
//contain the detected face
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);
//TermCriteria for face recognition with numbers of trained images
// like maxIteration
MCvTermCriteria termCrit = new MCvTermCriteria(ContTrain, 0.001);
//Eigen face recognizer
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(); //draw the face detected
// in the 0th (gray) channel with blue color
if (stringOutput[0] == "")
{
label1.Text = "Unknown";
label9.Text = "";
}
//Draw the label for each face detected and recognized
else
{
//string[] label = stringOutput[faceNo].Split(',');
label1.Text = "Known";
// for (int i = 0; i < 2; i++)
//{
label9.Text = stringOutput[0];
//label7.Text = label[1];
//}
}
}
if (faceNo == 0)
{
MessageBox.Show("No face detected");
}
else
{
btnNextRec.Enabled = true;
btnPreviousRec.Enabled = true;
}
}
private void RecognizeFaces()
{
//从灰度图像中检测人脸并存储到类型数组中
//“var”,即“MCvAvgComp[]”
Image grayframe=GetGrayframe();
stringOutput.Add(“”);
//将用户定义的值分配给参数变量:
MinNeighbors=int.Parse(comboBoxMinNeigh.Text);//第三个参数
WindowsSize=int.Parse(textBoxWinSiz.Text);//第5个参数
ScaleIncreaseRate=Double.Parse(comboboxscincrete.Text);//第二个
//参数
//从图像中检测人脸并将其保存到var i.t MCvAcgComp[][]
var faces=灰色帧。检测到ARCASCADE(haar,标度递增率,
明尼堡,
HAAR_检测类型。DO_CANNY_修剪,
新大小(WindowsSize,WindowsSize))[0];
如果(faces.Length>0&&trainingImages.ToArray().Length!=0)
{
位图提取面;//空
ExtFaces=新图像[faces.Length];
faceNo=0;
foreach(面中的面变量)
{
//ImageFrame.Draw(face.rect,新Bgr(Color.Green),3);
//设置稍后将显示的空框(ExtractedFace)的大小
//包含检测到的人脸
ExtractedFace=新位图(face.rect.Width、face.rect.Height);
ExtFaces[faceNo]=新图像(ExtractedFace);
ExtFaces[faceNo]=ExtFaces[faceNo]。调整大小(100100,
Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
//基于训练图像数的人脸识别准则
//像maxIteration
MCvTermCriteria termCrit=新的MCvTermCriteria(续,0.001);
//特征人脸识别器
特征对象识别器识别器=新的特征对象识别器(
trainingImages.ToArray(),
无法命名。ToArray(),
700,
参考termCrit);
stringOutput[faceNo]=recognizer.Recognize(ExtFaces[faceNo]);
stringOutput.Add(“”);
faceNo++;
}
pbExtractedFaces.Image=ExtFaces[0].ToBitmap();//绘制检测到的面
//在第0个(灰色)通道中使用蓝色
如果(stringOutput[0]==“”)
{
label1.Text=“未知”;
标签9.Text=“”;
}
//为检测到和识别的每个人脸绘制标签
其他的
{
//string[]label=stringOutput[faceNo].Split(',');
label1.Text=“已知”;
//对于(int i=0;i<2;i++)
//{
label9.Text=stringOutput[0];
//label7.Text=标签[1];
//}
}
}
如果(faceNo==0)
{
MessageBox.Show(“未检测到人脸”);
}
其他的
{
btnNextRec.Enabled=true;
btnprevousrec.Enabled=true;
}
}
使用检测到的人脸对训练集进行训练,如下所示
public FaceRecognizer()
{
InitializeComponent();
//Load faces from the dataset
try
{
ContTrain = ContTrain + 1;
//Load previous trained and labels for each image from the database Here
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;
// Converting the master image to a bitmap
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)
{
//Returns the following message if nothing saved in the training set
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);
}
}
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);
//Write the number of trained faces in a file text for further load
File.WriteAllText(Application.StartupPath + "/TrainedFaces
/TrainedNameLables.txt", trainingImages.ToArray().Length + "%");
File.WriteAllText(Application.StartupPath + "/TrainedFaces
/TrainedIDLables.txt", trainingImages.ToArray().Length + "%");
//Write the labels of trained faces in a file text for further load
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);
}
private void saveFaceToDB_单击(甚至是对象发送方)