Skip to content

Commit

Permalink
extract-pages: expose colordict as parameter (with PageViewer scheme …
Browse files Browse the repository at this point in the history
…as default)
  • Loading branch information
bertsky committed Feb 6, 2021
1 parent 60fc74d commit b8ea41a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
22 changes: 13 additions & 9 deletions ocrd_segment/extract_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
# (from prima-page-viewer/src/org/primaresearch/page/viewer/ui/render/PageContentColors,
# but added alpha channel to also discern subtype, if not visually;
# ordered such that overlaps still allows maximum separation)
# (Not used any more; now as default to ocrd-tool.json parameter.)
# pragma pylint: disable=bad-whitespace
CLASSES = {
'': 'FFFFFF00',
Expand Down Expand Up @@ -136,13 +137,14 @@ def process(self):
bin_image_grp = file_groups[0]
LOG.info("No output file group for binarized images specified, falling back to output filegrp '%s'", bin_image_grp)
self.output_file_grp = file_groups[0]
classes = self.parameter['colordict']

# COCO: init data structures
images = list()
annotations = list()
categories = list()
i = 0
for cat, color in CLASSES.items():
for cat, color in classes.items():
# COCO format does not allow alpha channel
color = (int(color[0:2], 16),
int(color[2:4], 16),
Expand Down Expand Up @@ -198,18 +200,20 @@ def process(self):
else:
raise
page_image_dbg = Image.new(mode='RGBA', size=page_image.size,
color='#' + CLASSES[''])
if page.get_Border():
color='#' + classes[''])
if 'Border' not in classes:
pass
elif page.get_Border():
polygon = coordinates_of_segment(
page.get_Border(), page_image, page_coords).tolist()
ImageDraw.Draw(page_image_dbg).polygon(
list(map(tuple, polygon)),
fill='#' + CLASSES['Border'])
fill='#' + classes['Border'])
else:
page_image_dbg.paste('#' + CLASSES['Border'],
page_image_dbg.paste('#' + classes['Border'],
(0, 0, page_image.width, page_image.height))
regions = dict()
for name in CLASSES.keys():
for name in classes.keys():
if not name or name == 'Border' or ':' in name:
# no subtypes here
continue
Expand Down Expand Up @@ -279,7 +283,7 @@ def process(self):
# draw region:
ImageDraw.Draw(page_image_dbg).polygon(
list(map(tuple, polygon)),
fill='#' + CLASSES[(rtype + ':' + subrtype) if subrtype else rtype])
fill='#' + classes[(rtype + ':' + subrtype) if subrtype else rtype])
# COCO: add annotations
i += 1
annotations.append(
Expand Down Expand Up @@ -334,7 +338,7 @@ def process(self):
file_id = dbg_image_grp + '.colordict.json'
LOG.info('Writing colordict file "%s" in .', file_id)
with open(file_id, 'w') as out:
json.dump(dict(('#' + col, name)
for name, col in CLASSES.items()
json.dump(dict((col, name)
for name, col in classes.items()
if name),
out)
57 changes: 57 additions & 0 deletions ocrd_segment/ocrd-tool.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,63 @@
"type": "boolean",
"default": true,
"description": "Add alpha channels with segment masks to the images"
},
"colordict": {
"type": "object",
"default": {
"": "FFFFFF00",
"Border": "FFFFFFFF",
"TableRegion": "8B4513FF",
"AdvertRegion": "4682B4FF",
"ChemRegion": "FF8C00FF",
"MusicRegion": "9400D3FF",
"MapRegion": "9ACDD2FF",
"TextRegion": "0000FFFF",
"TextRegion:paragraph": "0000FFFA",
"TextRegion:heading": "0000FFF5",
"TextRegion:caption": "0000FFF0",
"TextRegion:header": "0000FFEB",
"TextRegion:footer": "0000FFE6",
"TextRegion:page-number": "0000FFE1",
"TextRegion:drop-capital": "0000FFDC",
"TextRegion:credit": "0000FFD7",
"TextRegion:floating": "0000FFD2",
"TextRegion:signature-mark": "0000FFCD",
"TextRegion:catch-word": "0000FFC8",
"TextRegion:marginalia": "0000FFC3",
"TextRegion:footnote": "0000FFBE",
"TextRegion:footnote-continued": "0000FFB9",
"TextRegion:endnote": "0000FFB4",
"TextRegion:TOC-entry": "0000FFAF",
"TextRegion:list-label": "0000FFA5",
"TextRegion:other": "0000FFA0",
"ChartRegion": "800080FF",
"ChartRegion:bar": "800080FA",
"ChartRegion:line": "800080F5",
"ChartRegion:pie": "800080F0",
"ChartRegion:scatter": "800080EB",
"ChartRegion:surface": "800080E6",
"ChartRegion:other": "800080E1",
"GraphicRegion": "008000FF",
"GraphicRegion:logo": "008000FA",
"GraphicRegion:letterhead": "008000F0",
"GraphicRegion:decoration": "008000EB",
"GraphicRegion:frame": "008000E6",
"GraphicRegion:handwritten-annotation": "008000E1",
"GraphicRegion:stamp": "008000DC",
"GraphicRegion:signature": "008000D7",
"GraphicRegion:barcode": "008000D2",
"GraphicRegion:paper-grow": "008000CD",
"GraphicRegion:punch-hole": "008000C8",
"GraphicRegion:other": "008000C3",
"ImageRegion": "00CED1FF",
"LineDrawingRegion": "B8860BFF",
"MathsRegion": "00BFFFFF",
"NoiseRegion": "FF0000FF",
"SeparatorRegion": "FF00FFFF",
"UnknownRegion": "646464FF",
"CustomRegion": "637C81FF"},
"description": "Mapping from region types to extract to color values in the output mask images and COCO; color must be encoded hexadecimal (e.g. '00FF00'); region type equals the element name in PAGE-XML, optionally followed by a colon and a subtype (e.g. 'TextRegion:paragraph'; unmapped region types will be ignored (i.e. treated as background)). Default is PageViewer color scheme. Cf. colordict parameter of ocrd-segment-from-masks."
}
}
},
Expand Down

0 comments on commit b8ea41a

Please sign in to comment.