Search in sources :

Example 1 with DefaultImageDecoder

use of org.vitrivr.cineast.core.extraction.decode.image.DefaultImageDecoder in project cineast by vitrivr.

the class ImageCodebookGenerator method generate.

@Override
public void generate(Path source, Path destination, int words) throws IOException {
    long start = System.currentTimeMillis();
    final Decoder<BufferedImage> decoder = new DefaultImageDecoder();
    final MimetypesFileTypeMap filetypemap = new MimetypesFileTypeMap("mime.types");
    /* Filter the list of files and aggregate it. */
    /* Prepare array dequeue. */
    ArrayDeque<Path> files = Files.walk(source).filter(path -> {
        if (decoder.supportedFiles() != null) {
            String type = filetypemap.getContentType(path.toString());
            return decoder.supportedFiles().contains(type);
        } else {
            return true;
        }
    }).collect(Collectors.toCollection(ArrayDeque::new));
    /* Prepare data-structures to track progress. */
    int max = files.size();
    int counter = 0;
    int skipped = 0;
    char[] progressBar = new char[15];
    int update = max / progressBar.length;
    /* */
    System.out.println(String.format("Creating codebook of %d words from %d files.", words, files.size()));
    /*
     * Iterates over the files Dequeue. Every element that has been processed in removed from that Dequeue.
     */
    Path path = null;
    while ((path = files.poll()) != null) {
        if (decoder.init(path, null, null)) {
            BufferedImage image = decoder.getNext();
            if (image != null) {
                this.process(image);
            } else {
                skipped++;
            }
        } else {
            skipped++;
        }
        if (counter % update == 0) {
            this.updateProgressBar(progressBar, max, counter);
        }
        System.out.print(String.format("\rAdding vectors to codebook: %d/%d files processed (%d skipped) |%s| (Memory left: %.2f/%.2f GB)", counter, max, skipped, String.valueOf(progressBar), Runtime.getRuntime().freeMemory() / 1000000.0f, Runtime.getRuntime().totalMemory() / 1000000.0f));
        counter++;
    }
    /* Dispose of unnecessary elements. */
    files = null;
    progressBar = null;
    /* Start clustering.*/
    System.out.println(String.format("\nClustering... this could take a while."));
    this.cluster.process(words);
    /* Save file...*/
    System.out.println(String.format("Saving vocabulary with %d entries.", words));
    UtilIO.save(this.cluster.getAssignment(), destination.toString());
    long duration = System.currentTimeMillis() - start;
    System.out.println(String.format("Done! Took me %dhours %dmin %dsec", TimeUnit.MILLISECONDS.toHours(duration), TimeUnit.MILLISECONDS.toMinutes(duration), TimeUnit.MILLISECONDS.toSeconds(duration)));
}
Also used : Path(java.nio.file.Path) UtilIO(boofcv.io.UtilIO) ComputeClusters(org.ddogleg.clustering.ComputeClusters) BufferedImage(java.awt.image.BufferedImage) Files(java.nio.file.Files) IOException(java.io.IOException) Decoder(org.vitrivr.cineast.core.extraction.decode.general.Decoder) Collectors(java.util.stream.Collectors) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) ClusterVisualWords(boofcv.alg.bow.ClusterVisualWords) TimeUnit(java.util.concurrent.TimeUnit) ArrayDeque(java.util.ArrayDeque) DefaultImageDecoder(org.vitrivr.cineast.core.extraction.decode.image.DefaultImageDecoder) Path(java.nio.file.Path) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) DefaultImageDecoder(org.vitrivr.cineast.core.extraction.decode.image.DefaultImageDecoder) BufferedImage(java.awt.image.BufferedImage)

Aggregations

ClusterVisualWords (boofcv.alg.bow.ClusterVisualWords)1 UtilIO (boofcv.io.UtilIO)1 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 ArrayDeque (java.util.ArrayDeque)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 MimetypesFileTypeMap (javax.activation.MimetypesFileTypeMap)1 ComputeClusters (org.ddogleg.clustering.ComputeClusters)1 Decoder (org.vitrivr.cineast.core.extraction.decode.general.Decoder)1 DefaultImageDecoder (org.vitrivr.cineast.core.extraction.decode.image.DefaultImageDecoder)1