Metadata-Version: 2.4
Name: pero-ocr
Version: 0.8.0
Summary: Toolkit for advanced OCR of poor quality documents
Author-email: Michal Hradiš <hradis@fit.vutbr.cz>
Maintainer-email: Karel Beneš <ibenes@fit.vutbr.cz>
License: BSD 3-Clause License
        
        Copyright (c) 2019, DCGM
        All rights reserved.
        
        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are met:
        
        1. Redistributions of source code must retain the above copyright notice, this
           list of conditions and the following disclaimer.
        
        2. Redistributions in binary form must reproduce the above copyright notice,
           this list of conditions and the following disclaimer in the documentation
           and/or other materials provided with the distribution.
        
        3. Neither the name of the copyright holder nor the names of its
           contributors may be used to endorse or promote products derived from
           this software without specific prior written permission.
        
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
        AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
        FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
        SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
        CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
        OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
        OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        
Project-URL: homepage, https://pero.fit.vutbr.cz/
Project-URL: repository, https://github.com/DCGM/pero-ocr
Keywords: OCR,Layout analysis,handwriting recognition
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: BSD License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy<2.5,>=1.21
Requires-Dist: opencv-python<5,>=4.11
Requires-Dist: lxml>=4.7
Requires-Dist: lmdb>=1.3
Requires-Dist: lmdb<2; python_version < "3.9"
Requires-Dist: levenshtein>=0.17
Requires-Dist: scipy<2,>=1.10
Requires-Dist: numba>=0.55
Requires-Dist: torch<2.14,>=2.4
Requires-Dist: torchvision>=0.19
Requires-Dist: brnolm>=0.2
Requires-Dist: scikit-learn>=1.0
Requires-Dist: scikit-image>=0.19
Requires-Dist: shapely<2.1,>=1.8
Requires-Dist: safe-gpu>=1.5.1
Requires-Dist: arabic_reshaper>=2.1.3
Requires-Dist: ultralytics>=8.4
Requires-Dist: music21>=8.3
Provides-Extra: test
Requires-Dist: pytest>=7; extra == "test"
Dynamic: license-file

# 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`](https://github.com/DCGM/pero-ocr/blob/v0.8.0/docs/dependencies.md).

## Available models
General layout analysis (printed and handwritten) with european printed OCR specialized to czech newspapers can be [downloaded here](https://nextcloud.fit.vutbr.cz/s/NtAbHTNkZFpapdJ). 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](https://pero-ocr.fit.vutbr.cz). OCR engines are available also through API running at [pero-ocr.fit.vutbr.cz/api](https://pero-ocr.fit.vutbr.cz/api), [github repository](https://github.com/DCGM/pero-ocr-api).

## 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:
```shell
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](https://docs.docker.com/engine/reference/run/) for more information.

Container can be built like this:
```shell
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.

```python
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`](https://github.com/DCGM/pero-ocr/blob/v0.8.0/docs/architecture.md): how pero-ocr works, from the main point to how to extend it.
- [`CHANGELOG.md`](https://github.com/DCGM/pero-ocr/blob/v0.8.0/CHANGELOG.md): what changed in each release.
- [`docs/input-and-output.md`](https://github.com/DCGM/pero-ocr/blob/v0.8.0/docs/input-and-output.md): what pero-ocr reads and writes: page images, PAGE XML, ALTO, logits, line images.
- [`pero_ocr/README.md`](https://github.com/DCGM/pero-ocr/blob/v0.8.0/pero_ocr/README.md): every option of an engine's `config.ini` and of an OCR model's json.
- [`docs/`](https://github.com/DCGM/pero-ocr/blob/v0.8.0/docs/README.md): the other documents: testing, dependencies, the benchmark, the contributing rules.
- [`test/README.md`](https://github.com/DCGM/pero-ocr/blob/v0.8.0/test/README.md): running the tests (`pytest`, CPU only, no models or data needed).
- [`benchmark/README.md`](https://github.com/DCGM/pero-ocr/blob/v0.8.0/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`](https://github.com/DCGM/pero-ocr/blob/v0.8.0/docs/CONTRIBUTING.md).
