Skip to main content

dedoc-utils

This library contains useful utilities for automatic document images processing:

  1. Preprocessing
    • binarization
    • skew correction
  2. Text detection
  3. Line segmentation
  4. Text recognition

Installation

The library requires Tesseract OCR to be installed. To install the library use the following command:

pip install dedoc-utils

It's supposed that you already have torch and torchvision installed. If not you can use the following command for installation:

pip install "dedoc-utils[torch]"

If you cloned the repository, you can install the dependencies via pip:

pip install .

To install torch packages use:

pip install ."[torch]"

Basic usage

Using preprocessors

from dedocutils.preprocessing import AdaptiveBinarizer, SkewCorrector
import cv2
import matplotlib.pyplot as plt

binarizer = AdaptiveBinarizer()
skew_corrector = SkewCorrector()

image = cv2.imread("examples/before_preprocessing.jpg")
binarized_image, _ = binarizer.preprocess(image)
preprocessed_image, _ = skew_corrector.preprocess(binarized_image)

fig = plt.figure(figsize=(10, 7))
rows, columns = 1, 3

fig.add_subplot(rows, columns, 1)
plt.imshow(image)
plt.axis('off')
plt.title("Before preprocessing")
  
fig.add_subplot(rows, columns, 2)
plt.imshow(binarized_image)
plt.axis('off')
plt.title("After binarization")

fig.add_subplot(rows, columns, 3)
plt.imshow(preprocessed_image)
plt.axis('off')
plt.title("After preprocessing")

Using text detectors

from dedocutils.text_detection import DoctrTextDetector

text_detector = DoctrTextDetector()
bboxes = text_detector.detect(preprocessed_image)

for bbox in bboxes[:5]:
    print(bbox)

BBox(x_top_left=2415, y_top_left=3730, width=202, height=97)
BBox(x_top_left=790, y_top_left=3613, width=383, height=105)
BBox(x_top_left=1690, y_top_left=3488, width=407, height=104)
BBox(x_top_left=2171, y_top_left=3488, width=377, height=92)
BBox(x_top_left=885, y_top_left=3505, width=27, height=50)

Using text recognizers

from dedocutils.text_recognition import TesseractTextRecognizer

text_recognizer = TesseractTextRecognizer()

for bbox in bboxes[:10]:
    word_image = preprocessed_image[bbox.y_top_left:bbox.y_bottom_right, bbox.x_top_left:bbox.x_bottom_right]
    text = text_recognizer.recognize(word_image, parameters=dict(language="eng"))
    print(text)

Fie-
afjefjores.
coluntur,
dicuntur
delubro
eodem
dii in
plures

Using line segmenters

In the previous example, the order of the recognized words isn't the same as the order of the words in the document. It happens because of undetermined work of the text detector. In this case, one may use line segmenter to sort bboxes from the text detector.

from dedocutils.line_segmentation import ClusteringLineSegmenter

line_segmenter = ClusteringLineSegmenter()
sorted_bboxes = line_segmenter.segment(bboxes)
for bbox in sorted_bboxes[1]:
    word_image = preprocessed_image[bbox.y_top_left:bbox.y_bottom_right, bbox.x_top_left:bbox.x_bottom_right]
    text = text_recognizer.recognize(word_image, parameters=dict(language="eng"))
    print(text)

gentes,
fimul.
obibant
munera
fumma
facra,

Metadata

Release files for dedoc-utils 0.3.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for dedoc-utils 0.3.11
File Interpreter ABI Platform
dedoc_utils-0.3.11-py3-none-any.whl Python 3 none any Details

Release files / dedoc_utils-0.3.11-py3-none-any.whl

Download URL dedoc_utils-0.3.11-py3-none-any.whl
Size 81.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f46a9c7e5ccb22c9ebae402c3be47fd898ef145bbdc8b2999db6b4d586bb8a73
BLAKE2b-256 checksum
How to use checksums
60f20b7384cfa078fb8f5a90018354a9da50985a9c1ff75fbad5832311ffdf0c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.25

Release history Release notifications | RSS feed

0.3.12

1 release file

This release

0.3.11 This release

1 release file

0.3.10

1 release file

0.3.9

1 release file

0.3.8

1 release file

0.3.7

1 release file

0.3.6

1 release file

0.3.5

1 release file

0.3.4

1 release file

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page