segtformats 0.3__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.
- segtformats-0.3/LICENSE +8 -0
- segtformats-0.3/PKG-INFO +150 -0
- segtformats-0.3/README.md +128 -0
- segtformats-0.3/pyproject.toml +46 -0
- segtformats-0.3/setup.cfg +4 -0
- segtformats-0.3/setup.py +8 -0
- segtformats-0.3/src/segtformats/__init__.py +39 -0
- segtformats-0.3/src/segtformats/__main__.py +2 -0
- segtformats-0.3/src/segtformats/alto_to_json.py +73 -0
- segtformats-0.3/src/segtformats/alto_to_page_xml.py +52 -0
- segtformats-0.3/src/segtformats/anyseg_doctor.py +102 -0
- segtformats-0.3/src/segtformats/anyseg_to_ascii.py +148 -0
- segtformats-0.3/src/segtformats/anyseg_to_json.py +85 -0
- segtformats-0.3/src/segtformats/json_doctor.py +116 -0
- segtformats-0.3/src/segtformats/json_to_page_xml.py +71 -0
- segtformats-0.3/src/segtformats/libs/__init__.py +0 -0
- segtformats-0.3/src/segtformats/page_xml_split.py +41 -0
- segtformats-0.3/src/segtformats/page_xml_to_json.py +81 -0
- segtformats-0.3/src/segtformats/segtformat_documents.py +2769 -0
- segtformats-0.3/src/segtformats/segtformats.py +993 -0
- segtformats-0.3/src/segtformats.egg-info/PKG-INFO +150 -0
- segtformats-0.3/src/segtformats.egg-info/SOURCES.txt +25 -0
- segtformats-0.3/src/segtformats.egg-info/dependency_links.txt +1 -0
- segtformats-0.3/src/segtformats.egg-info/entry_points.txt +10 -0
- segtformats-0.3/src/segtformats.egg-info/requires.txt +7 -0
- segtformats-0.3/src/segtformats.egg-info/top_level.txt +1 -0
- segtformats-0.3/tests/test_segtformats.py +167 -0
segtformats-0.3/LICENSE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Copyright 2025 Nicolas Renet
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
8
|
+
|
segtformats-0.3/PKG-INFO
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: segtformats
|
|
3
|
+
Version: 0.3
|
|
4
|
+
Summary: Segmentation data formats: conversion tools
|
|
5
|
+
Author-email: Nicolas Renet <nprenet@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/nicolasrenet/segtformats
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Operating System :: MacOS
|
|
10
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: numpy
|
|
15
|
+
Requires-Dist: unidecode
|
|
16
|
+
Requires-Dist: shapely
|
|
17
|
+
Requires-Dist: jsonschema
|
|
18
|
+
Requires-Dist: lxml
|
|
19
|
+
Requires-Dist: fargv
|
|
20
|
+
Requires-Dist: unidecode
|
|
21
|
+
Dynamic: license-file
|
|
22
|
+
|
|
23
|
+
# SegtFormats: a toolbox for minimalistic segmentation metadata.
|
|
24
|
+
|
|
25
|
+
SegtFormat allows for handling a subset of features found in common page layout formats (PAGE, ALTO) with a focus on HTR tasks:
|
|
26
|
+
|
|
27
|
+
+ 1 label document = 1 image = 1 page[^1]
|
|
28
|
+
+ two types of objects only: regions (as bounding boxes) and lines (baselines and polygons)
|
|
29
|
+
+ conversion between common segmentation metadata formats ([Page](http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15), [ALTO](http://www.loc.gov/standards/alto/v4/alto-4-2.xsd)).
|
|
30
|
+
+ custom JSON representation documented in our [schema](doc/seg_schema.json): the dictionary allows for easy structural transformations (flattening, region and/or line extraction) or for semantic transformations (line-to-region assignments, boundaries).
|
|
31
|
+
+ for metadata only: the image is not needed.
|
|
32
|
+
+ validation options (PAGE, JSON)
|
|
33
|
+
+ no fancy visuals: however a page segmentation can be conveniently rendered in ASCII on the terminal, for diagnosis purpose.
|
|
34
|
+
|
|
35
|
+
[^1]: Neither of the commonly used formats enforces the 1-1-1 cardinality: with PAGE, 1 document may label _n_ images (each corresponding to exactly 1 `Page` element); in ALTO, 1 document labels exactly 1 image and possibly _n_ pages within it.
|
|
36
|
+
|
|
37
|
+

|
|
38
|
+
|
|
39
|
+
## How to install
|
|
40
|
+
|
|
41
|
+
<!--
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install segtformats
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
or from GitHub:
|
|
48
|
+
-->
|
|
49
|
+
|
|
50
|
+
From GitHub:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
git clone git@github.com:nicolasrenet/segtformats.git
|
|
54
|
+
cd segtformats
|
|
55
|
+
pip install .
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
From PyPi:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
TBA
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
## How to use
|
|
66
|
+
|
|
67
|
+
### Library functions: examples
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from segtformats import segtformats as sgf
|
|
71
|
+
|
|
72
|
+
# Alto to PageXML stream
|
|
73
|
+
page_xml_str = sgf.alto_to_page_xml('tests/data/217_d9c7f_default.alto.xml')
|
|
74
|
+
|
|
75
|
+
# Alto to PageXML file
|
|
76
|
+
sgf.alto_to_page_xml('tests/data/217_d9c7f_default.alto.xml', output_file='217_d9c7f_default.alto.page.xml')
|
|
77
|
+
|
|
78
|
+
# Alto to JSON dictionary
|
|
79
|
+
segmentation_dict = sgf.alto_to_segmentation_dict('tests/data/217_d9c7f_default.alto.xml')
|
|
80
|
+
# validation
|
|
81
|
+
assert sgf.json_validate( segmentation_dict )
|
|
82
|
+
|
|
83
|
+
# ASCII rendition
|
|
84
|
+
print(sgf.anyseg_to_ascii('tests/data/btv1b84473026_f25.chocomufin.xml', lines=1, scale_hw=.8))
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
### Command-line utilities
|
|
89
|
+
|
|
90
|
+
+ `alto_to_page_xml`: Alto → Page conversion. Eg.
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
alto_to_page_xml tests/data/217_d9c7f_default.alto.xml
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
+ `alto_to_json`: Alto → JSON conversion. Eg.
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
alto_to_json tests/data/217_d9c7f_default.alto.xml
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
+ `page_xml_to_json`: Page → JSON conversion. Eg.
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
page_xml_to_json tests/data/217_d9c7f_default.page.xml
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
+ `anyseg_to_json`: Detect input format and convert to JSON. Eg.
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
anyseg_to_json --file_paths tests/data/*.xml
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
+ `json_to_page_xml`: JSON → Page conversion.
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
json_to_page_xml tests/data/217_d9c7f_default.json
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
+ `anyseg_doctor`: Various transformations on JSON metadata, including repairs (region boundaries, line-to-region assignments).
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
# diagnosis-only by default (dry run)
|
|
124
|
+
anyseg_doctor tests/data/217_d9c7f_default.page.xml
|
|
125
|
+
# repairing an ALTO file, with PAGE output
|
|
126
|
+
anyseg_doctor --repair --output_format page --output_suffix .fixed.xml tests/data/217_d9c7f_default.alto.xml
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
+ `anyseg_to_ascii`: Render segmentation metadata on the terminal. Eg.
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
anyseg_to_ascii --file_paths tests/data/*.xml
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
+ `page_xml_split`: Split multi-page PAGE label into as many files (i.e. one label per image). Eg.
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
page_xml_split --file_paths tests/data/2923.xml
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Get the help on any `<command>` with
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
python3 -m segtformats.<command> -h
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
## TODO:
|
|
149
|
+
|
|
150
|
+
+ Page → Alto
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# SegtFormats: a toolbox for minimalistic segmentation metadata.
|
|
2
|
+
|
|
3
|
+
SegtFormat allows for handling a subset of features found in common page layout formats (PAGE, ALTO) with a focus on HTR tasks:
|
|
4
|
+
|
|
5
|
+
+ 1 label document = 1 image = 1 page[^1]
|
|
6
|
+
+ two types of objects only: regions (as bounding boxes) and lines (baselines and polygons)
|
|
7
|
+
+ conversion between common segmentation metadata formats ([Page](http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15), [ALTO](http://www.loc.gov/standards/alto/v4/alto-4-2.xsd)).
|
|
8
|
+
+ custom JSON representation documented in our [schema](doc/seg_schema.json): the dictionary allows for easy structural transformations (flattening, region and/or line extraction) or for semantic transformations (line-to-region assignments, boundaries).
|
|
9
|
+
+ for metadata only: the image is not needed.
|
|
10
|
+
+ validation options (PAGE, JSON)
|
|
11
|
+
+ no fancy visuals: however a page segmentation can be conveniently rendered in ASCII on the terminal, for diagnosis purpose.
|
|
12
|
+
|
|
13
|
+
[^1]: Neither of the commonly used formats enforces the 1-1-1 cardinality: with PAGE, 1 document may label _n_ images (each corresponding to exactly 1 `Page` element); in ALTO, 1 document labels exactly 1 image and possibly _n_ pages within it.
|
|
14
|
+
|
|
15
|
+

|
|
16
|
+
|
|
17
|
+
## How to install
|
|
18
|
+
|
|
19
|
+
<!--
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install segtformats
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
or from GitHub:
|
|
26
|
+
-->
|
|
27
|
+
|
|
28
|
+
From GitHub:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
git clone git@github.com:nicolasrenet/segtformats.git
|
|
32
|
+
cd segtformats
|
|
33
|
+
pip install .
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
From PyPi:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
TBA
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
## How to use
|
|
44
|
+
|
|
45
|
+
### Library functions: examples
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
from segtformats import segtformats as sgf
|
|
49
|
+
|
|
50
|
+
# Alto to PageXML stream
|
|
51
|
+
page_xml_str = sgf.alto_to_page_xml('tests/data/217_d9c7f_default.alto.xml')
|
|
52
|
+
|
|
53
|
+
# Alto to PageXML file
|
|
54
|
+
sgf.alto_to_page_xml('tests/data/217_d9c7f_default.alto.xml', output_file='217_d9c7f_default.alto.page.xml')
|
|
55
|
+
|
|
56
|
+
# Alto to JSON dictionary
|
|
57
|
+
segmentation_dict = sgf.alto_to_segmentation_dict('tests/data/217_d9c7f_default.alto.xml')
|
|
58
|
+
# validation
|
|
59
|
+
assert sgf.json_validate( segmentation_dict )
|
|
60
|
+
|
|
61
|
+
# ASCII rendition
|
|
62
|
+
print(sgf.anyseg_to_ascii('tests/data/btv1b84473026_f25.chocomufin.xml', lines=1, scale_hw=.8))
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
### Command-line utilities
|
|
67
|
+
|
|
68
|
+
+ `alto_to_page_xml`: Alto → Page conversion. Eg.
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
alto_to_page_xml tests/data/217_d9c7f_default.alto.xml
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
+ `alto_to_json`: Alto → JSON conversion. Eg.
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
alto_to_json tests/data/217_d9c7f_default.alto.xml
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
+ `page_xml_to_json`: Page → JSON conversion. Eg.
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
page_xml_to_json tests/data/217_d9c7f_default.page.xml
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
+ `anyseg_to_json`: Detect input format and convert to JSON. Eg.
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
anyseg_to_json --file_paths tests/data/*.xml
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
+ `json_to_page_xml`: JSON → Page conversion.
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
json_to_page_xml tests/data/217_d9c7f_default.json
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
+ `anyseg_doctor`: Various transformations on JSON metadata, including repairs (region boundaries, line-to-region assignments).
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
# diagnosis-only by default (dry run)
|
|
102
|
+
anyseg_doctor tests/data/217_d9c7f_default.page.xml
|
|
103
|
+
# repairing an ALTO file, with PAGE output
|
|
104
|
+
anyseg_doctor --repair --output_format page --output_suffix .fixed.xml tests/data/217_d9c7f_default.alto.xml
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
+ `anyseg_to_ascii`: Render segmentation metadata on the terminal. Eg.
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
anyseg_to_ascii --file_paths tests/data/*.xml
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
+ `page_xml_split`: Split multi-page PAGE label into as many files (i.e. one label per image). Eg.
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
page_xml_split --file_paths tests/data/2923.xml
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Get the help on any `<command>` with
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
python3 -m segtformats.<command> -h
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
## TODO:
|
|
127
|
+
|
|
128
|
+
+ Page → Alto
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "segtformats"
|
|
3
|
+
version = "0.3"
|
|
4
|
+
authors = [
|
|
5
|
+
{ name="Nicolas Renet", email="nprenet@gmail.com" },
|
|
6
|
+
]
|
|
7
|
+
description = "Segmentation data formats: conversion tools"
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
requires-python = ">=3.10"
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Programming Language :: Python :: 3",
|
|
12
|
+
"Operating System :: MacOS",
|
|
13
|
+
"Operating System :: POSIX :: Linux"
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
license="MIT"
|
|
17
|
+
license-files=["LICENSE"]
|
|
18
|
+
|
|
19
|
+
dependencies = [
|
|
20
|
+
"numpy",
|
|
21
|
+
"unidecode",
|
|
22
|
+
"shapely",
|
|
23
|
+
"jsonschema",
|
|
24
|
+
"lxml",
|
|
25
|
+
"fargv",
|
|
26
|
+
"unidecode"
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[project.urls]
|
|
30
|
+
Homepage = "https://github.com/nicolasrenet/segtformats"
|
|
31
|
+
|
|
32
|
+
[project.scripts]
|
|
33
|
+
alto_to_json = "segtformats.alto_to_json:main"
|
|
34
|
+
alto_to_page_xml = "segtformats.alto_to_page_xml:main"
|
|
35
|
+
page_xml_to_json = "segtformats.page_xml_to_json:main"
|
|
36
|
+
json_to_page_xml = "segtformats.json_to_page_xml:main"
|
|
37
|
+
anyseg_to_ascii = "segtformats.anyseg_to_ascii:main"
|
|
38
|
+
anyseg_to_json = "segtformats.anyseg_to_json:main"
|
|
39
|
+
json_doctor = "segtformats.json_doctor:main"
|
|
40
|
+
anyseg_doctor = "segtformats.anyseg_doctor:main"
|
|
41
|
+
page_xml_split = "segtformats.page_xml_split:main"
|
|
42
|
+
|
|
43
|
+
[build-system]
|
|
44
|
+
requires = ["setuptools"]
|
|
45
|
+
build-backend = "setuptools.build_meta"
|
|
46
|
+
|
segtformats-0.3/setup.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Manipulating segmentation formats (Alto, Page, JSON)
|
|
3
|
+
|
|
4
|
+
Command-line utilities
|
|
5
|
+
|
|
6
|
+
- `alto_to_page_xml`: Alto → Page conversion.
|
|
7
|
+
- `alto_to_json`: Page → JSON conversion.
|
|
8
|
+
- `page_xml_to_json`: Page → JSON conversion.
|
|
9
|
+
- `anyseg_to_json`: Detect input format and convert to JSON.
|
|
10
|
+
- `json_to_page_xml`: JSON → Page conversion.
|
|
11
|
+
- `json_to_json`: Various transformations on JSON metadata, including repairs (region boundaries, line-to-region assignments).
|
|
12
|
+
- `anyseg_to_ascii`: Render segmentation metadata on the terminal.
|
|
13
|
+
|
|
14
|
+
Help on any command with:
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
python3 -m segtformats.<command> -h
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
How to use the library functions:
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
from segtformats import segtformats as sgf
|
|
24
|
+
help( sgf )
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
"""
|
|
28
|
+
import logging
|
|
29
|
+
|
|
30
|
+
logging_format="%(asctime)s - %(levelname)s: %(filename)s: %(funcName)s - %(message)s"
|
|
31
|
+
logging_levels = {0: logging.ERROR, 1: logging.WARNING, 2: logging.INFO, 3: logging.DEBUG }
|
|
32
|
+
logging.basicConfig( level=logging.INFO, format=logging_format, force=True )
|
|
33
|
+
logger = logging.getLogger(__name__)
|
|
34
|
+
|
|
35
|
+
def set_logging_level( verbosity ):
|
|
36
|
+
if verbosity != 2:
|
|
37
|
+
logging.basicConfig( level=logging_levels[verbosity], format=logging_format, force=True )
|
|
38
|
+
|
|
39
|
+
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
Alto -> JSON conversion.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import sys
|
|
7
|
+
import json
|
|
8
|
+
import re
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
import fargv
|
|
12
|
+
fargv.util.verbosity=1 # for --help to display option descriptions
|
|
13
|
+
from fargv import FargvPositional, FargvChoice
|
|
14
|
+
from jsonschema import validate
|
|
15
|
+
|
|
16
|
+
from . import segtformats as sgf
|
|
17
|
+
from . import set_logging_level, logger
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
if __name__ == '__main__':
|
|
21
|
+
main()
|
|
22
|
+
|
|
23
|
+
def main():
|
|
24
|
+
|
|
25
|
+
p = {
|
|
26
|
+
'file_paths': FargvPositional(default=[]),
|
|
27
|
+
'out': FargvChoice(['','auto'], description="Set to 'auto' for output to filename <input stem>.<output_suffix>; leave empty for standard output."),
|
|
28
|
+
'input_suffix': '.xml',
|
|
29
|
+
'output_suffix': '.json',
|
|
30
|
+
'overwrite_existing': (False, "Overwrite an existing file."),
|
|
31
|
+
"comment": ('',"A text string to be added to the <Comments> elt."),
|
|
32
|
+
"verbosity": FargvChoice(['2','0','1','3'], description="Verbosity levels: 0 (quiet), 1 (WARNING), 2 (INFO-default), 3 (DEBUG)"),
|
|
33
|
+
"repair": (False, "Repair a faulty dictionary (reassign lines to regions, fix region bounding boxes"),
|
|
34
|
+
"validate": (False, "Validate against a JSON schema."),
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
args, _ = fargv.parse( p )
|
|
38
|
+
|
|
39
|
+
set_logging_level( int(args.verbosity) )
|
|
40
|
+
|
|
41
|
+
for file_path in args.file_paths:
|
|
42
|
+
|
|
43
|
+
logger.debug(file_path)
|
|
44
|
+
|
|
45
|
+
segdict = sgf.alto_to_segmentation_dict( file_path )
|
|
46
|
+
|
|
47
|
+
if args.repair:
|
|
48
|
+
segdict = sgf.json_doctor( segdict, verbose=args.verbose )
|
|
49
|
+
|
|
50
|
+
# Raise an exception if invalid
|
|
51
|
+
if args.validate:
|
|
52
|
+
sgf.json_validate( segdict )
|
|
53
|
+
|
|
54
|
+
segdict_str = json.dumps( segdict, indent=2 )
|
|
55
|
+
if not args.out:
|
|
56
|
+
print( segdict_str )
|
|
57
|
+
continue
|
|
58
|
+
|
|
59
|
+
out_path = ''
|
|
60
|
+
if args.out == 'auto':
|
|
61
|
+
if not re.search( r'{}$'.format(args.input_suffix), Path(file_path).name):
|
|
62
|
+
logger.warning(f"Input file path '{Path(file_path).name}' does not match input suffix '{args.input_suffix}': output aborted.")
|
|
63
|
+
continue
|
|
64
|
+
out_path = re.sub(r'{}$'.format( args.input_suffix ), args.output_suffix, file_path )
|
|
65
|
+
logger.debug(f"Output file = {out_path}")
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
if not args.overwrite_existing and Path(out_path.exists():
|
|
69
|
+
logger.info("File {} exists: skipping.".format( out_path ))
|
|
70
|
+
continue
|
|
71
|
+
with open(out_path, 'w') as json_outf:
|
|
72
|
+
json_outf.write( segdict_str )
|
|
73
|
+
return 0
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
ALTO → Page conversion tool with embedded XSL stylesheet.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import sys
|
|
7
|
+
import re
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
import fargv
|
|
11
|
+
fargv.util.verbosity=1 # for --help to display option descriptions
|
|
12
|
+
from fargv import FargvPositional, FargvChoice
|
|
13
|
+
from . import segtformats as sgf
|
|
14
|
+
from . import set_logging_level, logger
|
|
15
|
+
|
|
16
|
+
if __name__ == '__main__':
|
|
17
|
+
main()
|
|
18
|
+
|
|
19
|
+
def main():
|
|
20
|
+
|
|
21
|
+
p = {
|
|
22
|
+
'file_paths': FargvPositional(default=[], description="Input file (ALTO)."),
|
|
23
|
+
'out': FargvChoice(['','auto'], description="Set to 'auto' for output to filename <input stem>.<output_suffix>; leave empty for standard output."),
|
|
24
|
+
'input_suffix': ('.xml', "Input file suffix."),
|
|
25
|
+
'output_suffix': ('.xml', "Output file suffix."),
|
|
26
|
+
'overwrite_existing': (False, "Overwrite an existing output file."),
|
|
27
|
+
"verbosity": FargvChoice(['2','0','1','3'], description="Verbosity levels: 0 (quiet), 1 (WARNING), 2 (INFO-default), 3 (DEBUG)"),
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
args, _ = fargv.parse( p )
|
|
31
|
+
|
|
32
|
+
set_logging_level( int(args.verbosity) )
|
|
33
|
+
|
|
34
|
+
for file_path in args.file_paths:
|
|
35
|
+
|
|
36
|
+
if not args.out:
|
|
37
|
+
sgf.alto_to_page_xml( file_path )
|
|
38
|
+
continue
|
|
39
|
+
out_path = ''
|
|
40
|
+
if args.out == 'auto':
|
|
41
|
+
if not re.search( r'{}$'.format(args.input_suffix), Path(file_path).name):
|
|
42
|
+
logger.warning(f"Input file path '{Path(file_path).name}' does not match input suffix '{args.input_suffix}': output aborted.")
|
|
43
|
+
continue
|
|
44
|
+
out_path = re.sub(r'{}$'.format( args.input_suffix ), args.output_suffix, file_path )
|
|
45
|
+
logger.debug(f"Output file = {out_path}")
|
|
46
|
+
|
|
47
|
+
if not args.overwrite_existing and Path(out_path).exists():
|
|
48
|
+
logger.info("File {} exists: skipping.".format( out_path ))
|
|
49
|
+
continue
|
|
50
|
+
sgf.alto_to_page_xml( file_path, pagexml_filename=out_path )
|
|
51
|
+
|
|
52
|
+
return 0
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
#
|
|
3
|
+
# Repair segmentation meta-data:
|
|
4
|
+
# [ Alto, Page ] → [ JSON, Page ]
|
|
5
|
+
#
|
|
6
|
+
#
|
|
7
|
+
|
|
8
|
+
import sys
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
import json
|
|
11
|
+
import re
|
|
12
|
+
|
|
13
|
+
import fargv
|
|
14
|
+
fargv.util.verbosity=1 # for --help to display option descriptions
|
|
15
|
+
from fargv import FargvChoice, FargvPositional
|
|
16
|
+
|
|
17
|
+
from . import segtformats as sgf
|
|
18
|
+
from . import set_logging_level, logger
|
|
19
|
+
|
|
20
|
+
if __name__ == '__main__':
|
|
21
|
+
main()
|
|
22
|
+
|
|
23
|
+
def main():
|
|
24
|
+
|
|
25
|
+
p = {
|
|
26
|
+
"file_paths": FargvPositional(default=[], description="A JSON line segmentation file (e.g <prefix>.lines.pred.json)."),
|
|
27
|
+
'out': FargvChoice(['','auto'], description="Set to 'auto' for output to filename <input stem>.<output_suffix>; leave empty for standard output."),
|
|
28
|
+
"output_suffix": ('',"If empty, output file's suffix is determined by output format (.json or .xml)"),
|
|
29
|
+
"input_suffix": ('',"If empty, input file's suffix is determined by detected input format (.json or .xml)"),
|
|
30
|
+
"overwrite_existing": (False, "Do not overwrite existing output file"),
|
|
31
|
+
"repair": (False, "If True, fix semantic errors in the segmentation (line-to-region assignment, region boundaries)."),
|
|
32
|
+
"diagnose": (True, "If True, detect potential issue with the segmentation data, with a dry-run of json_doctor."),
|
|
33
|
+
"validate": False,
|
|
34
|
+
"output_format": FargvChoice(['auto', 'json', 'page'], description="Output format; if empty, same as detected input format.)"),
|
|
35
|
+
"verbosity": FargvChoice(['2','0','1','3'], description="Verbosity levels: 0 (quiet), 1 (WARNING), 2 (INFO-default), 3 (DEBUG)"),
|
|
36
|
+
}
|
|
37
|
+
args, _ = fargv.parse( p )
|
|
38
|
+
|
|
39
|
+
set_logging_level( int(args.verbosity) )
|
|
40
|
+
|
|
41
|
+
format_to_suffix = { sgf.SegFormat.PAGE: '.xml', sgf.SegFormat.ALTO: '.xml', sgf.SegFormat.JSON: '.json' }
|
|
42
|
+
for file_path in args.file_paths:
|
|
43
|
+
|
|
44
|
+
logger.info(f"{file_path}:")
|
|
45
|
+
|
|
46
|
+
# we need an access to the segmentation format
|
|
47
|
+
segdict, segmentation_format = sgf.anyseg_to_dict( file_path )
|
|
48
|
+
|
|
49
|
+
if not segdict:
|
|
50
|
+
print(f"Could not parse a dictionary from {segfile_path}. Skipping.")
|
|
51
|
+
continue
|
|
52
|
+
|
|
53
|
+
if args.diagnose and not args.repair:
|
|
54
|
+
segdict = sgf.json_doctor( segdict, dry_run=True )
|
|
55
|
+
logger.info("Diagnosis only: set --repair for repairing the file.")
|
|
56
|
+
continue
|
|
57
|
+
|
|
58
|
+
if args.repair:
|
|
59
|
+
segdict = sgf.json_doctor( segdict )
|
|
60
|
+
|
|
61
|
+
if args.validate:
|
|
62
|
+
if not sgf.json_validate( segdict ):
|
|
63
|
+
continue
|
|
64
|
+
|
|
65
|
+
segdict_str = json.dumps( segdict, indent=2 )
|
|
66
|
+
if not args.out:
|
|
67
|
+
print( segdict_str )
|
|
68
|
+
continue
|
|
69
|
+
|
|
70
|
+
output_format = None
|
|
71
|
+
# What is the output format?
|
|
72
|
+
if args.output_format == 'page':
|
|
73
|
+
output_format = sgf.SegFormat.PAGE
|
|
74
|
+
elif args.output_format == 'json':
|
|
75
|
+
output_format = sgf.SegFormat.JSON
|
|
76
|
+
elif args.output_format == 'auto':
|
|
77
|
+
output_format = segmentation_format
|
|
78
|
+
# What is the output file name?
|
|
79
|
+
output_suffix = args.output_suffix if args.output_suffix else format_to_suffix[ output_format ]
|
|
80
|
+
input_suffix = args.input_suffix if args.input_suffix else format_to_suffix[ segmentation_format ]
|
|
81
|
+
|
|
82
|
+
out_path = ''
|
|
83
|
+
if args.out == 'auto':
|
|
84
|
+
if not re.search( r'{}$'.format( input_suffix ), Path( file_path ).name):
|
|
85
|
+
logger.warning(f"Input file path '{Path(file_path).name}' does not match input suffix '{input_suffix}': output aborted.")
|
|
86
|
+
continue
|
|
87
|
+
out_path = re.sub(r'{}'.format(input_suffix), output_suffix, file_path )
|
|
88
|
+
logger.debug(f"Output file = {out_path}")
|
|
89
|
+
|
|
90
|
+
if not args.overwrite_existing and Path(out_path).exists():
|
|
91
|
+
logger.info( "File {} exists): skipping.".format(out_path))
|
|
92
|
+
continue
|
|
93
|
+
if output_format == sgf.SegFormat.JSON:
|
|
94
|
+
with open(out_path, 'w') as json_outf:
|
|
95
|
+
json_outf.write( segdict_str )
|
|
96
|
+
elif output_format == sgf.SegFormat.PAGE:
|
|
97
|
+
sgf.page_xml_from_segmentation_dict( segdict, output_file=out_path )
|
|
98
|
+
|
|
99
|
+
logger.info(f"→ {out_path}")
|
|
100
|
+
|
|
101
|
+
return 0
|
|
102
|
+
|