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

Rewrite #33

Merged
merged 39 commits into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
99e370e
update requirements
kba Dec 28, 2020
5a9ea7d
ocrd-tool: add parameter description (placeholders)
kba Dec 29, 2020
07fd913
[wip] upgrade to use 3.x baseline segmenter
kba Dec 29, 2020
c530706
blla segmenter basically working
kba Dec 30, 2020
ef40b8e
segment: more robust handling of lines not matched to regions
kba Dec 30, 2020
33e765f
implement legacy segmenter
kba Dec 30, 2020
5feb52c
decouple CLIs, add -recognize
kba Dec 30, 2020
28ef7cb
implement basic recognition
kba Dec 30, 2020
b67e7c3
recognize: output words and glyphs with conf
kba Dec 31, 2020
276f542
ci: do not build python 3.5 anymore
kba Dec 31, 2020
c2ceef9
require core 2.22.0b3+ for kraken resources
kba Dec 31, 2020
8be2abd
calculate word coords from glyph coords
kba Jan 1, 2021
8d93794
segment: convert coords back to absolute
bertsky Jan 8, 2021
232a055
segment: enlarge regions to check for contained lines, ensure polygon…
bertsky Jan 8, 2021
97ee297
Update ocrd_kraken/recognize.py
kba Jan 21, 2021
2f2a2c6
recognize: handle whitespace at BOL
kba Jan 22, 2021
38e9a7b
ocrd_tool.json: add model content type
bertsky Feb 10, 2022
f4a0caf
improve ocrd-tool.json, depend on released kraken > 3
kba Feb 18, 2022
cb719e7
Update ocrd_kraken/ocrd-tool.json
kba Mar 7, 2022
9c052df
recognize: fix decoding in case word is empty
bertsky Jul 13, 2022
b85e147
segment: use binarized input only if use_legacy
bertsky Jul 13, 2022
bd4609c
segment: decode arbitrary region types, add param blla_classes
bertsky Jul 13, 2022
5b6d79c
segment: move model init to setup fn
bertsky Jul 13, 2022
0ff4dd7
recognize: use binarized image only if model expects it
bertsky Jul 13, 2022
bfc675d
recognize: move model init to setup fn
bertsky Jul 13, 2022
5c7000a
recognize: use proper relative coordinate system for words and glyphs
bertsky Jul 13, 2022
0eecf6c
Revert "segment: use binarized input only if use_legacy"
bertsky Jul 15, 2022
104f138
Revert "Revert "segment: use binarized input only if use_legacy""
bertsky Jul 18, 2022
8b57e46
fix typos
kba Jul 22, 2022
d27f1a4
fix test_binarize
kba Jul 22, 2022
6f4a7bf
ci: download model first
kba Jul 22, 2022
f8235c4
:package: v0.2.0
kba Jul 22, 2022
ad8eb16
recognize: clip bboxes to canvas
bertsky Oct 13, 2022
36a463d
ocrd-tool: add model resources
bertsky Oct 13, 2022
6297b33
CI: add Py39, fix Py36
bertsky Oct 13, 2022
446af81
udpate+fix Docker build
bertsky Oct 13, 2022
c7b7f18
documentation
bertsky Oct 13, 2022
45143de
Makefile: fix typo
bertsky Oct 13, 2022
831e017
CI: remove assets
bertsky Oct 13, 2022
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
36 changes: 23 additions & 13 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,53 @@ version: 2

jobs:

build-python35:
build-python36:
docker:
- image: python:3.5.10-buster
- image: python:3.6.15-buster
steps:
- checkout
- run: make deps-ubuntu deps-test deps install assets
# workaround for OCR-D/core#923
- run: apt-get update && apt-get install -y libgeos-dev
- run: make deps-ubuntu deps-test deps install
- run: ocrd resmgr download ocrd-kraken-segment blla.mlmodel
- run: ocrd resmgr download ocrd-kraken-recognize en_best.mlmodel
- run: make test

