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

Adjust return value for image load error in extract line & line path #665

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 7 additions & 5 deletions kraken/lib/arrow_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ def _extract_line(xml_record, skip_empty_lines: bool = True, legacy_polygons: bo
im = Image.open(xml_record.imagename)
if is_bitonal(im):
im = im.convert('1')
except (OSError, FileNotFoundError, UnidentifiedImageError):
return lines, None, None
except (OSError, FileNotFoundError, UnidentifiedImageError) as err:
logger.warning(f'Error loading image {xml_record.imagename}: {err}')
return lines, None
for idx, rec in enumerate(xml_record.lines):
seg = Segmentation(text_direction='horizontal-lr',
imagename=xml_record.imagename,
Expand Down Expand Up @@ -79,10 +80,11 @@ def _extract_line(xml_record, skip_empty_lines: bool = True, legacy_polygons: bo
def _extract_path_line(xml_record, skip_empty_lines: bool = True):
try:
im = Image.open(xml_record['image'])
except (FileNotFoundError, UnidentifiedImageError):
return [], None, None
except (FileNotFoundError, UnidentifiedImageError) as err:
logger.warning(f'Error loading image {xml_record.imagename}: {err}')
return [], None
if not xml_record['lines'][0]['text'] and skip_empty_lines:
return [], None, None
return [], None
if is_bitonal(im):
im = im.convert('1')
fp = io.BytesIO()
Expand Down
25 changes: 24 additions & 1 deletion tests/test_arrow_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pyarrow as pa

from pathlib import Path
from pytest import raises
from pytest import raises, fixture

import kraken
from kraken.lib import xml
Expand Down Expand Up @@ -89,3 +89,26 @@ def test_build_empty_dataset(self):
format_type='xml',
skip_empty_lines=False)
_validate_ds(self, tmp_file.name, 5, 1, 'kraken_recognition_baseline')

@fixture(autouse=True)
def caplog_fixture(self, caplog):
# make pytest caplog fixture available
self.caplog = caplog

def test_build_image_error(self):
"""
Test that image load errors are handled.
"""
# change resource path so it will not resolve
bad_box_lines = [path.with_name(f"bogus_{path.stem}") for path in self.box_lines]
with tempfile.NamedTemporaryFile() as tmp_file:
build_binary_dataset(files=bad_box_lines,
output_file=tmp_file.name,
format_type='xml')
# expect zero resulting lines due to image load error
_validate_ds(self, tmp_file.name, 0, 0, 'kraken_recognition_baseline')
# expect one warning log message; should include the file image filename
assert len(self.caplog.records) == 1
log_record = self.caplog.records[0]
assert log_record.levelname == "WARNING"
assert f"Invalid input file {bad_box_lines[0]}" in log_record.message