ocris 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ocris-0.1.0/LICENSE +21 -0
- ocris-0.1.0/MANIFEST.in +8 -0
- ocris-0.1.0/PKG-INFO +193 -0
- ocris-0.1.0/README.md +141 -0
- ocris-0.1.0/ocris/__init__.py +18 -0
- ocris-0.1.0/ocris/api.py +139 -0
- ocris-0.1.0/ocris/cli.py +34 -0
- ocris-0.1.0/ocris/detector.py +1090 -0
- ocris-0.1.0/ocris/hub.py +166 -0
- ocris-0.1.0/ocris/kernels.py +401 -0
- ocris-0.1.0/ocris/layout.py +354 -0
- ocris-0.1.0/ocris/nn_blocks.py +342 -0
- ocris-0.1.0/ocris/pipeline.py +289 -0
- ocris-0.1.0/ocris/recognizer.py +482 -0
- ocris-0.1.0/ocris/rectify.py +571 -0
- ocris-0.1.0/ocris/vocab.py +358 -0
- ocris-0.1.0/ocris.egg-info/PKG-INFO +193 -0
- ocris-0.1.0/ocris.egg-info/SOURCES.txt +22 -0
- ocris-0.1.0/ocris.egg-info/dependency_links.txt +1 -0
- ocris-0.1.0/ocris.egg-info/entry_points.txt +2 -0
- ocris-0.1.0/ocris.egg-info/requires.txt +3 -0
- ocris-0.1.0/ocris.egg-info/top_level.txt +1 -0
- ocris-0.1.0/pyproject.toml +58 -0
- ocris-0.1.0/setup.cfg +4 -0
ocris-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lukas
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
ocris-0.1.0/MANIFEST.in
ADDED
ocris-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
|
+
Name: ocris
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Lightweight multilingual 360-degree & curved OCR on NetCL (OpenCL, Vulkan, CPU)
|
|
5
|
+
Author: Lukas
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2026 Lukas
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/GodOfLores/ocris
|
|
29
|
+
Project-URL: Repository, https://github.com/GodOfLores/ocris
|
|
30
|
+
Keywords: ocr,edge,edge-ai,embedded,document-ocr,scene-text,multilingual,netcl,opencl,vulkan
|
|
31
|
+
Classifier: Development Status :: 4 - Beta
|
|
32
|
+
Classifier: Intended Audience :: Developers
|
|
33
|
+
Classifier: Intended Audience :: Science/Research
|
|
34
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
35
|
+
Classifier: Operating System :: OS Independent
|
|
36
|
+
Classifier: Environment :: GPU
|
|
37
|
+
Classifier: Programming Language :: Python :: 3
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
40
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
41
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
42
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
43
|
+
Classifier: Topic :: Scientific/Engineering :: Image Recognition
|
|
44
|
+
Classifier: Topic :: Text Processing
|
|
45
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
46
|
+
Requires-Python: >=3.11
|
|
47
|
+
Description-Content-Type: text/markdown
|
|
48
|
+
License-File: LICENSE
|
|
49
|
+
Requires-Dist: netcl>=0.6.3
|
|
50
|
+
Requires-Dist: numpy
|
|
51
|
+
Requires-Dist: pillow
|
|
52
|
+
|
|
53
|
+
# ocris
|
|
54
|
+
|
|
55
|
+
Multilingual 360° & curved text OCR built on **NetCL** (OpenCL, Vulkan, and CPU).
|
|
56
|
+
|
|
57
|
+
- **Supports**: Latin & Pan-European, Cyrillic, Greek, CJK (incl. vertical *tategaki*), Hangul, Thai, Devanagari, and Arabic/Persian/Urdu (RTL + BiDi).
|
|
58
|
+
- **Geometry**: Arbitrary 0°–360° text orientations, perspective distortion, and curved text.
|
|
59
|
+
- **Minimal footprint**: Lightweight runtime built purely on `netcl >= 0.6.3`, `numpy`, and `pillow`.
|
|
60
|
+
- **License**: MIT.
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## Examples
|
|
65
|
+
|
|
66
|
+
| Latin (Tier 1) | Cyrillic & Greek (Tier 2) |
|
|
67
|
+
|---|---|
|
|
68
|
+
|  |  |
|
|
69
|
+
|
|
70
|
+
| Asian / CJK & Hangul (Tier 3) | Arabic / RTL + BiDi (Tier 4) |
|
|
71
|
+
|---|---|
|
|
72
|
+
|  |  |
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## Installation
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
pip install ocris
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## Quickstart
|
|
85
|
+
|
|
86
|
+
Model weights (~203 MiB total) are downloaded automatically on the **first inference call** and cached locally (`~/.cache/ocris`).
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
from ocris import Ocris
|
|
90
|
+
|
|
91
|
+
ocr = Ocris()
|
|
92
|
+
result = ocr("document.png")
|
|
93
|
+
|
|
94
|
+
print(result.text)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## Accepted Input Types
|
|
100
|
+
|
|
101
|
+
`ocr(...)` and `ocr.read(...)` accept any of:
|
|
102
|
+
|
|
103
|
+
- `str` or `pathlib.Path` (image file path)
|
|
104
|
+
- `PIL.Image.Image`
|
|
105
|
+
- `bytes` / `bytearray` (encoded PNG/JPEG/WebP/etc.)
|
|
106
|
+
- `numpy.ndarray` (`(H, W)` grayscale, `(H, W, 3)` RGB, or `(H, W, 4)` RGBA `uint8`)
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
from PIL import Image
|
|
110
|
+
from ocris import Ocris
|
|
111
|
+
|
|
112
|
+
ocr = Ocris()
|
|
113
|
+
|
|
114
|
+
res1 = ocr("photo.jpg")
|
|
115
|
+
res2 = ocr(Image.open("scan.png"))
|
|
116
|
+
res3 = ocr(Path("receipt.webp").read_bytes())
|
|
117
|
+
|
|
118
|
+
# Batch processing
|
|
119
|
+
results = ocr.read_batch(["page1.png", "page2.png"])
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
---
|
|
123
|
+
|
|
124
|
+
## Configuration
|
|
125
|
+
|
|
126
|
+
```python
|
|
127
|
+
ocr = Ocris(
|
|
128
|
+
mode="hybrid", # "fast" (CTC), "hybrid" (CTC + AR fallback), "accurate" (AR all)
|
|
129
|
+
backend="auto", # "auto", "vulkan", "opencl", or "cpu"
|
|
130
|
+
max_side=1024, # resize longest image side to <= max_side for detection (use 1536 for tiny dense text)
|
|
131
|
+
det_thresh=0.45, # detector probability threshold
|
|
132
|
+
kernel_thresh=0.45, # instance separation kernel threshold
|
|
133
|
+
min_det_score=0.50, # minimum detection confidence
|
|
134
|
+
preload=False, # set True to download & load models immediately in __init__
|
|
135
|
+
)
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
---
|
|
139
|
+
|
|
140
|
+
## Output Structure
|
|
141
|
+
|
|
142
|
+
`ocr(...)` returns an `OCRResult`:
|
|
143
|
+
|
|
144
|
+
```python
|
|
145
|
+
result = ocr("invoice.png")
|
|
146
|
+
|
|
147
|
+
# Full reconstructed text in reading order
|
|
148
|
+
print(result.text)
|
|
149
|
+
|
|
150
|
+
# Line-level results
|
|
151
|
+
for line in result.lines:
|
|
152
|
+
print(line.text, line.layout_direction, line.angle_deg)
|
|
153
|
+
|
|
154
|
+
# Word/box-level results
|
|
155
|
+
for box in result.boxes:
|
|
156
|
+
print(box.text, box.det_score, box.text_score, box.script, box.quad.tolist())
|
|
157
|
+
|
|
158
|
+
# JSON / dict export
|
|
159
|
+
data = result.to_dict()
|
|
160
|
+
json_str = result.to_json(indent=2)
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
## CLI Usage
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
# Plain text output
|
|
169
|
+
ocris image.jpg
|
|
170
|
+
|
|
171
|
+
# Choose backend, mode, or JSON output
|
|
172
|
+
ocris image.jpg --backend vulkan --mode hybrid --max-side 1536 --json
|
|
173
|
+
|
|
174
|
+
# Pre-download weights into cache without running inference
|
|
175
|
+
ocris --download-only
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
---
|
|
179
|
+
|
|
180
|
+
## Model Cache & Environment Variables
|
|
181
|
+
|
|
182
|
+
| Variable | Description |
|
|
183
|
+
|---|---|
|
|
184
|
+
| `OCRIS_CACHE_DIR` | Override cache directory (default: `~/.cache/ocris` or `$XDG_CACHE_HOME/ocris`) |
|
|
185
|
+
| `OCRIS_MODEL_BASE_URL` | Override base URL for model weight downloads |
|
|
186
|
+
| `OCRIS_DET_MODEL` | Explicit local path to detector weights (`.npz`) |
|
|
187
|
+
| `OCRIS_REC_MODEL` | Explicit local path to recognizer weights (`.npz`) |
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
## License
|
|
192
|
+
|
|
193
|
+
[MIT](LICENSE)
|
ocris-0.1.0/README.md
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# ocris
|
|
2
|
+
|
|
3
|
+
Multilingual 360° & curved text OCR built on **NetCL** (OpenCL, Vulkan, and CPU).
|
|
4
|
+
|
|
5
|
+
- **Supports**: Latin & Pan-European, Cyrillic, Greek, CJK (incl. vertical *tategaki*), Hangul, Thai, Devanagari, and Arabic/Persian/Urdu (RTL + BiDi).
|
|
6
|
+
- **Geometry**: Arbitrary 0°–360° text orientations, perspective distortion, and curved text.
|
|
7
|
+
- **Minimal footprint**: Lightweight runtime built purely on `netcl >= 0.6.3`, `numpy`, and `pillow`.
|
|
8
|
+
- **License**: MIT.
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Examples
|
|
13
|
+
|
|
14
|
+
| Latin (Tier 1) | Cyrillic & Greek (Tier 2) |
|
|
15
|
+
|---|---|
|
|
16
|
+
|  |  |
|
|
17
|
+
|
|
18
|
+
| Asian / CJK & Hangul (Tier 3) | Arabic / RTL + BiDi (Tier 4) |
|
|
19
|
+
|---|---|
|
|
20
|
+
|  |  |
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pip install ocris
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## Quickstart
|
|
33
|
+
|
|
34
|
+
Model weights (~203 MiB total) are downloaded automatically on the **first inference call** and cached locally (`~/.cache/ocris`).
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
from ocris import Ocris
|
|
38
|
+
|
|
39
|
+
ocr = Ocris()
|
|
40
|
+
result = ocr("document.png")
|
|
41
|
+
|
|
42
|
+
print(result.text)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Accepted Input Types
|
|
48
|
+
|
|
49
|
+
`ocr(...)` and `ocr.read(...)` accept any of:
|
|
50
|
+
|
|
51
|
+
- `str` or `pathlib.Path` (image file path)
|
|
52
|
+
- `PIL.Image.Image`
|
|
53
|
+
- `bytes` / `bytearray` (encoded PNG/JPEG/WebP/etc.)
|
|
54
|
+
- `numpy.ndarray` (`(H, W)` grayscale, `(H, W, 3)` RGB, or `(H, W, 4)` RGBA `uint8`)
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from PIL import Image
|
|
58
|
+
from ocris import Ocris
|
|
59
|
+
|
|
60
|
+
ocr = Ocris()
|
|
61
|
+
|
|
62
|
+
res1 = ocr("photo.jpg")
|
|
63
|
+
res2 = ocr(Image.open("scan.png"))
|
|
64
|
+
res3 = ocr(Path("receipt.webp").read_bytes())
|
|
65
|
+
|
|
66
|
+
# Batch processing
|
|
67
|
+
results = ocr.read_batch(["page1.png", "page2.png"])
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## Configuration
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
ocr = Ocris(
|
|
76
|
+
mode="hybrid", # "fast" (CTC), "hybrid" (CTC + AR fallback), "accurate" (AR all)
|
|
77
|
+
backend="auto", # "auto", "vulkan", "opencl", or "cpu"
|
|
78
|
+
max_side=1024, # resize longest image side to <= max_side for detection (use 1536 for tiny dense text)
|
|
79
|
+
det_thresh=0.45, # detector probability threshold
|
|
80
|
+
kernel_thresh=0.45, # instance separation kernel threshold
|
|
81
|
+
min_det_score=0.50, # minimum detection confidence
|
|
82
|
+
preload=False, # set True to download & load models immediately in __init__
|
|
83
|
+
)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
## Output Structure
|
|
89
|
+
|
|
90
|
+
`ocr(...)` returns an `OCRResult`:
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
result = ocr("invoice.png")
|
|
94
|
+
|
|
95
|
+
# Full reconstructed text in reading order
|
|
96
|
+
print(result.text)
|
|
97
|
+
|
|
98
|
+
# Line-level results
|
|
99
|
+
for line in result.lines:
|
|
100
|
+
print(line.text, line.layout_direction, line.angle_deg)
|
|
101
|
+
|
|
102
|
+
# Word/box-level results
|
|
103
|
+
for box in result.boxes:
|
|
104
|
+
print(box.text, box.det_score, box.text_score, box.script, box.quad.tolist())
|
|
105
|
+
|
|
106
|
+
# JSON / dict export
|
|
107
|
+
data = result.to_dict()
|
|
108
|
+
json_str = result.to_json(indent=2)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
## CLI Usage
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
# Plain text output
|
|
117
|
+
ocris image.jpg
|
|
118
|
+
|
|
119
|
+
# Choose backend, mode, or JSON output
|
|
120
|
+
ocris image.jpg --backend vulkan --mode hybrid --max-side 1536 --json
|
|
121
|
+
|
|
122
|
+
# Pre-download weights into cache without running inference
|
|
123
|
+
ocris --download-only
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
## Model Cache & Environment Variables
|
|
129
|
+
|
|
130
|
+
| Variable | Description |
|
|
131
|
+
|---|---|
|
|
132
|
+
| `OCRIS_CACHE_DIR` | Override cache directory (default: `~/.cache/ocris` or `$XDG_CACHE_HOME/ocris`) |
|
|
133
|
+
| `OCRIS_MODEL_BASE_URL` | Override base URL for model weight downloads |
|
|
134
|
+
| `OCRIS_DET_MODEL` | Explicit local path to detector weights (`.npz`) |
|
|
135
|
+
| `OCRIS_REC_MODEL` | Explicit local path to recognizer weights (`.npz`) |
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
139
|
+
## License
|
|
140
|
+
|
|
141
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""ocris: Zero-cv2 multilingual 360-degree & curved OCR on NetCL."""
|
|
2
|
+
|
|
3
|
+
__version__ = "0.1.0"
|
|
4
|
+
|
|
5
|
+
from .api import OCR, Ocris
|
|
6
|
+
from .hub import download_models, get_cache_dir
|
|
7
|
+
from .layout import OCRResult, TextBox, TextLine
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"__version__",
|
|
11
|
+
"Ocris",
|
|
12
|
+
"OCR",
|
|
13
|
+
"OCRResult",
|
|
14
|
+
"TextLine",
|
|
15
|
+
"TextBox",
|
|
16
|
+
"download_models",
|
|
17
|
+
"get_cache_dir",
|
|
18
|
+
]
|
ocris-0.1.0/ocris/api.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"""Ergonomic public Python API for Ocris."""
|
|
2
|
+
import io
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Iterable
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
from PIL import Image
|
|
8
|
+
from netcl.core.device import manager
|
|
9
|
+
|
|
10
|
+
from .hub import ensure_model
|
|
11
|
+
from .layout import OCRResult
|
|
12
|
+
from .pipeline import OmniOCR
|
|
13
|
+
|
|
14
|
+
ImageInput = str | Path | bytes | bytearray | Image.Image | np.ndarray
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _coerce_rgb(image: ImageInput) -> np.ndarray:
|
|
18
|
+
"""Convert path, bytes, PIL.Image, or NumPy array into contiguous RGB uint8 (H, W, 3)."""
|
|
19
|
+
if isinstance(image, (str, Path)):
|
|
20
|
+
with Image.open(image) as im:
|
|
21
|
+
return np.asarray(im.convert("RGB"), dtype=np.uint8)
|
|
22
|
+
if isinstance(image, (bytes, bytearray)):
|
|
23
|
+
with Image.open(io.BytesIO(image)) as im:
|
|
24
|
+
return np.asarray(im.convert("RGB"), dtype=np.uint8)
|
|
25
|
+
if isinstance(image, Image.Image):
|
|
26
|
+
return np.asarray(image.convert("RGB"), dtype=np.uint8)
|
|
27
|
+
if isinstance(image, np.ndarray):
|
|
28
|
+
arr = image
|
|
29
|
+
if arr.ndim == 2:
|
|
30
|
+
arr = np.stack([arr, arr, arr], axis=-1)
|
|
31
|
+
elif arr.ndim == 3 and arr.shape[2] == 4:
|
|
32
|
+
arr = arr[:, :, :3]
|
|
33
|
+
elif arr.ndim != 3 or arr.shape[2] != 3:
|
|
34
|
+
raise ValueError(f"Expected 2D/RGB/RGBA array, got shape {arr.shape}")
|
|
35
|
+
return np.ascontiguousarray(arr, dtype=np.uint8)
|
|
36
|
+
raise TypeError(
|
|
37
|
+
f"Unsupported image type {type(image).__name__}; expected str, Path, bytes, PIL.Image.Image, or np.ndarray"
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _resolve_queue(backend: str):
|
|
42
|
+
if backend == "auto":
|
|
43
|
+
return manager.default().queue
|
|
44
|
+
if backend in ("vulkan", "opencl", "cpu"):
|
|
45
|
+
manager.set_default_backend(backend)
|
|
46
|
+
return manager.default(backend).queue
|
|
47
|
+
raise ValueError(f"Unknown backend {backend!r}; expected 'auto', 'vulkan', 'opencl', or 'cpu'")
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class Ocris:
|
|
51
|
+
"""Zero-cv2 multilingual 360-degree & curved OCR engine on NetCL.
|
|
52
|
+
|
|
53
|
+
Models are downloaded lazily from GitHub on the first inference call and cached locally.
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
def __init__(
|
|
57
|
+
self,
|
|
58
|
+
*,
|
|
59
|
+
mode: str = "hybrid",
|
|
60
|
+
backend: str = "auto",
|
|
61
|
+
max_side: int = 1024,
|
|
62
|
+
det_thresh: float = 0.45,
|
|
63
|
+
kernel_thresh: float = 0.45,
|
|
64
|
+
min_det_score: float = 0.50,
|
|
65
|
+
ar_threshold: float = -0.35,
|
|
66
|
+
det_model: str | Path | None = None,
|
|
67
|
+
rec_model: str | Path | None = None,
|
|
68
|
+
cache_dir: str | Path | None = None,
|
|
69
|
+
base_url: str | None = None,
|
|
70
|
+
auto_download: bool = True,
|
|
71
|
+
verify_sha256: bool = True,
|
|
72
|
+
preload: bool = False,
|
|
73
|
+
queue=None,
|
|
74
|
+
):
|
|
75
|
+
self.mode = mode
|
|
76
|
+
self.backend = backend
|
|
77
|
+
self.max_side = int(max_side)
|
|
78
|
+
self.det_thresh = float(det_thresh)
|
|
79
|
+
self.kernel_thresh = float(kernel_thresh)
|
|
80
|
+
self.min_det_score = float(min_det_score)
|
|
81
|
+
self.ar_threshold = float(ar_threshold)
|
|
82
|
+
self.det_model = det_model
|
|
83
|
+
self.rec_model = rec_model
|
|
84
|
+
self.cache_dir = cache_dir
|
|
85
|
+
self.base_url = base_url
|
|
86
|
+
self.auto_download = auto_download
|
|
87
|
+
self.verify_sha256 = verify_sha256
|
|
88
|
+
self._queue = queue
|
|
89
|
+
self._engine: OmniOCR | None = None
|
|
90
|
+
if preload:
|
|
91
|
+
self._ensure_loaded()
|
|
92
|
+
|
|
93
|
+
def _ensure_loaded(self) -> OmniOCR:
|
|
94
|
+
if self._engine is None:
|
|
95
|
+
det_path = ensure_model(
|
|
96
|
+
"det",
|
|
97
|
+
explicit_path=self.det_model,
|
|
98
|
+
cache_dir=self.cache_dir,
|
|
99
|
+
base_url=self.base_url,
|
|
100
|
+
auto_download=self.auto_download,
|
|
101
|
+
verify_sha256=self.verify_sha256,
|
|
102
|
+
)
|
|
103
|
+
rec_path = ensure_model(
|
|
104
|
+
"rec",
|
|
105
|
+
explicit_path=self.rec_model,
|
|
106
|
+
cache_dir=self.cache_dir,
|
|
107
|
+
base_url=self.base_url,
|
|
108
|
+
auto_download=self.auto_download,
|
|
109
|
+
verify_sha256=self.verify_sha256,
|
|
110
|
+
)
|
|
111
|
+
q = self._queue if self._queue is not None else _resolve_queue(self.backend)
|
|
112
|
+
self._engine = OmniOCR(
|
|
113
|
+
det_path,
|
|
114
|
+
rec_path,
|
|
115
|
+
mode=self.mode,
|
|
116
|
+
max_side=self.max_side,
|
|
117
|
+
det_thresh=self.det_thresh,
|
|
118
|
+
kernel_thresh=self.kernel_thresh,
|
|
119
|
+
min_det_score=self.min_det_score,
|
|
120
|
+
ar_threshold=self.ar_threshold,
|
|
121
|
+
queue=q,
|
|
122
|
+
)
|
|
123
|
+
return self._engine
|
|
124
|
+
|
|
125
|
+
def read(self, image: ImageInput) -> OCRResult:
|
|
126
|
+
"""Run OCR on a single image (path, bytes, PIL Image, or RGB NumPy array)."""
|
|
127
|
+
rgb = _coerce_rgb(image)
|
|
128
|
+
engine = self._ensure_loaded()
|
|
129
|
+
return engine(rgb)
|
|
130
|
+
|
|
131
|
+
def read_batch(self, images: Iterable[ImageInput]) -> list[OCRResult]:
|
|
132
|
+
"""Run OCR sequentially over an iterable of images."""
|
|
133
|
+
return [self.read(im) for im in images]
|
|
134
|
+
|
|
135
|
+
def __call__(self, image: ImageInput) -> OCRResult:
|
|
136
|
+
return self.read(image)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
OCR = Ocris
|
ocris-0.1.0/ocris/cli.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"""Command-line interface for Ocris."""
|
|
2
|
+
import argparse
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from .api import Ocris
|
|
6
|
+
from .hub import download_models
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def main() -> None:
|
|
10
|
+
p = argparse.ArgumentParser(prog="ocris", description="Multilingual 360-degree & curved OCR on NetCL.")
|
|
11
|
+
p.add_argument("image", nargs="?", type=Path, help="Path to an input image")
|
|
12
|
+
p.add_argument("--mode", default="hybrid", choices=("fast", "hybrid", "accurate"), help="Decoding mode")
|
|
13
|
+
p.add_argument("--backend", default="auto", choices=("auto", "vulkan", "opencl", "cpu"), help="Compute backend")
|
|
14
|
+
p.add_argument("--max-side", type=int, default=1024, help="Maximum detector input side length")
|
|
15
|
+
p.add_argument("--json", action="store_true", help="Output structured JSON instead of plain text")
|
|
16
|
+
p.add_argument("--download-only", action="store_true", help="Download model weights to cache and exit")
|
|
17
|
+
p.add_argument("--cache-dir", type=Path, default=None, help="Override local model cache directory")
|
|
18
|
+
args = p.parse_args()
|
|
19
|
+
|
|
20
|
+
if args.download_only:
|
|
21
|
+
det, rec = download_models(cache_dir=args.cache_dir)
|
|
22
|
+
print(f"Models ready:\n det: {det}\n rec: {rec}")
|
|
23
|
+
return
|
|
24
|
+
|
|
25
|
+
if args.image is None:
|
|
26
|
+
p.error("the following arguments are required: image (unless --download-only is used)")
|
|
27
|
+
|
|
28
|
+
ocr = Ocris(mode=args.mode, backend=args.backend, max_side=args.max_side, cache_dir=args.cache_dir)
|
|
29
|
+
res = ocr(args.image)
|
|
30
|
+
print(res.to_json() if args.json else res.text)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
if __name__ == "__main__":
|
|
34
|
+
main()
|