Skip to main content

OCR engine

This library provides a clean interface to segment and recognize text in an image. It’s optimized for printed text, e.g. scanned documents and website screenshots.

Python version Github release PyPI version PyPI status

Installation

pip install liteocr

The installation includes both the liteocr Python3 library and a command line executable.

Usage

>> liteocr

Performs OCR on an image file and writes the recognition results to JSON.

usage: LiteOCR [-h] [-d] [--extra-whitelist str] [--all-unicode] [--lang str]
               [--min-text-size int] [--max-text-size int]
               [--uniformity-thresh :0.0<=float<1.0]
               [--thin-line-thresh :odd int] [--conf-thresh :0<=int<100]
               [--box-expand-factor :0.0<=float<1.0]
               [--horizontal-pooling int]
               str str

positional arguments:
  str                   image file
  str                   output JSON file

optional arguments:
  -h, --help            show this help message and exit
  -d, --display         display recognized bounding boxes and text on top of the image

engine:
  parameters to liteocr.OCREngine constructor

  --extra-whitelist str
                        string of extra chars for Tesseract to consider only
                        takes effect when all_unicode is False
  --all-unicode         if True, Tesseract will consider all possible unicode
                        characters
  --lang str            language in the text. Defaults to English.

recognition:
  parameters to OCREngine.recognize() method

  --min-text-size int   min text height/width in pixels, below which will be
                        ignored
  --max-text-size int   max text height/width in pixels, above which will be
                        ignored
  --uniformity-thresh :0.0<=float<1.0
                        ignore a region if the number of pixels neither black
                        nor white < [thresh]
  --thin-line-thresh :odd int
                        remove all lines thinner than [thresh] pixels.can be
                        used to remove the thin borders of web page textboxes.
  --conf-thresh :0<=int<100
                        ignore regions with OCR confidence < thresh.
  --box-expand-factor :0.0<=float<1.0
                        expand the bounding box outwards in case certain chars
                        are cutoff.
  --horizontal-pooling int
                        result bounding boxes will be more connected with more
                        pooling, but large pooling might lower accuracy.

Python3 library

from liteocr import OCREngine, load_img, draw_rect, draw_text, disp

image_file = 'my_img.png'
img = load_img(image_file)

# you can either use context manager or call engine.close() manually at the end.
with OCREngine() as engine:
    # engine.recognize() can accept a file name, a numpy image, or a PIL image.
    for text, box, conf in engine.recognize(image_file):
        print(box, '\tconfidence =', conf, '\ttext =', text)
        draw_rect(img, box)
        draw_text(img, text, box, color='bw')

# display the image with recognized text boxes overlaid
disp(img, pause=False)

Notes

I deprecated and moved the old code into a separate folder. The old API calls Tesseract directly on the entire image. The low recall wasn’t trivial to fix at all, as I realized later:

  • The command-line Tesseract makes really weird global page segmentation decisions. It ignores certain text regions with no apparent patterns. I have tried many different combinations of a handful of tuneable parameters, but none of them helps. My hands are tied because Tesseract is poorly documented and very few people asks such questions on Stackoverflow.

  • Tesserocr is a python package that builds a .pyx wrapper around Tesseract’s C++ API. There are a few native API methods that can iterate through text regions, but they randomly fail with SegFault (ughh!!!). I spent a lot of time trying to fix it, but gave up in despair …

  • Tesseract is the best open-source OCR engine, which means I don’t have other choices. I thought about using Google’s online OCR API, but we shouldn’t be bothered by internet connection and API call limits.

So I ended up using a new workflow:

  1. Apply OpenCV magic to produce better text segmentation.

  2. Run Tesseract on each of the segmented text box. It’s much more transparent than running on the whole image.

  3. Collect text result and mean confidence level (yield as a generator).

Metadata

Release files for liteocr 0.2.1

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

Source distribution (sdist)

Source distribution for liteocr 0.2.1
File Size Uploaded Provenance Actions
liteocr-0.2.1.tar.gz 8.5 kB Without provenance Details for liteocr-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for liteocr 0.2.1
File Interpreter ABI Platform Actions
liteocr-0.2.1-py3-none-any.whl Python 3 none any Details for liteocr-0.2.1-py3-none-any.whl

Total release size: 21.6 kB

Release files / liteocr-0.2.1.tar.gz

Download URL liteocr-0.2.1.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
5007798261954bcbee9b620c8551d7eaee5d85431c0c9f6044b548b7c30d3871
BLAKE2b-256 checksum
How to use checksums
44634dc0a0ed33ac159c3b133773b7f35fdac532107090106a28ed554674c40c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / liteocr-0.2.1-py3-none-any.whl

Download URL liteocr-0.2.1-py3-none-any.whl
Size 13.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f61f37ac8b3c802d28b6148424f3106b3de5a31dddbeb07d5b0a660c5c14ddc1
BLAKE2b-256 checksum
How to use checksums
c11b92c7ef25b8d424ae07b4d7530e81e5542f68c625df15d05121d4f34cfce0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for liteocr 0.2.1

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.

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files No provenance

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