OCR Multi Output
OCR Output used to gather information about the output of any OCR model
OcrMultiOutput
Class for keeping track of multiple OCR extracted information from an image
The idea is to make the code agnostic of the OCR engine used. The OCR output can be the yur favorite OCR engine such as Tesseract, EasyOCR, DocTR, Azure Document Intelligence, AWS Textract
Already handles:
Source code in otary/vision/ocr/ocr_multi_output.py
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 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 | |
__init__(ocrsos)
Initialize an OcrMultiOutput object
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ocrsos
|
list[OcrSingleOutput]
|
list of OcrSingleOutput objects |
required |
__len__()
Number of elements in the ocrsos attribute
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
number of ocrsos elements |
closest_word(word, dist_thresh, _to='right', enforce_horizontal_alignment=True, alignment_angle_error=math.pi / 50)
Given a OcrSingleOutput object, get the closest word in the image to the right or to the left.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
word
|
Word
|
input OcrsingleOutput object |
required |
_to
|
str
|
whether right or left. Defaults to "right". |
'right'
|
enforce_horizontal_alignment
|
bool
|
Whether to only consider words that are horizontally aligned. This is particularly useful because in some case the closest word can be slightly shifted and therefore is not a humanly logically correlated word. Defaults to True. |
True
|
alignment_angle_error
|
float
|
the angle error margin to consider two close word as following one from each other. Default to pi / 50. |
pi / 50
|
dist_thresh
|
float
|
maximum distance value to accept a close word |
required |
Returns:
| Type | Description |
|---|---|
Optional[OcrSingleOutput]
|
Optional[Word]: None if no OcrSingleOutput can be found else a OcrSingleOutput. |
Source code in otary/vision/ocr/ocr_multi_output.py
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 | |
confidence_mean(count_none=True)
Compute the confidence mean of all the OCR single outputs that compose the OcrMultiOutput as the mean of all the confidence scores.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count_none
|
bool
|
whether to count the None confidence values or not. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
confidence mean total score |
Source code in otary/vision/ocr/ocr_multi_output.py
copy()
Copy the OcrMultiOutput object
Returns:
| Name | Type | Description |
|---|---|---|
OcrMultiOutput |
OcrMultiOutput
|
new MultiOutput object |
drop_duplicates(dist_thresh, criteria='max_area')
Drop duplicates bbox in the OcrMultiOutput object. The criteria is used to decide which bbox to keep when multiple bbox are close to each other.
Currently the drop duplicates is done only on the bounding box without considering the text or the confidence. The idea is to keep the best bounding boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dist_thresh
|
float
|
minimum distance threshold between two centers of bounding boxes to be considered as duplicates. |
required |
criteria
|
str
|
criteria to keep the best bounding box. Defaults to "max_area" which means that the bounding box with the maximum area will be kept. |
'max_area'
|
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
returns the OcrMultiOutput object itself without duplicates |
Source code in otary/vision/ocr/ocr_multi_output.py
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 | |
from_aws_textract(textract_output, image_dim, block_type='WORD', is_bbox_cast_int_enabled=False)
classmethod
Convert a Textract formatted output (DetectDocumentText / AnalyzeDocument) into a common OcrMultiOutput format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
textract_output
|
dict
|
Raw JSON response from Textract, containing a "Blocks" list. |
required |
image_dim
|
tuple[int, int]
|
Dimensions of the image (width, height). |
required |
block_type
|
str
|
Which Textract BlockType to extract as OCR outputs, typically "LINE" or "WORD". Defaults to "WORD". |
'WORD'
|
is_bbox_cast_int_enabled
|
bool
|
whether to cast all bounding boxes coordinates into integers. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
OcrMultiOutput |
OcrMultiOutput
|
OcrMultiOutput object |
Source code in otary/vision/ocr/ocr_multi_output.py
from_azure_document_intelligence(azure_output, image_dim, page_nb_to_analyze=0, level='word', force_aabb=False)
classmethod
Instantiate OcrMultiOutput object from OCR Azure Intelligence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
azure_output
|
dict
|
azure OCR output dictionnary |
required |
image_dim
|
tuple[int, int]
|
image dimensions (width, height) |
required |
page_nb_to_analyze
|
int
|
page number to analyze. Defaults to 0. |
0
|
level
|
str
|
level of granularity for OCR results. Defaults to "word". |
'word'
|
force_aabb
|
bool
|
whether to force the use of axis-aligned bounding boxes (AABB). Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
OcrMultiOutput |
OcrMultiOutput
|
OcrMultiOutput object |
Source code in otary/vision/ocr/ocr_multi_output.py
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 | |
from_doctr(doctr_output, force_aabb=False, is_bbox_cast_int_enabled=True)
classmethod
Transform a single page DocTR output into a OcrMultiOutput object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doctr_output
|
dict
|
the output of the DocTR OCR pipeline. |
required |
force_aabb
|
bool
|
whether to force the use of axis-aligned bounding boxes (AABB). Defaults to False. |
False
|
is_bbox_cast_int_enabled
|
bool
|
whether to cast all bounding boxes coordinates into integers. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
OcrMultiOutput |
OcrMultiOutput
|
OcrMultiOutput object |
Source code in otary/vision/ocr/ocr_multi_output.py
from_easyocr(easyocr_output, is_bbox_cast_int_enabled=False)
classmethod
Convert an easyocr formatted output from the read method into a common OcrMultiOutput format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
easyocr_output
|
list
|
In easyocr package currently the output is a list of tuples that contains in this order the bounding box, the text and the confidence about the text read. |
required |
is_bbox_cast_int_enabled
|
bool
|
whether to cast all bounding boxes coordinates into integers. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
OcrMultiOutput |
OcrMultiOutput
|
OcrMultiOutput object |
Source code in otary/vision/ocr/ocr_multi_output.py
from_pytesseract(data, min_conf=0.0)
classmethod
Convert a pytesseract image_to_data dictionary into a list of
OcrSingleOutput objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict
|
Dictionary returned by
|
required |
min_conf
|
int
|
Minimum confidence threshold on the Tesseract scale [0, 100]
Words below this value are dropped. Defaults to |
0.0
|
Returns:
| Name | Type | Description |
|---|---|---|
OcrMultiOutput |
OcrMultiOutput
|
an OcrMultiOutput object, one per recognized word. |
Source code in otary/vision/ocr/ocr_multi_output.py
group_words(dist_thresh, max_n_words=1000000, min_n_words=2, symbol_splitter=None, restrict_word_definition=False, word_definition_regex='[a-zA-Z0-9]+')
Groups words into sentences based on their spatial proximity, simulating how a human would read lines from left to right.
This method iterates through OCR word outputs, grouping together words that are
close enough horizontally (within dist_thresh) to be considered part of the
same group of words. The grouping respects constraints on the minimum and
maximum number of words per group.
We can optionally restrict group of words formation based on a regular expression (regex) defining valid words.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dist_thresh
|
float
|
Maximum allowed distance between consecutive words to be grouped together. |
required |
max_n_words
|
int
|
Maximum number of words allowed in a group. Defaults to 1000000. |
1000000
|
min_n_words
|
int
|
Minimum number of words required to form a group. Defaults to 2. |
2
|
symbol_splitter
|
str
|
If provided, each group of words will be split by this symbol. Each part will be treated as a separate group. Defaults to None which implies no split. |
None
|
restrict_word_definition
|
bool
|
If True, only groups matching
the |
False
|
word_definition_regex
|
str
|
Regular expression pattern that
defines a valid word. Used when |
'[a-zA-Z0-9]+'
|
Returns:
| Type | Description |
|---|---|
tuple[OcrMultiOutput, OcrMultiOutput]
|
tuple[OcrMultiOutput, OcrMultiOutput]:
- The first element is an |
Source code in otary/vision/ocr/ocr_multi_output.py
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 | |
merge(ocrmos)
classmethod
Generate one single OcrMultiOutput object from a list of OcrMultiOutput
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ocrmos
|
list[OcrMultiOutput]
|
list of OcrMultiOutput |
required |
Returns:
| Name | Type | Description |
|---|---|---|
OcrMultiOutput |
OcrMultiOutput
|
one single OcrMultiOutput that contains all the OcrSingleOutput objects of all the OcrMultiOutput |
Source code in otary/vision/ocr/ocr_multi_output.py
words_in(box, box_expand_scale=1.0)
Return a list of OcrSingleOutput that are in the box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
box
|
Rectangle
|
rectangle box where we look for words |
required |
box_expand_scale
|
float
|
extend the width and heigth of the box by this value. Defaults to 0 meaning no extension just use the box as-is. |
1.0
|
Returns:
| Type | Description |
|---|---|
list[OcrSingleOutput]
|
list[OcrSingleOutput]: list of OcrSingleOutput found in the box. |