Create a matplotlib polygon 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 fill
(
geometry: np.ndarray,
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,
)
| 67 | |
| 68 | |
| 69 | def polygon_patch( |
| 70 | geometry: np.ndarray, |
| 71 | page_dimensions: tuple[int, int], |
| 72 | label: str | None = None, |
| 73 | color: tuple[float, float, float] = (0, 0, 0), |
| 74 | alpha: float = 0.3, |
| 75 | linewidth: int = 2, |
| 76 | fill: bool = True, |
| 77 | preserve_aspect_ratio: bool = False, |
| 78 | ) -> patches.Polygon: |
| 79 | """Create a matplotlib polygon patch for the element |
| 80 | |
| 81 | Args: |
| 82 | geometry: bounding box of the element |
| 83 | page_dimensions: dimensions of the Page in format (height, width) |
| 84 | label: label to display when hovered |
| 85 | color: color to draw box |
| 86 | alpha: opacity parameter to fill the boxes, 0 = transparent |
| 87 | linewidth: line width |
| 88 | fill: whether the patch should be filled |
| 89 | preserve_aspect_ratio: pass True if you passed True to the predictor |
| 90 | |
| 91 | Returns: |
| 92 | a polygon Patch |
| 93 | """ |
| 94 | if not geometry.shape == (4, 2): |
| 95 | raise ValueError("invalid geometry format") |
| 96 | |
| 97 | # Unpack |
| 98 | height, width = page_dimensions |
| 99 | geometry[:, 0] = geometry[:, 0] * (max(width, height) if preserve_aspect_ratio else width) |
| 100 | geometry[:, 1] = geometry[:, 1] * (max(width, height) if preserve_aspect_ratio else height) |
| 101 | |
| 102 | return patches.Polygon( |
| 103 | geometry, |
| 104 | fill=fill, |
| 105 | linewidth=linewidth, |
| 106 | edgecolor=(*color, alpha), |
| 107 | facecolor=(*color, alpha), |
| 108 | label=label, |
| 109 | ) |
| 110 | |
| 111 | |
| 112 | def create_obj_patch( |