scalable-htr 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.
- scalable_htr-0.1.0/LICENSE +21 -0
- scalable_htr-0.1.0/PKG-INFO +639 -0
- scalable_htr-0.1.0/README.md +600 -0
- scalable_htr-0.1.0/pyproject.toml +64 -0
- scalable_htr-0.1.0/setup.cfg +4 -0
- scalable_htr-0.1.0/src/scalable_htr/__init__.py +34 -0
- scalable_htr-0.1.0/src/scalable_htr/classification.py +173 -0
- scalable_htr-0.1.0/src/scalable_htr/htr/__init__.py +0 -0
- scalable_htr-0.1.0/src/scalable_htr/htr/evaluation/__init__.py +0 -0
- scalable_htr-0.1.0/src/scalable_htr/htr/evaluation/segmentation.py +177 -0
- scalable_htr-0.1.0/src/scalable_htr/htr/evaluation/transcription.py +235 -0
- scalable_htr-0.1.0/src/scalable_htr/htr/geometry.py +117 -0
- scalable_htr-0.1.0/src/scalable_htr/htr/kraken_engine.py +274 -0
- scalable_htr-0.1.0/src/scalable_htr/htr/pagexml.py +47 -0
- scalable_htr-0.1.0/src/scalable_htr/models/xgb_classifier.pkl +0 -0
- scalable_htr-0.1.0/src/scalable_htr/models/xgb_classifier_meta.json +32 -0
- scalable_htr-0.1.0/src/scalable_htr/post_segmentation/__init__.py +0 -0
- scalable_htr-0.1.0/src/scalable_htr/post_segmentation/data_loading.py +263 -0
- scalable_htr-0.1.0/src/scalable_htr/post_segmentation/line_classification.py +291 -0
- scalable_htr-0.1.0/src/scalable_htr/post_segmentation/line_sorting.py +333 -0
- scalable_htr-0.1.0/src/scalable_htr/post_segmentation/mapping_segmentation.py +117 -0
- scalable_htr-0.1.0/src/scalable_htr/reports/__init__.py +0 -0
- scalable_htr-0.1.0/src/scalable_htr/reports/per_image.py +1234 -0
- scalable_htr-0.1.0/src/scalable_htr/reports/transcription_summary.py +671 -0
- scalable_htr-0.1.0/src/scalable_htr/reports/xml_to_html.py +238 -0
- scalable_htr-0.1.0/src/scalable_htr/segmentation.py +186 -0
- scalable_htr-0.1.0/src/scalable_htr/transcription.py +253 -0
- scalable_htr-0.1.0/src/scalable_htr.egg-info/PKG-INFO +639 -0
- scalable_htr-0.1.0/src/scalable_htr.egg-info/SOURCES.txt +32 -0
- scalable_htr-0.1.0/src/scalable_htr.egg-info/dependency_links.txt +1 -0
- scalable_htr-0.1.0/src/scalable_htr.egg-info/entry_points.txt +9 -0
- scalable_htr-0.1.0/src/scalable_htr.egg-info/requires.txt +20 -0
- scalable_htr-0.1.0/src/scalable_htr.egg-info/top_level.txt +1 -0
- scalable_htr-0.1.0/tests/test_package.py +69 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Barcelona Supercomputing Center
|
|
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.
|
|
@@ -0,0 +1,639 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: scalable-htr
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Line segmentation, transcription and reading-order pipeline for historical manuscripts, built on Kraken
|
|
5
|
+
Author: Adrián Carrascosa, Mariona Coll Ardanuy
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://gitlab.bsc.es/cssh/releases/scalable-htr
|
|
8
|
+
Project-URL: Documentation, https://cssh.gitlab-pages.bsc.es/releases/scalable-htr/
|
|
9
|
+
Project-URL: Repository, https://gitlab.bsc.es/cssh/releases/scalable-htr
|
|
10
|
+
Project-URL: Issues, https://gitlab.bsc.es/cssh/releases/scalable-htr/-/issues
|
|
11
|
+
Keywords: htr,handwritten text recognition,kraken,pagexml,digital humanities
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
16
|
+
Classifier: Topic :: Scientific/Engineering :: Image Recognition
|
|
17
|
+
Requires-Python: >=3.12
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Requires-Dist: parse-pagexml<0.3,>=0.2.1
|
|
21
|
+
Requires-Dist: kraken<8,>=7.2
|
|
22
|
+
Requires-Dist: numpy>=2.0
|
|
23
|
+
Requires-Dist: pandas<3,>=2.3
|
|
24
|
+
Requires-Dist: scipy>=1.16
|
|
25
|
+
Requires-Dist: shapely>=2.1
|
|
26
|
+
Requires-Dist: pygeoops>=0.6
|
|
27
|
+
Requires-Dist: scikit-learn<2,>=1.7
|
|
28
|
+
Requires-Dist: xgboost<3.3,>=3.2
|
|
29
|
+
Requires-Dist: joblib>=1.5
|
|
30
|
+
Requires-Dist: matplotlib>=3.10
|
|
31
|
+
Requires-Dist: seaborn>=0.13
|
|
32
|
+
Requires-Dist: opencv-python-headless>=4.11
|
|
33
|
+
Requires-Dist: Pillow>=11.2
|
|
34
|
+
Provides-Extra: compss
|
|
35
|
+
Requires-Dist: python-dotenv>=1.0; extra == "compss"
|
|
36
|
+
Provides-Extra: test
|
|
37
|
+
Requires-Dist: pytest; extra == "test"
|
|
38
|
+
Dynamic: license-file
|
|
39
|
+
|
|
40
|
+
# Scalable HTR (Handwritten Text Recognition)
|
|
41
|
+
|
|
42
|
+
A complete pipeline for line segmentation and transcription of medieval handwritten documents using Kraken (BLLA segmentation + HTR).
|
|
43
|
+
|
|
44
|
+
📖 **[Full documentation](https://cssh.gitlab-pages.bsc.es/releases/scalable-htr/)**
|
|
45
|
+
|
|
46
|
+
## Description
|
|
47
|
+
|
|
48
|
+
This project provides tools for:
|
|
49
|
+
- **Line segmentation** in medieval manuscript documents using Kraken BLLA
|
|
50
|
+
- **Handwritten text transcription** using Kraken HTR models
|
|
51
|
+
- **Results evaluation** with detection and transcription metrics
|
|
52
|
+
- **Interactive HTML report generation** with visualizations
|
|
53
|
+
|
|
54
|
+
## Pipeline Overview
|
|
55
|
+
|
|
56
|
+
```mermaid
|
|
57
|
+
flowchart LR
|
|
58
|
+
IMG[/"JPG / PNG"/]
|
|
59
|
+
GT[/"PageXML GT\n(optional)"/]
|
|
60
|
+
|
|
61
|
+
IMG --> SEG["Segmentation\nKraken BLLA"]
|
|
62
|
+
SEG --> CLS["Classify + Sort\nXGBoost"]
|
|
63
|
+
CLS --> TRX["Transcription\nKraken HTR"]
|
|
64
|
+
|
|
65
|
+
GT -.->|evaluate| SEG
|
|
66
|
+
GT -.->|evaluate| TRX
|
|
67
|
+
|
|
68
|
+
SEG & TRX --> HTML["Interactive\nHTML Report"]
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Project Structure
|
|
72
|
+
|
|
73
|
+
```
|
|
74
|
+
scalable-htr/
|
|
75
|
+
├── src/scalable_htr/ # The installable Python package
|
|
76
|
+
│ ├── segmentation.py # Line segmentation with Kraken BLLA (htr-segment)
|
|
77
|
+
│ ├── transcription.py # HTR transcription with Kraken (htr-transcribe)
|
|
78
|
+
│ ├── classification.py # Classify + sort a folder of predictions (htr-classify-sort)
|
|
79
|
+
│ ├── post_segmentation/ # Line classification & sorting tools
|
|
80
|
+
│ │ ├── data_loading.py # Shared loader (JSON / PageXML), FEATURE_COLS
|
|
81
|
+
│ │ ├── mapping_segmentation.py # Map GT classes onto Kraken lines (IoP)
|
|
82
|
+
│ │ ├── line_classification.py # Line type classification with XGBoost (training)
|
|
83
|
+
│ │ └── line_sorting.py # Sort classified lines into reading order
|
|
84
|
+
│ ├── htr/ # HTR core library
|
|
85
|
+
│ │ ├── kraken_engine.py # Kraken model wrapper (KrakenEngine)
|
|
86
|
+
│ │ ├── geometry.py # Baseline/centerline geometry helpers
|
|
87
|
+
│ │ ├── pagexml.py # PAGE-XML → coords JSON
|
|
88
|
+
│ │ └── evaluation/
|
|
89
|
+
│ │ ├── segmentation.py # Segmentation evaluation
|
|
90
|
+
│ │ └── transcription.py # Transcription evaluation (CER/WER)
|
|
91
|
+
│ ├── reports/ # HTML report generation
|
|
92
|
+
│ │ ├── per_image.py # Per-image segmentation/transcription report
|
|
93
|
+
│ │ ├── transcription_summary.py # Standalone transcription summary report
|
|
94
|
+
│ │ └── xml_to_html.py # Standalone PAGE-XML → interactive HTML viewer
|
|
95
|
+
│ └── models/ # Trained XGBoost line classifier + metadata (shipped with the package)
|
|
96
|
+
├── compss/
|
|
97
|
+
│ └── compss_pipeline.py # Distributed segment+transcribe pipeline (PyCOMPSs, not in the PyPI package)
|
|
98
|
+
├── launchers/ # SLURM job submission scripts (MareNostrum 5)
|
|
99
|
+
│ ├── COMPSs_MN5.sh # Submit the distributed COMPSs pipeline (enqueue_compss)
|
|
100
|
+
│ ├── finetune_kraken_blla_MN5.sh # Kraken BLLA fine-tuning (ketos segtrain)
|
|
101
|
+
│ ├── finetune_kraken_htr_MN5.sh # Kraken HTR fine-tuning (ketos train)
|
|
102
|
+
│ ├── run_sample_MN5.sh # Run the segment+transcribe pipeline on sample_data
|
|
103
|
+
│ ├── serial.sh # Generic SLURM wrapper used by the Makefile targets
|
|
104
|
+
│ └── jupyter-GPU.sh # Launch a JupyterLab server on a GPU node
|
|
105
|
+
├── environments/
|
|
106
|
+
│ └── scalable-htr_env.yml # Conda environment definition
|
|
107
|
+
├── tests/ # Smoke tests (pytest)
|
|
108
|
+
├── docs/ # MkDocs documentation (see mkdocs.yml)
|
|
109
|
+
├── sample_data/ # Sample images + PageXML GT (AMSMB corpus)
|
|
110
|
+
├── Makefile # SLURM-based workflow entry points (see below)
|
|
111
|
+
├── pyproject.toml # Package metadata and dependencies
|
|
112
|
+
├── .env.template # Configuration template; copy to .env (Makefile and COMPSs pipeline)
|
|
113
|
+
└── README.md
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Installation
|
|
117
|
+
|
|
118
|
+
### From PyPI
|
|
119
|
+
|
|
120
|
+
```bash
|
|
121
|
+
pip install scalable-htr
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
This installs the package, its dependencies (Kraken, PyTorch, XGBoost…, and [`parse-pagexml`](https://pypi.org/project/parse-pagexml/)) and the command-line tools listed below. The XGBoost line classifier is included.
|
|
125
|
+
|
|
126
|
+
| Command | Purpose |
|
|
127
|
+
| --- | --- |
|
|
128
|
+
| `htr-segment` | Line segmentation (Kraken BLLA) |
|
|
129
|
+
| `htr-transcribe` | Segmentation + HTR transcription |
|
|
130
|
+
| `htr-classify-sort` | Classify and sort the lines of a folder of predictions |
|
|
131
|
+
| `htr-train-classifier` | Train the XGBoost line classifier |
|
|
132
|
+
| `htr-sort-lines` | Sort classified lines into reading order |
|
|
133
|
+
| `htr-map-segmentation` | Map GT classes onto Kraken lines |
|
|
134
|
+
| `htr-xml-to-html` | PAGE-XML / ALTO → interactive HTML viewer |
|
|
135
|
+
| `htr-summary-report` | Transcription summary HTML report |
|
|
136
|
+
|
|
137
|
+
### From Python
|
|
138
|
+
|
|
139
|
+
The three main steps are also functions:
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
from scalable_htr import segment, transcribe, classify_sort
|
|
143
|
+
|
|
144
|
+
# 1. Segment the lines (optionally evaluate against PageXML ground truth)
|
|
145
|
+
segment(
|
|
146
|
+
blla_model_path="blla.mlmodel",
|
|
147
|
+
images_path="images/", # a directory or a single image
|
|
148
|
+
output_path="out/",
|
|
149
|
+
gt_xml_path="pagexml/", # optional
|
|
150
|
+
generate_report=True, # one HTML report per image
|
|
151
|
+
device="auto", # "auto", "cpu", "cuda", "cuda:0"...
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
# 2. Classify the lines (default / interlinear / margin) and sort them into reading order.
|
|
155
|
+
# Uses the classifier shipped with the package unless classifier_path is given.
|
|
156
|
+
classify_sort(pred_dir="out/predictions", output_dir="out/sorted")
|
|
157
|
+
|
|
158
|
+
# 3. Segment (if needed) and transcribe
|
|
159
|
+
transcribe(
|
|
160
|
+
blla_model_path="blla.mlmodel",
|
|
161
|
+
htr_model_path="htr.mlmodel",
|
|
162
|
+
images_path="images/",
|
|
163
|
+
output_path="out/",
|
|
164
|
+
)
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Each command-line tool is a thin wrapper around the function with the same arguments.
|
|
168
|
+
|
|
169
|
+
### From source (development)
|
|
170
|
+
|
|
171
|
+
```bash
|
|
172
|
+
git clone https://gitlab.bsc.es/cssh/releases/scalable-htr.git
|
|
173
|
+
cd scalable-htr
|
|
174
|
+
conda env create -f environments/scalable-htr_env.yml # optional: pinned environment
|
|
175
|
+
conda activate scalable-htr_env
|
|
176
|
+
pip install -e ".[test]" # also installs parse-pagexml from PyPI
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
The distributed COMPSs pipeline (`compss/compss_pipeline.py`) is not part of the PyPI package; it needs PyCOMPSs and `pip install -e ".[compss]"`.
|
|
180
|
+
|
|
181
|
+
## Usage
|
|
182
|
+
|
|
183
|
+
### Quickstart on MareNostrum 5 (Makefile)
|
|
184
|
+
|
|
185
|
+
On MN5 the main steps can be submitted as SLURM jobs through the `Makefile`, which reads its configuration (models, paths, account, QOS, GPUs…) from the `.env` file at the repository root and wraps each script with `launchers/serial.sh`:
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
make help # list available targets
|
|
189
|
+
make segment # sbatch scalable_htr.segmentation over IMAGES_PATH
|
|
190
|
+
make classify_sort # sbatch scalable_htr.classification over OUTPUT_PATH/predictions
|
|
191
|
+
make transcribe # sbatch scalable_htr.transcription over IMAGES_PATH
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
Create your local configuration with `cp .env.template .env` (`.env` is not tracked by git), then edit `.env` to change models (`SEG_MODEL`, `HTR_MODEL`), data paths (`IMAGES_PATH`, `GT_XML_PATH`, `OUTPUT_PATH`) or SLURM resources. A `logs/` directory must exist at the repo root for the SLURM output files. The sections below document the underlying scripts, which can also be run directly on any machine.
|
|
195
|
+
|
|
196
|
+
---
|
|
197
|
+
|
|
198
|
+
### 1. Segmentation with Kraken
|
|
199
|
+
|
|
200
|
+
Detects and segments text lines using a fine-tuned Kraken BLLA model. Accepts a single image or a directory of images. Ground truth is provided as PageXML files.
|
|
201
|
+
|
|
202
|
+
#### Parameters
|
|
203
|
+
|
|
204
|
+
| Argument | Flag | Description | Required | Type |
|
|
205
|
+
| ------------------- | ---- | ---------------------------------------------------------------- | -------- | ---------- |
|
|
206
|
+
| `--blla_model_path` | `-m` | Path to Kraken BLLA model (`.safetensors` or `.mlmodel`) | Yes | str (path) |
|
|
207
|
+
| `--images_path` | `-i` | Path to an image file or directory of images | Yes | str (path) |
|
|
208
|
+
| `--gt_xml_path` | `-g` | Path to PageXML file or directory with GT annotations | No | str (path) |
|
|
209
|
+
| `--output_path` | `-o` | Output directory (default: `output`) | No | str (path) |
|
|
210
|
+
| `--generate_report` | | Generate an interactive HTML report per image | No | flag |
|
|
211
|
+
| `--device` | | Device for Kraken inference: `auto`, `cpu`, `cuda`, `cuda:0`… (default: `auto`) | No | str |
|
|
212
|
+
|
|
213
|
+
#### Example Usage
|
|
214
|
+
|
|
215
|
+
```bash
|
|
216
|
+
# Single image
|
|
217
|
+
htr-segment \
|
|
218
|
+
--blla_model_path ./models/blla.mlmodel \
|
|
219
|
+
--images_path ./sample_data/images/007_001_000045.jpg \
|
|
220
|
+
--gt_xml_path ./sample_data/pageXML/007_001_000045.xml \
|
|
221
|
+
--output_path ./sample_data/output \
|
|
222
|
+
--generate_report
|
|
223
|
+
|
|
224
|
+
# Directory of images
|
|
225
|
+
htr-segment \
|
|
226
|
+
--blla_model_path /path/to/blla.mlmodel \
|
|
227
|
+
--images_path ./sample_data/images \
|
|
228
|
+
--gt_xml_path ./sample_data/pageXML \
|
|
229
|
+
--output_path ./sample_data/output \
|
|
230
|
+
--generate_report
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
#### Output
|
|
234
|
+
|
|
235
|
+
- `predictions/`: `.pkl` (Kraken format) and `.json` (normalized polygon coordinates) per image
|
|
236
|
+
- `ground_truth/`: `.json` files extracted from PageXML (if `--gt_xml_path` provided)
|
|
237
|
+
- `evaluation_detection_results.json`: Segmentation metrics (Precision, Recall, F1, IoU)
|
|
238
|
+
- `reports/`: Interactive HTML reports (if `--generate_report`)
|
|
239
|
+
|
|
240
|
+
---
|
|
241
|
+
|
|
242
|
+
### 2. Transcription with Kraken
|
|
243
|
+
|
|
244
|
+
Transcribes detected text lines using a Kraken HTR model. If the output directory already contains segmentation predictions (`.pkl`/`.json` from a previous `htr-segment` run with the same `--output_path`), they are reused; otherwise segmentation is performed internally with the BLLA model.
|
|
245
|
+
|
|
246
|
+
#### Parameters
|
|
247
|
+
|
|
248
|
+
| Argument | Flag | Description | Required | Type |
|
|
249
|
+
| ------------------- | ---- | ---------------------------------------------------------------- | -------- | ---------- |
|
|
250
|
+
| `--blla_model_path` | `-b` | Path to Kraken BLLA model for segmentation (`.safetensors` or `.mlmodel`) | Yes | str (path) |
|
|
251
|
+
| `--htr_model_path` | `-t` | Path to Kraken HTR model for transcription (`.safetensors` or `.mlmodel`) | Yes | str (path) |
|
|
252
|
+
| `--images_path` | `-i` | Path to an image file or directory of images | Yes | str (path) |
|
|
253
|
+
| `--gt_xml_path` | `-g` | Path to PageXML file or directory with GT annotations | No | str (path) |
|
|
254
|
+
| `--output_path` | `-o` | Output directory (default: `output`) | No | str (path) |
|
|
255
|
+
| `--generate_report` | | Generate an interactive HTML report per image | No | flag |
|
|
256
|
+
| `--device` | | Device for Kraken inference: `auto`, `cpu`, `cuda`, `cuda:0`… (default: `auto`) | No | str |
|
|
257
|
+
|
|
258
|
+
#### Example Usage
|
|
259
|
+
|
|
260
|
+
```bash
|
|
261
|
+
# Single image
|
|
262
|
+
htr-transcribe \
|
|
263
|
+
--blla_model_path ./models/blla.mlmodel \
|
|
264
|
+
--htr_model_path ./models/htr.mlmodel \
|
|
265
|
+
--images_path ./sample_data/images/007_001_000045.jpg \
|
|
266
|
+
--gt_xml_path ./sample_data/pageXML/007_001_000045.xml \
|
|
267
|
+
--output_path ./sample_data/output \
|
|
268
|
+
--generate_report
|
|
269
|
+
|
|
270
|
+
# Directory of images
|
|
271
|
+
htr-transcribe \
|
|
272
|
+
--blla_model_path /path/to/blla.mlmodel \
|
|
273
|
+
--htr_model_path /path/to/htr.mlmodel \
|
|
274
|
+
--images_path ./sample_data/images \
|
|
275
|
+
--gt_xml_path ./sample_data/pageXML \
|
|
276
|
+
--output_path ./sample_data/output \
|
|
277
|
+
--generate_report
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
#### Output
|
|
281
|
+
|
|
282
|
+
- `predictions/`: `.pkl` and `.json` files with segmentation and transcription results
|
|
283
|
+
- `ground_truth/`: `.json` files extracted from PageXML
|
|
284
|
+
- `evaluation_transcription_results.json`: CER and WER metrics per image and global average
|
|
285
|
+
- `reports/`: HTML reports with side-by-side GT vs. prediction comparison
|
|
286
|
+
|
|
287
|
+
---
|
|
288
|
+
|
|
289
|
+
### 3. Post-Segmentation: Line Classification & Sorting
|
|
290
|
+
|
|
291
|
+
Post-segmentation tools that classify each segmented line by type and sort the
|
|
292
|
+
lines into a reading order. They are **independent** scripts (they can be run
|
|
293
|
+
separately), sharing a common data-loading layer that supports JSON, PageXML
|
|
294
|
+
and ALTO input.
|
|
295
|
+
|
|
296
|
+
#### Description
|
|
297
|
+
|
|
298
|
+
After segmentation, each line is labeled as one of three types — **Default**
|
|
299
|
+
(main text), **Interlinear** (annotations between lines), or **Margin**
|
|
300
|
+
(marginal notes) — with an XGBoost model, and the lines are then sorted into a
|
|
301
|
+
logical reading sequence, linking interlinear and margin lines to their parent
|
|
302
|
+
line. A **mapping** step builds the training data by transferring the
|
|
303
|
+
ground-truth classes onto Kraken-segmented lines (via IoP overlap), so the model
|
|
304
|
+
learns on the same geometry it sees in production.
|
|
305
|
+
|
|
306
|
+
#### Tools
|
|
307
|
+
|
|
308
|
+
| Script | Role |
|
|
309
|
+
| ------ | ---- |
|
|
310
|
+
| `post_segmentation/mapping_segmentation.py` | Assign GT classes to Kraken lines (build training data) |
|
|
311
|
+
| `post_segmentation/line_classification.py` | Train / apply the XGBoost line-type classifier |
|
|
312
|
+
| `post_segmentation/line_sorting.py` | Sort classified lines into reading order |
|
|
313
|
+
| `htr-classify-sort` | Run classification + sorting over a folder of predictions |
|
|
314
|
+
|
|
315
|
+
#### Example Usage
|
|
316
|
+
|
|
317
|
+
```bash
|
|
318
|
+
# Train the classifier (threshold found automatically)
|
|
319
|
+
htr-train-classifier \
|
|
320
|
+
--train_folder ./train/ --val_folder ./validation/ \
|
|
321
|
+
--format json
|
|
322
|
+
|
|
323
|
+
# Classify + sort a folder of Kraken predictions
|
|
324
|
+
# (threshold loaded automatically from xgb_classifier_meta.json)
|
|
325
|
+
htr-classify-sort \
|
|
326
|
+
--pred_dir output/predictions \
|
|
327
|
+
--output_dir output/classified_sorted
|
|
328
|
+
# --classifier_path my_model.pkl # optional: defaults to the classifier shipped with the package
|
|
329
|
+
|
|
330
|
+
# Sort already-classified lines standalone (also writes debug images
|
|
331
|
+
# with the reading order drawn on each page)
|
|
332
|
+
htr-sort-lines \
|
|
333
|
+
--input_folder output/predictions \
|
|
334
|
+
--output_folder output/sorted \
|
|
335
|
+
--image_folder ./sample_data/images \
|
|
336
|
+
--debug_folder output/debug_viz \
|
|
337
|
+
--format json
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
#### Output
|
|
341
|
+
|
|
342
|
+
- `classified_sorted/`: one JSON per page with predicted `class`, `order_id` and `parent_id`
|
|
343
|
+
- Classification report + confusion matrix (when training)
|
|
344
|
+
- The trained model, saved to `--model_dir` (default `./classifier_model/`)
|
|
345
|
+
|
|
346
|
+
---
|
|
347
|
+
|
|
348
|
+
### 4. Fine-tuning (MareNostrum 5)
|
|
349
|
+
|
|
350
|
+
Fine-tuning is run through the SLURM launchers in `launchers/`. Each launcher runs
|
|
351
|
+
**one** experiment configuration and writes its outputs (`model/best_<score>.safetensors`
|
|
352
|
+
and checkpoints, the training log, the train/eval file lists and, for segmentation,
|
|
353
|
+
`classes.yml`) into a self-contained `$NAME/` subfolder created in the submission
|
|
354
|
+
directory. To try another configuration, edit the `CONFIG` block near the top of the
|
|
355
|
+
script — the header documents the alternative configurations that were tried. See
|
|
356
|
+
[docs/finetuning.md](docs/finetuning.md) for the differences with Kraken 6, including
|
|
357
|
+
how the best segmentation model is chosen.
|
|
358
|
+
|
|
359
|
+
> Outputs land in the directory you launch `sbatch` from. Submit from a scratch/results
|
|
360
|
+
> folder (not the repo root) to avoid polluting the working tree, and make sure a
|
|
361
|
+
> `logs/` directory exists there for the SLURM `--output`/`--error` files.
|
|
362
|
+
|
|
363
|
+
#### Kraken BLLA (segmentation)
|
|
364
|
+
|
|
365
|
+
Uses `ketos segtrain` with PageXML input from the AHPB dataset:
|
|
366
|
+
|
|
367
|
+
```bash
|
|
368
|
+
sbatch launchers/finetune_kraken_blla_MN5.sh
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
#### Kraken HTR (transcription)
|
|
372
|
+
|
|
373
|
+
Uses `ketos train` with PageXML input from the AHPB dataset:
|
|
374
|
+
|
|
375
|
+
```bash
|
|
376
|
+
sbatch launchers/finetune_kraken_htr_MN5.sh
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
---
|
|
380
|
+
|
|
381
|
+
### 5. Distributed Pipeline with COMPSs (MareNostrum 5)
|
|
382
|
+
|
|
383
|
+
`compss/compss_pipeline.py` runs the full segment + transcribe pipeline distributed with [PyCOMPSs](https://compss.bsc.es/), processing every image as an independent task on its own GPU. It runs in three parts:
|
|
384
|
+
|
|
385
|
+
1. **Segmentation** — one GPU task per image (Kraken BLLA), saving `.pkl`/`.json` predictions and a per-image HTML report
|
|
386
|
+
2. **Transcription** — one GPU task per image (Kraken HTR), each writing its own partial result under `eval_parts/` to avoid concurrent writes
|
|
387
|
+
3. **Merge** — the per-image partial results are merged into a single `evaluation_transcription_results.json`
|
|
388
|
+
|
|
389
|
+
#### Configuration
|
|
390
|
+
|
|
391
|
+
The pipeline takes no command-line arguments; it is configured through the `.env` file at the repository root (created from `.env.template`):
|
|
392
|
+
|
|
393
|
+
| Variable | Description |
|
|
394
|
+
| -------------- | -------------------------------------------------------- |
|
|
395
|
+
| `IMAGES_PATH` | Directory of input images (relative to the project root) |
|
|
396
|
+
| `OUTPUT_PATH` | Output directory (relative to the project root) |
|
|
397
|
+
| `SEG_MODEL` | Path to the Kraken BLLA model (`.safetensors` or `.mlmodel`) |
|
|
398
|
+
| `HTR_MODEL` | Path to the Kraken HTR model (`.safetensors` or `.mlmodel`) |
|
|
399
|
+
|
|
400
|
+
Resources (nodes, GPUs per node, QOS, execution time) are set in the launcher itself — edit the `enqueue_compss` flags in `launchers/COMPSs_MN5.sh` (defaults: 1 node, 4 GPUs, `acc_debug`, 30 min).
|
|
401
|
+
|
|
402
|
+
#### Example Usage
|
|
403
|
+
|
|
404
|
+
```bash
|
|
405
|
+
# From an ACC login node (alogin1-4). Requires a logs/ directory at the repo root.
|
|
406
|
+
mkdir -p logs
|
|
407
|
+
bash launchers/COMPSs_MN5.sh
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
The launcher loads the `COMPSs/3.4.post2607` module, activates the conda environment and submits the job through `enqueue_compss` (no `sbatch` needed).
|
|
411
|
+
|
|
412
|
+
#### Output
|
|
413
|
+
|
|
414
|
+
Same layout as steps 1 and 2, written to `OUTPUT_PATH`:
|
|
415
|
+
|
|
416
|
+
- `predictions/`: `.pkl` and `.json` files with segmentation and transcription results per image
|
|
417
|
+
- `reports/`: one interactive HTML report per image
|
|
418
|
+
- `eval_parts/`: per-image partial transcription results (intermediate)
|
|
419
|
+
- `evaluation_transcription_results.json`: merged transcription results
|
|
420
|
+
- COMPSs runtime logs and the task dependency graph (`--graph=true`) under `logs/`
|
|
421
|
+
|
|
422
|
+
---
|
|
423
|
+
|
|
424
|
+
### 6. PAGE-XML to Interactive HTML Viewer
|
|
425
|
+
|
|
426
|
+
Converts one or more PAGE-XML or ALTO files into standalone, self-contained HTML pages. Each page shows the manuscript image with the transcribed text lines overlaid as SVG polygons. Hovering over a polygon reveals the transcribed text for that line.
|
|
427
|
+
|
|
428
|
+
The image is embedded as base64, so the resulting HTML file works offline and can be shared without any additional assets.
|
|
429
|
+
|
|
430
|
+
#### Parameters
|
|
431
|
+
|
|
432
|
+
| Argument | Description | Required |
|
|
433
|
+
| ------------------- | --------------------------------------------------------- | -------- |
|
|
434
|
+
| `input_xml_or_dir` | Path to a single PAGE-XML or ALTO file, or a directory of `.xml` files | Yes |
|
|
435
|
+
| `output_dir` | Directory where the generated `.html` files will be saved | Yes |
|
|
436
|
+
|
|
437
|
+
#### Example Usage
|
|
438
|
+
|
|
439
|
+
```bash
|
|
440
|
+
# Single XML file
|
|
441
|
+
htr-xml-to-html ./data/gt/AMSMB_1-3-100.xml ./output/viewer/
|
|
442
|
+
|
|
443
|
+
# Directory of XML files
|
|
444
|
+
htr-xml-to-html ./data/gt/ ./output/viewer/
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
#### Output
|
|
448
|
+
|
|
449
|
+
One `.html` file per input XML, named after the XML (e.g. `AMSMB_1-3-100.html`). Open in any browser — no server required.
|
|
450
|
+
|
|
451
|
+
#### Notes
|
|
452
|
+
|
|
453
|
+
- The script expects the source image (`.jpg`, `.jpeg`, `.png`, or `.tif`) to be in the same directory as its XML file, as indicated by the `imageFilename` attribute (PageXML) or the `fileName` element (ALTO). If that file does not exist, an image with the same name as the XML is used.
|
|
454
|
+
- If the image is not found, that file is skipped with a warning.
|
|
455
|
+
|
|
456
|
+
---
|
|
457
|
+
|
|
458
|
+
### 7. Transcription Summary Report
|
|
459
|
+
|
|
460
|
+
Builds a single self-contained HTML dashboard summarising transcription quality across every evaluated image, from the `evaluation_transcription_results.json` produced by `htr-transcribe` (or the [COMPSs pipeline](#5-distributed-pipeline-with-compss-marenostrum-5)) when a ground truth is supplied.
|
|
461
|
+
|
|
462
|
+
While the per-image reports (`--generate_report`) focus on one page at a time, this is the aggregate view: global CER/WER plus a sidebar ranking every image from best to worst.
|
|
463
|
+
|
|
464
|
+
#### Parameters
|
|
465
|
+
|
|
466
|
+
| Argument | Description | Required |
|
|
467
|
+
| ------------------- | ------------------------------------------------------------------------------ | -------- |
|
|
468
|
+
| `input_json` | Path to the `evaluation_transcription_results.json` file | Yes |
|
|
469
|
+
| `output_html` | Destination file. Defaults to `<input_dir>/transcription_summary_report.html`. The "View full report" links expect the per-image reports in `reports/` next to the summary | No |
|
|
470
|
+
|
|
471
|
+
#### Example Usage
|
|
472
|
+
|
|
473
|
+
```bash
|
|
474
|
+
# Default output: ./sample_data/output/transcription_summary_report.html
|
|
475
|
+
htr-summary-report \
|
|
476
|
+
./sample_data/output/evaluation_transcription_results.json
|
|
477
|
+
|
|
478
|
+
# Explicit output path
|
|
479
|
+
htr-summary-report \
|
|
480
|
+
./sample_data/output/evaluation_transcription_results.json \
|
|
481
|
+
./sample_data/output/summary.html
|
|
482
|
+
```
|
|
483
|
+
|
|
484
|
+
#### Output
|
|
485
|
+
|
|
486
|
+
A single `.html` file, self-contained and browsable offline. It includes:
|
|
487
|
+
|
|
488
|
+
- Overall CER/WER with total characters, words and error counts
|
|
489
|
+
- Sidebar listing every image ranked best → worst by CER, with a color-coded pill
|
|
490
|
+
- Per image: CER/WER cards, matched vs. predicted lines, and its rank
|
|
491
|
+
- Line-by-line GT vs. prediction table with character-level diff highlighting
|
|
492
|
+
|
|
493
|
+
#### Notes
|
|
494
|
+
|
|
495
|
+
- The input JSON must contain the `overall` and `per_image` sections, which are only written when transcription is evaluated against a ground truth (`--gt_xml_path`).
|
|
496
|
+
- Color scale: green at 0% CER → red at ≥30%.
|
|
497
|
+
- Running the script without arguments prints the usage and exits with code 1.
|
|
498
|
+
|
|
499
|
+
---
|
|
500
|
+
|
|
501
|
+
## Data Formats
|
|
502
|
+
|
|
503
|
+
### Ground Truth Input (PageXML or ALTO)
|
|
504
|
+
|
|
505
|
+
`.xml` files following the PageXML or ALTO standard (detected from the root element; ALTO must use pixel units and have one page per file), containing polygon coordinates and transcriptions per text line. Used as input for `--gt_xml_path` in both segmentation and transcription scripts.
|
|
506
|
+
|
|
507
|
+
### Coordinates (JSON — internal format)
|
|
508
|
+
|
|
509
|
+
`.json` files generated by the pipeline with normalized polygon coordinates:
|
|
510
|
+
|
|
511
|
+
```json
|
|
512
|
+
{
|
|
513
|
+
"image_id": "AMSMB_1-3-100",
|
|
514
|
+
"lines": [
|
|
515
|
+
{
|
|
516
|
+
"order_id": 0,
|
|
517
|
+
"class": "default",
|
|
518
|
+
"parent_id": "r1",
|
|
519
|
+
"polygon": "[[0.12,0.05],[0.88,0.05],...]",
|
|
520
|
+
"eval": "TP",
|
|
521
|
+
"iou": 0.83,
|
|
522
|
+
"matched_pred_id": 2
|
|
523
|
+
}
|
|
524
|
+
]
|
|
525
|
+
}
|
|
526
|
+
```
|
|
527
|
+
|
|
528
|
+
#### Field Descriptions
|
|
529
|
+
|
|
530
|
+
| Field | Type | Description |
|
|
531
|
+
| :--- | :--- | :--- |
|
|
532
|
+
| **`image_id`** | `string` | The unique identifier or filename reference of the processed manuscript image (without file extension). |
|
|
533
|
+
| **`lines`** | `array` | A list of objects representing all annotated or detected text lines in the document. |
|
|
534
|
+
| **`lines[].order_id`** | `integer` | The reading order or sequential index of the line within the page layout. |
|
|
535
|
+
| **`lines[].class`** | `string` | The classification or type of the text line:<br>• `"default"`: Main body text lines.<br>• `"margin"`: Marginalia or annotations in the margins (`MarginTextZone`).<br>• `"interlinear"`: Small text written between the main lines. |
|
|
536
|
+
| **`lines[].parent_id`** | `string` | For marginal or interlinear lines, this field contains the identifier (`TextLine id`) of the main/default line to which they refer. |
|
|
537
|
+
| **`lines[].polygon`** | `string` | A serialized JSON string containing an array of normalized 2D coordinates `[[x1, y1], [x2, y2], ...]` representing the boundary of the line. Coordinates are normalized relative to the image dimensions (scale from `0.0` to `1.0`). |
|
|
538
|
+
| **`lines[].eval`** | `string` | The evaluation status of the detected line compared against the Ground Truth (GT):<br>• `"TP"` (True Positive): Correctly detected line.<br>• `"FP"` (False Positive): Predicted line that does not exist in the GT.<br>• `"FN"` (False Negative): Missing line (exists in the GT but was not predicted). |
|
|
539
|
+
| **`lines[].iou`** | `float` | The *Intersection over Union* (or IoP) score, measuring the spatial overlap between the predicted line and the matched GT line (ranging from `0.0` to `1.0`). |
|
|
540
|
+
| **`lines[].matched_pred_id`** | `integer` | The index or ID of the corresponding predicted line that matched this Ground Truth line during evaluation. |
|
|
541
|
+
### Coordinates (Kraken format)
|
|
542
|
+
|
|
543
|
+
Serialized `.pkl` files with Kraken's `Segmentation` object. Used internally by `htr-transcribe`.
|
|
544
|
+
|
|
545
|
+
---
|
|
546
|
+
|
|
547
|
+
## Evaluation
|
|
548
|
+
|
|
549
|
+
**Segmentation metrics:**
|
|
550
|
+
- Precision, Recall, F1-Score
|
|
551
|
+
- IoU (Intersection over Union) at threshold 0.5
|
|
552
|
+
- True Positives, False Positives, False Negatives per image and aggregated
|
|
553
|
+
|
|
554
|
+
**Transcription metrics:**
|
|
555
|
+
- CER (Character Error Rate)
|
|
556
|
+
- WER (Word Error Rate)
|
|
557
|
+
|
|
558
|
+
Results are saved as JSON and visualized in HTML reports.
|
|
559
|
+
|
|
560
|
+
---
|
|
561
|
+
|
|
562
|
+
## HTML Reports
|
|
563
|
+
|
|
564
|
+
Two complementary reports are available.
|
|
565
|
+
|
|
566
|
+
**Per image** — written to `output/reports/` when `--generate_report` is passed to `htr-segment` or `htr-transcribe`:
|
|
567
|
+
- Original image with predicted and GT polygons overlaid
|
|
568
|
+
- Color-coded bounding boxes (TP green, FP red, FN blue)
|
|
569
|
+
- Side-by-side transcription comparison (predicted vs. GT)
|
|
570
|
+
- Per-image evaluation metrics
|
|
571
|
+
- Tab navigation between views
|
|
572
|
+
|
|
573
|
+
**Across the whole corpus** — generated at the end of `htr-transcribe` when `--generate_report` and ground truth are given, or on demand with [`htr-summary-report`](#7-transcription-summary-report):
|
|
574
|
+
- Overall CER/WER with total characters, words and error counts
|
|
575
|
+
- Every image ranked best → worst by CER
|
|
576
|
+
- Line-by-line GT vs. prediction with character-level diff highlighting
|
|
577
|
+
|
|
578
|
+
---
|
|
579
|
+
|
|
580
|
+
## Typical Workflow
|
|
581
|
+
|
|
582
|
+
1. **Segment documents:**
|
|
583
|
+
```bash
|
|
584
|
+
htr-segment \
|
|
585
|
+
-m blla.mlmodel -i images/ -g gt_xmls/ -o output/
|
|
586
|
+
```
|
|
587
|
+
|
|
588
|
+
2. **Classify and sort segmented lines by type:**
|
|
589
|
+
```bash
|
|
590
|
+
htr-classify-sort \
|
|
591
|
+
--pred_dir output/predictions \
|
|
592
|
+
--output_dir output/classified_sorted
|
|
593
|
+
# --classifier_path my_model.pkl # optional: defaults to the classifier shipped with the package
|
|
594
|
+
```
|
|
595
|
+
|
|
596
|
+
3. **Transcribe detected lines:**
|
|
597
|
+
```bash
|
|
598
|
+
htr-transcribe \
|
|
599
|
+
-b blla.mlmodel -t htr.mlmodel -i images/ -g gt_xmls/ -o output/
|
|
600
|
+
```
|
|
601
|
+
|
|
602
|
+
4. **Review the per-image HTML reports:**
|
|
603
|
+
Open `output/reports/*.html` in a browser
|
|
604
|
+
|
|
605
|
+
5. **Review the corpus-wide transcription summary:**
|
|
606
|
+
With `--generate_report` and ground truth, `htr-transcribe` writes it automatically. To regenerate it by hand:
|
|
607
|
+
```bash
|
|
608
|
+
htr-summary-report \
|
|
609
|
+
output/evaluation_transcription_results.json
|
|
610
|
+
```
|
|
611
|
+
Then open `output/transcription_summary_report.html` in a browser.
|
|
612
|
+
|
|
613
|
+
---
|
|
614
|
+
|
|
615
|
+
## Sample Data
|
|
616
|
+
|
|
617
|
+
Sample images are included in `sample_data/images/`. Ground-truth line polygons and transcriptions are available as PAGE-XML in `sample_data/pageXML/`.
|
|
618
|
+
|
|
619
|
+
Sample images were provided by the Arxiu Històric de Protocols de Barcelona (AHPB):
|
|
620
|
+
* `007_001_000045`: AHPB 7/1, f. 23r.
|
|
621
|
+
* `010_003_000030`: AHPB 10/3, f. 17r.
|
|
622
|
+
* `044_003_000102`: AHPB 44/03, f. 55r.
|
|
623
|
+
* `053_015_000011`: AHPB 53/15, f. 8v.
|
|
624
|
+
|
|
625
|
+
---
|
|
626
|
+
|
|
627
|
+
## Notes
|
|
628
|
+
|
|
629
|
+
- Kraken BLLA and HTR models must be downloaded or fine-tuned beforehand
|
|
630
|
+
- GPU is strongly recommended for processing large image collections
|
|
631
|
+
- Ground truth must be in PageXML or ALTO format. With `--gt_xml_path`, the XML and the image must share the same base name
|
|
632
|
+
- HTML reports require images to be accessible at their original local paths
|
|
633
|
+
|
|
634
|
+
## Contact
|
|
635
|
+
For project inquiries, contact the BSC (Barcelona Supercomputing Center) team.
|
|
636
|
+
|
|
637
|
+
Created by Adrian Carrascosa Lopez (adrian.carrascosa@bsc.es), Mariona Coll Ardanuy (mariona.coll@bsc.es) and Laura Servat from the Computational Social Sciences and Humanities Laboratory (CSSH) at the Barcelona Supercomputing Center (BSC).
|
|
638
|
+
|
|
639
|
+
---
|