pero-ocr
The package provides a full OCR pipeline including text paragraph detection, text line detection, text transcription, and text refinement using a language model. The package can be used as a command line application or as a python package which provides a document processing class and a class which represents document page content.
Please cite
If you use pero-ocr, please cite:
- O Kodym, M Hradiš: Page Layout Analysis System for Unconstrained Historic Documents. ICDAR, 2021.
- M Kišš, K Beneš, M Hradiš: AT-ST: Self-Training Adaptation Strategy for OCR in Domains with Limited Transcriptions. ICDAR, 2021.
- J Kohút, M Hradiš: TS-Net: OCR Trained to Switch Between Text Transcription Styles. ICDAR, 2021.
Installation
pero-ocr runs on Python 3.8 to 3.10 with the versions pinned in requirements/lock.txt, the tested and recommended way to run it on its own:
pip install -r requirements/lock.txt
pip install --no-deps .
To use pero-ocr as a dependency of another project, install it with pip install ., which takes the version ranges in pyproject.toml. Which install to choose and what has been tested: docs/dependencies.md.
Available models
General layout analysis (printed and handwritten) with european printed OCR specialized to czech newspapers can be downloaded here. The OCR engine is suitable for most european printed documents. It is specialized for low-quality czech newspapers digitized from microfilms, but it provides very good results for almost all types of printed documents in most languages. If you are interested in processing printed fraktur fonts, handwritten documents or medieval manuscripts, feel free to contact the authors. The newest OCR engines are available at pero-ocr.fit.vutbr.cz. OCR engines are available also through API running at pero-ocr.fit.vutbr.cz/api, github repository.
Command line application
A command line application is user_scripts/parse_folder.py, installed as the command parse-folder. It is able to process images in a directory using an OCR engine. It can render detected lines in an image and provide document content in Page XML and ALTO XML formats. Additionally, it is able to crop all text lines as rectangular regions of normalized size and save them into separate image files.
Running command line application in container
A docker container can be built from the sourcecode to run scripts and programs based on the pero-ocr. Example of running the parse_folder.py script to generate page-xml files for images in input directory:
docker run --rm --tty --interactive \
--volume path/to/input/dir:/input \
--volume path/to/output/dir:/output \
--volume path/to/ocr/engine:/engine \
--gpus all \
pero-ocr /usr/bin/python3 user_scripts/parse_folder.py \
--config /engine/config.ini \
--input-image-path /input \
--output-xml-path /output
Be sure to use container internal paths for passed in data in the command. All input and output data locations have to be passed to container via --volume argument due to container isolation. See docker run command reference for more information.
Container can be built like this:
docker build -f Dockerfile -t pero-ocr .
Integration of the pero-ocr python module
This example shows how to directly use the OCR pipeline provided by pero-ocr package. This shows how to integrate pero-ocr into other applications. Class PageLayout represents content of a single document page and can be loaded from Page XML and exported to Page XML and ALTO XML formats. The OCR pipeline is represented by the PageParser class.
import os
import configparser
import cv2
import numpy as np
from pero_ocr.core.layout import PageLayout
from pero_ocr.document_ocr.page_parser import PageParser
# Read config file.
config_path = "./config_file.ini"
config = configparser.ConfigParser()
config.read(config_path)
# Init the OCR pipeline.
# You have to specify config_path to be able to use relative paths
# inside the config file.
page_parser = PageParser(config, config_path=os.path.dirname(config_path))
# Read the document page image.
input_image_path = "page_image.jpg"
image = cv2.imread(input_image_path, 1)
# Init empty page content.
# This object will be updated by the ocr pipeline. id can be any string and it is used to identify the page.
page_layout = PageLayout(id=input_image_path,
page_size=(image.shape[0], image.shape[1]))
# Process the image by the OCR pipeline
page_layout = page_parser.process_page(image, page_layout)
page_layout.to_pagexml('output_page.xml') # Save results as Page XML.
page_layout.to_altoxml('output_ALTO.xml') # Save results as ALTO XML.
# Render detected text regions and text lines into the image and
# save it into a file.
rendered_image = page_layout.render_to_image(image)
cv2.imwrite('page_image_render.jpg', rendered_image)
# Save each cropped text line in a separate .jpg file.
for region in page_layout.regions:
for line in region.lines:
cv2.imwrite(f'file_id-{line.id}.jpg', line.crop.astype(np.uint8))
Documentation
docs/architecture.md: how pero-ocr works, from the main point to how to extend it.CHANGELOG.md: what changed in each release.docs/input-and-output.md: what pero-ocr reads and writes: page images, PAGE XML, ALTO, logits, line images.pero_ocr/README.md: every option of an engine'sconfig.iniand of an OCR model's json.docs/: the other documents: testing, dependencies, the benchmark, the contributing rules.test/README.md: running the tests (pytest, CPU only, no models or data needed).benchmark/README.md: running the benchmark, which records, scores and compares what engines produce on a set of pages.
Contributing
Changes happen on the develop branch. The rules every change follows are in docs/CONTRIBUTING.md.
Metadata
Release files for pero-ocr 0.8.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pero_ocr-0.8.0.tar.gz | 108.8 kB | Without provenance | Details for pero_ocr-0.8.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pero_ocr-0.8.0-py3-none-any.whl | Python 3 | none | any | Details for pero_ocr-0.8.0-py3-none-any.whl |
Total release size: 232.5 kB
Release files / pero_ocr-0.8.0.tar.gz
| Download URL | pero_ocr-0.8.0.tar.gz |
|---|---|
| Size | 108.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
15f0560807bd0137c5d4824e0c5346c3bcc64188dd7b38c2d4b784958c18a965
|
|
BLAKE2b-256 checksum How to use checksums |
62fdf03fb959dd8350cc94b93188458f1b3d6a786eb5960f866b702697f099b1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.18
|
Release files / pero_ocr-0.8.0-py3-none-any.whl
| Download URL | pero_ocr-0.8.0-py3-none-any.whl |
|---|---|
| Size | 123.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7323ddb783f4481bd1db081f8a422fb4e2f0f189ac0977c5a424bc506b10e190
|
|
BLAKE2b-256 checksum How to use checksums |
367ea2b7384a900245eb0a051b0dc31df15e95cfb7b5fe94aebbac66e858a32b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.18
|
Security for pero-ocr 0.8.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.