Light, fluffy, and always free - The AWS Local Emulator alternative
refactor(rekognition): dedupe Geometry builder with Textract (#3794)
Priamo Germosenb38a2c8c3b2a
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsGeometry.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsGeometry.javanew file mode 100644index 0000000000..207f9f4072--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsGeometry.java@@ -0,0 +1,41 @@+package io.github.hectorvent.floci.core.common;++import com.fasterxml.jackson.databind.node.ArrayNode;+import com.fasterxml.jackson.databind.node.JsonNodeFactory;+import com.fasterxml.jackson.databind.node.ObjectNode;++/**+ * Builds the AWS Geometry response shape, a BoundingBox plus the 4-point Polygon tracing its+ * corners, that Rekognition and Textract both return for detected text and labels. The two+ * services' Geometry shapes are structurally identical; RekognitionService and TextractService+ * each implemented this independently before this class existed.+ *+ * @see <a href="https://docs.aws.amazon.com/rekognition/latest/APIReference/API_Geometry.html">Rekognition Geometry</a>+ * @see <a href="https://docs.aws.amazon.com/textract/latest/dg/API_Geometry.html">Textract Geometry</a>+ */+public final class AwsGeometry {++ private AwsGeometry() {+ }++ public static ObjectNode buildGeometry(double left, double top, double width, double height) {+ ObjectNode geometry = JsonNodeFactory.instance.objectNode();+ ObjectNode bbox = geometry.putObject("BoundingBox");+ bbox.put("Width", width);+ bbox.put("Height", height);+ bbox.put("Left", left);+ bbox.put("Top", top);+ ArrayNode polygon = geometry.putArray("Polygon");+ addPoint(polygon, left, top);+ addPoint(polygon, left + width, top);+ addPoint(polygon, left + width, top + height);+ addPoint(polygon, left, top + height);+ return geometry;+ }++ private static void addPoint(ArrayNode polygon, double x, double y) {+ ObjectNode point = polygon.addObject();+ point.put("X", x);+ point.put("Y", y);+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/rekognition/RekognitionService.java b/src/main/java/io/github/hectorvent/floci/services/rekognition/RekognitionService.javaindex ae3e0e3169..2a91d14ff7 100644--- a/src/main/java/io/github/hectorvent/floci/services/rekognition/RekognitionService.java+++ b/src/main/java/io/github/hectorvent/floci/services/rekognition/RekognitionService.java@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.node.ArrayNode;import com.fasterxml.jackson.databind.node.ObjectNode;import io.github.hectorvent.floci.core.common.AiMockConfigLoader;+import io.github.hectorvent.floci.core.common.AwsGeometry;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;import jakarta.ws.rs.core.Response;@@ -86,14 +87,14 @@ public class RekognitionService {line.put("Type", "LINE");line.put("Id", 0);line.put("Confidence", 99.9);- line.set("Geometry", buildGeometry(0.1, 0.1, 0.15, 0.05));+ line.set("Geometry", AwsGeometry.buildGeometry(0.1, 0.1, 0.15, 0.05));ObjectNode word = detections.addObject();word.put("DetectedText", "Floci");word.put("Type", "WORD");word.put("Id", 1);word.put("ParentId", 0);word.put("Confidence", 99.9);- word.set("Geometry", buildGeometry(0.1, 0.1, 0.15, 0.05));+ word.set("Geometry", AwsGeometry.buildGeometry(0.1, 0.1, 0.15, 0.05));root.put("TextModelVersion", MODEL_VERSION);return Response.ok(root).build();}@@ -126,29 +127,4 @@ public class RekognitionService {root.put("ModerationModelVersion", MODEL_VERSION);return Response.ok(root).build();}- // Private helpers- /**- * Builds a Geometry object with BoundingBox and a 4-point Polygon — the same- * shape Textract uses (Rekognition's Geometry shape is structurally identical).- * @see <a href="https://docs.aws.amazon.com/rekognition/latest/APIReference/API_Geometry.html">Geometry</a>- */- private ObjectNode buildGeometry(double left, double top, double width, double height) {- ObjectNode geometry = objectMapper.createObjectNode();- ObjectNode bbox = geometry.putObject("BoundingBox");- bbox.put("Width", width);- bbox.put("Height", height);- bbox.put("Left", left);- bbox.put("Top", top);- ArrayNode polygon = geometry.putArray("Polygon");- addPoint(polygon, left, top);- addPoint(polygon, left + width, top);- addPoint(polygon, left + width, top + height);- addPoint(polygon, left, top + height);- return geometry;- }- private void addPoint(ArrayNode polygon, double x, double y) {- ObjectNode point = polygon.addObject();- point.put("X", x);- point.put("Y", y);- }}diff --git a/src/main/java/io/github/hectorvent/floci/services/textract/TextractService.java b/src/main/java/io/github/hectorvent/floci/services/textract/TextractService.javaindex 44013d4f0c..6df006bb05 100644--- a/src/main/java/io/github/hectorvent/floci/services/textract/TextractService.java+++ b/src/main/java/io/github/hectorvent/floci/services/textract/TextractService.java@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode;import com.fasterxml.jackson.databind.node.ObjectNode;import io.github.hectorvent.floci.core.common.AiMockConfigLoader;import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.core.common.AwsGeometry;import io.github.hectorvent.floci.core.common.Resettable;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;@@ -162,7 +163,7 @@ public class TextractService implements Resettable {word.put("Id", wordId);word.put("Confidence", 99.9);word.put("Text", "Floci");- word.set("Geometry", buildGeometry(0.1, 0.1, 0.15, 0.05));+ word.set("Geometry", AwsGeometry.buildGeometry(0.1, 0.1, 0.15, 0.05));word.put("Page", 1);blocks.add(word);// LINE block (child: WORD)@@ -171,7 +172,7 @@ public class TextractService implements Resettable {line.put("Id", lineId);line.put("Confidence", 99.9);line.put("Text", "Floci");- line.set("Geometry", buildGeometry(0.1, 0.1, 0.15, 0.05));+ line.set("Geometry", AwsGeometry.buildGeometry(0.1, 0.1, 0.15, 0.05));line.set("Relationships", buildRelationships("CHILD", wordId));line.put("Page", 1);blocks.add(line);@@ -180,35 +181,12 @@ public class TextractService implements Resettable {page.put("BlockType", "PAGE");page.put("Id", pageId);page.put("Confidence", 99.9);- page.set("Geometry", buildGeometry(0.0, 0.0, 1.0, 1.0));+ page.set("Geometry", AwsGeometry.buildGeometry(0.0, 0.0, 1.0, 1.0));page.set("Relationships", buildRelationships("CHILD", lineId));page.put("Page", 1);blocks.add(page);return blocks;}- /**- * Builds a Geometry object with BoundingBox and a 4-point Polygon.- * @see <a href="https://docs.aws.amazon.com/textract/latest/dg/API_Geometry.html">Geometry</a>- */- private ObjectNode buildGeometry(double left, double top, double width, double height) {- ObjectNode geometry = objectMapper.createObjectNode();- ObjectNode bbox = geometry.putObject("BoundingBox");- bbox.put("Width", width);- bbox.put("Height", height);- bbox.put("Left", left);- bbox.put("Top", top);- ArrayNode polygon = geometry.putArray("Polygon");- addPoint(polygon, left, top);- addPoint(polygon, left + width, top);- addPoint(polygon, left + width, top + height);- addPoint(polygon, left, top + height);- return geometry;- }- private void addPoint(ArrayNode polygon, double x, double y) {- ObjectNode point = polygon.addObject();- point.put("X", x);- point.put("Y", y);- }/*** Builds a single Relationship entry.* @see <a href="https://docs.aws.amazon.com/textract/latest/dg/API_Relationship.html">Relationship</a>