private async void trainButton_Click(object sender, EventArgs e) { PersonGroup selectedPersonGroup = personGroups[personGroupSelector.SelectedIndex]; #if USE_OPENCV List <Mat> images = new List <Mat>(); List <int> labels = new List <int>(); using (StreamReader r = new StreamReader(selectedPersonGroup.PersonGroupId + "/TrainData.dat")) { while (!r.EndOfStream) { string line = r.ReadLine(); string[] parts = line.Split('|'); Mat image = new Mat(parts[0], ImreadModes.GrayScale); OpenCvSharp.Rect[] faces = DetectFacesInImage(image, HaarDetectionType.DoRoughSearch, new OpenCvSharp.Size(300, 300)); if (faces.Length == 1) { Mat crop = new Mat(image, faces[0]); Mat resizedImage = new Mat(); Cv2.Resize(crop, resizedImage, new OpenCvSharp.Size(400, 400)); images.Add(resizedImage); labels.Add(Convert.ToInt32(parts[1])); } } faceRecognizer = OpenCvSharp.Face.FisherFaceRecognizer.Create(); faceRecognizer.Train(images, labels); faceRecognizer.Write(selectedPersonGroup.PersonGroupId + "/Recognizer.dat"); } #else await faceServiceClient.TrainPersonGroupAsync(selectedPersonGroup.PersonGroupId); #endif }
private void identifyButton_Click(object sender, EventArgs e) { DialogResult result = openFileDialog.ShowDialog(); if (result == DialogResult.OK) { #if USE_OPENCV PersonGroup selectedPersonGroup = personGroups[personGroupSelector.SelectedIndex]; faceRecognizer = OpenCvSharp.Face.FisherFaceRecognizer.Create(); faceRecognizer.Read(selectedPersonGroup.PersonGroupId + "/Recognizer.dat"); #endif files = openFileDialog.FileNames; currFileIdx = 0; OpenAndIdentifyImage(files[currFileIdx]); EnableNextAndPrevButtons(); } }