API Reference¶
The public API of natocr. Everything here is importable straight from the
top-level package, e.g. from natocr import OCR.
OCR¶
natocr.OCR ¶
Run OCR using the operating system's native engine.
Picks the right backend for the current platform - the Vision framework on macOS, Windows Runtime OCR on Windows - and gives you one API over both.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
language
|
str
|
language code for text recognition (default: |
'en'
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
on an unsupported platform, or when the platform's native OCR dependencies aren't installed. |
Source code in natocr/core.py
supported_languages
property
¶
Language codes the current platform's backend supports.
recognize ¶
Recognize text on every page of an image or document.
Reads each page in order and returns one result per page. Single-page inputs (a PNG, a JPEG, ...) come back as a one-element list, so you can always iterate the result. Multi-page formats - DjVu, multi-page TIFF, and animated GIF - give one OCRResult per page.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Union[str, Image, ndarray, bytes]
|
what to read. One of: a file path ( |
required |
Returns:
| Type | Description |
|---|---|
List[OCRResult]
|
One OCRResult per page, in page order. At least |
List[OCRResult]
|
one element for any valid input. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
Source code in natocr/core.py
recognize_many ¶
recognize_many(images: Iterable[Union[str, Image, ndarray, bytes]], max_concurrency: int = None) -> List[List[OCRResult]]
Recognize many inputs concurrently, with bounded parallelism.
Runs recognize() across a thread pool. The native engines release the GIL while doing the actual recognition, so threads give real throughput on bulk jobs instead of plodding through the inputs one at a time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
Iterable[Union[str, Image, ndarray, bytes]]
|
an iterable of inputs, each one of the types recognize() accepts. |
required |
max_concurrency
|
int
|
most inputs to process at once. Defaults to the CPU count (never more workers than there are inputs). |
None
|
Returns:
| Type | Description |
|---|---|
List[List[OCRResult]]
|
One result list per input, in the same order as |
List[List[OCRResult]]
|
element is itself a list of OCRResult, one per |
List[List[OCRResult]]
|
page - same shape recognize() returns). |
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
Source code in natocr/core.py
arecognize
async
¶
Awaitable recognize() for one input.
Offloads the blocking native call to a worker thread so it doesn't stall the event loop - handy inside FastAPI or any async server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Union[str, Image, ndarray, bytes]
|
same inputs recognize() accepts. |
required |
Returns:
| Type | Description |
|---|---|
List[OCRResult]
|
One OCRResult per page, in page order. |
Source code in natocr/core.py
arecognize_many
async
¶
arecognize_many(images: Iterable[Union[str, Image, ndarray, bytes]], max_concurrency: int = None) -> List[List[OCRResult]]
Awaitable recognize_many().
Same bounded-concurrency fan-out, but the blocking work runs on threads off the event loop so the calling coroutine stays responsive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
Iterable[Union[str, Image, ndarray, bytes]]
|
an iterable of inputs, each one of the types recognize() accepts. |
required |
max_concurrency
|
int
|
most inputs to process at once. Defaults to the CPU count (never more workers than there are inputs). |
None
|
Returns:
| Type | Description |
|---|---|
List[List[OCRResult]]
|
One result list per input, in the same order as |
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
Source code in natocr/core.py
Results¶
natocr.OCRResult
dataclass
¶
Everything an OCR pass found in one image.
Attributes:
| Name | Type | Description |
|---|---|---|
text |
str
|
all detected text joined into a single string. |
confidence |
Optional[float]
|
average confidence across detections, or |
elements |
List[TextElement]
|
per-detection breakdown with text, bounds, and confidence. |
lines
property
¶
Detected text grouped into lines by vertical position.
Elements whose y are close together are treated as one line and
joined left-to-right. Falls back to [text] (or []) when there
are no elements.
text_lines
property
¶
paragraphs
property
¶
Lines merged into paragraphs by the vertical gaps between them.
Walks the lines top-to-bottom and starts a new paragraph whenever the
gap to the next line is more than ~1.5x the line's height. Each
paragraph comes back in the same TextLine shape - its
text is the member lines joined by newlines, with confidence and
bounds aggregated across all of their elements.
filter ¶
A copy keeping only elements at or above min_confidence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_confidence
|
float
|
lowest confidence to keep, in |
required |
drop_unknown
|
bool
|
what to do with elements that have no confidence
(Windows OCR never reports one). |
False
|
Returns:
| Type | Description |
|---|---|
OCRResult
|
A new OCRResult holding the surviving elements, |
OCRResult
|
with |
Source code in natocr/models.py
natocr.TextLine
dataclass
¶
A run of detected text grouped into one line.
The structured cousin of OCRResult.lines - same
grouping, but each line keeps its elements, an aggregated confidence, and
the box that wraps them. Also the shape
OCRResult.paragraphs returns.
Attributes:
| Name | Type | Description |
|---|---|---|
text |
str
|
the line's text, its elements joined left-to-right. |
elements |
List[TextElement]
|
the detections that make up the line. |
confidence |
Optional[float]
|
mean confidence across the elements that report one, or
|
bounds |
BoundingBox
|
the box enclosing every element in the line. |
natocr.TextElement
dataclass
¶
A single detected piece of text with its location.
Attributes:
| Name | Type | Description |
|---|---|---|
text |
str
|
the recognized string. |
bounds |
BoundingBox
|
where it was found in the image. |
confidence |
Optional[float]
|
recognition confidence in |
natocr.BoundingBox
dataclass
¶
Pixel-space bounding box for a piece of detected text.
The origin is the top-left of the image, with y growing downward.
Attributes:
| Name | Type | Description |
|---|---|---|
x |
float
|
left edge, in pixels. |
y |
float
|
top edge, in pixels. |
width |
float
|
box width, in pixels. |
height |
float
|
box height, in pixels. |
bounds
property
¶
The box as an (x, y, width, height) tuple.