Thibault Clérice commited on
Commit
e8fb686
1 Parent(s): 97c3808

Overcome issue with None in sequences

Browse files
.gitignore CHANGED
@@ -1,2 +1,3 @@
1
  .idea
2
  altos
 
 
1
  .idea
2
  altos
3
+ *.tar.gz
data/dev/metadata.jsonl CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:18761d7264695244282cf1844818b5c0fc4da85f512be426e0dd7d31f9d14f84
3
- size 4739767
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6888e0bf991ed356de2cc4650b3fb57a079ec86e43ab212caed9012b4450625a
3
+ size 4736329
data/test/metadata.jsonl CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:11e3dcff16fb0b2cc23479810aefbb53bae38e31df30bcc5423afef72b2f9f2e
3
- size 6090128
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8f298de3008445f048877d1c4e5576204a5b8e38f9bfa6a42cb73a76292b333d
3
+ size 6087022
data/train/metadata.jsonl CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:de554e3cb610117a380be15806122cb400219757ba769f2adb3440099dcbf31c
3
- size 35567787
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:71b19924362cbc2036efa09430b0e6e692b17df6d0f8b22b767d3a26d10496f2
3
+ size 35541933
src/alto.py CHANGED
@@ -3,7 +3,7 @@ import os
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
@@ -12,7 +12,7 @@ import itertools
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
 
@@ -89,28 +89,27 @@ class Block:
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(
@@ -120,4 +119,3 @@ for split in ds:
120
  tags=tags
121
  )
122
  )
123
- break
 
3
  from datasets import load_dataset
4
  from dataclasses import dataclass, field
5
  import tqdm.auto as tqdm
6
+ from typing import List, Dict
7
  from jinja2 import Environment, FileSystemLoader
8
  from shapely.geometry import Polygon
9
  import itertools
 
12
  env = Environment(loader=FileSystemLoader('.'))
13
  template = env.get_template('template.xml')
14
 
15
+ ds = load_dataset("../data")
16
 
17
  RATIO = .05
18
 
 
89
 
90
 
91
  for split in ds:
 
92
  os.makedirs(f"altos/{split}", exist_ok=True)
93
  for image_idx, image in enumerate(tqdm.tqdm(ds[split])):
94
  blocks: Dict[str, Block] = {}
95
  tags: Dict[str, Tag] = {}
96
+ objs = image["objects"]
97
+ for i, idx in enumerate(objs["id"]):
98
+ if objs["category"][i] and objs["category"][i] not in tags:
99
+ tags[objs["category"][i]] = Tag(f"LABEL_{len(tags)}", objs["category"][i], objs["type"][i])
100
+
101
+ if objs["type"][i] == "block":
102
+ blocks[idx] = Block(idx, objs["bbox"][i], objs["polygons"][i], objs["category"][i])
 
 
 
103
  else:
104
+ if objs["parent"][i] == "":
105
+ blocks[""] = Block(None, [], [], "")
106
+ if objs["baseline"][i] is None:
107
+ print(f"Line broken ? {image['shelfmark']} {idx} {objs['polygons'][i]}")
108
+ continue
109
+ blocks[objs["parent"][i]].lines.append(
110
+ Line(idx, objs["bbox"][i], objs["polygons"][i], objs["category"][i], objs['baseline'][i])
111
  )
112
+ image["image"].convert('RGB').save(f"./altos/{split}/image-{image_idx:04}.jpg")
113
  with open(f"./altos/{split}/image-{image_idx:04}.xml", "w") as f:
114
  f.write(
115
  template.render(
 
119
  tags=tags
120
  )
121
  )
 
src/template.xml CHANGED
@@ -24,7 +24,7 @@
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 %}
 
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|join(" ")}}">
28
  <Shape><Polygon POINTS="{{ line.simple_polygon|join(" ") }}" /></Shape>
29
  </TextLine>
30
  {% endfor %}