Skip to content
Snippets Groups Projects
Commit 69223ae9 authored by Yonatan's avatar Yonatan
Browse files

Color identification starts

parent 9f8a8038
No related branches found
No related tags found
1 merge request!1Final merge
......@@ -36,12 +36,12 @@
<dependency>
<groupId>net.sourceforge.lept4j</groupId>
<artifactId>lept4j</artifactId>
<version>1.18.0</version>
<version>1.17.0</version>
</dependency>
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>5.6.0</version>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
......
package es.yonatan.tfg;
import es.yonatan.tfg.model.HasBoundingBox;
import es.yonatan.tfg.model.UnitGroup;
import es.yonatan.tfg.recognizer.color.ColorRecognizer;
import es.yonatan.tfg.recognizer.group.SimpleUnitGrouper;
import es.yonatan.tfg.recognizer.unit.TesseractRecognizer;
import net.sourceforge.tess4j.util.ImageHelper;
......@@ -16,6 +18,8 @@ public class Main {
public static void main(String[] args) throws IOException {
var file = new File("C:/Users/Yonatan/Desktop/04.jpg");
new ColorRecognizer().getColorAt(file, 0, 0, 100, 100);
var time = System.currentTimeMillis();
//TODO: 10/04/2023 transform originalImage for best Tesseract OCR results: grayscale, binarize, noise reduction, etc.
var originalImage = ImageIO.read(file);
......@@ -25,11 +29,10 @@ public class Main {
var tess = new TesseractRecognizer.Builder()
.setDataPath(new File("C:/Proyectos/TFG/src/main/resources/tessdata/"))
.setLanguage("spa")
//.excludeNumbers()
.setVariable("user_defined_dpi", "300") // TODO: 10/04/2023 300 dpi is too high, test lower dpi and see how it affects the results and completion time
.build();
var words = tess.recognizeSync(image, 75);
var words = tess.recognizeSync(image, 80);
if (words == null) return;
......@@ -39,6 +42,13 @@ public class Main {
System.out.println(System.currentTimeMillis() - time);
groups.sort(HasBoundingBox::compareTo);
var test = new StringBuilder();
groups.forEach(a -> test.append(a).append(';'));
System.out.println(test);
// basic GUI to render the bounding boxes resulting of the OCR recognition as well as testing the word merge algorithm
var scale = 1.5;
......@@ -59,7 +69,7 @@ public class Main {
g2d.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 15));
for (HasBoundingBox group : groups) {
for (UnitGroup group : groups) {
var boundingBox = group.bounds();
......@@ -67,7 +77,7 @@ public class Main {
g2d.drawRect((int) (boundingBox.x() / scale), (int) (boundingBox.y() / scale), (int) (boundingBox.width() / scale), (int) (boundingBox.height() / scale));
g2d.setColor(Color.BLACK);
//g2d.drawString(sentence.toString(), boundingBox.x(), boundingBox.y() + boundingBox.height() / 2);
//g2d.drawString(group.toString(), (int) (boundingBox.x() / scale), (int) ((boundingBox.y() + boundingBox.height()) / scale));
}
}
};
......
......@@ -3,7 +3,19 @@ package es.yonatan.tfg.model;
import es.yonatan.tfg.util.BoundingBox;
import org.jetbrains.annotations.NotNull;
public interface HasBoundingBox {
public interface HasBoundingBox extends Comparable<HasBoundingBox> {
@Override
default int compareTo(@NotNull HasBoundingBox test) {
var bounds = bounds();
var otherBounds = test.bounds();
if (bounds.x() == otherBounds.x()) {
return Integer.compare(bounds.y(), otherBounds.y());
} else {
return Integer.compare(bounds.x(), otherBounds.x());
}
}
@NotNull BoundingBox bounds();
}
package es.yonatan.tfg;
\ No newline at end of file
package es.yonatan.tfg.recognizer.color;
import es.yonatan.tfg.model.ColorData;
import net.sourceforge.lept4j.Box;
import net.sourceforge.lept4j.Leptonica1;
import java.awt.*;
import java.io.File;
import java.nio.IntBuffer;
public class ColorRecognizer implements IColorRecognizer {
@Override
public ColorData getColorAt(File image, int u, int v, int width, int height) {
// TODO: 4/04/2023 get primary color of image using leptonica library lept4j
var pix = Leptonica1.pixRead(image.getPath());
pix = Leptonica1.pixClipRectangle(pix, new Box(u, v, width, height, 0), null);
var pix32 = Leptonica1.pixConvertTo32(pix);
var bufferr = IntBuffer.allocate(8);
var bufferg = IntBuffer.allocate(8);
var bufferb = IntBuffer.allocate(8);
Leptonica1.pixGetRGBPixel(pix, 1, 1, bufferr, bufferg, bufferb);
System.out.println(bufferr.get() + " " + bufferg.get() + " " + bufferb.get());
return new ColorData(Color.BLACK, Color.WHITE);
}
}
package es.yonatan.tfg.recognizer.unit;
public enum Accuracy {
LOW_FAST,
HIGH_SLOW
}
......@@ -39,4 +39,10 @@ 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
}
}
module TFG {
requires org.jetbrains.annotations;
requires java.desktop;
requires tess4j;
requires lept4j;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment