예제 #1
0
        private static XMLAnnotation createBoxLabelFileFromJson(JsonFile jsonFile, Bitmap bitmap)
        {
            XMLAnnotation annot   = new XMLAnnotation(jsonFile.getFilename(), bitmap.Width, bitmap.Height, 1);
            List <Region> regions = jsonFile.getRegions();

            foreach (Region r in regions)
            {
                int xMin = bitmap.Width, yMin = bitmap.Height, xMax = 0, yMax = 0;

                foreach (Point p in r.getPoints())
                {
                    if (p.X < xMin)
                    {
                        xMin = p.X;
                    }

                    if (p.X > xMax)
                    {
                        xMax = p.X;
                    }

                    if (p.Y < yMin)
                    {
                        yMin = p.Y;
                    }

                    if (p.Y > yMax)
                    {
                        yMax = p.Y;
                    }
                }

                annot.addBox(r.getKlasse(), xMin, yMin, xMax, yMax);
            }


            return(annot);
        }
예제 #2
0
        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();
        }