Skip to content
Snippets Groups Projects
Commit 1fbe776f authored by Yonatan's avatar Yonatan
Browse files

Refactor

parent ac9a86c3
Branches
No related tags found
1 merge request!1Final merge
......@@ -26,6 +26,7 @@ public class Main {
var tess = new TesseractRecognizer.Builder()
.setDataPath(new File("C:/Proyectos/TFG/src/main/resources/tessdata/"))
.setSupportedLanguages("eng", "spa")
.setLanguage("spa")
.setVariable("user_defined_dpi", "70") // TODO: 10/04/2023 300 dpi is too high, test lower dpi and see how it affects the results and completion time
.build();
......@@ -81,7 +82,6 @@ public class Main {
g2d.drawString(group.getTranslation(), (int) (boundingBox.x() / scale), (int) ((boundingBox.y() + boundingBox.height() * 0.75) / scale));
}
//colors.free();
System.out.println("Draw time: " + (System.currentTimeMillis() - time));
}
};
......
package es.yonatan.tfg.recognizer.color;
package es.yonatan.tfg.color;
import es.yonatan.tfg.model.ColorData;
import org.jetbrains.annotations.NotNull;
......
package es.yonatan.tfg.recognizer.color;
package es.yonatan.tfg.color;
import es.yonatan.tfg.model.ColorData;
import net.sourceforge.lept4j.Leptonica1;
......
......@@ -9,7 +9,6 @@ import java.util.List;
public interface IUnitGrouper<In extends Unit, Out extends UnitGroup> {
@NotNull List<Out> group(@NotNull Collection<In> units, int width, int height, int horizontalCellCount, int verticalCellCount);
@NotNull List<Out> group(@NotNull Collection<In> units, int width, int height, int horizontalCellCount, int verticalCellCount, int horizontalTolerance, int verticalTolerance);
......
......@@ -16,6 +16,9 @@ import java.util.concurrent.Executors;
*/
public interface IUnitRecognizer<T extends Unit> {
String DIGITS = "0123456789";
String NUMBERS = DIGITS + ".,";
Executor DEFAULT_EXECUTOR = Executors.newSingleThreadExecutor();
default @Nullable List<T> recognizeSync(@NotNull BufferedImage image, double minConfidence) {
......@@ -39,10 +42,4 @@ public interface IUnitRecognizer<T extends Unit> {
default @Nullable CompletableFuture<List<T>> recognize(@NotNull List<BufferedImage> images, double minConfidence, Executor threadExecutor) {
return CompletableFuture.supplyAsync(() -> recognizeSync(images, minConfidence), threadExecutor);
}
enum Accuracy {
LOW_FAST,
HIGH_SLOW
}
}
......@@ -10,17 +10,17 @@ import org.jetbrains.annotations.NotNull;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
public final class TesseractRecognizer implements IUnitRecognizer<Unit> {
private final Tesseract tess;
private TesseractRecognizer(Tesseract tess) {
private final Set<String> supportedLanguages; // TODO: 02/05/2023 allow tesseract to change the operating language
private TesseractRecognizer(Tesseract tess, Set<String> supportedLanguages) {
this.tess = tess;
this.supportedLanguages = supportedLanguages;
}
@Override
......@@ -52,8 +52,7 @@ public final class TesseractRecognizer implements IUnitRecognizer<Unit> {
public static final class Builder {
private File dataPath;
private Accuracy accuracy;
private List<String> supportedLanguages;
private Set<String> supportedLanguages;
private String language;
private String charWhitelist;
private String charBlacklist;
......@@ -65,57 +64,64 @@ public final class TesseractRecognizer implements IUnitRecognizer<Unit> {
public Builder withDefaults() {
this.dataPath = LoadLibs.extractTessResources("tessdata");
this.accuracy = Accuracy.LOW_FAST;
this.supportedLanguages = List.of("eng");
this.supportedLanguages = Set.of("eng");
this.language = "eng";
this.charWhitelist = null;
this.charBlacklist = null;
return this;
}
public Builder setVariable(String variable, String value) {
public Builder setVariable(@NotNull String variable, @NotNull String value) {
if (customVariables == null) customVariables = new HashMap<>();
this.customVariables.put(variable, value);
return this;
}
public Builder setDataPath(File dataPath) {
public Builder setDataPath(@NotNull File dataPath) {
this.dataPath = dataPath;
return this;
}
public Builder setAccuracy(Accuracy accuracy) {
this.accuracy = accuracy;
return this;
public Builder setSupportedLanguages(@NotNull String... supportedLanguages) {
return setSupportedLanguages(Set.of(supportedLanguages));
}
public Builder setSupportedLanguages(List<String> supportedLanguages) {
public Builder setSupportedLanguages(@NotNull Set<String> supportedLanguages) {
if (supportedLanguages.isEmpty())
throw new IllegalArgumentException("Please provide a list of supported languages");
this.supportedLanguages = supportedLanguages;
return this;
}
public Builder setLanguage(String language) {
public Builder setLanguage(@NotNull String language) {
if (supportedLanguages.isEmpty())
throw new IllegalStateException("Please provide a list of supported languages");
if (!supportedLanguages.contains(language))
throw new IllegalArgumentException("Language not supported");
this.language = language;
return this;
}
public Builder setCharBlacklist(String charBlacklist) {
public Builder setCharBlacklist(@NotNull String charBlacklist) {
this.charBlacklist = charBlacklist;
return this;
}
public Builder setCharWhitelist(String charWhitelist) {
public Builder setCharWhitelist(@NotNull String charWhitelist) {
this.charWhitelist = charWhitelist;
return this;
}
public Builder excludeNumbers() {
return setCharBlacklist("0123456789");
return setCharBlacklist(DIGITS);
}
public Builder numbersOnly() {
return setCharWhitelist("0123456789.,");
return setCharWhitelist(NUMBERS);
}
public TesseractRecognizer build() {
......@@ -138,7 +144,7 @@ public final class TesseractRecognizer implements IUnitRecognizer<Unit> {
if (customVariables != null)
customVariables.forEach(tess::setVariable);
return new TesseractRecognizer(tess);
return new TesseractRecognizer(tess, supportedLanguages);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment