Thibault Clérice commited on
Commit
4d1c378
1 Parent(s): 133cb35

Update model with baseline

Browse files
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ .idea
2
+ altos
data/dev/metadata.jsonl CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:bfafc30e06a853ea5f8021ba71264e20d8bd077569678d8fc513e599cb39e346
3
- size 4428268
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e7a08de61cf70feeb2797c44f55eff3645eb83dae61857eefeb637d2d6274b74
3
+ size 4433417
data/test/metadata.jsonl CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:133124bfe738c34f3bce3095212a798266d8ebaa8cfa52ca6b5a6c66c9999169
3
- size 5747430
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b47a9740332f97e41784c67d48ecc73378e9d22afef7d4e97ba3a257db1a54cf
3
+ size 5752239
data/train/metadata.jsonl CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:582da2a53b6f56cc5a896907e43c13e68d917f1ff3a6a67db256bb4d3b30ec7b
3
- size 33576212
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d7eb400c3e995dd0787a022521b5a469318f08ab06dbd21523f03b4be4f18506
3
+ size 33611067
src/__init__.py ADDED
File without changes
src/alto.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ from datasets import load_dataset
4
+ from dataclasses import dataclass, field
5
+ import tqdm.auto as tqdm
6
+ from typing import List, Dict, Set
7
+ from jinja2 import Environment, FileSystemLoader
8
+ from shapely.geometry import Polygon
9
+ import itertools
10
+
11
+ # Load the Jinja2 template
12
+ env = Environment(loader=FileSystemLoader('.'))
13
+ template = env.get_template('template.xml')
14
+
15
+ ds = load_dataset("CATMuS/medieval-segmentation")
16
+
17
+ RATIO = .05
18
+
19
+
20
+ @dataclass
21
+ class Tag:
22
+ idx: str
23
+ label: str
24
+ element_type: str
25
+
26
+
27
+ @dataclass
28
+ class Line:
29
+ idx: str
30
+ bbox: List[int]
31
+ polygons: List[int]
32
+ category: str
33
+ baseline: List[int]
34
+
35
+ @property
36
+ def simple_polygon(self):
37
+ x = list(
38
+ map(
39
+ int,
40
+ itertools.chain(
41
+ *Polygon(
42
+ list(zip(self.polygons[::2], self.polygons[1::2])) # Flat list to list of points
43
+ ).simplify(RATIO * self.height).exterior.coords
44
+ )
45
+ )
46
+ )[:-2] # Polygon repeats the starting point
47
+ return x
48
+
49
+ @property
50
+ def hpos(self):
51
+ return self.bbox[0]
52
+
53
+ @property
54
+ def vpos(self):
55
+ return self.bbox[1]
56
+
57
+ @property
58
+ def width(self):
59
+ return self.bbox[2] - self.bbox[0]
60
+
61
+ @property
62
+ def height(self):
63
+ return self.bbox[3] - self.bbox[1]
64
+
65
+
66
+ @dataclass
67
+ class Block:
68
+ idx: str
69
+ bbox: List[int]
70
+ polygons: List[int]
71
+ category: str
72
+ lines: List[Line] = field(default_factory=list)
73
+
74
+ @property
75
+ def hpos(self):
76
+ return self.bbox[0]
77
+
78
+ @property
79
+ def vpos(self):
80
+ return self.bbox[1]
81
+
82
+ @property
83
+ def width(self):
84
+ return self.bbox[2] - self.bbox[0]
85
+
86
+ @property
87
+ def height(self):
88
+ return self.bbox[3] - self.bbox[1]
89
+
90
+
91
+ for split in ds:
92
+ print(f"Split: {split}")
93
+ os.makedirs(f"altos/{split}", exist_ok=True)
94
+ for image_idx, image in enumerate(tqdm.tqdm(ds[split])):
95
+ blocks: Dict[str, Block] = {}
96
+ tags: Dict[str, Tag] = {}
97
+ print(image)
98
+ for idx, bbox, polygons, category, typ, parent, baseline in zip(*[
99
+ image["objects"][key]
100
+ for key in ['id', 'bbox', 'polygons', 'category', 'type', 'parent', "baselines"]
101
+ ]):
102
+ if category and category not in tags:
103
+ tags[category] = Tag(f"LABEL_{len(tags)}", category, typ)
104
+
105
+ if typ == "block":
106
+ blocks[idx] = Block(idx, bbox, polygons, category)
107
+ else:
108
+ if parent == None:
109
+ blocks[None] = Block(None, [], [], "")
110
+ blocks[parent].lines.append(
111
+ Line(idx, bbox, polygons, category)
112
+ )
113
+ image["image"].save(f"./altos/{split}/image-{image_idx:04}.jpg")
114
+ with open(f"./altos/{split}/image-{image_idx:04}.xml", "w") as f:
115
+ f.write(
116
+ template.render(
117
+ blocks=blocks,
118
+ image=image,
119
+ path=f"image-{image_idx:04}.jpg",
120
+ tags=tags
121
+ )
122
+ )
123
+ break
src/template.xml ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <alto xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2
+ xmlns="http://www.loc.gov/standards/alto/ns-v4#"
3
+ xsi:schemaLocation="http://www.loc.gov/standards/alto/ns-v4# http://www.loc.gov/standards/alto/v4/alto-4-3.xsd">
4
+ <Description>
5
+ <MeasurementUnit>pixel</MeasurementUnit>
6
+ <sourceImageInformation>
7
+ <fileName>{{ path }}</fileName>
8
+ </sourceImageInformation>
9
+ <OCRProcessing>
10
+ <ocrProcessingStep>
11
+ <processingSoftware>
12
+ <softwareName>Kraken</softwareName>
13
+ </processingSoftware>
14
+ </ocrProcessingStep>
15
+ </OCRProcessing>
16
+ </Description>
17
+ <Tags>
18
+ {% for tag in tags.values() %}
19
+ <OtherTag ID="{{tag.idx}}" LABEL="{{tag.label}}" DESCRIPTION="{{tag.element_type}} type {{tag.label}}"/>{% endfor %}
20
+ </Tags>
21
+ <Layout>
22
+ <Page WIDTH="{{ image.width }}" HEIGHT="{{ image.height }}" PHYSICAL_IMG_NR="1">
23
+ <PrintSpace HPOS="0" VPOS="0" WIDTH="{{ image.width }}" HEIGHT="{{ image.height }}">
24
+ {% for bid, block in blocks.items() %}
25
+ <TextBlock ID="{{ bid or 'anonymous' }}" {%if block.category%}TAGREFS="{{tags[block.category].idx}}" {% endif%} {% if block.bbox %} HPOS="{{ block.hpos }}" VPOS="{{ block.vpos }}" WIDTH="{{ block.width }}" HEIGHT="{{ block.height }}" {% endif %}>
26
+ {%-if block.bbox %}<Shape><Polygon POINTS="{{ block.polygons|join(" ") }}" /></Shape>{% endif -%}
27
+ {% for line in block.lines %}<TextLine ID="{{ line.idx }}" {%if block.category%}TAGREFS="{{tags[line.category].idx}}" {% endif%} HPOS="{{ line.hpos }}" VPOS="{{ line.vpos }}" WIDTH="{{ line.width }}" HEIGHT="{{ line.height }}" BASELINE="{{line.baseline}}">
28
+ <Shape><Polygon POINTS="{{ line.simple_polygon|join(" ") }}" /></Shape>
29
+ </TextLine>
30
+ {% endfor %}
31
+ </TextBlock>
32
+ {% endfor %}
33
+ </PrintSpace>
34
+ </Page>
35
+ </Layout>
36
+ </alto>