refactor(rekognition): dedupe Geometry builder with Textract (#3794)

RekognitionService and TextractService each carried a byte-for-byte
identical buildGeometry/addPoint pair (BoundingBox plus 4-point
Polygon construction), found via CPD as a 26-line block (#3792).
Rekognition's own Javadoc already documented this as known,
intentional duplication: "the same shape Textract uses (Rekognition's
Geometry shape is structurally identical)".

Extracted it into AwsGeometry.buildGeometry in core.common, and
pointed both services at it.

No behavior change: the response shape is identical to both copies
before this, only its location changed. Both RekognitionIntegrationTest
and TextractIntegrationTest already assert directly on the Geometry/
BoundingBox/Polygon shape, so that path is covered.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-17 00:57:15 -05:00
1 parent 8ef8f786b1
commit b38a2c8c3b
3 files changed
+48 -53

No files matched your search

@@ -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);
}
}
@@ -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);
}
}
@@ -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>