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 2 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
14 changes: 4 additions & 10 deletions src/nv_ingest/extraction_workflows/pdf/pdfium_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# SPDX-License-Identifier: Apache-2.0


import io
import logging
from math import ceil
from math import floor
Expand All @@ -14,15 +13,16 @@
import numpy as np
import pypdfium2 as libpdfium
import tritonclient.grpc as grpcclient
from PIL import Image

from nv_ingest.extraction_workflows.pdf import yolox_utils
from nv_ingest.schemas.metadata_schema import AccessLevelEnum
from nv_ingest.schemas.metadata_schema import TextTypeEnum
from nv_ingest.schemas.pdf_extractor_schema import PDFiumConfigSchema
from nv_ingest.util.converters import bytetools
from nv_ingest.util.image_processing.table_and_chart import join_cached_and_deplot_output
from nv_ingest.util.image_processing.transforms import numpy_to_base64
from nv_ingest.util.nim.helpers import call_image_inference_model
from nv_ingest.util.nim.helpers import create_inference_client
from nv_ingest.util.nim.helpers import perform_model_inference
from nv_ingest.util.pdf.metadata_aggregators import Base64Image
from nv_ingest.util.pdf.metadata_aggregators import ImageChart
from nv_ingest.util.pdf.metadata_aggregators import ImageTable
Expand All @@ -33,9 +33,6 @@
from nv_ingest.util.pdf.pdfium import PDFIUM_PAGEOBJ_MAPPING
from nv_ingest.util.pdf.pdfium import pdfium_pages_to_numpy
from nv_ingest.util.pdf.pdfium import pdfium_try_get_bitmap_as_numpy
from nv_ingest.util.nim.helpers import call_image_inference_model
from nv_ingest.util.nim.helpers import create_inference_client
from nv_ingest.util.nim.helpers import perform_model_inference

# Copyright (c) 2024, NVIDIA CORPORATION.
#
Expand Down Expand Up @@ -309,10 +306,7 @@ def handle_table_chart_extraction(
h1, w1, h2, w2 = bbox * np.array([height, width, height, width])
cropped = original_image[floor(w1) : ceil(w2), floor(h1) : ceil(h2)] # noqa: E203

img = Image.fromarray(cropped.astype(np.uint8))
with io.BytesIO() as buffer:
img.save(buffer, format="PNG")
base64_img = bytetools.base64frombytes(buffer.getvalue())
base64_img = numpy_to_base64(cropped)

if label == "table":
table_content = call_image_inference_model(paddle_client, "paddle", cropped)
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 @@ -138,6 +138,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