/// this method is called when we close the window and it writes the CSV and copies the tagged images in their respective folder public void OnWindowClosed(object sender, CancelEventArgs e) { // cancel the automatic CSV writing in the other thread tokenSource.Cancel(); lock (lockObject) { // write to CSV and copy images using (var writer = new StreamWriter(Path.Combine(ImageLabeling.output_path, ImageLabeling.csv_name))) { //hacking this to be able to write the header even if no image is tagged string[] header = new string[ImageLabeling.classes.Length + 1]; header[0] = "Filepath"; for (int i = 0; i < ImageLabeling.classes.Length; i++) { header[i + 1] = ImageLabeling.classes[i]; } writer.WriteLine(string.Join(",", header)); // create row for each tagged image and write it to CSV and copy image to correct folder string[] row = new string[ImageLabeling.classes.Length + 1]; foreach (var image in TaggedImages.OrderBy(x => x.Filename)) { row[0] = image.Filepath; for (int i = 0; i < ImageLabeling.classes.Length; i++) { if (image.Tag == ImageLabeling.classes[i]) { row[i + 1] = "1"; File.Copy(image.Filepath, Path.Combine(ImageLabeling.output_path, ImageLabeling.classes[i], image.Filename)); } else { row[i + 1] = "0"; } } writer.WriteLine(string.Join(",", row)); } } } tokenSource.Dispose(); }
/// This method is used to periodically write the CSV in another thread private void WriteCSV() { while (true) { // sleep for 1 minute Thread.Sleep(new TimeSpan(0, 1, 0)); token.ThrowIfCancellationRequested(); lock (lockObject) { using (var writer = new StreamWriter(Path.Combine(ImageLabeling.output_path, ImageLabeling.csv_name))) { //hacking this to be able to write the header even if no image is tagged string[] header = new string[ImageLabeling.classes.Length + 1]; header[0] = "Filepath"; for (int i = 0; i < ImageLabeling.classes.Length; i++) { header[i + 1] = ImageLabeling.classes[i]; } writer.WriteLine(string.Join(",", header)); // create row for each tagged image and write it to CSV string[] row = new string[ImageLabeling.classes.Length + 1]; foreach (var image in TaggedImages.OrderBy(x => x.Filename)) { row[0] = image.Filepath; for (int i = 0; i < ImageLabeling.classes.Length; i++) { if (image.Tag == ImageLabeling.classes[i]) { row[i + 1] = "1"; } else { row[i + 1] = "0"; } } writer.WriteLine(string.Join(",", row)); } } } } }