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

workspace_validator: allow skipping pcgtsid check #1066

Merged
merged 4 commits into from
Jul 10, 2023
Merged
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
4 changes: 3 additions & 1 deletion ocrd/ocrd/cli/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ def workspace_cli(ctx, directory, mets, mets_basename, backup):
(r' \[METS_URL\]', ''))) # XXX deprecated argument
@pass_workspace
@click.option('-a', '--download', is_flag=True, help="Download all files")
@click.option('-s', '--skip', help="Tests to skip", default=[], multiple=True, type=click.Choice(['imagefilename', 'dimension', 'mets_unique_identifier', 'mets_file_group_names', 'mets_files', 'pixel_density', 'page', 'page_xsd', 'mets_xsd', 'url']))
@click.option('-s', '--skip', help="Tests to skip", default=[], multiple=True, type=click.Choice(
['imagefilename', 'dimension', 'pixel_density', 'page', 'url', 'page_xsd', 'mets_fileid_page_pcgtsid',
'mets_unique_identifier', 'mets_file_group_names', 'mets_files', 'mets_xsd']))
@click.option('--page-textequiv-consistency', '--page-strictness', help="How strict to check PAGE multi-level textequiv consistency", type=click.Choice(['strict', 'lax', 'fix', 'off']), default='strict')
@click.option('--page-coordinate-consistency', help="How fierce to check PAGE multi-level coordinate consistency", type=click.Choice(['poly', 'baseline', 'both', 'off']), default='poly')
@click.argument('mets_url', default=None, required=False)
Expand Down
70 changes: 47 additions & 23 deletions ocrd_validators/ocrd_validators/workspace_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

class WorkspaceValidator():
"""
Validates an OCR-D/METS workspace against the specs.
Validator for `OcrdMets <../ocrd_models/ocrd_models.ocrd_mets.html>`.
"""

