Create a matplotlib rectangular patch for the element Args: geometry: bounding box of the element page_dimensions: dimensions of the Page in format (height, width) label: label to display when hovered color: color to draw box alpha: opacity parameter to f
(
geometry: BoundingBox,
page_dimensions: tuple[int, int],
label: str | None = None,
color: tuple[float, float, float] = (0, 0, 0),
alpha: float = 0.3,
linewidth: int = 2,
fill: bool = True,
preserve_aspect_ratio: bool = False,
)
| 18 | |
| 19 | |
| 20 | def rect_patch( |
| 21 | geometry: BoundingBox, |
| 22 | page_dimensions: tuple[int, int], |
| 23 | label: str | None = None, |
| 24 | color: tuple[float, float, float] = (0, 0, 0), |
| 25 | alpha: float = 0.3, |
| 26 | linewidth: int = 2, |
| 27 | fill: bool = True, |
| 28 | preserve_aspect_ratio: bool = False, |
| 29 | ) -> patches.Rectangle: |
| 30 | """Create a matplotlib rectangular patch for the element |
| 31 | |
| 32 | Args: |
| 33 | geometry: bounding box of the element |
| 34 | page_dimensions: dimensions of the Page in format (height, width) |
| 35 | label: label to display when hovered |
| 36 | color: color to draw box |
| 37 | alpha: opacity parameter to fill the boxes, 0 = transparent |
| 38 | linewidth: line width |
| 39 | fill: whether the patch should be filled |
| 40 | preserve_aspect_ratio: pass True if you passed True to the predictor |
| 41 | |
| 42 | Returns: |
| 43 | a rectangular Patch |
| 44 | """ |
| 45 | if len(geometry) != 2 or any(not isinstance(elt, tuple) or len(elt) != 2 for elt in geometry): |
| 46 | raise ValueError("invalid geometry format") |
| 47 | |
| 48 | # Unpack |
| 49 | height, width = page_dimensions |
| 50 | (xmin, ymin), (xmax, ymax) = geometry |
| 51 | # Switch to absolute coords |
| 52 | if preserve_aspect_ratio: |
| 53 | width = height = max(height, width) |
| 54 | xmin, w = xmin * width, (xmax - xmin) * width |
| 55 | ymin, h = ymin * height, (ymax - ymin) * height |
| 56 | |
| 57 | return patches.Rectangle( |
| 58 | (xmin, ymin), |
| 59 | w, |
| 60 | h, |
| 61 | fill=fill, |
| 62 | linewidth=linewidth, |
| 63 | edgecolor=(*color, alpha), |
| 64 | facecolor=(*color, alpha), |
| 65 | label=label, |
| 66 | ) |
| 67 | |
| 68 | |
| 69 | def polygon_patch( |