Engines¶
An engine reads a page and returns lines of text with their x positions. pdfexorcist runs several, parses each one’s lines the same way, and keeps a value only when enough of them read it identically.
The engines¶
Engine |
Reads |
Used |
Platform |
Positions |
|---|---|---|---|---|
|
text layer |
default |
needs poppler |
no |
|
text layer |
default |
all |
yes |
|
text layer |
default |
all |
yes |
|
text layer |
default |
all |
yes |
|
text layer |
default |
all |
yes |
|
text layer |
|
needs Java, extra |
|
|
text layer |
|
all, extra |
no |
|
text layer |
|
needs Node and |
yes |
|
pixels |
|
needs Tesseract |
yes |
|
pixels |
|
CUDA or Apple GPU, extra |
no |
|
pixels |
|
all, CPU, extra |
yes |
|
pixels |
|
all, CPU, extra |
yes |
|
pixels |
|
all, CPU, extra |
yes |
|
pixels |
|
macOS, extra |
yes |
|
pixels |
|
Apple Silicon, extra |
no |
|
pixels |
|
CUDA or Apple GPU, |
no |
default: runs on every
extract.--ocr: added by--ocr,ocr = truein a recipe, or an image input.--engines: runs only when named.Positions: whether the engine says where each word sits. show borrows the other engines’ boxes for those that do not.
pdfexorcist engines shows which are installed on your machine. An engine that is not installed, or crashes, is skipped with a warning.
pdfjs finds pdfjs-dist in the global npm root, or at $PDFEXORCIST_PDFJS. pypdf drops rotated text.
unlimitedocr runs its model in a separate Python, because the model’s code needs transformers 4.52.
Set $PDFEXORCIST_UOCR_PYTHON to a Python with torch, transformers==4.52.1, tokenizers==0.21.4,
huggingface-hub==0.36.0, addict, einops, easydict and pillow. It reads each picture of a page at its own
resolution, or the whole page when the page has no picture.
chandra, glmocr and unlimitedocr cache each page’s reading under $PDFEXORCIST_CACHE (default ~/.cache/pdfexorcist/<engine>/), and a second run on the same file reads that cache.
The vote¶
For each cell, every engine’s reading is a vote.
A value is
verifiedwhen at leastmin_agreeengines read it and no other value ties it.min_agreedefaults to a strict majority of the engines, and at least 3: 3 of 4, 3 of 5, 4 of 6. With only OCR engines, at least 2.An engine that reads two different values for one cell loses its vote there.
Anything else is
unresolved: left blank, with every reading kept invotesanddissent.
pdfexorcist extract report.pdf -k 4 # 4 engines must agree
pdfexorcist extract report.pdf -e pdfplumber,pymupdf,pdfium,camelot
min_agree does not drop when an engine is missing. With fewer engines than min_agree, extract stops with exit code 1.
min_unopposed (off by default) also accepts a value that this many engines read when none read anything else.
Families¶
A family groups engines that share code or a source. A family votes once: its members first settle on their own majority value, and that value counts as one vote. min_agree then defaults to a majority of families, at least 2.
camelot reads the text layer through pdfminer, as pdfplumber does, so you can put them in one family:
from pdfexorcist import extract
cells = extract(
"report.pdf",
families={"pdfminer": ["pdfplumber", "camelot"]},
)
print(cells.status.value_counts().to_dict())
{'verified': 242, 'unresolved': 1}
Five engines now cast four votes, and three must agree.
pdfexorcist detects a PDF whose text layer ocrmypdf made. Its text engines all read one Tesseract reading, so they become one family, and extract asks for independent engines:
pdfexorcist extract scan.pdf
╭─ Error ──────────────────────────────────────────────────────────────────────────────────────────╮
│ This PDF's text layer was made by OCR (ocrmypdf), so the text engines all repeat that one │
│ reading and count as a single vote. │
│ │
│ How to fix: Add independent OCR engines: pdfexorcist extract scan.pdf --ocr │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
For the same reason, each OCR lab gets one vote. indra and chandra both come from Datalab. --ocr runs indra when both are installed. When you name both in --engines, they vote once, as the family datalab. Surya (also from Datalab) and PaddleOCR-VL (the PaddleOCR lab) are not included.
The Indra OCR model weights have a separate licence, a modified OpenRAIL-M. Read it on the model page before you use indra for commercial work.
Chandra or Indra¶
chandra and indra both come from Datalab, so a run uses one of them.
|
|
|
|---|---|---|
Runs on |
CPU; an NVIDIA GPU with |
GPU (CUDA or Apple) |
Download |
76 MB of ONNX models |
the Chandra OCR 2 model |
Positions |
yes, a box for each line |
no |
Each engine ran alone on the 16 BMC lake-level phone photos in tests/fixtures/bmc. The photos have 1,322 hand-checked cells.
Engine |
Right |
Wrong |
Not read |
|---|---|---|---|
|
1,277 |
1 |
44 |
|
1,026 |
1 |
295 |
|
882 |
2 |
438 |
|
10 |
2 |
1,003 |
Tesseract read nothing on 4 photos. Its row shows the other 12 photos.
On an Apple M4 Max, indra read each photo in about 0.5 s. This time includes the model load. On the faded 1995 ADSI scan (tests/fixtures/adsi/1995_t2A_p6.pdf), indra misread the state names. The ADSI parser placed none of its rows.
indra uses CUDA when onnxruntime-gpu is installed in place of onnxruntime (pip uninstall onnxruntime && pip install onnxruntime-gpu). On a Mac, indra stays on the CPU. Through CoreML, it took 0.56 s for each photo. On the CPU, it took 0.20 s. Both read the same cells.
Use
indrawhen you have no GPU, when you read many pages, or whenshowneeds boxes.Use
chandrafor phone photos. Datalab also suggests it for handwriting and faded scans. When both engines are installed, name the engines to usechandra:pdfexorcist extract photo.jpg --engines chandra,ocrmac,paddleocr.
When to use –ocr¶
pdfexorcist inspect prints the command to run.
The file |
Run |
|---|---|
A PDF with a text layer |
|
A scan with no text layer |
|
A scan with an ocrmypdf text layer |
|
A photo or screenshot (.png, .jpg) |
|
On a text PDF, --ocr adds slower engines that read the same characters from the pixels.
For OCR, place values by position: type = "columns" in a recipe, or parse=parse_by_columns in Python, so a value one engine missed leaves the others in their columns.
A page that is a single image is read at its own resolution. Tesseract is a weak voter on photos: on a skewed, grainy phone photo it ran the table’s columns together.