-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from Samyssmile/feat/augmentation
Batch Processing and Random Augmentation for EDUX Image Augmentation
- Loading branch information
Showing
20 changed files
with
242 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
org.gradle.parallel=true | ||
org.gradle.caching=true | ||
org.gradle.jvmargs=-Xmx8g | ||
org.gradle.jvmargs=-Xmx32g |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
lib/src/main/java/de/edux/augmentation/core/AugmentationSequence.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
package de.edux.augmentation.core; | ||
|
||
import java.awt.image.BufferedImage; | ||
import java.io.IOException; | ||
|
||
public interface AugmentationSequence { | ||
|
||
BufferedImage applyTo(BufferedImage image); | ||
|
||
AugmentationSequence run(String directoryPath, int numberOfWorkers, String outputPath) | ||
throws IOException, InterruptedException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
lib/src/main/java/de/edux/augmentation/io/ImageConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package de.edux.augmentation.io; | ||
|
||
import de.edux.augmentation.core.AugmentationSequence; | ||
import java.awt.image.BufferedImage; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Paths; | ||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.TimeUnit; | ||
import javax.imageio.ImageIO; | ||
|
||
public class ImageConsumer implements Runnable { | ||
private final BlockingQueue<ImageWithName> queue; | ||
private final AugmentationSequence augmentationSequence; | ||
private final String outputDirectoryPath; | ||
|
||
public ImageConsumer( | ||
BlockingQueue<ImageWithName> queue, | ||
AugmentationSequence augmentationSequence, | ||
String outputDirectoryPath) { | ||
this.queue = queue; | ||
this.augmentationSequence = augmentationSequence; | ||
this.outputDirectoryPath = outputDirectoryPath; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
while (true) { | ||
ImageWithName image = queue.poll(2000, TimeUnit.MILLISECONDS); | ||
BufferedImage augmentedImage = augmentationSequence.applyTo(image.image()); | ||
processImage(augmentedImage, image.fileName()); | ||
} | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
System.out.println("Consumer wurde unterbrochen."); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private void processImage(BufferedImage image, String fileName) throws IOException { | ||
// Erstellen des Dateinamens mit Zeitstempel | ||
File outputFile = Paths.get(outputDirectoryPath, fileName).toFile(); | ||
|
||
// Stellen Sie sicher, dass das Ausgabeverzeichnis existiert | ||
File outputDir = outputFile.getParentFile(); | ||
if (!outputDir.exists()) { | ||
outputDir.mkdirs(); | ||
} | ||
|
||
// Speichern des Bildes | ||
ImageIO.write(image, "png", outputFile); | ||
System.out.println("Bild gespeichert: " + outputFile.getAbsolutePath()); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
lib/src/main/java/de/edux/augmentation/io/ImageProcessingManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package de.edux.augmentation.io; | ||
|
||
import de.edux.augmentation.core.AugmentationSequence; | ||
import java.io.IOException; | ||
import java.util.concurrent.ArrayBlockingQueue; | ||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class ImageProcessingManager { | ||
private final int numberOfConsumers; | ||
private final String inputDirectory; | ||
private final AugmentationSequence augmentationSequence; | ||
private final String outputDirectory; | ||
|
||
public ImageProcessingManager( | ||
String inputDirectoryPath, | ||
int numberOfConsumers, | ||
AugmentationSequence augmentationSequence, | ||
String outputDirectory) { | ||
this.inputDirectory = inputDirectoryPath; | ||
this.numberOfConsumers = numberOfConsumers; | ||
this.augmentationSequence = augmentationSequence; | ||
this.outputDirectory = outputDirectory; | ||
} | ||
|
||
public void processImages() throws InterruptedException, IOException { | ||
BlockingQueue<ImageWithName> queue = new ArrayBlockingQueue<>(1); | ||
|
||
Thread producerThread = new Thread(new ImageProducer(queue, inputDirectory, numberOfConsumers)); | ||
producerThread.start(); | ||
|
||
ExecutorService executorService = Executors.newFixedThreadPool(numberOfConsumers); | ||
for (int i = 0; i < numberOfConsumers; i++) { | ||
executorService.submit(new ImageConsumer(queue, augmentationSequence, outputDirectory)); | ||
} | ||
|
||
producerThread.join(); | ||
executorService.shutdown(); | ||
executorService.awaitTermination(2000, TimeUnit.MILLISECONDS); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
lib/src/main/java/de/edux/augmentation/io/ImageProducer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package de.edux.augmentation.io; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.AccessDeniedException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.concurrent.BlockingQueue; | ||
import javax.imageio.ImageIO; | ||
|
||
public class ImageProducer implements Runnable { | ||
|
||
private final BlockingQueue<ImageWithName> queue; | ||
private final String directoryPathString; | ||
private final int numberOfConsumers; | ||
private AugmentationImageReader augmentationImageReader = new AugmentationImageReader(); | ||
|
||
public ImageProducer( | ||
BlockingQueue<ImageWithName> queue, String directoryPathString, int numberOfConsumers) | ||
throws IOException { | ||
this.queue = queue; | ||
this.directoryPathString = directoryPathString; | ||
this.numberOfConsumers = numberOfConsumers; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
this.augmentationImageReader | ||
.readImagePathsAsStream(directoryPathString) | ||
.forEach( | ||
path -> { | ||
try (InputStream is = Files.newInputStream(Paths.get(path.toString()))) { | ||
queue.put(new ImageWithName(ImageIO.read(is), path.getFileName().toString())); | ||
} catch (AccessDeniedException ex) { | ||
System.out.println("Access denied: " + path.toString()); | ||
} catch (IOException | InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
}); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package de.edux.augmentation.io; | ||
|
||
import java.awt.image.BufferedImage; | ||
|
||
public record ImageWithName(BufferedImage image, String fileName) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.