Skip to content
Open
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
20 changes: 18 additions & 2 deletions unstructured/partition/pdf_image/analysis/bbox_visualisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import numpy as np
from matplotlib import colors, font_manager
from numba import njit
from PIL import Image, ImageDraw, ImageFont
from unstructured_inference.constants import ElementType

Expand Down Expand Up @@ -75,6 +76,7 @@ def get_rgb_color(color: str) -> tuple[int, int, int]:
return int(rgb_colors[0] * 255), int(rgb_colors[1] * 255), int(rgb_colors[2] * 255)


@njit(cache=True, fastmath=True)
def _get_bbox_to_page_ratio(bbox: tuple[int, int, int, int], page_size: tuple[int, int]) -> float:
"""Compute the ratio of the bounding box to the page size.

Expand Down Expand Up @@ -117,8 +119,10 @@ def _get_optimal_value_for_bbox(
The optimal value for the given bounding box and parameters given.
"""
bbox_to_page_ratio = _get_bbox_to_page_ratio(bbox, page_size)
coefficients = np.polyfit((ratio_for_min_value, ratio_for_max_value), (min_value, max_value), 1)
value = int(bbox_to_page_ratio * coefficients[0] + coefficients[1])
slope, intercept = _linear_polyfit_2point(
ratio_for_min_value, ratio_for_max_value, min_value, max_value
)
value = int(bbox_to_page_ratio * slope + intercept)
return max(min_value, min(max_value, value))


Expand Down Expand Up @@ -383,6 +387,18 @@ def draw_bbox_on_image(
)


@njit(cache=True, fastmath=True)
def _linear_polyfit_2point(x0: float, x1: float, y0: float, y1: float):
"""Compute slope and intercept for a line passing through (x0, y0), (x1, y1)."""
if x1 == x0:
slope = 0.0
intercept = (y0 + y1) / 2.0
else:
slope = (y1 - y0) / (x1 - x0)
intercept = y0 - slope * x0
return slope, intercept


class LayoutDrawer(ABC):
layout_source: str = "unknown"
laytout_dump: dict
Expand Down