c# - Emgu CV -人脸识别代码在EigenObjectRecognizer处停止执行并无错误退出
本文关键字:执行 无错误 退出 EigenObjectRecognizer CV Emgu 人脸识别 代码 | 更新日期: 2023-09-27 18:13:12
我正在研究人脸识别,当我运行代码时,它在初始化EigenObjectRecognizer并退出程序的地方停止执行,没有任何错误。以前有人遇到过同样的问题吗?如果你需要额外的代码,我可以张贴更多。我看到我的代码一直在工作,直到识别器使用训练集
中的数据进行训练。 EigenObjectRecognizer recognizer = new EigenObjectRecognizer(
trainingImages.ToArray(),
NameLabless.ToArray(),
3000,
ref termCrit);
name = recognizer.Recognize(ExtFaces[faceNo]).ToString();
我用来从训练集加载的代码是
public FaceRecognizer()
{
InitializeComponent();
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)
{
//MessageBox.Show(e.ToString());
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 RecognizeFaces()
{
//detect faces from the gray-scale image and store into an array of type
// 'var',i.e 'MCvAvgComp[]'
Image<Gray, byte> grayframe = GetGrayframe();
//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
var faces = grayframe.DetectHaarCascade(haar, ScaleIncreaseRate, MinNeighbors,
HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
new Size(WindowsSize, WindowsSize))[0];
if (faces.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);
t = t + 1;
//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);
//= newExtractedImage;
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);
if (trainingImages.ToArray().Length != 0)
{
//Eigen face recognizer
EigenObjectRecognizer recognizer = new EigenObjectRecognizer(
trainingImages.ToArray(),
NameLabless.ToArray(),
3000,
ref termCrit);
name = recognizer.Recognize(ExtFaces[faceNo]).ToString();
stringOutput[faceNo] = name;
}
faceNo++;
}
pbExtractedFaces.Image = ExtFaces[0].ToBitmap(); //draw the face detected
in the 0th (gray) channel with blue
color
t = 0;
if (stringOutput[0] == null)
{
label1.Text = "Unknown";
label9.Text = "";
}
//Draw the label for each face detected and recognized
else
{
label1.Text = "Known";
label9.Text = stringOutput[0];
}
}
if (faceNo == 0)
{
MessageBox.Show("No face detected");
}
else
{
btnNextRec.Enabled = true;
btnPreviousRec.Enabled = true;
}
}
当这个人脸识别方法作为一个事件被调用时,它一直工作到EigenObjectRecognizer被训练的点,然后它停止工作(退出运行),程序完全停止运行。
期待您的回复,谢谢Sisay
在市中心呆了5个小时后,我做了第一次使用try-catch块获得调用堆栈的机会例外,我意识到保存到训练集的图像和检测到要识别的图像捕获的图像没有相同的大小。因此,这就是为什么我的程序停止并退出而没有任何错误通知的原因。http://www.mediafire.com/view/?bfysqsze6n2zs9y是错误消息,它阻止了我在eigenObjectRecognizer,我通过调整输入到训练集的图像的大小来解决它,使其与检测到的图像具有相同的大小被识别。