/// <summary> /// 人脸识别 /// </summary> /// <param name="emguImage"></param> /// <returns></returns> public faceDetectedObj faceRecognize(Mat emguImage) { faceDetectedObj fdo = GetFaceRectangle(emguImage); Image <Gray, byte> tempImg = fdo.originalImg.ToImage <Gray, byte>(); #region 给识别出的所有人脸画矩形框 using (Graphics g = Graphics.FromImage(fdo.originalImg.Bitmap)) { foreach (Rectangle face in fdo.facesRectangle) { g.DrawRectangle(new Pen(Color.Red, 2), face); //给识别出的人脸画矩形框 Image <Gray, byte> GrayFace = tempImg.Copy(face).Resize(100, 100, Emgu.CV.CvEnum.Inter.Cubic); GrayFace._EqualizeHist(); //得到均衡化人脸的灰度图像 #region 得到匹配姓名,并画出 Emgu.CV.Face.FaceRecognizer.PredictionResult pr = tfr.faceRecognizer.Predict(GrayFace); string recogniseName = tfr.trainedFileList.trainedFileName[pr.Label].ToString(); name = tfr.trainedFileList.trainedFileName[pr.Label].ToString(); Font font = new Font("宋体", 16, GraphicsUnit.Pixel); SolidBrush fontLine = new SolidBrush(Color.Yellow); float xPos = face.X + (face.Width / 2 - (name.Length * 14) / 2); float yPos = face.Y - 21; g.DrawString(name, font, fontLine, xPos, yPos); #endregion fdo.names.Add(name); } } #endregion return(fdo); }
static void RunRecognizer() { RandomizeSets(); //re - randomize the set when training again //train the set Console.WriteLine("Training set"); GlobalVars.fishFace.Train(GlobalVars.fishFaceImages_training, GlobalVars.emotionForClassifier_training); int count = 0; int amountCorrect = 0; foreach (Image <Gray, Byte> img in GlobalVars.fishFaceImages_prediction) //predict all images in the prediction set { Emgu.CV.Face.FaceRecognizer.PredictionResult prediction = GlobalVars.fishFace.Predict(img); // Console.WriteLine("Predicted emotion is: " + (Emotions)prediction.Label + " With a " + prediction.Distance + " Distance. Actual emotion is " + (Emotions)GlobalVars.emotionForClassifier_prediction[count]); if (GlobalVars.emotionForClassifier_prediction[count] == prediction.Label) //check to see if the predicted emotion is correct { amountCorrect++; } else //write the wrong image to disk for inspection, can comment out if this feature is not required { CvInvoke.Imwrite(GlobalVars.path + @"\Cohn-Kanade Images\IncorrectImages\" + (Emotions)prediction.Label + (Emotions)GlobalVars.emotionForClassifier_prediction[count] + count + ".png", img); } count++; } Console.WriteLine("Got " + amountCorrect + " out of " + count + " images"); //inform user the amount that was correctly predicted }
/// <summary> /// 人脸识别 /// </summary> /// <param name="emguImage"></param> /// <returns></returns> public faceDetectedObj faceRecognize(Mat emguImage) { faceDetectedObj fdo = GetFaceRectangle(emguImage); Image <Gray, byte> tempImg = fdo.originalImg.ToImage <Gray, byte>(); #region 给识别出的所有人脸画矩形框 using (Graphics g = Graphics.FromImage(fdo.originalImg.Bitmap)) { foreach (Rectangle face in fdo.facesRectangle) { Image <Gray, byte> GrayFace = tempImg.Copy(face).Resize(100, 100, Emgu.CV.CvEnum.Inter.Cubic); GrayFace._EqualizeHist();//得到均衡化人脸的灰度图像 #region 得到匹配姓名 Emgu.CV.Face.FaceRecognizer.PredictionResult pr = tfr.faceRecognizer.Predict(GrayFace); string name = ""; //Distance越小表示 越可能是同一个人 if (pr.Distance < 4000) { name = tfr.trainedFileList.trainedFileName[pr.Label].ToString(); } #endregion fdo.names.Add(name); } } #endregion return(fdo); }
/// <summary> /// 人脸识别 /// </summary> /// <param name="emguImage"></param> /// <returns></returns> public FaceDetectedObj faceRecognize(Mat emguImage) { try { FaceDetectedObj fdo = GetFaceRectangle(emguImage); Image <Gray, byte> tempImg = fdo.originalImg.ToImage <Gray, byte>(); #region 给识别出的所有人脸画矩形框 using (Graphics g = Graphics.FromImage(fdo.originalImg.Bitmap)) { foreach (Rectangle face in fdo.facesRectangle) { g.DrawRectangle(new Pen(Color.Red, 2), face);//给识别出的人脸画矩形框 Image <Gray, byte> grayFace = tempImg.Copy(face).Resize(100, 100, Emgu.CV.CvEnum.Inter.Cubic); grayFace._EqualizeHist(); //得到均衡化人脸的灰度图像 #region 得到匹配姓名,并画出 Emgu.CV.Face.FaceRecognizer.PredictionResult pr = _trainedFaceRecognizer.faceRecognizer.Predict(grayFace); int label = pr.Label; if (label < 0) { continue; } string recogniseName = _trainedFaceRecognizer.trainedFileList.trainedFileName[pr.Label].ToString(); string name = _trainedFaceRecognizer.trainedFileList.trainedFileName[pr.Label].ToString(); Font font = new Font("宋体", 16, GraphicsUnit.Pixel); SolidBrush fontLine = new SolidBrush(Color.Yellow); float xPos = face.X + (face.Width / 2 - (name.Length * 14) / 2); float yPos = face.Y - 21; g.DrawString(name, font, fontLine, xPos, yPos); if (_nameCahce != name) { _nameCahce = name; SpeechSynthesizer synth = new SpeechSynthesizer(); synth.SetOutputToDefaultAudioDevice(); synth.SpeakAsync("欢迎您" + name); } #endregion fdo.names.Add(name); } } #endregion return(fdo); } catch (Exception ex) { throw ex; } }
/// <summary> /// 人脸识别 /// </summary> /// <param name="emguImage"></param> /// <returns></returns> public faceDetectedObj faceRecognize(Mat emguImage) { faceDetectedObj fdo = GetFaceRectangle(emguImage); Image <Gray, byte> tempImg = fdo.originalImg.ToImage <Gray, byte>(); #region 给识别出的所有人脸画矩形框 using (Graphics g = Graphics.FromImage(fdo.originalImg.Bitmap)) { foreach (Rectangle face in fdo.facesRectangle) { g.DrawRectangle(new Pen(Color.Red, 2), face);//给识别出的人脸画矩形框 Image <Gray, byte> GrayFace = tempImg.Copy(face).Resize(100, 100, Emgu.CV.CvEnum.Inter.Cubic); GrayFace._EqualizeHist();//得到均衡化人脸的灰度图像 //Mat pMat = new Mat(); //float[] ranges = new float[] { 0.0f, 256.0f }; //using (Emgu.CV.Util.VectorOfMat vm = new Emgu.CV.Util.VectorOfMat()) //{ // vm.Push(GrayFace); // CvInvoke.CalcHist(vm, new int[] { 0 }, null, pMat, new int[] { 1 }, ranges, false); //} #region 得到匹配姓名,并画出 Emgu.CV.Face.FaceRecognizer.PredictionResult pr = tfr.faceRecognizer.Predict(GrayFace); System.Diagnostics.Debug.WriteLine(pr.Distance + " " + pr.Label); string recogniseName = tfr.trainedFileList.trainedFileName[pr.Label].ToString(); string name = tfr.trainedFileList.trainedFileName[pr.Label].ToString(); Font font = new Font("宋体", 16, GraphicsUnit.Pixel); SolidBrush fontLine = new SolidBrush(Color.Yellow); float xPos = face.X + (face.Width / 2 - (name.Length * 14) / 2); float yPos = face.Y - 21; g.DrawString(name, font, fontLine, xPos, yPos); #endregion fdo.names.Add(name); } } GC.Collect(); #endregion return(fdo); }
/// <summary> /// 人脸识别 /// </summary> /// <param name="emguImage"></param> /// <returns></returns> public faceDetectedObj faceRecognize(Mat emguImage) { //tfr = new TrainedFaceRecognizer(); //tfr.trainedFileList = SetSampleFacesList(); //tfr.faceRecognizer = new Emgu.CV.Face.LBPHFaceRecognizer(1, 8, 8, 8, 100); faceDetectedObj fdo = GetFaceRectangle(emguImage); Image <Gray, byte> tempImg = fdo.originalImg.ToImage <Gray, byte>(); #region 给识别出的所有人脸画矩形框 using (Graphics g = Graphics.FromImage(fdo.originalImg.Bitmap)) { foreach (Rectangle face in fdo.facesRectangle) { g.DrawRectangle(new Pen(Color.Red, 2), face); Image <Gray, byte> GrayFace = tempImg.Copy(face).Resize(100, 100, Emgu.CV.CvEnum.Inter.Cubic); GrayFace._EqualizeHist();//得到均衡化人脸的灰度图像 #region 得到匹配姓名 //tfr.faceRecognizer.Load("test.xml"); Emgu.CV.Face.FaceRecognizer.PredictionResult pr = tfr.faceRecognizer.Predict(GrayFace); string name = ""; //Distance越小表示 越可能是同一个人 //if (pr.Distance < 4000) //{ name = tfr.trainedFileList.trainedFileName[pr.Label].ToString(); // } Font font = new Font("宋体", 16, GraphicsUnit.Pixel); SolidBrush fontLine = new SolidBrush(Color.Yellow); float xPos = face.X + (face.Width / 2 - (name.Length * 14) / 2); float yPos = face.Y - 21; g.DrawString(name, font, fontLine, xPos, yPos); #endregion fdo.names.Add(name); } } GC.Collect(); #endregion return(fdo); }