public void TrainImages() { string path = Application.StartupPath + @"/../../Images/"; string[] files = Directory.GetFiles(path, "*.jpg", SearchOption.AllDirectories); // int[] labelsDb = _context.Labels.Select(_ => _.LabelNumber).ToArray(); List <int> labelsDb = new List <int>(); Mat[] matImages = new Mat[files.Length]; for (int i = 0; i < files.Length; i++) { matImages[i] = new Image <Gray, byte>(files[i]).Mat; string[] strings = files[i].Split('-'); string number = strings[strings.Length - 1].Split('.')[0]; labelsDb.Add(int.Parse(number)); } VectorOfMat images = new VectorOfMat(matImages); VectorOfInt labels = new VectorOfInt(labelsDb.ToArray()); faceRecognizer.Train(images, labels); faceRecognizer.Write(Application.StartupPath + @"/../../Images/eigenRecognizer.yml"); fisherRecognizer.Train(images, labels); fisherRecognizer.Write(Application.StartupPath + @"/../../Images/fisherRecognizer.yml"); LBPHFaceRecognizer.Train(images, labels); LBPHFaceRecognizer.Write(Application.StartupPath + @"/../../Images/lpbhRecognizer.yml"); isTrained = true; }
public async Task TrainAsync() { await Task.CompletedTask; var files = Directory.GetFiles(DataFolder, "*.image"); if (files?.Length > 0) { var count = files.Length; var counter = 0; var faceImages = new Mat[count]; var faceLabels = new int[count]; foreach (var file in files) { var faceImage = new Image <Gray, byte>(file); faceImage = ResizeImage(faceImage); faceImages[counter] = faceImage.Mat; faceLabels[counter] = int.Parse(Path.GetFileName(file).Split('.').First()); counter++; } //_fisherFaceRecognizer.Train(faceImages, faceLabels); //_fisherFaceRecognizer.Write(FisherTrainingDataPath); _lbphFaceRecognizer.Train(faceImages, faceLabels); _lbphFaceRecognizer.Write(LBPHTrainingDataPath); } }