Skip to content
Snippets Groups Projects
Commit 2d60a569 authored by Yoann Schneider's avatar Yoann Schneider :tennis: Committed by Erwan Rouchet
Browse files

Port rotation code from tesseract worker

parent a2ef1d92
No related branches found
No related tags found
1 merge request!160Port rotation code from tesseract worker
Pipeline #79142 passed
......@@ -8,4 +8,4 @@ line_length = 88
default_section=FIRSTPARTY
known_first_party = arkindex,arkindex_common
known_third_party =PIL,apistar,gitlab,gnupg,peewee,playhouse,pytest,recommonmark,requests,setuptools,sh,tenacity,yaml
known_third_party =PIL,apistar,gitlab,gnupg,peewee,playhouse,pytest,recommonmark,requests,setuptools,sh,shapely,tenacity,yaml
......@@ -6,6 +6,8 @@ from math import ceil
import requests
from PIL import Image
from shapely.affinity import rotate, scale, translate
from shapely.geometry import LinearRing
from tenacity import (
retry,
retry_if_exception_type,
......@@ -227,3 +229,63 @@ def trim_polygon(polygon, image_width: int, image_height: int):
updated_polygon.append(updated_polygon[0])
return updated_polygon
def revert_orientation(element, polygon):
"""Update the coordinates of the polygon of a child element based on the orientation of
its parent.
:param element: Parent element
:type element: Element|CachedElement
:param polygon: Polygon corresponding to the child element.
:type polygon: list
:return: A polygon with updated coordinates
:rtype: list
"""
from arkindex_worker.models import Element
from arkindex_worker.cache import CachedElement
assert element and isinstance(
element, (Element, CachedElement)
), "element shouldn't be null and should be an Element or CachedElement"
assert polygon and isinstance(
polygon, list
), "polygon shouldn't be null and should be a list"
# Rotating with Pillow can cause it to move the image around, as the image cannot have negative coordinates
# and must be a rectangle. This means the origin point of any coordinates from an image is invalid, and the
# center of the bounding box of the rotated image is different from the center of the element's bounding box.
# To properly undo the mirroring and rotation implicitly applied by open_image, we first need to find the center
# of the rotated bounding box.
if isinstance(element, Element):
assert (
element.zone and element.zone.polygon
), "element should have a zone and a polygon"
parent_ring = LinearRing(element.zone.polygon)
elif isinstance(element, CachedElement):
assert element.polygon, "cached element should have a polygon"
parent_ring = LinearRing(element.polygon)
rotated_ring = rotate(parent_ring, element.rotation_angle, origin="center")
# This rotated ring might have negative coordinates, so we get the vector that Pillow applies to offset the
# image to non-negative coordinates using the rotated bounding box.
offset_x, offset_y, _, _ = rotated_ring.bounds
# This uses the same calculation as what Shapely does for rotate/scale(origin='center').
# We will use this below to rotate around the center of the parent bounding box and not of each child polygon.
# https://github.com/Toblerity/Shapely/blob/462de3aa7a8bbd80408762a2d5aaf84b04476e4d/shapely/affinity.py#L98-L101
minx, miny, maxx, maxy = parent_ring.bounds
origin = ((maxx + minx) / 2.0, (maxy + miny) / 2.0)
ring = LinearRing(polygon)
# First undo the negative coordinates offset, since this is the last step of the original rotation
ring = translate(ring, xoff=offset_x, yoff=offset_y)
if element.rotation_angle:
ring = rotate(ring, -element.rotation_angle, origin=origin)
if element.mirrored:
ring = scale(ring, xfact=-1, origin=origin)
return [[int(x), int(y)] for x, y in ring.coords]
......@@ -4,4 +4,5 @@ Pillow==9.1.0
python-gitlab==2.7.1
python-gnupg==0.4.8
sh==1.14.2
shapely==1.8.1.post1
tenacity==8.0.1
# -*- coding: utf-8 -*-
import math
import unittest
import uuid
from io import BytesIO
from pathlib import Path
import pytest
from PIL import Image, ImageChops, ImageOps
from arkindex_worker.image import download_tiles, open_image, trim_polygon
from arkindex_worker.cache import CachedElement, create_tables, init_cache_db
from arkindex_worker.image import (
download_tiles,
open_image,
revert_orientation,
trim_polygon,
)
from arkindex_worker.models import Element
FIXTURES = Path(__file__).absolute().parent / "data"
TILE = FIXTURES / "test_image.jpg"
......@@ -356,3 +364,95 @@ class TestTrimPolygon(unittest.TestCase):
AssertionError, msg="Polygon points must be tuples or lists of 2 elements."
):
trim_polygon(bad_polygon, TEST_IMAGE["width"], TEST_IMAGE["height"])
@pytest.mark.parametrize(
"current_polygon,angle,mirrored,original_polygon",
(
(
[[73, 40], [1013, 40], [1013, 178], [73, 178]],
0,
False,
[[73, 40], [1013, 40], [1013, 178], [73, 178], [73, 40]],
),
(
[[502, 73], [588, 73], [588, 1013], [502, 1013]],
90,
False,
[[73, 126], [73, 40], [1013, 40], [1013, 126], [73, 126]],
),
(
[[254, 205], [540, 205], [540, 327], [254, 327]],
180,
False,
[[785, 423], [499, 423], [499, 301], [785, 301], [785, 423]],
),
(
[[301, 139], [423, 139], [423, 540], [301, 540]],
270,
False,
[[900, 301], [900, 423], [499, 423], [499, 301], [900, 301]],
),
(
[[26, 40], [966, 40], [966, 191], [26, 191]],
0,
True,
[[1013, 40], [73, 40], [73, 191], [1013, 191], [1013, 40]],
),
(
[[502, 26], [588, 26], [588, 966], [502, 966]],
90,
True,
[[1013, 126], [1013, 40], [73, 40], [73, 126], [1013, 126]],
),
(
[[486, 89], [900, 89], [900, 311], [486, 311]],
180,
True,
[[486, 539], [900, 539], [900, 317], [486, 317], [486, 539]],
),
(
[[317, 486], [539, 486], [539, 900], [317, 900]],
270,
True,
[[486, 317], [486, 539], [900, 539], [900, 317], [486, 317]],
),
),
)
def test_revert_orientation(
current_polygon, angle, mirrored, original_polygon, tmp_path
):
"""Test cases, for both Elements and CachedElements:
- no rotation or orientation
- rotation with 3 different angles (90, 180, 270)
- rotation + mirror with 4 angles (0, 90, 180, 270)
"""
# Setup cache db to test with CachedElements
db_path = f"{tmp_path}/db.sqlite"
init_cache_db(db_path)
create_tables()
image_polygon = [
[0, 0],
[0, 628],
[1039, 628],
[1039, 0],
[0, 0],
]
element = Element(
{
"mirrored": mirrored,
"rotation_angle": angle,
"zone": {"polygon": image_polygon},
}
)
cached_element = CachedElement.create(
id=uuid.uuid4(),
type="paragraph",
polygon=image_polygon,
mirrored=mirrored,
rotation_angle=angle,
)
assert revert_orientation(element, current_polygon) == original_polygon
assert revert_orientation(cached_element, current_polygon) == original_polygon
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment