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

Extracting images only #138

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
7983a65
filtering separators in a correct way without missing them
vahidrezanezhad Oct 14, 2023
f2811ee
add supported OS to readme
cneud Oct 24, 2023
6018b35
comment unnecessary print commands
vahidrezanezhad Nov 27, 2023
e7d12d3
first update for only images extraction
vahidrezanezhad Nov 27, 2023
6aac0b8
avoiding artifact images on the boundary of documents
vahidrezanezhad Nov 27, 2023
364ccac
adding extracting images only in cli
vahidrezanezhad Nov 27, 2023
aa41e4d
The contours of images can now be written in an XML file
vahidrezanezhad Nov 28, 2023
7cbca79
replacing images cotour with bounding box
vahidrezanezhad Dec 1, 2023
f09b7c1
use tf1 compatibility for keras backend
cneud Mar 19, 2024
b3fa684
pin tf2 version to 2.12.1
cneud Mar 19, 2024
533736a
update supported Python+Tensorflow version combinations
cneud Mar 19, 2024
ba64282
Update README.md
cneud Apr 3, 2024
899bb9f
update GitHub actions
cneud Apr 10, 2024
f88ee99
non-legacy namespace package
bertsky May 23, 2024
45bd76f
fix namespace pkg setup
bertsky May 24, 2024
721d3f7
Merge pull request #127 from bertsky/new-namespace-pkg
vahidrezanezhad May 24, 2024
ad133e3
Update model download url
cneud Jul 31, 2024
3cfa447
remove CircleCI
cneud Jul 31, 2024
40f5408
improve huggingface url
cneud Jul 31, 2024
38698c6
Update README.md
cneud Jul 31, 2024
8862df9
format options table
cneud Jul 31, 2024
c9f6382
create draft pyproject.toml
cneud Jul 31, 2024
7ded54a
rename GH action
cneud Jul 31, 2024
f0e7f75
Update README.md
cneud Jul 31, 2024
9170a9f
only images extraction - update inference parameters
vahidrezanezhad Aug 6, 2024
8e2cdad
extracting images only - avoid artifacts with heuristics
vahidrezanezhad Aug 7, 2024
e3edb0e
update
vahidrezanezhad Aug 9, 2024
23ac584
update pyproject.toml
cneud Aug 13, 2024
28ee1e5
update pyproject.toml for v0.3.1
cneud Aug 14, 2024
8f76966
update pyproject.toml for v0.3.1
cneud Aug 14, 2024
7f99526
update Makefile model location
cneud Aug 15, 2024
84d05bd
s,url,local_filename,
kba Aug 23, 2024
78bfa97
Merge pull request #129 from qurator-spk/resolving_issue_106
cneud Aug 23, 2024
9ae0575
:memo: changelog
kba Aug 27, 2024
a5c7f22
:package: v0.3.1
kba Aug 27, 2024
62314c4
fully transition to pyproject
kba Aug 27, 2024
9367f86
remove setup.py stub completely
kba Aug 29, 2024
84b8442
switch from qurator namespace to src-layout
kba Aug 29, 2024
256a7c3
Merge pull request #133 from qurator-spk/src-layout
cneud Aug 29, 2024
de32d86
Merge branch 'refs/heads/main' into extracting_images_only
cneud Sep 2, 2024
b6d3d2b
fix indentation
cneud Sep 2, 2024
6b2e5d1
all tests are passed
vahidrezanezhad Sep 3, 2024
c156a16
Exclude `run_image_extraction_over_ppn_lists.py` from merge
cneud Sep 3, 2024
351e9a8
update `ocrd-tool.json` with v0.3.1 models
cneud Sep 17, 2024
327b446
update Makefile with v0.3.1 models
cneud Sep 17, 2024
74a0699
extracting images only now works for a single image input
vahidrezanezhad Sep 19, 2024
723f27b
Add -eoi option to README.md
michalbubula Sep 19, 2024
d168edf
Update cli.py to block other processing in the case of extract_image_…
michalbubula Sep 19, 2024
f7e5fb9
resolving merge conflict of machine based reading order and extractin…
vahidrezanezhad Nov 5, 2024
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
51 changes: 0 additions & 51 deletions .circleci/config.yml

This file was deleted.

8 changes: 4 additions & 4 deletions .github/workflows/test-eynollah.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python package
name: Test

on: [push]

Expand All @@ -14,8 +14,8 @@ jobs:
python-version: ['3.8', '3.9', '3.10', '3.11']

steps:
- uses: actions/checkout@v2
- uses: actions/cache@v2
- uses: actions/checkout@v4
- uses: actions/cache@v4
id: model_cache
with:
path: models_eynollah
Expand All @@ -24,7 +24,7 @@ jobs:
if: steps.model_cache.outputs.cache-hit != 'true'
run: make models
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ Versioned according to [Semantic Versioning](http://semver.org/).

## Unreleased

## [0.3.1] - 2024-08-27

Fixed:

* regression in OCR-D processor, #106
* Expected Ptrcv::UMat for argument 'contour', #110
* Memory usage explosion with very narrow images (e.g. book spine), #67

## [0.3.0] - 2023-05-13

Changed:
Expand Down Expand Up @@ -117,6 +125,8 @@ Fixed:
Initial release

<!-- link-labels -->
[0.3.1]: ../../compare/v0.3.1...v0.3.0
[0.3.0]: ../../compare/v0.3.0...v0.2.0
[0.2.0]: ../../compare/v0.2.0...v0.1.0
[0.1.0]: ../../compare/v0.1.0...v0.0.11
[0.0.11]: ../../compare/v0.0.11...v0.0.10
Expand Down
8 changes: 3 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ help:
models: models_eynollah

models_eynollah: models_eynollah.tar.gz
# tar xf models_eynollah_renamed.tar.gz --transform 's/models_eynollah_renamed/models_eynollah/'
# tar xf models_eynollah_renamed.tar.gz
# tar xf 2022-04-05.SavedModel.tar.gz --transform 's/models_eynollah_renamed/models_eynollah/'
tar xf models_eynollah.tar.gz

models_eynollah.tar.gz:
# wget 'https://qurator-data.de/eynollah/2021-04-25/models_eynollah.tar.gz'
# wget 'https://qurator-data.de/eynollah/2022-04-05/models_eynollah_renamed.tar.gz'
# wget 'https://ocr-d.kba.cloud/2022-04-05.SavedModel.tar.gz'
wget https://github.com/qurator-spk/eynollah/releases/download/v0.3.0/models_eynollah.tar.gz
# wget 'https://qurator-data.de/eynollah/2022-04-05/models_eynollah_renamed_savedmodel.tar.gz'
# wget 'https://github.com/qurator-spk/eynollah/releases/download/v0.3.0/models_eynollah.tar.gz'
wget 'https://github.com/qurator-spk/eynollah/releases/download/v0.3.1/models_eynollah.tar.gz'

# Install with pip
install:
Expand Down
69 changes: 39 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Eynollah
> Document Layout Analysis (segmentation) using pre-trained models and heuristics
> Document Layout Analysis with Deep Learning and Heuristics

[![PyPI Version](https://img.shields.io/pypi/v/eynollah)](https://pypi.org/project/eynollah/)
[![CircleCI Build Status](https://circleci.com/gh/qurator-spk/eynollah.svg?style=shield)](https://circleci.com/gh/qurator-spk/eynollah)
[![GH Actions Test](https://github.com/qurator-spk/eynollah/actions/workflows/test-eynollah.yml/badge.svg)](https://github.com/qurator-spk/eynollah/actions/workflows/test-eynollah.yml)
[![License: ASL](https://img.shields.io/github/license/qurator-spk/eynollah)](https://opensource.org/license/apache-2-0/)
[![DOI](https://img.shields.io/badge/DOI-10.1145%2F3604951.3605513-red)](https://doi.org/10.1145/3604951.3605513)

![](https://user-images.githubusercontent.com/952378/102350683-8a74db80-3fa5-11eb-8c7e-f743f7d6eae2.jpg)

Expand All @@ -14,16 +14,18 @@
* Support for various image optimization operations:
* cropping (border detection), binarization, deskewing, dewarping, scaling, enhancing, resizing
* Text line segmentation to bounding boxes or polygons (contours) including for curved lines and vertical text
* Detection of reading order
* Detection of reading order (left-to-right or right-to-left)
* Output in [PAGE-XML](https://github.com/PRImA-Research-Lab/PAGE-XML)
* [OCR-D](https://github.com/qurator-spk/eynollah#use-as-ocr-d-processor) interface

:warning: Development is currently focused on achieving the best possible quality of results for a wide variety of historical documents and therefore processing can be very slow. We aim to improve this, but contributions are welcome.

## Installation
Python versions `3.8-3.11` with Tensorflow versions >=`2.12` are currently supported.
Python `3.8-3.11` with Tensorflow `2.12-2.15` on Linux are currently supported.

For (limited) GPU support the CUDA toolkit needs to be installed.

You can either install via
You can either install from PyPI

```
pip install eynollah
Expand All @@ -39,45 +41,49 @@ cd eynollah; pip install -e .
Alternatively, you can run `make install` or `make install-dev` for editable installation.

## Models
Pre-trained models can be downloaded from [qurator-data.de](https://qurator-data.de/eynollah/).
Pre-trained models can be downloaded from [qurator-data.de](https://qurator-data.de/eynollah/) or [huggingface](https://huggingface.co/SBB?search_models=eynollah).

In case you want to train your own model to use with Eynollah, have a look at [sbb_pixelwise_segmentation](https://github.com/qurator-spk/sbb_pixelwise_segmentation).
## Train
🚧 **Work in progress**

In case you want to train your own model, have a look at [`sbb_pixelwise_segmentation`](https://github.com/qurator-spk/sbb_pixelwise_segmentation).

## Usage
The command-line interface can be called like this:

```sh
eynollah \
-i <image file> \
-i <single image file> | -di <directory containing image files> \
-o <output directory> \
-m <path to directory containing model files> \
-m <directory containing model files> \
[OPTIONS]
```

The following options can be used to further configure the processing:

| option | description |
|----------|:-------------|
| `-fl` | full layout analysis including all steps and segmentation classes |
| `-light` | lighter and faster but simpler method for main region detection and deskewing |
| `-tab` | apply table detection |
| `-ae` | apply enhancement (the resulting image is saved to the output directory) |
| `-as` | apply scaling |
| `-cl` | apply contour detection for curved text lines instead of bounding boxes |
| `-ib` | apply binarization (the resulting image is saved to the output directory) |
| `-ep` | enable plotting (MUST always be used with `-sl`, `-sd`, `-sa`, `-si` or `-ae`) |
| `-ho` | ignore headers for reading order dectection |
| `-di <directory>` | process all images in a directory in batch mode |
| `-si <directory>` | save image regions detected to this directory |
| `-sd <directory>` | save deskewed image to this directory |
| `-sl <directory>` | save layout prediction as plot to this directory |
| `-sp <directory>` | save cropped page image to this directory |
| `-sa <directory>` | save all (plot, enhanced/binary image, layout) to this directory |

If no option is set, the tool will perform layout detection of main regions (background, text, images, separators and marginals).
The tool produces better quality output when RGB images are used as input than greyscale or binarized images.
| option | description |
|-------------------|:-------------------------------------------------------------------------------|
| `-fl` | full layout analysis including all steps and segmentation classes |
| `-light` | lighter and faster but simpler method for main region detection and deskewing |
| `-tab` | apply table detection |
| `-ae` | apply enhancement (the resulting image is saved to the output directory) |
| `-as` | apply scaling |
| `-cl` | apply contour detection for curved text lines instead of bounding boxes |
| `-ib` | apply binarization (the resulting image is saved to the output directory) |
| `-ep` | enable plotting (MUST always be used with `-sl`, `-sd`, `-sa`, `-si` or `-ae`) |
| `-eoi` | extract only images to output directory (other processing will not be done) |
| `-ho` | ignore headers for reading order dectection |
| `-si <directory>` | save image regions detected to this directory |
| `-sd <directory>` | save deskewed image to this directory |
| `-sl <directory>` | save layout prediction as plot to this directory |
| `-sp <directory>` | save cropped page image to this directory |
| `-sa <directory>` | save all (plot, enhanced/binary image, layout) to this directory |

If no option is set, the tool performs layout detection of main regions (background, text, images, separators and marginals).
The best output quality is produced when RGB images are used as input rather than greyscale or binarized images.

#### Use as OCR-D processor
🚧 **Work in progress**

Eynollah ships with a CLI interface to be used as [OCR-D](https://ocr-d.de) processor.

Expand All @@ -95,11 +101,14 @@ ocrd-eynollah-segment -I OCR-D-IMG-BIN -O SEG-LINE -P models

uses the original (RGB) image despite any binarization that may have occured in previous OCR-D processing steps

#### Additional documentation
Please check the [wiki](https://github.com/qurator-spk/eynollah/wiki).

## How to cite
If you find this tool useful in your work, please consider citing our paper:

```bibtex
@inproceedings{rezanezhad2023eynollah,
@inproceedings{hip23rezanezhad,
title = {Document Layout Analysis with Deep Learning and Heuristics},
author = {Rezanezhad, Vahid and Baierer, Konstantin and Gerber, Mike and Labusch, Kai and Neudecker, Clemens},
booktitle = {Proceedings of the 7th International Workshop on Historical Document Imaging and Processing {HIP} 2023,
Expand Down
2 changes: 1 addition & 1 deletion ocrd-tool.json
51 changes: 30 additions & 21 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"
requires = ["setuptools>=61.0", "wheel", "setuptools-ocrd"]

[project]
name = "eynollah"
version = "0.1.0"




dependencies = [
"ocrd >= 2.23.3",
"tensorflow == 2.12.1",
"scikit-learn >= 0.23.2",
"imutils >= 0.5.3",
"numpy < 1.24.0",
"matplotlib",
"torch == 2.0.1",
"transformers == 4.30.2",
"numba == 0.58.1",
authors = [
{name = "Vahid Rezanezhad"},
{name = "Staatsbibliothek zu Berlin - Preußischer Kulturbesitz"},
]
description = "Document Layout Analysis"
readme = "README.md"
license.file = "LICENSE"
requires-python = ">=3.8"
keywords = ["document layout analysis", "image segmentation"]

dynamic = ["dependencies", "version"]

classifiers = [
"Development Status :: 4 - Beta",
"Environment :: Console",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Topic :: Scientific/Engineering :: Image Processing",
]

[project.scripts]
eynollah = "qurator.eynollah.cli:main"
ocrd-eynollah-segment="qurator.eynollah.ocrd_cli:main"
eynollah = "eynollah.cli:main"
ocrd-eynollah-segment = "eynollah.ocrd_cli:main"

[project.urls]
Homepage = "https://github.com/qurator-spk/eynollah"
Repository = "https://github.com/qurator-spk/eynollah.git"

[tool.setuptools.dynamic]
dependencies = {file = ["requirements.txt"]}

[tool.setuptools.packages.find]
where = ["."]
include = ["qurator"]
where = ["src"]

[tool.setuptools.package-data]
"*" = ["*.json", '*.yml', '*.xml', '*.xsd']
1 change: 0 additions & 1 deletion qurator/__init__.py

This file was deleted.

1 change: 0 additions & 1 deletion qurator/eynollah/__init__.py

This file was deleted.

File renamed without changes.
17 changes: 14 additions & 3 deletions qurator/eynollah/cli.py → src/eynollah/cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import sys
import click
from ocrd_utils import initLogging, setOverrideLogLevel
from qurator.eynollah.eynollah import Eynollah
from qurator.eynollah.sbb_binarize import SbbBinarizer
from eynollah.eynollah import Eynollah
from eynollah.eynollah import Eynollah
from eynollah.sbb_binarize import SbbBinarizer

@click.group()
def main():
Expand Down Expand Up @@ -146,6 +147,12 @@ def binarization(patches, model_dir, input_image, output_image, dir_in, dir_out)
is_flag=True,
help="If set, will plot intermediary files and images",
)
@click.option(
"--extract_only_images/--disable-extracting_only_images",
"-eoi/-noeoi",
is_flag=True,
help="If a directory is given, only images in documents will be cropped and saved there and the other processing will not be done",
)
@click.option(
"--allow-enhancement/--no-allow-enhancement",
"-ae/-noae",
Expand Down Expand Up @@ -247,7 +254,7 @@ def binarization(patches, model_dir, input_image, output_image, dir_in, dir_out)
help="Override log level globally to this",
)

def layout(image, out, dir_in, model, save_images, save_layout, save_deskewed, save_all, save_page, enable_plotting, allow_enhancement, curved_line, textline_light, full_layout, tables, right2left, input_binary, allow_scaling, headers_off, light_version, reading_order_machine_based, do_ocr, num_col_upper, num_col_lower, skip_layout_and_reading_order, ignore_page_extraction, log_level):
def layout(image, out, dir_in, model, save_images, save_layout, save_deskewed, save_all, extract_only_images, save_page, enable_plotting, allow_enhancement, curved_line, textline_light, full_layout, tables, right2left, input_binary, allow_scaling, headers_off, light_version, reading_order_machine_based, do_ocr, num_col_upper, num_col_lower, skip_layout_and_reading_order, ignore_page_extraction, log_level):
if log_level:
setOverrideLogLevel(log_level)
initLogging()
Expand All @@ -260,6 +267,9 @@ def layout(image, out, dir_in, model, save_images, save_layout, save_deskewed, s
if textline_light and not light_version:
print('Error: You used -tll to enable light textline detection but -light is not enabled')
sys.exit(1)

if extract_only_images and (allow_enhancement or allow_scaling or light_version or curved_line or textline_light or full_layout or tables or right2left or headers_off) :
print('Error: You used -eoi which can not be enabled alongside light_version -light or allow_scaling -as or allow_enhancement -ae or curved_line -cl or textline_light -tll or full_layout -fl or tables -tab or right2left -r2l or headers_off -ho')
if light_version and not textline_light:
print('Error: You used -light without -tll. Light version need light textline to be enabled.')
sys.exit(1)
Expand All @@ -269,6 +279,7 @@ def layout(image, out, dir_in, model, save_images, save_layout, save_deskewed, s
dir_in=dir_in,
dir_models=model,
dir_of_cropped_images=save_images,
extract_only_images=extract_only_images,
dir_of_layout=save_layout,
dir_of_deskewed=save_deskewed,
dir_of_all=save_all,
Expand Down
Loading
Loading