public static List <RecognizerResult> Detect(string imageFile) { List <RecognizerResult> rcgnResultList = new List <RecognizerResult>(); Image <Bgr, Byte> image; try { image = new Image <Bgr, byte>(imageFile); //Read the files as an 8-bit Bgr image } catch (Exception e) { return(rcgnResultList); } // if image is too large, resize it if (image.Width > 480) { image = image.Resize(((double)480 / (double)image.Width), INTER.CV_INTER_CUBIC); } List <Rectangle> faces = new List <Rectangle>(); DetectFace.Detect(image, "haarcascade_frontalface_default.xml", faces); Image <Bgr, Byte> markedImage = image.Copy(); for (int index = 0; index < faces.Count; index++) { Rectangle face = faces[index]; // Save the detected faces to testing set Image <Gray, Byte> result = image.Copy(face).Convert <Gray, Byte>(); result = result.Resize(trainingImgWidth, trainingImgHeight, INTER.CV_INTER_CUBIC); string fileName = String.Format(@".\testingset\{0}.face{1}.bmp", Path.GetFileName(imageFile), index); result.Save(fileName); // Mark the detected face on the image markedImage.Draw(face, new Bgr(Color.Blue), 2); // Try to recognize the face RecognizerResult rcgnResult = Recognize(fileName); rcgnResultList.Add(rcgnResult); } // Save the marked image to testing set markedImage.Save(String.Format(@".\testingset\marked\{0}.bmp", Path.GetFileName(imageFile))); return(rcgnResultList); }
public static RecognizerResult Recognize(string unknownImg) { RecognizerResult rcgnResult = new RecognizerResult(); if (recognizer == null) return null; // Recognize the image Image<Gray, Byte> testImage = new Image<Gray, Byte>(unknownImg); float[] distances = recognizer.GetEigenDistances(testImage); string[] labels = recognizer.Labels; int num = labels.Count(); for (int i = 0; i < num; i++) { rcgnResult.Distances.Add(labels[i], distances[i]); } EigenObjectRecognizer.RecognitionResult result = recognizer.Recognize(testImage); if (result != null) { rcgnResult.Distance = result.Distance; rcgnResult.Label = result.Label; } rcgnResult.FileName = Path.GetFullPath(unknownImg); return rcgnResult; }