Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Fix python ground truth orientation issue on heif

laurenspriem025a6ea7a73e
diff --git a/infra/ml/test/ground_truth/_runtime.py b/infra/ml/test/ground_truth/_runtime.py
index d8c2176e05..6d4e984410 100644
--- a/infra/ml/test/ground_truth/_runtime.py
+++ b/infra/ml/test/ground_truth/_runtime.py
@@ -27,6 +27,16 @@ except Exception:
DEFAULT_MODEL_BASE_URL: Final[str] = "https://models.ente.io/"
DEFAULT_PADDING_RGB: Final[tuple[int, int, int]] = (114, 114, 114)
ALLOW_OPENCV_FALLBACK_ENV: Final[str] = "ML_PARITY_ALLOW_OPENCV_DECODE_FALLBACK"
+EXIF_ORIENTATION_TAG: Final[int] = 274
+ORIENTATION_TRANSPOSE_METHODS: Final[dict[int, Image.Transpose]] = {
+ 2: Image.Transpose.FLIP_LEFT_RIGHT,
+ 3: Image.Transpose.ROTATE_180,
+ 4: Image.Transpose.FLIP_TOP_BOTTOM,
+ 5: Image.Transpose.TRANSPOSE,
+ 6: Image.Transpose.ROTATE_270,
+ 7: Image.Transpose.TRANSVERSE,
+ 8: Image.Transpose.ROTATE_90,
+}
@dataclass(frozen=True)
@@ -114,12 +124,36 @@ def l2_normalize_rows(values: np.ndarray) -> np.ndarray:
return values / norms
+def _parse_orientation(value: object) -> int | None:
+ try:
+ orientation = int(value)
+ except (TypeError, ValueError):
+ return None
+ return orientation if 1 <= orientation <= 8 else None
+
+
+def _correct_image_orientation(image: Image.Image) -> Image.Image:
+ exif_orientation = _parse_orientation(image.getexif().get(EXIF_ORIENTATION_TAG, 1)) or 1
+ # pillow-heif can normalize EXIF orientation to 1 while preserving the original
+ # value in `image.info["original_orientation"]`.
+ original_orientation = _parse_orientation(image.info.get("original_orientation"))
+
+ corrected = ImageOps.exif_transpose(image)
+ if exif_orientation != 1:
+ return corrected
+
+ manual_method = ORIENTATION_TRANSPOSE_METHODS.get(original_orientation)
+ if manual_method is None:
+ return corrected
+ return corrected.transpose(manual_method)
+
+
def decode_image_rgb(source_path: Path) -> np.ndarray:
source_path = source_path.expanduser().resolve()
try:
with Image.open(source_path) as image:
- image = ImageOps.exif_transpose(image)
+ image = _correct_image_orientation(image)
image = image.convert("RGB")
pixels = np.array(image, dtype=np.uint8, copy=True)
return np.ascontiguousarray(pixels)
diff --git a/infra/ml/test/tests/test_runtime.py b/infra/ml/test/tests/test_runtime.py
new file mode 100644
index 0000000000..46af60a60a
--- /dev/null
+++ b/infra/ml/test/tests/test_runtime.py
@@ -0,0 +1,49 @@
+from __future__ import annotations
+
+import numpy as np
+from PIL import Image
+
+from ground_truth._runtime import _correct_image_orientation
+
+
+def _sample_image() -> Image.Image:
+ # Width != height so orientation swaps are easy to assert.
+ pixels = np.array(
+ [
+ [[255, 0, 0], [0, 255, 0], [0, 0, 255]],
+ [[10, 20, 30], [40, 50, 60], [70, 80, 90]],
+ ],
+ dtype=np.uint8,
+ )
+ return Image.fromarray(pixels, mode="RGB")
+
+
+def test_correct_image_orientation_applies_standard_exif_orientation() -> None:
+ image = _sample_image()
+ exif = image.getexif()
+ exif[274] = 6
+ image.info["exif"] = exif.tobytes()
+
+ corrected = _correct_image_orientation(image)
+
+ assert corrected.size == (2, 3)
+
+
+def test_correct_image_orientation_uses_original_orientation_when_exif_is_normalized() -> None:
+ image = _sample_image()
+ exif = image.getexif()
+ exif[274] = 1
+ image.info["exif"] = exif.tobytes()
+ image.info["original_orientation"] = 6
+
+ corrected = _correct_image_orientation(image)
+
+ assert corrected.size == (2, 3)
+
+
+def test_correct_image_orientation_leaves_image_unchanged_without_orientation_metadata() -> None:
+ image = _sample_image()
+
+ corrected = _correct_image_orientation(image)
+
+ assert corrected.size == image.size