From 143e5764bb3f5517aced96b2b6a48e2c2a95072e Mon Sep 17 00:00:00 2001 From: RavSinghChandan Date: Thu, 29 Oct 2026 19:05:54 +0530 Subject: [PATCH 1/2] [utils] Return the corners of bbox_to_polygon in order around the box --- doctr/utils/geometry.py | 4 ++-- tests/common/test_utils_geometry.py | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/doctr/utils/geometry.py b/doctr/utils/geometry.py index a49cd0d406..40427b42ee 100644 --- a/doctr/utils/geometry.py +++ b/doctr/utils/geometry.py @@ -37,9 +37,9 @@ def bbox_to_polygon(bbox: BoundingBox) -> Polygon4P: bbox: a bounding box Returns: - a polygon + a polygon with the top left, top right, bottom right and bottom left corners """ - return bbox[0], (bbox[1][0], bbox[0][1]), (bbox[0][0], bbox[1][1]), bbox[1] + return bbox[0], (bbox[1][0], bbox[0][1]), bbox[1], (bbox[0][0], bbox[1][1]) def polygon_to_bbox(polygon: Polygon4P) -> BoundingBox: diff --git a/tests/common/test_utils_geometry.py b/tests/common/test_utils_geometry.py index 844bbf6d03..7a63e63a03 100644 --- a/tests/common/test_utils_geometry.py +++ b/tests/common/test_utils_geometry.py @@ -1,6 +1,7 @@ from copy import deepcopy from math import hypot +import cv2 import numpy as np import pytest @@ -9,7 +10,9 @@ def test_bbox_to_polygon(): - assert geometry.bbox_to_polygon(((0, 0), (1, 1))) == ((0, 0), (1, 0), (0, 1), (1, 1)) + assert geometry.bbox_to_polygon(((0, 0), (1, 1))) == ((0, 0), (1, 0), (1, 1), (0, 1)) + # the corners go around the box, so the polygon is not self-intersecting + assert cv2.contourArea(np.array(geometry.bbox_to_polygon(((0, 0), (10, 10))), dtype=np.float32)) == 100 def test_polygon_to_bbox(): From 53d477217415f4d267cb0925c26f27026b196117 Mon Sep 17 00:00:00 2001 From: RavSinghChandan Date: Thu, 29 Oct 2026 19:56:26 +0530 Subject: [PATCH 2/2] Use the corner order around the box in the polygon_to_bbox test --- tests/common/test_utils_geometry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/common/test_utils_geometry.py b/tests/common/test_utils_geometry.py index 7a63e63a03..963f57f5e9 100644 --- a/tests/common/test_utils_geometry.py +++ b/tests/common/test_utils_geometry.py @@ -16,7 +16,7 @@ def test_bbox_to_polygon(): def test_polygon_to_bbox(): - assert geometry.polygon_to_bbox(((0, 0), (1, 0), (0, 1), (1, 1))) == ((0, 0), (1, 1)) + assert geometry.polygon_to_bbox(((0, 0), (1, 0), (1, 1), (0, 1))) == ((0, 0), (1, 1)) def test_order_points():