Ocr
domain.fetch_pipeline.utils.ocr
Summary
OCR helpers for statement images and PDFs.
Covers number cleaning, tesseract text normalization, PDF rasterization, and per-page OCR with optional binarization.
Functions
clean_num
Parse a numeric token into a float amount.
Strips non-digits and divides by the correct power of ten based on
the recorded decimal part; a token without digits yields 0.0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s |
str
|
Raw numeric string. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Parsed value. |
normalize
Normalize a raw OCR string for parsing.
Fixes a tesseract digit misread, expands pipes to spaces, trims stray punctuation from the leading edge (including the replacement character), and removes commas.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text |
str
|
Raw OCR output. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Normalized text. |
ocr_all_pages
OCR every page of a PDF in a temporary directory.
With covert_to_image pages are binarized and OCRed with the
numeric-mode OEM 3 config; otherwise they are OCRed with the default
PSM 6 config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pdf_bytes |
bytes
|
Raw PDF content. |
required |
covert_to_image |
bool
|
When truthy, binarize pages to images before OCR. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
OCR text, one string per page. |
rasterize_pdf
Rasterize a PDF into per-page JPEGs.
Writes the bytes as input.pdf in out_dir and invokes
pdftoppm at the requested DPI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pdf_bytes |
bytes
|
Raw PDF content. |
required |
out_dir |
str
|
Directory used to write the PDF and JPEG outputs. |
required |
dpi |
int
|
Rasterization resolution, defaults to 300. |
300
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
Sorted list of JPEG path strings. |
Raises:
| Type | Description |
|---|---|
CalledProcessError
|
If |