Skip to content

Commit

Permalink
Merge pull request #1 from wrznr/add_basic_stuff
Browse files Browse the repository at this point in the history
Add the basic project layout and minimal functionality
  • Loading branch information
wrznr committed Aug 13, 2019
2 parents 27e8e4a + 5705e82 commit ed0a68c
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 0 deletions.
20 changes: 20 additions & 0 deletions LICENSE
@@ -0,0 +1,20 @@
Copyright © 2019 Kay-Michael Würzner

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1 change: 1 addition & 0 deletions ocrd-tool.json
1 change: 1 addition & 0 deletions ocrd_evaluate_segmentation/__init__.py
@@ -0,0 +1 @@
from .evaluate import EvaluateSegmentation
9 changes: 9 additions & 0 deletions ocrd_evaluate_segmentation/cli.py
@@ -0,0 +1,9 @@
import click

from ocrd.decorators import ocrd_cli_options, ocrd_cli_wrap_processor
from ocrd_evaluate_segmentation.evaluate import EvaluateSegmentation

@click.command()
@ocrd_cli_options
def ocrd_evaluate_segmentation(*args, **kwargs):
return ocrd_cli_wrap_processor(EvaluateSegmentation, *args, **kwargs)
5 changes: 5 additions & 0 deletions ocrd_evaluate_segmentation/config.py
@@ -0,0 +1,5 @@
import os
import json
from pkg_resources import resource_string

OCRD_TOOL = json.loads(resource_string(__name__, 'ocrd-tool.json').decode('utf8'))
118 changes: 118 additions & 0 deletions ocrd_evaluate_segmentation/evaluate.py
@@ -0,0 +1,118 @@
from __future__ import absolute_import

import os.path

from ocrd import Processor
from ocrd_utils import (
getLogger, concat_padded,
polygon_from_points,
MIMETYPE_PAGE
)
from ocrd_modelfactory import page_from_file
from ocrd_models.ocrd_page import (
CoordsType,
LabelType, LabelsType,
MetadataItemType,
TextLineType,
to_xml
)

from .config import OCRD_TOOL

from shapely.geometry import Polygon

TOOL = 'ocrd-evaluate-segmentation'
LOG = getLogger('processor.EvaluateSegmentation')

class EvaluateSegmentation(Processor):

def __init__(self, *args, **kwargs):
kwargs['ocrd_tool'] = OCRD_TOOL['tools'][TOOL]
kwargs['version'] = OCRD_TOOL['version']
super(EvaluateSegmentation, self).__init__(*args, **kwargs)


def process(self):
"""Performs segmentation evaluation with Shapely on the workspace.
Open and deserialize PAGE input files and their respective images,
then iterate over the element hierarchy down to the region level.
Return information on the plausibility of the segmentation into
regions on the logging level.
"""
plausibilize = self.parameter['plausibilize']

for (n, input_file) in enumerate(self.input_files):
page_id = input_file.pageId or input_file.ID
LOG.info("INPUT FILE %i / %s", n, page_id)
pcgts = page_from_file(self.workspace.download_file(input_file))
metadata = pcgts.get_Metadata() # ensured by from_file()
metadata.add_MetadataItem(
MetadataItemType(type_="processingStep",
name=self.ocrd_tool['steps'][0],
value=TOOL,
# FIXME: externalRef is invalid by pagecontent.xsd, but ocrd does not reflect this
# what we want here is `externalModel="ocrd-tool" externalId="parameters"`
Labels=[LabelsType(#externalRef="parameters",
Label=[LabelType(type_=name,
value=self.parameter[name])
for name in self.parameter.keys()])]))
page = pcgts.get_Page()

regions = page.get_TextRegion()

mark_for_deletion = set()
mark_for_merging = set()

for i in range(0,len(regions)):
for j in range(i+1,len(regions)):
LOG.info('Comparing regions "%s" and "%s"', regions[i].id, regions[j].id)
region_poly1 = Polygon(polygon_from_points(regions[i].get_Coords().points))
region_poly2 = Polygon(polygon_from_points(regions[j].get_Coords().points))

