Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segmentation on raw images #144

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions ocrd_tesserocr/crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,13 @@ def process(self):
page_image, page_xywh, page_image_info = self.workspace.image_from_page(
page, page_id,
# image must not have been cropped already,
# abort if no such image can be produced:
feature_filter='cropped')
# abort if no such image can be produced;
# moreover, for some reason, external binarization
# degrades Tesseract segmentation quality
# (probably because C_OUTLINE::ComputeEdgeOffsets,
# which needs the greyscale image, is more
# accurate than C_OUTLINE::ComputeBinaryOffsets):
feature_filter='cropped,binarized')
if self.parameter['dpi'] > 0:
dpi = self.parameter['dpi']
LOG.info("Page '%s' images will use %d DPI from parameter override", page_id, dpi)
Expand Down
16 changes: 14 additions & 2 deletions ocrd_tesserocr/segment_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ def process(self):
page = pcgts.get_Page()

page_image, page_coords, page_image_info = self.workspace.image_from_page(
page, page_id)
page, page_id,
# for some reason, external binarization
# degrades Tesseract segmentation quality
# (probably because C_OUTLINE::ComputeEdgeOffsets,
# which needs the greyscale image, is more
# accurate than C_OUTLINE::ComputeBinaryOffsets):
feature_filter='binarized')
if self.parameter['dpi'] > 0:
dpi = self.parameter['dpi']
LOG.info("Page '%s' images will use %d DPI from parameter override", page_id, dpi)
Expand All @@ -92,7 +98,13 @@ def process(self):
LOG.warning('keeping existing TextLines in region "%s"', region.id)
LOG.debug("Detecting lines in region '%s'", region.id)
region_image, region_coords = self.workspace.image_from_segment(
region, page_image, page_coords)
region, page_image, page_coords,
# for some reason, external binarization
# degrades Tesseract segmentation quality
# (probably because C_OUTLINE::ComputeEdgeOffsets,
# which needs the greyscale image, is more
# accurate than C_OUTLINE::ComputeBinaryOffsets):
feature_filter='binarized')
tessapi.SetImage(region_image)
for line_no, component in enumerate(tessapi.GetComponentImages(RIL.TEXTLINE, True, raw_image=True)):
line_id = '%s_line%04d' % (region.id, line_no)
Expand Down
8 changes: 7 additions & 1 deletion ocrd_tesserocr/segment_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,13 @@ def process(self):
LOG.warning('keeping existing ReadingOrder')

page_image, page_coords, page_image_info = self.workspace.image_from_page(
page, page_id)
page, page_id,
# for some reason, external binarization
# degrades Tesseract segmentation quality
# (probably because C_OUTLINE::ComputeEdgeOffsets,
# which needs the greyscale image, is more
# accurate than C_OUTLINE::ComputeBinaryOffsets):
feature_filter='binarized')
if self.parameter['dpi'] > 0:
dpi = self.parameter['dpi']
LOG.info("Page '%s' images will use %d DPI from parameter override", page_id, dpi)
Expand Down
25 changes: 22 additions & 3 deletions ocrd_tesserocr/segment_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ def process(self):
page = pcgts.get_Page()

page_image, page_coords, page_image_info = self.workspace.image_from_page(
page, page_id)
page, page_id,
# for some reason, external binarization
# degrades Tesseract segmentation quality
# (probably because C_OUTLINE::ComputeEdgeOffsets,
# which needs the greyscale image, is more
# accurate than C_OUTLINE::ComputeBinaryOffsets):
feature_filter='binarized')
if self.parameter['dpi'] > 0:
dpi = self.parameter['dpi']
LOG.info("Page '%s' images will use %d DPI from parameter override", page_id, dpi)
Expand Down Expand Up @@ -124,12 +130,25 @@ def process(self):
LOG.warning('keeping existing TextRegions in block "%s" of page "%s"', region.id, page_id)
# get region image
region_image, region_coords = self.workspace.image_from_segment(
region, page_image, page_coords)
region, page_image, page_coords,
# for some reason, external binarization
# degrades Tesseract segmentation quality
# (probably because C_OUTLINE::ComputeEdgeOffsets,
# which needs the greyscale image, is more
# accurate than C_OUTLINE::ComputeBinaryOffsets):
feature_filter='binarized')
tessapi.SetImage(region_image)
LOG.info("Detecting table cells in region '%s'", region.id)
#
# detect the region segments:
tessapi.SetPageSegMode(PSM.SPARSE_TEXT) # retrieve "cells"
tessapi.SetPageSegMode(PSM.SPARSE_TEXT_OSD) # retrieve "cells"
# FIXME: _OSD is necessary to get VERTICAL_TEXT (90°) blocks, but
# this also causes looking for vertical gaps/alignments everywhere
# (not just blocks that end up as vertical), so often cells
# will span more than 1 line and some text will even be missed!
# We should check whether some strokewidth params can influence this.
# Otherwise, Tesseract should become more consistent in deciding for
# vertically aligned blobs (either the whole block, or keep horizontal).
# TODO: we should XY-cut the sparse cells in regroup them into consistent cells
layout = tessapi.AnalyseLayout()
roelem = reading_order.get(region.id)
Expand Down