Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process grayscale images #71

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions src/nv_ingest/extraction_workflows/pdf/doughnut_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
# SPDX-License-Identifier: Apache-2.0

import re
from io import BytesIO
from math import ceil
from math import floor
from typing import List
from typing import Optional
from typing import Tuple

import numpy as np
from PIL import Image

from nv_ingest.util.converters import bytetools
from nv_ingest.util.pdf.metadata_aggregators import LatexTable
from nv_ingest.util.image_processing.transforms import numpy_to_base64

DEFAULT_DPI = 300
DEFAULT_MAX_WIDTH = 1024
Expand Down Expand Up @@ -112,10 +109,7 @@ def crop_image(array: np.array, bbox: Tuple[int, int, int, int], format="PNG") -
if (w2 - w1 <= 0) or (h2 - h1 <= 0):
return None
cropped = array[h1:h2, w1:w2]
pil_image = Image.fromarray(cropped.astype(np.uint8))
with BytesIO() as buffer:
pil_image.save(buffer, format="PNG")
base64_img = bytetools.base64frombytes(buffer.getvalue())
base64_img = numpy_to_base64(cropped)

return base64_img

Expand Down
6 changes: 6 additions & 0 deletions src/nv_ingest/util/image_processing/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ def numpy_to_base64(array: np.ndarray) -> str:
>>> isinstance(encoded_str, str)
True
"""
# If the array represents a grayscale image, drop the redundant axis in
# (h, w, 1). PIL.Image.fromarray() expects an array of form (h, w) if it's
# a grayscale image.
if array.ndim == 3 and array.shape[2] == 1:
array = np.squeeze(array, axis=2)

# Check if the array is valid and can be converted to an image
try:
# Convert the NumPy array to a PIL image
Expand Down
9 changes: 2 additions & 7 deletions src/nv_ingest/util/nim/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
# All rights reserved.
# SPDX-License-Identifier: Apache-2.0

import io
import logging
from typing import Optional
from typing import Tuple

import numpy as np
import requests
import tritonclient.grpc as grpcclient
from PIL import Image

from nv_ingest.util.converters import bytetools
from nv_ingest.util.image_processing.transforms import numpy_to_base64

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -90,10 +88,7 @@ def call_image_inference_model(client, model_name: str, image_data):
logger.error(err_msg)
raise RuntimeError(err_msg)
else:
image = Image.fromarray(image_data)
with io.BytesIO() as buffer:
image.save(buffer, format="PNG")
base64_img = bytetools.base64frombytes(buffer.getvalue())
base64_img = numpy_to_base64(image_data)

try:
url = client["endpoint_url"]
Expand Down
27 changes: 27 additions & 0 deletions tests/nv_ingest/util/image_processing/test_transforms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import numpy as np

from nv_ingest.util.image_processing.transforms import numpy_to_base64


def test_numpy_to_base64_valid_rgba_image():
array = np.random.randint(0, 255, (100, 100, 4), dtype=np.uint8)
result = numpy_to_base64(array)

assert isinstance(result, str)
assert len(result) > 0


def test_numpy_to_base64_valid_rgb_image():
array = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)
result = numpy_to_base64(array)

assert isinstance(result, str)
assert len(result) > 0


def test_numpy_to_base64_grayscale_redundant_axis():
array = np.random.randint(0, 255, (100, 100, 1), dtype=np.uint8)
result = numpy_to_base64(array)

assert isinstance(result, str)
assert len(result) > 0
Loading