LOG.debug('Checking for equality ...')
equality = region_poly1.almost_equals(region_poly2)
if equality:
LOG.warn('Warning: regions %s and %s cover the same area.' % (regions[i].id, regions[j].id))
mark_for_deletion.add(j)

LOG.debug('Checking for containment ...')
containment_r = region_poly1.contains(region_poly2)
containment_l = region_poly2.contains(region_poly1)
if containment_r:
LOG.warn('Warning: %s contains %s' % (regions[i].id, regions[j].id))
mark_for_deletion.add(j)
if containment_l:
LOG.warn('Warning: %s contains %s' % (regions[j].id, regions[i].id))
mark_for_deletion.add(i)

if plausibilize:
new_regions = []
for i in range(0,len(regions)):
if not i in mark_for_deletion:
new_regions.append(regions[i])
page.set_TextRegion(new_regions)


#LOG.info('Intersection %i', region_poly1.intersects(region_poly2))
#LOG.info('Containment %i', region_poly1.contains(region_poly2))
#if region_poly1.intersects(region_poly2):
# LOG.info('Area 1 %d', region_poly1.area)
# LOG.info('Area 2 %d', region_poly2.area)
# LOG.info('Area intersect %d', region_poly1.intersection(region_poly2).area)


# Use input_file's basename for the new file -
# this way the files retain the same basenames:
file_id = input_file.ID.replace(self.input_file_grp, self.output_file_grp)
if file_id == input_file.ID:
file_id = concat_padded(self.output_file_grp, n)
self.workspace.add_file(
ID=file_id,
file_grp=self.output_file_grp,
pageId=input_file.pageId,
mimetype=MIMETYPE_PAGE,
local_filename=os.path.join(self.output_file_grp,
file_id + '.xml'),
content=to_xml(pcgts))
26 changes: 26 additions & 0 deletions ocrd_evaluate_segmentation/ocrd-tool.json
@@ -0,0 +1,26 @@
{
"version": "0.0.1",
"git_url": "https://github.com/OCR-D/ocrd_evaluate_segmentation",
"tools": {
"ocrd-evaluate-segmentation": {
"executable": "ocrd-evaluate-segmentation",
"categories": ["Image preprocessing"],
"description": "Evaluate regions",
"input_file_grp": [
"OCR-D-IMG",
"OCR-D-SEG-BLOCK"
],
"output_file_grp": [
"OCR-D-EVAL-BLOCK"
],
"steps": ["preprocessing/optimization/deskewing"],
"parameters": {
"plausibilize": {
"type": "boolean",
"default": false,
"description": "Modify the region segmentation to make it (more) plausible"
}
}
}
}
}
3 changes: 3 additions & 0 deletions requirements.txt
@@ -0,0 +1,3 @@
ocrd >= 1.0.0b11
click
shapely
30 changes: 30 additions & 0 deletions setup.py
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
"""
Installs:
- ocrd-evaluate-segmentation
"""
import codecs

from setuptools import setup, find_packages

setup(
name='ocrd_evaluate_segmentation',
version='0.0.1',
description='Segmentation evaluation',
long_description=codecs.open('README.md', encoding='utf-8').read(),
author='Konstantin Baierer, Kay-Michael Würzner, Robert Sachunsky',
author_email='unixprog@gmail.com, wuerzner@gmail.com, sachunsky@informatik.uni-leipzig.de',
url='https://github.com/OCR-D/ocrd_evaluate_segmentation',
license='Apache License 2.0',
packages=find_packages(exclude=('tests', 'docs')),
install_requires=open('requirements.txt').read().split('\n'),
package_data={
'': ['*.json', '*.yml', '*.yaml'],
},
entry_points={
'console_scripts': [
'ocrd-evaluate-segmentation=ocrd_evaluate_segmentation.cli:ocrd_evaluate_segmentation',
]
},
)

0 comments on commit ed0a68c

Please sign in to comment.