@staticmethod
Expand Down Expand Up @@ -72,8 +72,14 @@ def __init__(self, resolver, mets_url, src_dir=None, skip=None, download=False,
src_dir (string):
skip (list):
download (boolean):
page_strictness ("strict"|"lax"|"fix"|"off"):
page_coordinate_consistency ("poly"|"baseline"|"both"|"off"):
page_strictness ("strict"|"lax"|"fix"|"off"): how strict to check
multi-level TextEquiv consistency of PAGE XML files
page_coordinate_consistency ("poly"|"baseline"|"both"|"off"): check whether each
segment's coords are fully contained within its parent's:
* `"poly"`: *Region/TextLine/Word/Glyph in Border/*Region/TextLine/Word
* `"baseline"`: Baseline in TextLine
* `"both"`: both `poly` and `baseline` checks
* `"off"`: no coordinate checks
"""
self.report = ValidationReport()
self.skip = skip if skip else []
Expand All @@ -86,6 +92,10 @@ def __init__(self, resolver, mets_url, src_dir=None, skip=None, download=False,
self.download = download
self.page_strictness = page_strictness
self.page_coordinate_consistency = page_coordinate_consistency
self.page_checks = []
# there will be more options to come
if 'mets_fileid_page_pcgtsid' not in self.skip:
self.page_checks.append('pcgtsid')

self.src_dir = src_dir
self.workspace = None
Expand All @@ -100,8 +110,13 @@ def validate(*args, **kwargs):
resolver (:class:`ocrd.Resolver`): Resolver
mets_url (string): URL of the METS file
src_dir (string, None): Directory containing mets file
skip (list): Tests to skip. One or more of 'mets_unique_identifier', 'mets_file_group_names', 'mets_files', 'pixel_density', 'dimension', 'url'
download (boolean): Whether to download files
skip (list): Validation checks to omit. One or more of
'mets_unique_identifier', 'mets_file_group_names',
'mets_files', 'pixel_density', 'dimension', 'url',
'multipage', 'page', 'page_xsd', 'mets_xsd',
'mets_fileid_page_pcgtsid'
download (boolean): Whether to download remote file references
temporarily during validation (like a processor would)

Returns:
report (:class:`ValidationReport`) Report on the validity
Expand Down Expand Up @@ -151,7 +166,7 @@ def _resolve_workspace(self):
"""
self.log.debug('_resolve_workspace')
if self.workspace is None:
self.workspace = self.resolver.workspace_from_url(self.mets_url, src_baseurl=self.src_dir, download=self.download)
self.workspace = self.resolver.workspace_from_url(self.mets_url, src_baseurl=self.src_dir)
self.mets = self.workspace.mets

def _validate_mets_unique_identifier(self):
Expand All @@ -171,7 +186,7 @@ def _validate_imagefilename(self):
self.log.debug('_validate_imagefilename')
for f in self.mets.find_files(mimetype=MIMETYPE_PAGE):
if not is_local_filename(f.url) and not self.download:
self.report.add_notice("Won't download remote PAGE XML <%s>" % f.url)
self.log.warning("Won't download remote PAGE XML '%s'", f.url)
continue
self.workspace.download_file(f)
page = page_from_file(f).get_Page()
Expand All @@ -188,8 +203,9 @@ def _validate_dimension(self):
self.log.info('_validate_dimension')
for f in self.mets.find_files(mimetype=MIMETYPE_PAGE):
if not is_local_filename(f.url) and not self.download:
self.report.add_notice("_validate_dimension: Not executed because --download wasn't set and PAGE might reference remote (Alternative)Images <%s>" % f.url)
self.log.warning("Won't download remote PAGE XML '%s'", f.url)
continue
self.workspace.download_file(f)
page = page_from_file(f).get_Page()
_, _, exif = self.workspace.image_from_page(page, f.pageId)
if page.imageHeight != exif.height:
Expand All @@ -204,10 +220,11 @@ def _validate_multipage(self):
See `spec <https://ocr-d.github.io/mets#no-multi-page-images>`_.
"""
self.log.debug('_validate_multipage')
for f in [f for f in self.mets.find_files() if f.mimetype.startswith('image/')]:
for f in self.mets.find_files(mimetype='//image/.*'):
if not is_local_filename(f.url) and not self.download:
self.report.add_notice("Won't download remote image <%s>" % f.url)
self.log.warning("Won't download remote image '%s'", f.url)
continue
self.workspace.download_file(f)
try:
exif = self.workspace.resolve_image_exif(f.url)
if exif.n_frames > 1:
Expand All @@ -223,10 +240,11 @@ def _validate_pixel_density(self):
See `spec <https://ocr-d.github.io/mets#pixel-density-of-images-must-be-explicit-and-high-enough>`_.
"""
self.log.debug('_validate_pixel_density')
for f in [f for f in self.mets.find_files() if f.mimetype.startswith('image/')]:
for f in self.mets.find_files(mimetype='//image/.*'):
if not is_local_filename(f.url) and not self.download:
self.report.add_notice("Won't download remote image <%s>" % f.url)
self.log.warning("Won't download remote image '%s'", f.url)
continue
self.workspace.download_file(f)
exif = self.workspace.resolve_image_exif(f.url)
for k in ['xResolution', 'yResolution']:
v = exif.__dict__.get(k)
Expand Down Expand Up @@ -285,27 +303,33 @@ def _validate_page(self):
Run PageValidator on the PAGE-XML documents referenced in the METS.
"""
self.log.debug('_validate_page')
for ocrd_file in self.mets.find_files(mimetype=MIMETYPE_PAGE):
self.workspace.download_file(ocrd_file)
page_report = PageValidator.validate(ocrd_file=ocrd_file,
for f in self.mets.find_files(mimetype=MIMETYPE_PAGE):
if not is_local_filename(f.url) and not self.download:
self.log.warning("Won't download remote PAGE XML '%s'", f.url)
continue
self.workspace.download_file(f)
page_report = PageValidator.validate(ocrd_file=f,
page_textequiv_consistency=self.page_strictness,
check_coords=self.page_coordinate_consistency in ['poly', 'both'],
check_baseline=self.page_coordinate_consistency in ['baseline', 'both'])
pg = page_from_file(ocrd_file)
if pg.pcGtsId != ocrd_file.ID:
page_report.add_warning('pc:PcGts/@pcGtsId differs from mets:file/@ID: "%s" !== "%s"' % (pg.pcGtsId or '', ocrd_file.ID or ''))
pg = page_from_file(f)
if 'pcgtsid' in self.page_checks and pg.pcGtsId != f.ID:
page_report.add_warning('pc:PcGts/@pcGtsId differs from mets:file/@ID: "%s" !== "%s"' % (pg.pcGtsId or '', f.ID or ''))
self.report.merge_report(page_report)

def _validate_page_xsd(self):
"""
Validate all PAGE-XML files against PAGE XSD schema
"""
self.log.debug('_validate_page_xsd')
for ocrd_file in self.mets.find_files(mimetype=MIMETYPE_PAGE):
self.workspace.download_file(ocrd_file)
for err in XsdPageValidator.validate(Path(ocrd_file.local_filename)).errors:
self.report.add_error("%s: %s" % (ocrd_file.ID, err))
self.log.debug("Finished alidating all PAGE-XML files against XSD")
for f in self.mets.find_files(mimetype=MIMETYPE_PAGE):
if not is_local_filename(f.url) and not self.download:
self.log.warning("Won't download remote PAGE XML '%s'", f.url)
continue
self.workspace.download_file(f)
for err in XsdPageValidator.validate(Path(f.local_filename)).errors:
self.report.add_error("%s: %s" % (f.ID, err))
self.log.debug("Finished validating all PAGE-XML files against XSD")

def _validate_mets_xsd(self):
"""
Expand Down