Spaces:
Runtime error
Runtime error
File size: 13,277 Bytes
153628e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# Copyright (C) 2021-2024, Mindee.
# This program is licensed under the Apache License 2.0.
# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details.
import colorsys
from copy import deepcopy
from typing import Any, Dict, List, Optional, Tuple, Union
import cv2
import matplotlib.patches as patches
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.figure import Figure
from .common_types import BoundingBox, Polygon4P
__all__ = ["visualize_page", "visualize_kie_page", "draw_boxes"]
def rect_patch(
geometry: BoundingBox,
page_dimensions: Tuple[int, int],
label: Optional[str] = None,
color: Tuple[float, float, float] = (0, 0, 0),
alpha: float = 0.3,
linewidth: int = 2,
fill: bool = True,
preserve_aspect_ratio: bool = False,
) -> patches.Rectangle:
"""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 fill the boxes, 0 = transparent
linewidth: line width
fill: whether the patch should be filled
preserve_aspect_ratio: pass True if you passed True to the predictor
Returns:
-------
a rectangular Patch
"""
if len(geometry) != 2 or any(not isinstance(elt, tuple) or len(elt) != 2 for elt in geometry):
raise ValueError("invalid geometry format")
# Unpack
height, width = page_dimensions
(xmin, ymin), (xmax, ymax) = geometry
# Switch to absolute coords
if preserve_aspect_ratio:
width = height = max(height, width)
xmin, w = xmin * width, (xmax - xmin) * width
ymin, h = ymin * height, (ymax - ymin) * height
return patches.Rectangle(
(xmin, ymin),
w,
h,
fill=fill,
linewidth=linewidth,
edgecolor=(*color, alpha),
facecolor=(*color, alpha),
label=label,
)
def polygon_patch(
geometry: np.ndarray,
page_dimensions: Tuple[int, int],
label: Optional[str] = None,
color: Tuple[float, float, float] = (0, 0, 0),
alpha: float = 0.3,
linewidth: int = 2,
fill: bool = True,
preserve_aspect_ratio: bool = False,
) -> patches.Polygon:
"""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 the boxes, 0 = transparent
linewidth: line width
fill: whether the patch should be filled
preserve_aspect_ratio: pass True if you passed True to the predictor
Returns:
-------
a polygon Patch
"""
if not geometry.shape == (4, 2):
raise ValueError("invalid geometry format")
# Unpack
height, width = page_dimensions
geometry[:, 0] = geometry[:, 0] * (max(width, height) if preserve_aspect_ratio else width)
geometry[:, 1] = geometry[:, 1] * (max(width, height) if preserve_aspect_ratio else height)
return patches.Polygon(
geometry,
fill=fill,
linewidth=linewidth,
edgecolor=(*color, alpha),
facecolor=(*color, alpha),
label=label,
)
def create_obj_patch(
geometry: Union[BoundingBox, Polygon4P, np.ndarray],
page_dimensions: Tuple[int, int],
**kwargs: Any,
) -> patches.Patch:
"""Create a matplotlib patch for the element
Args:
----
geometry: bounding box (straight or rotated) of the element
page_dimensions: dimensions of the page in format (height, width)
**kwargs: keyword arguments for the patch
Returns:
-------
a matplotlib Patch
"""
if isinstance(geometry, tuple):
if len(geometry) == 2: # straight word BB (2 pts)
return rect_patch(geometry, page_dimensions, **kwargs)
elif len(geometry) == 4: # rotated word BB (4 pts)
return polygon_patch(np.asarray(geometry), page_dimensions, **kwargs)
elif isinstance(geometry, np.ndarray) and geometry.shape == (4, 2): # rotated line
return polygon_patch(geometry, page_dimensions, **kwargs)
raise ValueError("invalid geometry format")
def get_colors(num_colors: int) -> List[Tuple[float, float, float]]:
"""Generate num_colors color for matplotlib
Args:
----
num_colors: number of colors to generate
Returns:
-------
colors: list of generated colors
"""
colors = []
for i in np.arange(0.0, 360.0, 360.0 / num_colors):
hue = i / 360.0
lightness = (50 + np.random.rand() * 10) / 100.0
saturation = (90 + np.random.rand() * 10) / 100.0
colors.append(colorsys.hls_to_rgb(hue, lightness, saturation))
return colors
def visualize_page(
page: Dict[str, Any],
image: np.ndarray,
words_only: bool = True,
display_artefacts: bool = True,
scale: float = 10,
interactive: bool = True,
add_labels: bool = True,
**kwargs: Any,
) -> Figure:
"""Visualize a full page with predicted blocks, lines and words
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> from doctr.utils.visualization import visualize_page
>>> from doctr.models import ocr_db_crnn
>>> model = ocr_db_crnn(pretrained=True)
>>> input_page = (255 * np.random.rand(600, 800, 3)).astype(np.uint8)
>>> out = model([[input_page]])
>>> visualize_page(out[0].pages[0].export(), input_page)
>>> plt.show()
Args:
----
page: the exported Page of a Document
image: np array of the page, needs to have the same shape than page['dimensions']
words_only: whether only words should be displayed
display_artefacts: whether artefacts should be displayed
scale: figsize of the largest windows side
interactive: whether the plot should be interactive
add_labels: for static plot, adds text labels on top of bounding box
**kwargs: keyword arguments for the polygon patch
Returns:
-------
the matplotlib figure
"""
# Get proper scale and aspect ratio
h, w = image.shape[:2]
size = (scale * w / h, scale) if h > w else (scale, h / w * scale)
fig, ax = plt.subplots(figsize=size)
# Display the image
ax.imshow(image)
# hide both axis
ax.axis("off")
if interactive:
artists: List[patches.Patch] = [] # instantiate an empty list of patches (to be drawn on the page)
for block in page["blocks"]:
if not words_only:
rect = create_obj_patch(
block["geometry"], page["dimensions"], label="block", color=(0, 1, 0), linewidth=1, **kwargs
)
# add patch on figure
ax.add_patch(rect)
if interactive:
# add patch to cursor's artists
artists.append(rect)
for line in block["lines"]:
if not words_only:
rect = create_obj_patch(
line["geometry"], page["dimensions"], label="line", color=(1, 0, 0), linewidth=1, **kwargs
)
ax.add_patch(rect)
if interactive:
artists.append(rect)
for word in line["words"]:
rect = create_obj_patch(
word["geometry"],
page["dimensions"],
label=f"{word['value']} (confidence: {word['confidence']:.2%})",
color=(0, 0, 1),
**kwargs,
)
ax.add_patch(rect)
if interactive:
artists.append(rect)
elif add_labels:
if len(word["geometry"]) == 5:
text_loc = (
int(page["dimensions"][1] * (word["geometry"][0] - word["geometry"][2] / 2)),
int(page["dimensions"][0] * (word["geometry"][1] - word["geometry"][3] / 2)),
)
else:
text_loc = (
int(page["dimensions"][1] * word["geometry"][0][0]),
int(page["dimensions"][0] * word["geometry"][0][1]),
)
if len(word["geometry"]) == 2:
# We draw only if boxes are in straight format
ax.text(
*text_loc,
word["value"],
size=10,
alpha=0.5,
color=(0, 0, 1),
)
if display_artefacts:
for artefact in block["artefacts"]:
rect = create_obj_patch(
artefact["geometry"],
page["dimensions"],
label="artefact",
color=(0.5, 0.5, 0.5),
linewidth=1,
**kwargs,
)
ax.add_patch(rect)
if interactive:
artists.append(rect)
if interactive:
import mplcursors
# Create mlp Cursor to hover patches in artists
mplcursors.Cursor(artists, hover=2).connect("add", lambda sel: sel.annotation.set_text(sel.artist.get_label()))
fig.tight_layout(pad=0.0)
return fig
def visualize_kie_page(
page: Dict[str, Any],
image: np.ndarray,
words_only: bool = False,
display_artefacts: bool = True,
scale: float = 10,
interactive: bool = True,
add_labels: bool = True,
**kwargs: Any,
) -> Figure:
"""Visualize a full page with predicted blocks, lines and words
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> from doctr.utils.visualization import visualize_page
>>> from doctr.models import ocr_db_crnn
>>> model = ocr_db_crnn(pretrained=True)
>>> input_page = (255 * np.random.rand(600, 800, 3)).astype(np.uint8)
>>> out = model([[input_page]])
>>> visualize_kie_page(out[0].pages[0].export(), input_page)
>>> plt.show()
Args:
----
page: the exported Page of a Document
image: np array of the page, needs to have the same shape than page['dimensions']
words_only: whether only words should be displayed
display_artefacts: whether artefacts should be displayed
scale: figsize of the largest windows side
interactive: whether the plot should be interactive
add_labels: for static plot, adds text labels on top of bounding box
**kwargs: keyword arguments for the polygon patch
Returns:
-------
the matplotlib figure
"""
# Get proper scale and aspect ratio
h, w = image.shape[:2]
size = (scale * w / h, scale) if h > w else (scale, h / w * scale)
fig, ax = plt.subplots(figsize=size)
# Display the image
ax.imshow(image)
# hide both axis
ax.axis("off")
if interactive:
artists: List[patches.Patch] = [] # instantiate an empty list of patches (to be drawn on the page)
colors = {k: color for color, k in zip(get_colors(len(page["predictions"])), page["predictions"])}
for key, value in page["predictions"].items():
for prediction in value:
if not words_only:
rect = create_obj_patch(
prediction["geometry"],
page["dimensions"],
label=f"{key} \n {prediction['value']} (confidence: {prediction['confidence']:.2%}",
color=colors[key],
linewidth=1,
**kwargs,
)
# add patch on figure
ax.add_patch(rect)
if interactive:
# add patch to cursor's artists
artists.append(rect)
if interactive:
import mplcursors
# Create mlp Cursor to hover patches in artists
mplcursors.Cursor(artists, hover=2).connect("add", lambda sel: sel.annotation.set_text(sel.artist.get_label()))
fig.tight_layout(pad=0.0)
return fig
def draw_boxes(boxes: np.ndarray, image: np.ndarray, color: Optional[Tuple[int, int, int]] = None, **kwargs) -> None:
"""Draw an array of relative straight boxes on an image
Args:
----
boxes: array of relative boxes, of shape (*, 4)
image: np array, float32 or uint8
color: color to use for bounding box edges
**kwargs: keyword arguments from `matplotlib.pyplot.plot`
"""
h, w = image.shape[:2]
# Convert boxes to absolute coords
_boxes = deepcopy(boxes)
_boxes[:, [0, 2]] *= w
_boxes[:, [1, 3]] *= h
_boxes = _boxes.astype(np.int32)
for box in _boxes.tolist():
xmin, ymin, xmax, ymax = box
image = cv2.rectangle(
image, (xmin, ymin), (xmax, ymax), color=color if isinstance(color, tuple) else (0, 0, 255), thickness=2
)
plt.imshow(image)
plt.plot(**kwargs)
|