Beispiel #1
0
  public static void main(String[] args) throws IOException {

    /* args[0] is the pixel to which the image will  be converted */
    String[] piexels = args[0].split("x");

    int scaledWidth = Integer.parseInt(piexels[0]);
    int scaledHeight = Integer.parseInt(piexels[1]);

    final ArrayList<String> malePaths = new ArrayList<String>();
    // final  ArrayList<String> femalePaths = new ArrayList<String>();

    /* Traverse All the files inside the Folder and sub folder. args[1] is the path of the folder having the images */
    Files.walkFileTree(
        Paths.get(args[1].toString()),
        new SimpleFileVisitor<Path>() {
          @Override
          public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
              throws IOException {
            // Files.delete(file);
            malePaths.add(file.toFile().getAbsolutePath());
            return FileVisitResult.CONTINUE;
          }

          @Override
          public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            // Files.delete(dir);
            return FileVisitResult.CONTINUE;
          }
        });

    Files.walkFileTree(
        Paths.get(args[2].toString()),
        new SimpleFileVisitor<Path>() {
          @Override
          public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
              throws IOException {
            // Files.delete(file);
            femalePaths.add(file.toFile().getAbsolutePath());
            return FileVisitResult.CONTINUE;
          }

          @Override
          public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            // Files.delete(dir);
            return FileVisitResult.CONTINUE;
          }
        });

    for (String file : malePaths) {
      String extension = "";

      int extIndex = file.indexOf('.');
      extension = file.substring(extIndex + 1);

      System.out.println(file + " extension " + extension);

      File input = new File(file.toString());
      BufferedImage image =
          createResizedCopy(ImageIO.read(input), scaledWidth, scaledHeight, Boolean.TRUE);
      toGray(image);
      File output = new File(file.toString());
      ImageIO.write(image, extension, output);
    }

    for (String file : femalePaths) {
      String extension = "";

      int extIndex = file.indexOf('.');
      extension = file.substring(extIndex + 1);

      System.out.println(file + " extension " + extension);

      File input = new File(file.toString());
      BufferedImage image =
          createResizedCopy(ImageIO.read(input), scaledWidth, scaledHeight, Boolean.TRUE);
      toGray(image);
      File output = new File(file.toString());
      ImageIO.write(image, extension, output);
    }

    try {
      /* args[2] is the Class of the image, Class = Male/Female. Vector will be written into a text file */
      try (PrintWriter out =
          new PrintWriter(new BufferedWriter(new FileWriter("test-data" + ".txt", true)))) {

        for (String file : malePaths) {
          File file1 = new File(file.toString());
          BufferedImage img = ImageIO.read(file1);
          if (img == null) continue;
          Raster raster = img.getData();
          int w = raster.getWidth(), h = raster.getHeight();
          // out.print(file1.getName());
          out.print("1" + ",");
          for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
              out.print(raster.getSample(x, y, 0) + " ");
            }
            out.print(" ");
          }
          out.println("");
        }
        for (String file : femalePaths) {
          File file1 = new File(file.toString());
          BufferedImage img = ImageIO.read(file1);
          if (img == null) continue;
          Raster raster = img.getData();
          int w = raster.getWidth(), h = raster.getHeight();
          // out.print(file1.getName());
          out.print("0" + ",");
          for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
              out.print(raster.getSample(x, y, 0) + " ");
            }
            out.print(" ");
          }
          out.println("");
        }
      } catch (IOException e) {
        // exception handling skipped for the reader
      }
    } catch (Exception e) {
      // exception handling skipped for the reader
    }
  }