Skip to content

Commit 2ee38e6

Browse files
authored
fix: annotate image is fixed after refactor (#262)
Currently the code for annotate() don't add details when add_details=True, for example: ``` from unstructured_inference.inference.layout import DocumentLayout from unstructured_inference.models.base import get_model file = "example-docs/layout-parser-paper-fast.pdf" model = get_model("yolox_quantized") doc = DocumentLayout.from_file(file,model) for i,page in enumerate(doc.pages): page.annotate(add_details=True).save(f"{i}.png") ``` After this PR is merged, the resulting image should show origin for all elements using the same code.
1 parent 7b99828 commit 2ee38e6

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* fix: Reduce Chipper memory consumption on x86_64 cpus
44
* fix: Skips ordering elements coming from Chipper
5+
* fix: After refactoring to introduce Chipper, annotate() weren't able to show text with extra info from elements, this is fixed now.
56

67
## 0.7.9
78

test_unstructured_inference/test_visualization.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
import pytest
55
from PIL import Image
66

7-
from unstructured_inference.inference.elements import Rectangle
7+
from unstructured_inference.inference.elements import TextRegion
88
from unstructured_inference.visualize import draw_bbox, show_plot
99

1010

1111
def test_draw_bbox():
1212
test_image_arr = np.ones((100, 100, 3), dtype="uint8")
1313
image = Image.fromarray(test_image_arr)
1414
x1, y1, x2, y2 = (1, 10, 7, 11)
15-
rect = Rectangle(x1, y1, x2, y2)
16-
annotated_image = draw_bbox(image=image, rect=rect)
15+
rect = TextRegion.from_coords(x1, y1, x2, y2)
16+
annotated_image = draw_bbox(image=image, element=rect, details=False)
1717
annotated_array = np.array(annotated_image)
1818
# Make sure the pixels on the edge of the box are red
1919
for i, expected in zip(range(3), [255, 0, 0]):

unstructured_inference/inference/layout.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def annotate(
360360
if annotation_data is None:
361361
for el, color in zip(self.elements, colors):
362362
if sources is None or el.source in sources:
363-
img = draw_bbox(img, el.bbox, color=color, details=add_details)
363+
img = draw_bbox(img, el, color=color, details=add_details)
364364
else:
365365
for attribute, style in annotation_data.items():
366366
if hasattr(self, attribute) and getattr(self, attribute):

unstructured_inference/visualize.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
from PIL.Image import Image
1111
from PIL.ImageDraw import ImageDraw
1212

13-
from unstructured_inference.inference.elements import Rectangle
13+
from unstructured_inference.inference.elements import TextRegion
1414

1515

1616
@typing.no_type_check
1717
def draw_bbox(
1818
image: Image,
19-
rect: Rectangle,
19+
element: TextRegion,
2020
color: str = "red",
2121
width=1,
2222
details: bool = False,
@@ -25,17 +25,17 @@ def draw_bbox(
2525
try:
2626
img = image.copy()
2727
draw = ImageDraw(img)
28-
topleft, _, bottomright, _ = rect.coordinates
29-
c = getattr(rect, "color", color)
28+
topleft, _, bottomright, _ = element.bbox.coordinates
29+
c = getattr(element, "color", color)
3030
if details:
31-
source = getattr(rect, "source", "Unknown")
32-
type = getattr(rect, "type", "")
31+
source = getattr(element, "source", "Unknown")
32+
type = getattr(element, "type", "")
3333
kbd = ImageFont.truetype("Keyboard.ttf", 20)
3434
draw.text(topleft, text=f"{type} {source}", fill=c, font=kbd)
3535
draw.rectangle((topleft, bottomright), outline=c, width=width)
3636
except OSError:
3737
print("Failed to find font file. Skipping details.")
38-
img = draw_bbox(image, rect, color, width)
38+
img = draw_bbox(image, element, color, width)
3939
except Exception as e:
4040
print(f"Failed to draw bounding box: {e}")
4141
return img

0 commit comments

Comments
 (0)