Skip to content

Commit

Permalink
Fix test transposing image with EXIF Orientation tag (#30319)
Browse files Browse the repository at this point in the history
* Fix test with exif_transpose image

* Replace datasets with PIL to load image in tests
  • Loading branch information
albertvillanova authored and ydshieh committed Apr 23, 2024
1 parent 66ecc2b commit d232b8b
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions tests/utils/test_image_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
import os
import tempfile
import unittest
from io import BytesIO
from typing import Optional

import datasets
import numpy as np
import pytest
from huggingface_hub.file_download import http_get
import requests
from huggingface_hub.file_download import hf_hub_url, http_get
from requests import ConnectTimeout, ReadTimeout

from tests.pipelines.test_pipelines_document_question_answering import INVOICE_URL
Expand All @@ -39,6 +41,11 @@
from transformers.image_utils import get_image_size, infer_channel_dimension_format, load_image


def get_image_from_hub_dataset(dataset_id: str, filename: str, revision: Optional[str] = None) -> "PIL.Image.Image":
url = hf_hub_url(dataset_id, filename, repo_type="dataset", revision=revision)
return PIL.Image.open(BytesIO(requests.get(url).content))


def get_random_image(height, width):
random_array = np.random.randint(0, 256, (height, width, 3), dtype=np.uint8)
return PIL.Image.fromarray(random_array)
Expand Down Expand Up @@ -540,9 +547,11 @@ def test_load_img_base64(self):
def test_load_img_rgba(self):
# we use revision="refs/pr/1" until the PR is merged
# https://hf.co/datasets/hf-internal-testing/fixtures_image_utils/discussions/1
dataset = datasets.load_dataset("hf-internal-testing/fixtures_image_utils", split="test", revision="refs/pr/1")
img = get_image_from_hub_dataset(
"hf-internal-testing/fixtures_image_utils", "0-test-lena.png", revision="refs/pr/1"
)

img = load_image(dataset[0]["image"]) # img with mode RGBA
img = load_image(img) # img with mode RGBA
img_arr = np.array(img)

self.assertEqual(
Expand All @@ -553,9 +562,11 @@ def test_load_img_rgba(self):
def test_load_img_la(self):
# we use revision="refs/pr/1" until the PR is merged
# https://hf.co/datasets/hf-internal-testing/fixtures_image_utils/discussions/1
dataset = datasets.load_dataset("hf-internal-testing/fixtures_image_utils", split="test", revision="refs/pr/1")
img = get_image_from_hub_dataset(
"hf-internal-testing/fixtures_image_utils", "1-test-parrots.png", revision="refs/pr/1"
)

img = load_image(dataset[1]["image"]) # img with mode LA
img = load_image(img) # img with mode LA
img_arr = np.array(img)

self.assertEqual(
Expand All @@ -566,9 +577,11 @@ def test_load_img_la(self):
def test_load_img_l(self):
# we use revision="refs/pr/1" until the PR is merged
# https://hf.co/datasets/hf-internal-testing/fixtures_image_utils/discussions/1
dataset = datasets.load_dataset("hf-internal-testing/fixtures_image_utils", split="test", revision="refs/pr/1")
img = get_image_from_hub_dataset(
"hf-internal-testing/fixtures_image_utils", "2-test-tree.png", revision="refs/pr/1"
)

img = load_image(dataset[2]["image"]) # img with mode L
img = load_image(img) # img with mode L
img_arr = np.array(img)

self.assertEqual(
Expand All @@ -579,17 +592,18 @@ def test_load_img_l(self):
def test_load_img_exif_transpose(self):
# we use revision="refs/pr/1" until the PR is merged
# https://hf.co/datasets/hf-internal-testing/fixtures_image_utils/discussions/1
dataset = datasets.load_dataset("hf-internal-testing/fixtures_image_utils", split="test", revision="refs/pr/1")

img_without_exif_transpose = dataset[3]["image"]
img_without_exif_transpose = get_image_from_hub_dataset(
"hf-internal-testing/fixtures_image_utils", "3-test-cat-rotated.jpg", revision="refs/pr/1"
)
img_arr_without_exif_transpose = np.array(img_without_exif_transpose)

self.assertEqual(
img_arr_without_exif_transpose.shape,
(333, 500, 3),
)

img_with_exif_transpose = load_image(dataset[3]["image"])
img_with_exif_transpose = load_image(img_without_exif_transpose)
img_arr_with_exif_transpose = np.array(img_with_exif_transpose)

self.assertEqual(
Expand Down

0 comments on commit d232b8b

Please sign in to comment.