From 9bc7a44da867ee1b8638220656957132ff6b10b4 Mon Sep 17 00:00:00 2001 From: The pix2struct Authors Date: Wed, 12 Apr 2023 08:56:21 -0700 Subject: [PATCH] small change PiperOrigin-RevId: 523713513 --- pix2struct/__init__.py | 2 +- pix2struct/demo.py | 2 +- pix2struct/demo_utils.py | 2 +- pix2struct/example_inference.py | 2 +- pix2struct/inference_utils.py | 2 +- pix2struct/metrics.py | 2 +- pix2struct/metrics_test.py | 2 +- pix2struct/models.py | 2 +- pix2struct/models_test.py | 2 +- pix2struct/postprocessors.py | 2 +- pix2struct/preprocessing/__init__.py | 2 +- pix2struct/preprocessing/convert_ai2d.py | 2 +- pix2struct/preprocessing/convert_chartqa.py | 2 +- pix2struct/preprocessing/convert_docvqa.py | 2 +- pix2struct/preprocessing/convert_ocrvqa.py | 2 +- pix2struct/preprocessing/convert_refexp.py | 2 +- .../preprocessing/convert_screen2words.py | 2 +- pix2struct/preprocessing/convert_textcaps.py | 2 +- .../convert_widget_captioning.py | 2 +- .../preprocessing/preprocessing_utils.py | 23 +++++++++++-------- pix2struct/preprocessors.py | 2 +- pix2struct/preprocessors_test.py | 2 +- pix2struct/tasks.py | 2 +- pix2struct/transfer_utils.py | 2 +- setup.py | 2 +- 25 files changed, 38 insertions(+), 33 deletions(-) diff --git a/pix2struct/__init__.py b/pix2struct/__init__.py index dbad525..e363d95 100644 --- a/pix2struct/__init__.py +++ b/pix2struct/__init__.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/pix2struct/demo.py b/pix2struct/demo.py index 8500a11..513c7e9 100644 --- a/pix2struct/demo.py +++ b/pix2struct/demo.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/pix2struct/demo_utils.py b/pix2struct/demo_utils.py index 56e86ed..2ea088e 100644 --- a/pix2struct/demo_utils.py +++ b/pix2struct/demo_utils.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/pix2struct/example_inference.py b/pix2struct/example_inference.py index 8e76d88..2648887 100644 --- a/pix2struct/example_inference.py +++ b/pix2struct/example_inference.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/pix2struct/inference_utils.py b/pix2struct/inference_utils.py index cce6f6a..22e24dd 100644 --- a/pix2struct/inference_utils.py +++ b/pix2struct/inference_utils.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/pix2struct/metrics.py b/pix2struct/metrics.py index 01c37b0..59e39be 100644 --- a/pix2struct/metrics.py +++ b/pix2struct/metrics.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/pix2struct/metrics_test.py b/pix2struct/metrics_test.py index 68c606f..81ff010 100644 --- a/pix2struct/metrics_test.py +++ b/pix2struct/metrics_test.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/pix2struct/models.py b/pix2struct/models.py index 099ff6f..4385349 100644 --- a/pix2struct/models.py +++ b/pix2struct/models.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/pix2struct/models_test.py b/pix2struct/models_test.py index 8a96431..62a676d 100644 --- a/pix2struct/models_test.py +++ b/pix2struct/models_test.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/pix2struct/postprocessors.py b/pix2struct/postprocessors.py index 31a9169..05ef7d7 100644 --- a/pix2struct/postprocessors.py +++ b/pix2struct/postprocessors.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/pix2struct/preprocessing/__init__.py b/pix2struct/preprocessing/__init__.py index fc41a17..21aab30 100644 --- a/pix2struct/preprocessing/__init__.py +++ b/pix2struct/preprocessing/__init__.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/pix2struct/preprocessing/convert_ai2d.py b/pix2struct/preprocessing/convert_ai2d.py index ddde451..3aaea72 100644 --- a/pix2struct/preprocessing/convert_ai2d.py +++ b/pix2struct/preprocessing/convert_ai2d.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/pix2struct/preprocessing/convert_chartqa.py b/pix2struct/preprocessing/convert_chartqa.py index b6d684e..02850f3 100644 --- a/pix2struct/preprocessing/convert_chartqa.py +++ b/pix2struct/preprocessing/convert_chartqa.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/pix2struct/preprocessing/convert_docvqa.py b/pix2struct/preprocessing/convert_docvqa.py index 31613f9..79b902a 100644 --- a/pix2struct/preprocessing/convert_docvqa.py +++ b/pix2struct/preprocessing/convert_docvqa.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/pix2struct/preprocessing/convert_ocrvqa.py b/pix2struct/preprocessing/convert_ocrvqa.py index cc51fdf..8e2ab68 100644 --- a/pix2struct/preprocessing/convert_ocrvqa.py +++ b/pix2struct/preprocessing/convert_ocrvqa.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/pix2struct/preprocessing/convert_refexp.py b/pix2struct/preprocessing/convert_refexp.py index 925f5be..d13bf9b 100644 --- a/pix2struct/preprocessing/convert_refexp.py +++ b/pix2struct/preprocessing/convert_refexp.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/pix2struct/preprocessing/convert_screen2words.py b/pix2struct/preprocessing/convert_screen2words.py index aa4b773..950195a 100644 --- a/pix2struct/preprocessing/convert_screen2words.py +++ b/pix2struct/preprocessing/convert_screen2words.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/pix2struct/preprocessing/convert_textcaps.py b/pix2struct/preprocessing/convert_textcaps.py index 08c6908..d9facdb 100644 --- a/pix2struct/preprocessing/convert_textcaps.py +++ b/pix2struct/preprocessing/convert_textcaps.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/pix2struct/preprocessing/convert_widget_captioning.py b/pix2struct/preprocessing/convert_widget_captioning.py index 051e7ee..42899d9 100644 --- a/pix2struct/preprocessing/convert_widget_captioning.py +++ b/pix2struct/preprocessing/convert_widget_captioning.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/pix2struct/preprocessing/preprocessing_utils.py b/pix2struct/preprocessing/preprocessing_utils.py index 22b0d3f..8126004 100644 --- a/pix2struct/preprocessing/preprocessing_utils.py +++ b/pix2struct/preprocessing/preprocessing_utils.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -28,6 +28,8 @@ DEFAULT_FONT_PATH = "arial.ttf" +IMAGE = Image.Image + def add_int_feature(example: tf.train.Example, key: str, @@ -78,13 +80,13 @@ def deterministic_sample(items: Iterable[Any], value_fn) -> Any: return max(items, key=lambda x: get_hash(value_fn(x))) -def image_to_bytes(image: Image.Image) -> bytes: +def image_to_bytes(image: IMAGE) -> bytes: img_byte_arr = io.BytesIO() image.save(img_byte_arr, format="PNG") return img_byte_arr.getvalue() -def render_header(image: Image.Image, header: str) -> Image.Image: +def render_header(image: IMAGE, header: str) -> IMAGE: """Renders a header on a PIL image and returns a new PIL image.""" header_image = render_text(header) new_width = max(header_image.width, image.width) @@ -111,13 +113,16 @@ def render_text(text: str, right_padding: int = 5, top_padding: int = 5, bottom_padding: int = 5, - font_bytes: Optional[bytes] = None) -> Image.Image: + font_bytes: Optional[bytes] = None, + use_wrapper: bool = True) -> IMAGE: """Render text.""" # Add new lines so that each line is no more than 80 characters. - wrapper = textwrap.TextWrapper(width=80) - lines = wrapper.wrap(text=text) - wrapped_text = "\n".join(lines) - + if use_wrapper: + wrapper = textwrap.TextWrapper(width=80) + lines = wrapper.wrap(text=text) + wrapped_text = "\n".join(lines) + else: + wrapped_text = text if font_bytes is not None: font_spec = io.BytesIO(font_bytes) else: @@ -145,7 +150,7 @@ def render_text(text: str, def render_text_on_bounding_box( text: str, bounding_box: Iterable[Iterable[int]], - image: Image.Image): + image: IMAGE): """Render text on top of a specific bounding box.""" draw = ImageDraw.Draw(image) (x0, y0), (x1, y1) = bounding_box diff --git a/pix2struct/preprocessors.py b/pix2struct/preprocessors.py index cf5bdac..da071ad 100644 --- a/pix2struct/preprocessors.py +++ b/pix2struct/preprocessors.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/pix2struct/preprocessors_test.py b/pix2struct/preprocessors_test.py index c0627b1..1178390 100644 --- a/pix2struct/preprocessors_test.py +++ b/pix2struct/preprocessors_test.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/pix2struct/tasks.py b/pix2struct/tasks.py index af959d9..591e6eb 100644 --- a/pix2struct/tasks.py +++ b/pix2struct/tasks.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/pix2struct/transfer_utils.py b/pix2struct/transfer_utils.py index d8db659..874367f 100644 --- a/pix2struct/transfer_utils.py +++ b/pix2struct/transfer_utils.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/setup.py b/setup.py index d232291..a617f75 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ -# Copyright 2022 The pix2struct Authors. +# Copyright 2023 The pix2struct Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License.