// Load an array of training set items from the csv file public static List <TrainingItem> Load(string csvFile) { List <TrainingItem> trainingItems = new List <TrainingItem>(); string line; string[] items; // Read the file and display it line by line. System.IO.StreamReader file = new System.IO.StreamReader(csvFile); // Skip the first line since it is the header file.ReadLine(); while ((line = file.ReadLine()) != null) { TrainingItem trainingItem = new TrainingItem(); items = line.Split(','); trainingItem.FileName = items[0]; trainingItem.Label = items[1]; trainingItems.Add(trainingItem); } file.Close(); return(trainingItems); }
// Load an array of training set items from the csv file public static List<TrainingItem> Load(string csvFile) { List<TrainingItem> trainingItems = new List<TrainingItem>(); string line; string[] items; // Read the file and display it line by line. System.IO.StreamReader file = new System.IO.StreamReader(csvFile); // Skip the first line since it is the header file.ReadLine(); while ((line = file.ReadLine()) != null) { TrainingItem trainingItem = new TrainingItem(); items = line.Split(','); trainingItem.FileName = items[0]; trainingItem.Label = items[1]; trainingItems.Add(trainingItem); } file.Close(); return trainingItems; }
// Converts the images in a directory into the appropriate dimension // and saves them to the trianing set directory public static void Convert() { // Load the images in the directory with training set images DirectoryInfo di = new DirectoryInfo(@".\trainingset\tests"); FileInfo[] fi = di.GetFiles("*", SearchOption.TopDirectoryOnly); // Empty the data csv file System.IO.File.WriteAllText(trainingSetCSVFile, "FileName, Label\r\n"); foreach (FileInfo file in fi) { //// Convert the image into gray and the training width and height //Image<Gray, Byte> image = new Image<Gray, Byte>(file.FullName); //image = image.Resize(trainingImgWidth, trainingImgHeight, INTER.CV_INTER_CUBIC); //// Save file in bmp //string label = Path.GetFileNameWithoutExtension(file.FullName); //string fileName = label + imageExt; //image.Save(@".\trainingset\" + fileName); List<RecognizerResult> list = Detect(file.FullName); string fileName = Path.GetFileName(list[0].FileName); File.Move(list[0].FileName, trainingSetDir + @"/" + fileName); // Add item to CSV TrainingItem item = new TrainingItem(); item.FileName = fileName; item.Label = Path.GetFileNameWithoutExtension(list[0].FileName); item.AddToCSV(trainingSetCSVFile); } }
private void btnSave_Click(object sender, EventArgs e) { int selectedIndex = lvResults.SelectedIndices[0]; if (selectedIndex >= 0 && selectedIndex < rcgnResultList.Count) { string sourceFile = rcgnResultList[selectedIndex].FileName; string destFile = Program.trainingSetDir + @"\" + Path.GetFileName(sourceFile); // Check if this image is already part of the training set if (File.Exists(destFile)) { System.Windows.Forms.MessageBox.Show("This image already exist in the training set."); return; } // Copy the result image file to training set folder System.IO.File.Copy(sourceFile, destFile, true); // Add the item to the training set TrainingItem item = new TrainingItem(); item.FileName = Path.GetFileName(sourceFile); item.Label = txtLabel.Text; item.AddToCSV(Program.trainingSetCSVFile); // Process the update training set and update the display tabs //ilTrainingSet.Images.Clear(); //lvTrainingSet.Items.Clear(); //ilEigenFaces.Images.Clear(); //lvEigenFaces.Items.Clear(); //Program.Train(); //showTrainingSet(); //showEigenFaces(); } }
// Converts the images in a directory into the appropriate dimension // and saves them to the trianing set directory public static void Convert() { // Load the images in the directory with training set images DirectoryInfo di = new DirectoryInfo(@".\trainingset\tests"); FileInfo[] fi = di.GetFiles("*", SearchOption.TopDirectoryOnly); // Empty the data csv file System.IO.File.WriteAllText(trainingSetCSVFile, "FileName, Label\r\n"); foreach (FileInfo file in fi) { //// Convert the image into gray and the training width and height //Image<Gray, Byte> image = new Image<Gray, Byte>(file.FullName); //image = image.Resize(trainingImgWidth, trainingImgHeight, INTER.CV_INTER_CUBIC); //// Save file in bmp //string label = Path.GetFileNameWithoutExtension(file.FullName); //string fileName = label + imageExt; //image.Save(@".\trainingset\" + fileName); List <RecognizerResult> list = Detect(file.FullName); string fileName = Path.GetFileName(list[0].FileName); File.Move(list[0].FileName, trainingSetDir + @"/" + fileName); // Add item to CSV TrainingItem item = new TrainingItem(); item.FileName = fileName; item.Label = Path.GetFileNameWithoutExtension(list[0].FileName); item.AddToCSV(trainingSetCSVFile); } }
public static void Train() { List <TrainingItem> items = TrainingItem.Load(trainingSetCSVFile); Image <Gray, Byte>[] trainingImages = new Image <Gray, Byte> [items.Count]; String[] labels = new String[items.Count]; for (int index = 0; index < items.Count; index++) { trainingImages[index] = new Image <Gray, byte>(trainingSetDir + @"\" + items[index].FileName); labels[index] = Path.GetFileNameWithoutExtension(items[index].Label); } MCvTermCriteria termCrit = new MCvTermCriteria(maxEigenFaces, 0.001); // Initialize Eigen Recognizer Object recognizer = new EigenObjectRecognizer( trainingImages, labels, eigenDistanceThreshold, ref termCrit); // Remove all old eigen images DirectoryInfo eigenDir = new DirectoryInfo(eigenImageDir); FileInfo[] eigenFiles = eigenDir.GetFiles(); foreach (FileInfo file in eigenFiles) { file.Delete(); } // Save the average Image recognizer.AverageImage.Save(averageImageName); // Save all eigen images for (int index = 0; index < recognizer.EigenImages.Length; index++) { String fileName = eigenImageName + (index + 1) + imageExt; recognizer.EigenImages[index].ToBitmap().Save(fileName); } // Print eigen values Console.WriteLine(String.Format("Num eigen values: {0}", recognizer.EigenValues.Length)); Console.WriteLine(String.Format("Num rows in eigen values: {0}", recognizer.EigenValues[0].Rows)); Console.WriteLine(String.Format("Num cols in eigen values: {0}", recognizer.EigenValues[0].Cols)); }
private void btnSave_Click(object sender, EventArgs e) { int selectedIndex = lvResults.SelectedIndices[0]; if (selectedIndex >= 0 && selectedIndex < rcgnResultList.Count) { string sourceFile = rcgnResultList[selectedIndex].FileName; string destFile = Program.trainingSetDir + @"\" + Path.GetFileName(sourceFile); // Check if this image is already part of the training set if (File.Exists(destFile)) { System.Windows.Forms.MessageBox.Show("This image already exist in the training set."); return; } // Copy the result image file to training set folder System.IO.File.Copy(sourceFile, destFile, true); // Add the item to the training set TrainingItem item = new TrainingItem(); item.FileName = Path.GetFileName(sourceFile); item.Label = txtLabel.Text; item.AddToCSV(Program.trainingSetCSVFile); // Process the update training set and update the display tabs //ilTrainingSet.Images.Clear(); //lvTrainingSet.Items.Clear(); //ilEigenFaces.Images.Clear(); //lvEigenFaces.Items.Clear(); //Program.Train(); //showTrainingSet(); //showEigenFaces(); } }