Compare commits
203 Commits
@ -1,20 +0,0 @@
|
|||||||
version: 2.1
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
black:
|
|
||||||
parameters:
|
|
||||||
python-version:
|
|
||||||
type: string
|
|
||||||
docker:
|
|
||||||
- image: cimg/python:<< parameters.python-version >>
|
|
||||||
steps:
|
|
||||||
- checkout
|
|
||||||
- run: pip3 install --upgrade pip
|
|
||||||
- run: pip3 install black
|
|
||||||
- run: black .
|
|
||||||
|
|
||||||
workflows:
|
|
||||||
black:
|
|
||||||
jobs:
|
|
||||||
- black:
|
|
||||||
python-version: "3.11"
|
|
@ -0,0 +1,5 @@
|
|||||||
|
src/dinglehopper/tests
|
||||||
|
dist
|
||||||
|
build
|
||||||
|
*.egg-info
|
||||||
|
.git
|
@ -0,0 +1,20 @@
|
|||||||
|
name: 'Test - Report results'
|
||||||
|
on:
|
||||||
|
workflow_run:
|
||||||
|
workflows: ['test']
|
||||||
|
types:
|
||||||
|
- completed
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
actions: read
|
||||||
|
checks: write
|
||||||
|
jobs:
|
||||||
|
report:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: dorny/test-reporter@v1
|
||||||
|
with:
|
||||||
|
artifact: /test-results-(.*)/
|
||||||
|
name: 'test - Results ($1)'
|
||||||
|
path: '*junit.xml'
|
||||||
|
reporter: java-junit
|
@ -0,0 +1,16 @@
|
|||||||
|
variables:
|
||||||
|
http_proxy: "http://http-proxy.sbb.spk-berlin.de:3128/"
|
||||||
|
https_proxy: "http://http-proxy.sbb.spk-berlin.de:3128/"
|
||||||
|
HTTP_PROXY: "http://http-proxy.sbb.spk-berlin.de:3128/"
|
||||||
|
HTTPS_PROXY: "http://http-proxy.sbb.spk-berlin.de:3128/"
|
||||||
|
|
||||||
|
stages:
|
||||||
|
- triggers
|
||||||
|
|
||||||
|
mirror:
|
||||||
|
stage: triggers
|
||||||
|
trigger:
|
||||||
|
include: .gitlab/mirror.yml
|
||||||
|
strategy: depend
|
||||||
|
rules:
|
||||||
|
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
|
@ -0,0 +1,47 @@
|
|||||||
|
stages:
|
||||||
|
- check
|
||||||
|
- pull
|
||||||
|
- push
|
||||||
|
|
||||||
|
default:
|
||||||
|
image: debian
|
||||||
|
|
||||||
|
|
||||||
|
check:
|
||||||
|
stage: check
|
||||||
|
|
||||||
|
script:
|
||||||
|
- whoami; env
|
||||||
|
- if [ -z "$CI_COMMIT_BRANCH" ]; then echo "Not on a branch" >&2; exit 3; fi
|
||||||
|
|
||||||
|
|
||||||
|
pull-gitlab:
|
||||||
|
stage: pull
|
||||||
|
script:
|
||||||
|
- echo "This is redundant"
|
||||||
|
|
||||||
|
pull-github:
|
||||||
|
stage: pull
|
||||||
|
before_script:
|
||||||
|
- apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/*
|
||||||
|
script:
|
||||||
|
- git remote remove github 2>/dev/null || true
|
||||||
|
- git remote add github https://github.com/qurator-spk/dinglehopper.git
|
||||||
|
- git remote -v
|
||||||
|
|
||||||
|
- git pull github "$CI_COMMIT_BRANCH"
|
||||||
|
|
||||||
|
|
||||||
|
push-gitlab:
|
||||||
|
stage: push
|
||||||
|
before_script:
|
||||||
|
- apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/*
|
||||||
|
script:
|
||||||
|
- git push origin "$CI_COMMIT_SHA":"$CI_COMMIT_BRANCH"
|
||||||
|
|
||||||
|
push-github:
|
||||||
|
stage: push
|
||||||
|
before_script:
|
||||||
|
- apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/*
|
||||||
|
script:
|
||||||
|
- git push github "$CI_COMMIT_SHA":"$CI_COMMIT_BRANCH"
|
@ -0,0 +1,33 @@
|
|||||||
|
PYTHON = python3
|
||||||
|
PIP = pip3
|
||||||
|
PYTHONIOENCODING=utf8
|
||||||
|
PYTEST_ARGS = -vv
|
||||||
|
|
||||||
|
DOCKER_BASE_IMAGE = docker.io/ocrd/core:v3.3.0
|
||||||
|
DOCKER_TAG = ocrd/dinglehopper
|
||||||
|
|
||||||
|
help:
|
||||||
|
@echo
|
||||||
|
@echo " Targets"
|
||||||
|
@echo
|
||||||
|
@echo " install Install full Python package via pip"
|
||||||
|
@echo " docker Build the ocrd/dinglehopper docker image"
|
||||||
|
|
||||||
|
# Install Python package via pip
|
||||||
|
install:
|
||||||
|
$(PIP) install .
|
||||||
|
|
||||||
|
install-dev:
|
||||||
|
$(PIP) install -e .
|
||||||
|
|
||||||
|
test:
|
||||||
|
pytest $(PYTEST_ARGS)
|
||||||
|
|
||||||
|
docker:
|
||||||
|
docker build \
|
||||||
|
--build-arg DOCKER_BASE_IMAGE=$(DOCKER_BASE_IMAGE) \
|
||||||
|
--build-arg VCS_REF=$$(git rev-parse --short HEAD) \
|
||||||
|
--build-arg BUILD_DATE=$$(date -u +"%Y-%m-%dT%H:%M:%SZ") \
|
||||||
|
-t $(DOCKER_TAG) .
|
||||||
|
|
||||||
|
.PHONY: help install install-dev test docker
|
@ -1,8 +1,14 @@
|
|||||||
pytest
|
pytest
|
||||||
pytest-cov
|
pytest-cov
|
||||||
pytest-mypy
|
|
||||||
black
|
black
|
||||||
pre-commit
|
pre-commit
|
||||||
|
|
||||||
ruff ; python_version >= "3.7"
|
ruff
|
||||||
pytest-ruff ; python_version >= "3.7"
|
pytest-ruff
|
||||||
|
|
||||||
|
mypy
|
||||||
|
types-lxml
|
||||||
|
types-setuptools
|
||||||
|
pytest-mypy
|
||||||
|
|
||||||
|
liccheck
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
click
|
click
|
||||||
jinja2
|
jinja2
|
||||||
lxml
|
lxml
|
||||||
uniseg
|
uniseg >= 0.8.0
|
||||||
numpy
|
numpy
|
||||||
colorama
|
colorama
|
||||||
MarkupSafe
|
MarkupSafe
|
||||||
ocrd >= 2.20.1
|
ocrd >= 3.3.0
|
||||||
attrs
|
attrs
|
||||||
multimethod == 1.3 # latest version to officially support Python 3.5
|
multimethod >= 1.3
|
||||||
tqdm
|
tqdm
|
||||||
rapidfuzz >= 2.4.2
|
rapidfuzz >= 2.7.0
|
||||||
six # XXX workaround OCR-D/core#730
|
|
||||||
chardet
|
chardet
|
||||||
|
importlib_resources
|
||||||
|
@ -1,78 +1,78 @@
|
|||||||
import json
|
from functools import cached_property
|
||||||
import os
|
import os
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
from ocrd_models import OcrdFileType
|
||||||
from ocrd import Processor
|
from ocrd import Processor
|
||||||
from ocrd.decorators import ocrd_cli_options, ocrd_cli_wrap_processor
|
from ocrd.decorators import ocrd_cli_options, ocrd_cli_wrap_processor
|
||||||
from ocrd_utils import assert_file_grp_cardinality, getLogger, make_file_id
|
from ocrd_utils import make_file_id
|
||||||
from pkg_resources import resource_string
|
|
||||||
|
|
||||||
from .cli import process as cli_process
|
from .cli import process as cli_process
|
||||||
|
|
||||||
OCRD_TOOL = json.loads(resource_string(__name__, "ocrd-tool.json").decode("utf8"))
|
|
||||||
|
|
||||||
|
|
||||||
@click.command()
|
@click.command()
|
||||||
@ocrd_cli_options
|
@ocrd_cli_options
|
||||||
def ocrd_dinglehopper(*args, **kwargs):
|
def ocrd_dinglehopper(*args, **kwargs):
|
||||||
return ocrd_cli_wrap_processor(OcrdDinglehopperEvaluate, *args, **kwargs)
|
return ocrd_cli_wrap_processor(OcrdDinglehopperEvaluate, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class OcrdDinglehopperEvaluate(Processor):
|
class OcrdDinglehopperEvaluate(Processor):
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
kwargs["ocrd_tool"] = OCRD_TOOL["tools"]["ocrd-dinglehopper"]
|
|
||||||
super(OcrdDinglehopperEvaluate, self).__init__(*args, **kwargs)
|
|
||||||
|
|
||||||
def process(self):
|
@cached_property
|
||||||
assert_file_grp_cardinality(self.input_file_grp, 2, "GT and OCR")
|
def executable(self):
|
||||||
assert_file_grp_cardinality(self.output_file_grp, 1)
|
return 'ocrd-dinglehopper'
|
||||||
|
|
||||||
log = getLogger("processor.OcrdDinglehopperEvaluate")
|
def process_page_file(self, *input_files: Optional[OcrdFileType]) -> None:
|
||||||
|
|
||||||
|
assert self.parameter
|
||||||
metrics = self.parameter["metrics"]
|
metrics = self.parameter["metrics"]
|
||||||
textequiv_level = self.parameter["textequiv_level"]
|
textequiv_level = self.parameter["textequiv_level"]
|
||||||
gt_grp, ocr_grp = self.input_file_grp.split(",")
|
plain_encoding = self.parameter["plain_encoding"]
|
||||||
|
|
||||||
input_file_tuples = self.zip_input_files(on_error="abort")
|
# wrong number of inputs: let fail
|
||||||
for n, (gt_file, ocr_file) in enumerate(input_file_tuples):
|
gt_file, ocr_file = input_files
|
||||||
if not gt_file or not ocr_file:
|
# missing on either side: skip (zip_input_files already warned)
|
||||||
# file/page was not found in this group
|
if not gt_file or not ocr_file:
|
||||||
continue
|
return
|
||||||
gt_file = self.workspace.download_file(gt_file)
|
# missing download (i.e. OCRD_DOWNLOAD_INPUT=false):
|
||||||
ocr_file = self.workspace.download_file(ocr_file)
|
if not gt_file.local_filename:
|
||||||
page_id = gt_file.pageId
|
if config.OCRD_MISSING_INPUT == 'ABORT':
|
||||||
|
raise MissingInputFile(gt_file.fileGrp, gt_file.pageId, gt_file.mimetype)
|
||||||
log.info("INPUT FILES %i / %s↔ %s", n, gt_file, ocr_file)
|
return
|
||||||
|
if not ocr_file.local_filename:
|
||||||
file_id = make_file_id(ocr_file, self.output_file_grp)
|
if config.OCRD_MISSING_INPUT == 'ABORT':
|
||||||
report_prefix = os.path.join(self.output_file_grp, file_id)
|
raise MissingInputFile(ocr_file.fileGrp, ocr_file.pageId, ocr_file.mimetype)
|
||||||
|
return
|
||||||
# Process the files
|
|
||||||
try:
|
page_id = gt_file.pageId
|
||||||
os.mkdir(self.output_file_grp)
|
|
||||||
except FileExistsError:
|
file_id = make_file_id(ocr_file, self.output_file_grp)
|
||||||
pass
|
cli_process(
|
||||||
cli_process(
|
gt_file.local_filename,
|
||||||
gt_file.local_filename,
|
ocr_file.local_filename,
|
||||||
ocr_file.local_filename,
|
file_id,
|
||||||
report_prefix,
|
self.output_file_grp,
|
||||||
metrics=metrics,
|
metrics=metrics,
|
||||||
textequiv_level=textequiv_level,
|
textequiv_level=textequiv_level,
|
||||||
|
plain_encoding=plain_encoding,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Add reports to the workspace
|
||||||
|
for report_suffix, mimetype in [
|
||||||
|
[".html", "text/html"],
|
||||||
|
[".json", "application/json"],
|
||||||
|
]:
|
||||||
|
output_file_id = file_id + report_suffix
|
||||||
|
output_file = next(self.workspace.mets.find_files(ID=output_file_id), None)
|
||||||
|
if output_file and config.OCRD_EXISTING_OUTPUT != 'OVERWRITE':
|
||||||
|
raise FileExistsError(f"A file with ID=={output_file_id} already exists {output_file} and neither force nor ignore are set")
|
||||||
|
self.workspace.add_file(
|
||||||
|
file_id=output_file_id,
|
||||||
|
file_grp=self.output_file_grp,
|
||||||
|
page_id=page_id,
|
||||||
|
mimetype=mimetype,
|
||||||
|
local_filename=file_id + report_suffix,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add reports to the workspace
|
|
||||||
for report_suffix, mimetype in [
|
|
||||||
[".html", "text/html"],
|
|
||||||
[".json", "application/json"],
|
|
||||||
]:
|
|
||||||
self.workspace.add_file(
|
|
||||||
file_id=file_id + report_suffix,
|
|
||||||
file_grp=self.output_file_grp,
|
|
||||||
page_id=page_id,
|
|
||||||
mimetype=mimetype,
|
|
||||||
local_filename=report_prefix + report_suffix,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
ocrd_dinglehopper()
|
ocrd_dinglehopper()
|
||||||
|
@ -0,0 +1 @@
|
|||||||
|
This is a test.
|
@ -0,0 +1 @@
|
|||||||
|
Another test.
|
@ -0,0 +1 @@
|
|||||||
|
Tis is a test.
|
@ -0,0 +1 @@
|
|||||||
|
AnÖther test.
|
@ -0,0 +1 @@
|
|||||||
|
This is a test.
|
@ -0,0 +1 @@
|
|||||||
|
Tis is a test.
|
@ -0,0 +1 @@
|
|||||||
|
Another test.
|
@ -0,0 +1 @@
|
|||||||
|
AnÖther test.
|
@ -0,0 +1 @@
|
|||||||
|
This is a test.
|
@ -0,0 +1 @@
|
|||||||
|
Another test.
|
@ -0,0 +1 @@
|
|||||||
|
Tis is a test.
|
@ -0,0 +1 @@
|
|||||||
|
AnÖther test.
|
@ -0,0 +1,61 @@
|
|||||||
|
import json
|
||||||
|
import os.path
|
||||||
|
import re
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from ..cli_line_dirs import process
|
||||||
|
from .util import working_directory
|
||||||
|
|
||||||
|
data_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.integration
|
||||||
|
def test_cli_line_dirs_basic(tmp_path):
|
||||||
|
"""Test that the cli/process() produces a good report"""
|
||||||
|
|
||||||
|
with working_directory(tmp_path):
|
||||||
|
gt_dir = os.path.join(data_dir, "line_dirs/basic/gt")
|
||||||
|
ocr_dir = os.path.join(data_dir, "line_dirs/basic/ocr")
|
||||||
|
process(gt_dir, ocr_dir, "report")
|
||||||
|
with open("report.json", "r") as jsonf:
|
||||||
|
print(jsonf.read())
|
||||||
|
with open("report.json", "r") as jsonf:
|
||||||
|
j = json.load(jsonf)
|
||||||
|
assert j["cer"] == pytest.approx(0.1071429)
|
||||||
|
assert j["wer"] == pytest.approx(0.5)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.integration
|
||||||
|
def test_cli_line_dirs_basic_report_diff(tmp_path):
|
||||||
|
"""Test that the cli/process() produces a report wiff char+word diff"""
|
||||||
|
|
||||||
|
with working_directory(tmp_path):
|
||||||
|
gt_dir = os.path.join(data_dir, "line_dirs/basic/gt")
|
||||||
|
ocr_dir = os.path.join(data_dir, "line_dirs/basic/ocr")
|
||||||
|
process(gt_dir, ocr_dir, "report")
|
||||||
|
|
||||||
|
with open("report.html", "r") as htmlf:
|
||||||
|
html_report = htmlf.read()
|
||||||
|
|
||||||
|
# Counting GT lines in the diff
|
||||||
|
assert len(re.findall(r"gt.*l\d+-cdiff", html_report)) == 2
|
||||||
|
assert len(re.findall(r"gt.*l\d+-wdiff", html_report)) == 2
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.integration
|
||||||
|
def test_cli_line_dirs_merged(tmp_path):
|
||||||
|
"""Test that the cli/process() produces a good report"""
|
||||||
|
|
||||||
|
with working_directory(tmp_path):
|
||||||
|
gt_dir = os.path.join(data_dir, "line_dirs/merged")
|
||||||
|
ocr_dir = os.path.join(data_dir, "line_dirs/merged")
|
||||||
|
process(
|
||||||
|
gt_dir, ocr_dir, "report", gt_suffix=".gt.txt", ocr_suffix=".some-ocr.txt"
|
||||||
|
)
|
||||||
|
with open("report.json", "r") as jsonf:
|
||||||
|
print(jsonf.read())
|
||||||
|
with open("report.json", "r") as jsonf:
|
||||||
|
j = json.load(jsonf)
|
||||||
|
assert j["cer"] == pytest.approx(0.1071429)
|
||||||
|
assert j["wer"] == pytest.approx(0.5)
|
@ -0,0 +1,35 @@
|
|||||||
|
from __future__ import division, print_function
|
||||||
|
|
||||||
|
import math
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from .. import character_error_rate, plain_text
|
||||||
|
from .util import working_directory
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.integration
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"gt_file_content,ocr_file_content,cer_expected",
|
||||||
|
[
|
||||||
|
("", "Lorem ipsum", math.inf),
|
||||||
|
("Lorem ipsum", "", 1.0),
|
||||||
|
("\ufeff", "Lorem ipsum", math.inf),
|
||||||
|
("Lorem ipsum", "\ufeff", 1.0),
|
||||||
|
("", "", 0.0),
|
||||||
|
("\ufeff", "", 0.0),
|
||||||
|
("", "\ufeff", 0.0),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_empty_files(tmp_path, gt_file_content, ocr_file_content, cer_expected):
|
||||||
|
with working_directory(tmp_path):
|
||||||
|
|
||||||
|
with open("gt.txt", "w") as gtf:
|
||||||
|
gtf.write(gt_file_content)
|
||||||
|
with open("ocr.txt", "w") as ocrf:
|
||||||
|
ocrf.write(ocr_file_content)
|
||||||
|
|
||||||
|
gt_text = plain_text("gt.txt")
|
||||||
|
ocr_text = plain_text("ocr.txt")
|
||||||
|
|
||||||
|
assert character_error_rate(gt_text, ocr_text) == cer_expected
|
@ -0,0 +1,71 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
from ..cli_line_dirs import find_gt_and_ocr_files, find_gt_and_ocr_files_autodetect
|
||||||
|
|
||||||
|
data_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
|
||||||
|
|
||||||
|
|
||||||
|
def test_basic():
|
||||||
|
"""Test the dumb method: User gives directories and suffixes."""
|
||||||
|
pairs = list(
|
||||||
|
find_gt_and_ocr_files(
|
||||||
|
os.path.join(data_dir, "line_dirs/basic/gt"),
|
||||||
|
".gt.txt",
|
||||||
|
os.path.join(data_dir, "line_dirs/basic/ocr"),
|
||||||
|
".some-ocr.txt",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
assert len(pairs) == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_basic_autodetect():
|
||||||
|
"""Test autodetect: User gives directories, suffixes are autodetected if possible"""
|
||||||
|
pairs = list(
|
||||||
|
find_gt_and_ocr_files_autodetect(
|
||||||
|
os.path.join(data_dir, "line_dirs/basic/gt"),
|
||||||
|
os.path.join(data_dir, "line_dirs/basic/ocr"),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
assert len(pairs) == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_subdirs():
|
||||||
|
"""Test the dumb method: Should also work when subdirectories are involved."""
|
||||||
|
pairs = list(
|
||||||
|
find_gt_and_ocr_files(
|
||||||
|
os.path.join(data_dir, "line_dirs/subdirs/gt"),
|
||||||
|
".gt.txt",
|
||||||
|
os.path.join(data_dir, "line_dirs/subdirs/ocr"),
|
||||||
|
".some-ocr.txt",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
assert len(pairs) == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_subdirs_autodetect():
|
||||||
|
"""Test the autodetect method: Should also work when subdirectories are involved."""
|
||||||
|
pairs = list(
|
||||||
|
find_gt_and_ocr_files_autodetect(
|
||||||
|
os.path.join(data_dir, "line_dirs/subdirs/gt"),
|
||||||
|
os.path.join(data_dir, "line_dirs/subdirs/ocr"),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
assert len(pairs) == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_merged():
|
||||||
|
"""Test the dumb method: GT and OCR texts are in the same directories."""
|
||||||
|
pairs = list(
|
||||||
|
find_gt_and_ocr_files(
|
||||||
|
os.path.join(data_dir, "line_dirs/merged"),
|
||||||
|
".gt.txt",
|
||||||
|
os.path.join(data_dir, "line_dirs/merged"),
|
||||||
|
".some-ocr.txt",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
assert len(pairs) == 2
|
Loading…
Reference in New Issue