C# - Emgu CV - 人脸识别代码在 EigenObjectRecognizer 处停止执行并无错误退出
Posted
技术标签:
【中文标题】C# - Emgu CV - 人脸识别代码在 EigenObjectRecognizer 处停止执行并无错误退出【英文标题】:C# - Emgu CV - Face Recognition code stops execution at EigenObjectRecognizer and exit without error 【发布时间】:2013-05-06 03:59:46 【问题描述】:我正在处理人脸识别,当我运行代码时,它会在 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("face0.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 被训练,然后它停止工作(退出运行)并且程序完全停止运行。
期待您的回复,谢谢 西赛
【问题讨论】:
贴出代码,有必要。当您说“退出”时,究竟是什么意思。你怎么知道它到达那个部分然后退出,你在调试吗? FaceRecognizer() 是从保存训练集的文件夹加载训练集的位置。RecognizeFaces() 方法调用的事件来自我在主窗体上设置的按钮是“识别”按钮。我的系统是 64 位的,其他带有 Emgu CV 的代码和示例工作正常(即配置正常)。我已附加并加载了 VS2010 项目,您也可以在 mediafire 上获取它(单击此处)。毕竟它应该可以工作,但不是。 mediafire.com/view/?bfysqsze6n2zs9y 是我在 EigenObjectInitializer 分配 try catch 后得到的错误消息 【参考方案1】:在市中心呆了 5 小时后,我第一次遇到异常,使用 try-catch 块获取调用堆栈,我意识到保存到训练集的图像和捕获的图像检测到可以识别没有相同的大小。这就是为什么我的程序停止并退出而没有任何错误通知的原因。http://www.mediafire.com/view/?bfysqsze6n2zs9y 是在 eigenObjectRecognizer 处阻止我的错误消息,我通过调整输入训练集的图像大小来解决它具有相同的大小检测到要识别的图像。
【讨论】:
以上是关于C# - Emgu CV - 人脸识别代码在 EigenObjectRecognizer 处停止执行并无错误退出的主要内容,如果未能解决你的问题,请参考以下文章
C# - Emgu Cv - 人脸识别 - 将保存到 Access 数据库的人脸训练集作为二进制文件加载到 EigenObjectRecognizer 中以进行人脸识别