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)));
}
Aggregations