Skip to content
Snippets Groups Projects
Commit 33ddb4a3 authored by Yonatan's avatar Yonatan
Browse files

Vertical phrase ordering and initial Spark REST works

parent 9923fa40
Branches master
No related tags found
1 merge request!1Final merge
......@@ -23,6 +23,11 @@
</build>
<dependencies>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.9.4</version>
</dependency>
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
......@@ -69,6 +74,11 @@
<artifactId>deepl-java</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -4,7 +4,6 @@ import es.yonatan.tfg.grouper.SimpleUnitGrouper;
import es.yonatan.tfg.image.OpenCVPostProcessor;
import es.yonatan.tfg.model.UnitGroup;
import es.yonatan.tfg.recognizer.TesseractRecognizer;
import es.yonatan.tfg.translator.DeeplTranslator;
import net.sourceforge.tess4j.util.ImageHelper;
import javax.imageio.ImageIO;
......@@ -37,11 +36,11 @@ public class Main {
var merger = new SimpleUnitGrouper();
var groups = merger.group(words, image.getWidth(), image.getHeight(), 4, 4, 25, -5);
var groups = merger.group(words, image.getWidth(), image.getHeight(), 1, 1, 25, -5);
System.out.println(System.currentTimeMillis() - time);
var translated = new DeeplTranslator<UnitGroup>("f6542fe3-8e30-5cd2-064e-5eb18c7dbbf3:fx").translate(groups, null, "es");
//var translated = new DeeplTranslator<UnitGroup>("f6542fe3-8e30-5cd2-064e-5eb18c7dbbf3:fx").translate(groups, null, "es");
time = System.currentTimeMillis();
......@@ -58,6 +57,8 @@ public class Main {
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null);
groups.forEach(group -> System.out.println(group.getText()));
var imagePanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
......@@ -65,7 +66,7 @@ public class Main {
var time = System.currentTimeMillis();
g.drawImage(processedImage.getScaledInstance((int) (originalImage.getWidth() / scale), (int) (originalImage.getHeight() / scale), BufferedImage.SCALE_SMOOTH), 0, 0, null);
g.drawImage(originalImage.getScaledInstance((int) (originalImage.getWidth() / scale), (int) (originalImage.getHeight() / scale), BufferedImage.SCALE_SMOOTH), 0, 0, null);
var g2d = ((Graphics2D) g);
g2d.setStroke(new BasicStroke(1.5f));
......@@ -75,11 +76,11 @@ public class Main {
var boundingBox = group.bounds();
//g2d.setColor(Color.red);
//g2d.drawRect((int) (boundingBox.x() / scale), (int) (boundingBox.y() / scale), (int) (boundingBox.width() / scale), (int) (boundingBox.height() / scale));
g2d.setColor(Color.red);
g2d.drawRect((int) (boundingBox.x() / scale), (int) (boundingBox.y() / scale), (int) (boundingBox.width() / scale), (int) (boundingBox.height() / scale));
g2d.setColor(Color.black);
g2d.drawString(group.getTranslation(), (int) (boundingBox.x() / scale), (int) ((boundingBox.y() + boundingBox.height() * 0.75) / scale));
//g2d.drawString(group.getText(), (int) (boundingBox.x() / scale), (int) ((boundingBox.y() + boundingBox.height() * 0.75) / scale));
}
System.out.println("Draw time: " + (System.currentTimeMillis() - time));
......
......@@ -6,10 +6,8 @@ import es.yonatan.tfg.model.UnitGroup;
import es.yonatan.tfg.util.HashGrid2D;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
public class SimpleUnitGrouper implements IUnitGrouper<Unit, UnitGroup> {
......@@ -27,12 +25,24 @@ public class SimpleUnitGrouper implements IUnitGrouper<Unit, UnitGroup> {
// ensure no previously added items are still in the grid
var grid = new HashGrid2D<>(width, height, horizontalCellCount, verticalCellCount);
var checked = new HashSet<HasBoundingBox>();
units.forEach(grid::insert);
// for each unit, check if it collides with any other unit, if it does,
// create an UnitGroup of them or merge if it's already an UnitGroup
computeGroups(grid, units, horizontalTolerance, verticalTolerance);
var flatten = grid.flatten();
return flatten.stream()
.filter(item -> item instanceof UnitGroup)
.map(unit -> (UnitGroup) unit)
.sorted(Comparator.comparingInt(group -> group.bounds().y()))
.collect(Collectors.toList());
}
private void computeGroups(@NotNull HashGrid2D<HasBoundingBox> grid, @NotNull Collection<Unit> units, int horizontalTolerance, int verticalTolerance) {
var checked = new HashSet<HasBoundingBox>();
for (Unit unit : units) {
if (checked.contains(unit)) continue;
checked.add(unit);
......@@ -49,7 +59,7 @@ public class SimpleUnitGrouper implements IUnitGrouper<Unit, UnitGroup> {
grid.remove(collider);
if (collider instanceof UnitGroup)
group.merge((UnitGroup) collider);
group.merge(false, (UnitGroup) collider);
else
group.addUnit((Unit) collider, false);
});
......@@ -57,12 +67,5 @@ public class SimpleUnitGrouper implements IUnitGrouper<Unit, UnitGroup> {
group.recomputeOrder();
grid.insert(group);
}
var flatten = grid.flatten();
var list = new ArrayList<UnitGroup>();
flatten.stream().filter(item -> item instanceof UnitGroup).forEach(item -> list.add((UnitGroup) item));
return list;
}
}
......@@ -21,14 +21,15 @@ public class UnitGroup implements HasBoundingBox, ITranslatable {
units = new LinkedList<>();
}
public void merge(UnitGroup... unitGroups) {
merge(List.of(unitGroups));
public void merge(boolean recomputeOrder, UnitGroup... unitGroups) {
merge(List.of(unitGroups), recomputeOrder);
}
public void merge(Collection<UnitGroup> unitGroups) {
public void merge(Collection<UnitGroup> unitGroups, boolean recomputeOrder) {
for (UnitGroup unitGroup : unitGroups) {
addWords(unitGroup.units, false);
}
if (recomputeOrder)
recomputeOrder();
}
......@@ -57,8 +58,11 @@ public class UnitGroup implements HasBoundingBox, ITranslatable {
recomputeOrder();
}
// sort the units by their bounds' x coordinate
public void recomputeOrder() {
units.sort(Comparator.comparingInt(word -> word.bounds().x()));
units.sort(Comparator
.comparingInt((Unit a) -> a.bounds().x())
);
}
public @NotNull LinkedList<Unit> getUnits() {
......
package es.yonatan.tfg.rest;
import com.google.gson.Gson;
import spark.Spark;
public class RESTServer {
public static void main(String[] args) {
var server = new RESTServer();
server.start(8080);
}
// starts Spark REST server and sets up the routes
protected void start(int port) {
System.out.println("Starting server on port " + port);
Spark.port(port);
Spark.get("/hello", (req, res) -> new Gson().toJson("Hello World"));
System.out.println("Server started on port " + Spark.port());
System.out.println("Loaded routes:");
Spark.routes().forEach(route ->
System.out.println("\t- " + route.getHttpMethod() + " " + route.getMatchUri())
);
}
protected void stop() {
Spark.stop();
System.out.println("Server stopped");
}
}
......@@ -74,8 +74,8 @@ public class HashGrid2D<T extends HasBoundingBox> {
var collidingObjects = new ArrayList<T>();
for (int i = cellX - 1; i <= cellX + 1; i++)
for (int j = cellY - 1; j <= cellY + 1; j++) {
for (int i = Math.max(0, cellX - 2); i <= Math.min(horizontalCellCount, cellX + 2); i++)
for (int j = Math.max(0, cellY - 2); j <= Math.min(verticalCellCount, cellY + 2); j++) {
var hash = hashCoordinates(i, j);
var cell = grid.get(hash);
if (cell != null)
......
......@@ -6,4 +6,6 @@ module TFG {
requires lept4j;
requires deepl.java;
requires opencv;
requires spark.core;
requires com.google.gson;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment