mirror of
https://github.com/qurator-spk/sbb_binarization.git
synced 2025-06-09 12:19:56 +02:00
convert between cv2 and pil, DRY binarizer call
This commit is contained in:
parent
fabb63834a
commit
12b44af329
2 changed files with 32 additions and 23 deletions
|
@ -4,7 +4,11 @@ import os.path
|
||||||
from pkg_resources import resource_string
|
from pkg_resources import resource_string
|
||||||
from json import loads
|
from json import loads
|
||||||
|
|
||||||
|
from PIL import Image
|
||||||
|
import numpy as np
|
||||||
|
import cv2
|
||||||
from click import command
|
from click import command
|
||||||
|
|
||||||
from ocrd_utils import (
|
from ocrd_utils import (
|
||||||
getLogger,
|
getLogger,
|
||||||
assert_file_grp_cardinality,
|
assert_file_grp_cardinality,
|
||||||
|
@ -21,6 +25,16 @@ from .sbb_binarize import SbbBinarizer
|
||||||
OCRD_TOOL = loads(resource_string(__name__, 'ocrd-tool.json').decode('utf8'))
|
OCRD_TOOL = loads(resource_string(__name__, 'ocrd-tool.json').decode('utf8'))
|
||||||
TOOL = 'ocrd-sbb-binarize'
|
TOOL = 'ocrd-sbb-binarize'
|
||||||
|
|
||||||
|
def cv2pil(img):
|
||||||
|
color_coverted = cv2.cvtColor(img, cv2.COLOR_BGRA2RGB)
|
||||||
|
return Image.fromarray(color_coverted)
|
||||||
|
|
||||||
|
def pil2cv(img):
|
||||||
|
# from ocrd/workspace.py
|
||||||
|
color_conversion = cv2.COLOR_GRAY2BGR if img.mode in ('1', 'L') else cv2.COLOR_RGB2BGR
|
||||||
|
pil_as_np_array = np.array(img).astype('uint8') if img.mode == '1' else np.array(img)
|
||||||
|
return cv2.cvtColor(pil_as_np_array, color_conversion)
|
||||||
|
|
||||||
class SbbBinarizeProcessor(Processor):
|
class SbbBinarizeProcessor(Processor):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
@ -28,6 +42,14 @@ class SbbBinarizeProcessor(Processor):
|
||||||
kwargs['version'] = OCRD_TOOL['version']
|
kwargs['version'] = OCRD_TOOL['version']
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
def _run_binarizer(self, img):
|
||||||
|
return cv2pil(
|
||||||
|
SbbBinarizer(
|
||||||
|
image=pil2cv(img),
|
||||||
|
model=self.model_path,
|
||||||
|
patches=self.use_patches,
|
||||||
|
save=None).run())
|
||||||
|
|
||||||
def process(self):
|
def process(self):
|
||||||
"""
|
"""
|
||||||
Binarize with sbb_binarization
|
Binarize with sbb_binarization
|
||||||
|
@ -37,8 +59,8 @@ class SbbBinarizeProcessor(Processor):
|
||||||
assert_file_grp_cardinality(self.output_file_grp, 1)
|
assert_file_grp_cardinality(self.output_file_grp, 1)
|
||||||
|
|
||||||
oplevel = self.parameter['operation_level']
|
oplevel = self.parameter['operation_level']
|
||||||
use_patches = self.parameter['patches']
|
self.use_patches = self.parameter['patches'] # pylint: disable=attribute-defined-outside-init
|
||||||
model_path = self.parameter['model']
|
self.model_path = self.parameter['model'] # pylint: disable=attribute-defined-outside-init
|
||||||
|
|
||||||
for n, input_file in enumerate(self.input_files):
|
for n, input_file in enumerate(self.input_files):
|
||||||
file_id = make_file_id(input_file, self.output_file_grp)
|
file_id = make_file_id(input_file, self.output_file_grp)
|
||||||
|
@ -52,12 +74,7 @@ class SbbBinarizeProcessor(Processor):
|
||||||
if oplevel == 'page':
|
if oplevel == 'page':
|
||||||
LOG.info("Binarizing on 'page' level in page '%s'", page_id)
|
LOG.info("Binarizing on 'page' level in page '%s'", page_id)
|
||||||
page_image, page_xywh, _ = self.workspace.image_from_page(page, page_id)
|
page_image, page_xywh, _ = self.workspace.image_from_page(page, page_id)
|
||||||
bin_image = SbbBinarizer(
|
bin_image = self._run_binarizer(page_image)
|
||||||
image=page_image,
|
|
||||||
model=model_path,
|
|
||||||
patches=use_patches,
|
|
||||||
save=None
|
|
||||||
).run()
|
|
||||||
# update METS (add the image file):
|
# update METS (add the image file):
|
||||||
bin_image_path = self.workspace.save_image_file(bin_image,
|
bin_image_path = self.workspace.save_image_file(bin_image,
|
||||||
file_id + '.IMG-BIN',
|
file_id + '.IMG-BIN',
|
||||||
|
@ -74,12 +91,7 @@ class SbbBinarizeProcessor(Processor):
|
||||||
region_image, region_xywh = self.workspace.image_from_segment(region, page_image, page_xywh)
|
region_image, region_xywh = self.workspace.image_from_segment(region, page_image, page_xywh)
|
||||||
|
|
||||||
if oplevel == 'region':
|
if oplevel == 'region':
|
||||||
region_image_bin = SbbBinarizer(
|
region_image_bin = self._run_binarizer(region_image)
|
||||||
image=region_image,
|
|
||||||
model=model_path,
|
|
||||||
patches=use_patches,
|
|
||||||
save=None
|
|
||||||
).run()
|
|
||||||
region_image_bin_path = self.workspace.save_image_file(
|
region_image_bin_path = self.workspace.save_image_file(
|
||||||
region_image_bin,
|
region_image_bin,
|
||||||
"%s_%s.IMG-BIN" % (file_id, region.id),
|
"%s_%s.IMG-BIN" % (file_id, region.id),
|
||||||
|
@ -94,12 +106,7 @@ class SbbBinarizeProcessor(Processor):
|
||||||
LOG.warning("Page '%s' region '%s' contains no text lines", page_id, region.id)
|
LOG.warning("Page '%s' region '%s' contains no text lines", page_id, region.id)
|
||||||
for line in lines:
|
for line in lines:
|
||||||
line_image, line_xywh = self.workspace.image_from_segment(line, page_image, page_xywh)
|
line_image, line_xywh = self.workspace.image_from_segment(line, page_image, page_xywh)
|
||||||
line_image_bin = SbbBinarizer(
|
line_image_bin = self._run_binarizer(line_image)
|
||||||
image=line_image,
|
|
||||||
model=model_path,
|
|
||||||
patches=use_patches,
|
|
||||||
save=None
|
|
||||||
).run()
|
|
||||||
line_image_bin_path = self.workspace.save_image_file(
|
line_image_bin_path = self.workspace.save_image_file(
|
||||||
line_image_bin,
|
line_image_bin,
|
||||||
"%s_%s_%s.IMG-BIN" % (file_id, region.id, line.id),
|
"%s_%s_%s.IMG-BIN" % (file_id, region.id, line.id),
|
||||||
|
|
|
@ -8,6 +8,7 @@ from os.path import join
|
||||||
from warnings import catch_warnings, simplefilter
|
from warnings import catch_warnings, simplefilter
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
from PIL import Image
|
||||||
import cv2
|
import cv2
|
||||||
environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
||||||
stderr = sys.stderr
|
stderr = sys.stderr
|
||||||
|
@ -23,9 +24,10 @@ class SbbBinarizer:
|
||||||
|
|
||||||
# TODO use True/False for patches
|
# TODO use True/False for patches
|
||||||
def __init__(self, model, image=None, image_path=None, patches='false', save=None):
|
def __init__(self, model, image=None, image_path=None, patches='false', save=None):
|
||||||
if not(image or image_path) or (image and image_path):
|
if (image is not None and image_path is not None) or \
|
||||||
raise ValueError("Must pass either a PIL image or an image_path")
|
(image is None and image_path is None):
|
||||||
if image:
|
raise ValueError("Must pass either a opencv2 image or an image_path")
|
||||||
|
if image is not None:
|
||||||
self.image = image
|
self.image = image
|
||||||
else:
|
else:
|
||||||
self.image = cv2.imread(self.image)
|
self.image = cv2.imread(self.image)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue