public static void createMoreJson(string sourceImagePath, string sourceLabelFileDirectory, string targetImageDirectory, string targetAnnotationDirectory, double maxRot = 360, int maxShiftHor = -1, int maxShiftVer = -1, int sampleCount = 30) { JSON_Reader labelFile = new JSON_Reader(sourceLabelFileDirectory); JSON_Reader createdFiles = new JSON_Reader(Path.Combine(targetAnnotationDirectory, "created.json")); string[] filePaths = Directory.GetFiles(sourceImagePath); int currentImageCount = 0; Image currentImage = Image.FromFile(filePaths[currentImageCount]); foreach (JsonFile file in labelFile.getFiles()) { Console.Write(file.getFilename() + ": "); Random rnd = new Random(); Bitmap currentBitmap = (Bitmap)Bitmap.FromFile(Path.Combine(sourceImagePath, file.getFilename())); createdFiles.addFile(file); currentBitmap.Save(Path.Combine(targetImageDirectory, file.getFilename()), ImageFormat.Jpeg); for (int i = 0; i < sampleCount; i++) { float angle = (float)(rnd.Next(-50, 50) * maxRot / 100.0); int shift_x = (int)(rnd.Next(-50, 50) * maxShiftVer / 100); int shift_y = (int)(rnd.Next(-50, 50) * maxShiftHor / 100); Bitmap changed = changeImage(currentBitmap, angle, shift_x, shift_y); string fileName = generateFileName(file.getFilename(), i); String newFilePath = Path.Combine(targetImageDirectory, fileName); changed.Save(newFilePath, ImageFormat.Jpeg); JsonFile changedFile = file.getChanged(newFilePath, angle, shift_x, shift_y); createdFiles.addFile(changedFile); Console.Write("#"); } createdFiles.writeToFile(); Console.WriteLine(); } }
public static void createBoxesFromJson(string sourceImagePath, string sourceLabelFileDirectory, string targetImageDirectory, string targetAnnotationDirectory, double maxRot = 360, int maxShiftHor = -1, int maxShiftVer = -1, int sampleCount = 30) { JSON_Reader labelFile = new JSON_Reader(Directory.GetFiles(sourceLabelFileDirectory)[0]); string[] filePaths = Directory.GetFiles(sourceImagePath); int currentImageCount = 0; Image currentImage = Image.FromFile(filePaths[currentImageCount]); String targetXMLDirectory = Path.Combine(targetAnnotationDirectory, "xmls"); List <String> labels = new List <string>(); StreamWriter trainvalFile = new StreamWriter(Path.Combine(targetAnnotationDirectory, "trainval.txt"), false); StreamWriter label_mapFile = new StreamWriter(Path.Combine(targetAnnotationDirectory, "label_map.pbtxt"), false); foreach (JsonFile file in labelFile.getFiles()) { foreach (Region r in file.getRegions()) { labels.Add(r.getKlasse()); } Console.Write(file.getFilename() + ": "); Random rnd = new Random(); Bitmap currentBitmap = (Bitmap)Bitmap.FromFile(Path.Combine(sourceImagePath, file.getFilename())); //currentBitmap.Save(Path.Combine(targetImageDirectory, file.getFilename()), ImageFormat.Jpeg); for (int i = 0; i < sampleCount; i++) { float angle = (float)(rnd.Next(-50, 50) * maxRot / 100.0); int shift_x = (int)(rnd.Next(-50, 50) * maxShiftVer / 100); int shift_y = (int)(rnd.Next(-50, 50) * maxShiftHor / 100); Bitmap changed = changeImage(currentBitmap, angle, shift_x, shift_y); string fileName = generateFileName(file.getFilename(), i); String newFilePath = Path.Combine(targetImageDirectory, fileName); changed.Save(newFilePath, ImageFormat.Jpeg); JsonFile changedFile = file.getChanged(newFilePath, angle, shift_x, shift_y); XMLAnnotation changedLabelFile = createBoxLabelFileFromJson(changedFile, changed); changedLabelFile.save(targetXMLDirectory); trainvalFile.WriteLine(Path.GetFileNameWithoutExtension(fileName)); changed.Dispose(); Console.Write("#"); } Console.WriteLine(); currentBitmap.Dispose(); } int id = 1; foreach (String label in labels.Distinct()) { label_mapFile.WriteLine("item {"); label_mapFile.WriteLine(" id: " + id); label_mapFile.WriteLine(" name: \'" + label + "\'"); label_mapFile.WriteLine("}"); label_mapFile.WriteLine(""); id++; } trainvalFile.Dispose(); label_mapFile.Dispose(); }