build-python36:
build-python37:
docker:
- image: python:3.6.12-buster
- image: python:3.7.15-buster
steps:
- checkout
- run: make deps-ubuntu deps-test deps install assets
- run: make deps-ubuntu deps-test deps install
- run: ocrd resmgr download ocrd-kraken-segment blla.mlmodel
- run: ocrd resmgr download ocrd-kraken-recognize en_best.mlmodel
- run: make test

build-python37:
build-python38:
docker:
- image: python:3.7.9-buster
- image: python:3.8.14-buster
steps:
- checkout
- run: make deps-ubuntu deps-test deps install assets
- run: make deps-ubuntu deps-test deps install
- run: ocrd resmgr download ocrd-kraken-segment blla.mlmodel
- run: ocrd resmgr download ocrd-kraken-recognize en_best.mlmodel
- run: make test

build-python38:
build-python39:
docker:
- image: python:3.8.5-buster
- image: python:3.9.14-buster
steps:
- checkout
- run: make deps-ubuntu deps-test deps install assets
- run: make deps-ubuntu deps-test deps install
- run: ocrd resmgr download ocrd-kraken-segment blla.mlmodel
- run: ocrd resmgr download ocrd-kraken-recognize en_best.mlmodel
- run: make test

workflows:
version: 2
build:
jobs:
- build-python35
- build-python36
- build-python37
- build-python38
- build-python39
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ Versioned according to [Semantic Versioning](http://semver.org/).

## Unreleased

## [0.1.2] - 2019-09-24
## [0.2.0] - 2022-07-18

Added:

* text recognition with `ocrd-kraken-recognize`, #33
* rewrite of `ocrd-kraken-segment`, #33

## [0.1.2] - 2020-09-24

Fixed:

Expand Down Expand Up @@ -43,6 +50,7 @@ Changed:
Initial release

<!-- link-labels -->
[0.2.0]: v0.2.0...v0.1.2
[0.1.2]: v0.1.2...v0.1.1
[0.1.1]: v0.1.1...v0.1.0
[0.1.0]: v0.1.0...v0.0.2
Expand Down
34 changes: 24 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
FROM ocrd/core
MAINTAINER OCR-D
ARG VCS_REF
ARG BUILD_DATE
LABEL \
maintainer="https://ocr-d.de/kontakt" \
org.label-schema.vcs-ref=$VCS_REF \
org.label-schema.vcs-url="https://github.com/OCR-D/ocrd_tesserocr" \
org.label-schema.build-date=$BUILD_DATE

ENV DEBIAN_FRONTEND noninteractive
ENV PYTHONIOENCODING utf8
ENV LC_ALL C.UTF-8
ENV LANG C.UTF-8
ENV PIP pip3

# avoid HOME/.local/share (hard to predict USER here)
# so let XDG_DATA_HOME coincide with fixed system location
# (can still be overridden by derived stages)
ENV XDG_DATA_HOME /usr/local/share

WORKDIR /build-ocrd
COPY setup.py .
COPY requirements.txt .
RUN apt-get update && \
apt-get -y install --no-install-recommends \
ca-certificates \
make \
git
COPY ocrd_kraken ./ocrd_kraken
RUN pip3 install --upgrade pip
RUN pip3 install .
COPY ocrd_kraken/ocrd-tool.json .
COPY README.md .
COPY requirements.txt .
COPY Makefile .
RUN make deps-ubuntu \
&& make deps install \
&& rm -fr /build-ocrd

WORKDIR /data
VOLUME /data

ENTRYPOINT ["/bin/sh", "-c"]
23 changes: 16 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ help:
@echo " deps Install python deps via pip"
@echo " deps-test Install testing deps via pip"
@echo " install Install"
@echo " docker Build docker image"
@echo " install-dev Install in editable mode"
@echo " docker Build Docker image"
@echo " test Run test"
@echo " repo/assets Clone OCR-D/assets to ./repo/assets"
@echo " assets Setup test assets"
@echo " tests/assets Setup test assets"
@echo ""
@echo " Variables"
@echo ""
@echo " DOCKER_TAG Docker container tag ("$(DOCKER_TAG)")"
@echo " PYTEST_ARGS Additional runtime options for pytest ("$(PYTEST_ARGS)")"

# END-EVAL

Expand All @@ -44,16 +46,21 @@ deps-test:

# Install
install:
$(PIP) install .

install-dev:
$(PIP) install -e .

# Build docker image
docker:
docker build -t $(DOCKER_TAG) .
docker build \
--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: test
# Run test
test:
$(PYTHON) -m pytest tests
test: tests/assets
$(PYTHON) -m pytest tests $(PYTEST_ARGS)

#
# Assets
Expand All @@ -66,6 +73,8 @@ repo/assets:


# Setup test assets
assets: repo/assets
tests/assets: repo/assets
mkdir -p tests/assets
cp -r -t tests/assets repo/assets/data/*

.PHONY: docker install install-dev deps deps-ubuntu deps-test test help
91 changes: 90 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,96 @@
# ocrd_kraken

> Wrapper for the kraken OCR engine
> OCR-D wrapper for the Kraken OCR engine

[![image](https://travis-ci.org/OCR-D/ocrd_kraken.svg?branch=master)](https://travis-ci.org/OCR-D/ocrd_kraken)
[![Docker Automated build](https://img.shields.io/docker/automated/ocrd/kraken.svg)](https://hub.docker.com/r/ocrd/kraken/tags/)
[![image](https://circleci.com/gh/OCR-D/ocrd_kraken.svg?style=svg)](https://circleci.com/gh/OCR-D/ocrd_kraken)

## Introduction

This package offers [OCR-D](https://ocr-d.de/en/spec) compliant [workspace processors](https://ocr-d.de/en/spec/cli)
for (some of) the functionality of [Kraken](https://kraken.re).

(Each processor is a parameterizable step in a configurable [workflow](https://ocr-d.de/en/workflows)
of the [OCR-D functional model](https://ocr-d.de/en/about).
There are usually various alternative processor implementations for each step.
Data is represented with [METS](https://ocr-d.de/en/spec/mets) and [PAGE](https://ocr-d.de/en/spec/page).)

It includes image preprocessing (binarization), layout analysis (region and line+baseline segmentation), and text recognition.

## Installation

### With Docker

This is the best option if you want to run the software in a container.

You need to have [Docker](https://docs.docker.com/install/linux/docker-ce/ubuntu/)


docker pull ocrd/kraken


To run with Docker:


docker run -v path/to/workspaces:/data ocrd/kraken ocrd-kraken-recognize ...

### Native, from PyPI

This is the best option if you want to use the stable, released version.

pip install ocrd_kraken


### Native, from git

Use this option if you want to change the source code or install the latest, unpublished changes.

We strongly recommend to use [venv](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/).

git clone https://github.com/OCR-D/ocrd_kraken
cd ocrd_kraken
sudo make deps-ubuntu # or manually from git or via ocrd_all
make deps # or pip install -r requirements.txt
make install # or pip install .

## Models

Kraken uses data-driven (neural) models for segmentation and recognition, but comes with no pretrained "official" models.
There is a [public repository](https://zenodo.org/communities/ocr_models) of community-provided models, which can also
be queried and downloaded from via `kraken` standalone CLI.
(See [Kraken docs](https://kraken.re/master/advanced.html#repo) for details.)

For the OCR-D wrapper, since all OCR-D processors must resolve file/data resources in a [standardized way](https://ocr-d.de/en/spec/cli#processor-resources), there is a general mechanism for managing models, i.e. installing and using them by name. We currently manage our own list of recommended models (without delegating to the above repo).

Models always use the filename suffix `.mlmodel`, but are just loaded by their basename.

See the [OCR-D model guide](https://ocr-d.de/en/models) and

ocrd resmgr --help

## Usage

For details, see docstrings in the individual processors and [ocrd-tool.json](ocrd_tesserocr/ocrd-tool.json) descriptions,
or simply `--help`.

Available [OCR-D processors](https://ocr-d.de/en/spec/cli) are:

- [ocrd-kraken-binarize](ocrd_kraken/binarize.py) (nlbin – not recommended)
- adds `AlternativeImage` files (per page, region or line) to the output fileGrp
- [ocrd-kraken-segment](ocrd_kraken/segment.py) (all-in-one segmentation – recommended for handwriting and simply layouted prints)
- adds `TextRegion`s, `TableRegion`s, `ImageRegion`s, `MathsRegion`s, `NoiseRegion`s, `ReadingOrder` and `AlternativeImage` to `Page` (depending on model training)
- adds `TextLine`s to `TextRegion`s, including their `Baseline`
- [ocrd-kraken-recognize](ocrd_kraken/recognize.py)
- adds `Word`s to `TextLine`s
- adds `Glyph`s to `Word`s
- adds `TextEquiv`

## Testing

make test


This downloads test data from https://github.com/OCR-D/assets under `repo/assets`, and runs some basic tests of the Python API.

Set `PYTEST_ARGS="-s --verbose"` to see log output (`-s`) and individual test results (`--verbose`).
3 changes: 0 additions & 3 deletions ocrd_kraken/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
from .binarize import KrakenBinarize
from .ocr import KrakenOcr
from .segment import KrakenSegment
5 changes: 0 additions & 5 deletions ocrd_kraken/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

from ocrd.decorators import ocrd_cli_options, ocrd_cli_wrap_processor
from ocrd_kraken.binarize import KrakenBinarize
from ocrd_kraken.segment import KrakenSegment

@click.command()
@ocrd_cli_options
def ocrd_kraken_binarize(*args, **kwargs):
return ocrd_cli_wrap_processor(KrakenBinarize, *args, **kwargs)

@click.command()
@ocrd_cli_options
def ocrd_kraken_segment(*args, **kwargs):
return ocrd_cli_wrap_processor(KrakenSegment, *args, **kwargs)
Empty file added ocrd_kraken/cli/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions ocrd_kraken/cli/binarize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import click

from ocrd.decorators import ocrd_cli_options, ocrd_cli_wrap_processor
from ocrd_kraken.binarize import KrakenBinarize

@click.command()
@ocrd_cli_options
def cli(*args, **kwargs):
return ocrd_cli_wrap_processor(KrakenBinarize, *args, **kwargs)
11 changes: 11 additions & 0 deletions ocrd_kraken/cli/recognize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import click

from ocrd.decorators import ocrd_cli_options, ocrd_cli_wrap_processor
from ocrd_kraken.recognize import KrakenRecognize

@click.command()
@ocrd_cli_options
def cli(*args, **kwargs):
return ocrd_cli_wrap_processor(KrakenRecognize, *args, **kwargs)


8 changes: 8 additions & 0 deletions ocrd_kraken/cli/segment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import click
from ocrd.decorators import ocrd_cli_options, ocrd_cli_wrap_processor
from ocrd_kraken.segment import KrakenSegment

@click.command()
@ocrd_cli_options
def cli(*args, **kwargs):
return ocrd_cli_wrap_processor(KrakenSegment, *args, **kwargs)
5 changes: 3 additions & 2 deletions ocrd_kraken/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from pkg_resources import resource_string
from pkg_resources import resource_filename

OCRD_TOOL = json.loads(resource_string(__name__, 'ocrd-tool.json').decode('utf8'))
with open(resource_filename(__name__, 'ocrd-tool.json'), 'r', encoding='utf-8') as f:
OCRD_TOOL = json.load(f)
44 changes: 0 additions & 44 deletions ocrd_kraken/ocr.py

This file was deleted.

Loading