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

Propagation of the --raise-on-error for raising non-blocking errors in blla segmentation #444

Merged
merged 2 commits into from
Feb 21, 2023
Merged
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
19 changes: 16 additions & 3 deletions kraken/blla.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def vec_lines(heatmap: torch.Tensor,
scal_im: np.ndarray = None,
suppl_obj: List[np.ndarray] = None,
topline: Optional[bool] = False,
raise_on_error: bool = False,
**kwargs) -> List[Dict[str, Any]]:
r"""
Computes lines from a stack of heatmaps, a class mapping, and scaling
Expand All @@ -185,6 +186,8 @@ def vec_lines(heatmap: torch.Tensor,
polygonization.
topline: True for a topline, False for baseline, or None for a
centerline.
raise_on_error: Raises error instead of logging them when they are
not-blocking

Returns:
A list of dictionaries containing the baselines, bounding polygons, and
Expand Down Expand Up @@ -222,7 +225,13 @@ def vec_lines(heatmap: torch.Tensor,
if reg_pol.contains(mid_point):
suppl_obj.append(regions[reg_idx])

pol = calculate_polygonal_environment(baselines=[bl[1]], im_feats=im_feats, suppl_obj=suppl_obj, topline=topline)
pol = calculate_polygonal_environment(
baselines=[bl[1]],
im_feats=im_feats,
suppl_obj=suppl_obj,
topline=topline,
raise_on_error=raise_on_error
)
if pol[0] is not None:
lines.append((bl[0], bl[1], pol[0]))

Expand All @@ -239,7 +248,8 @@ def segment(im: PIL.Image.Image,
mask: Optional[np.ndarray] = None,
reading_order_fn: Callable = polygonal_reading_order,
model: Union[List[vgsl.TorchVGSLModel], vgsl.TorchVGSLModel] = None,
device: str = 'cpu') -> Dict[str, Any]:
device: str = 'cpu',
raise_on_error: bool = False) -> Dict[str, Any]:
r"""
Segments a page into text lines using the baseline segmenter.

Expand All @@ -260,6 +270,8 @@ def segment(im: PIL.Image.Image,
model: One or more TorchVGSLModel containing a segmentation model. If
none is given a default model will be loaded.
device: The target device to run the neural network on.
raise_on_error: Raises error instead of logging them when they are
not-blocking

Returns:
A dictionary containing the text direction and under the key 'lines' a
Expand Down Expand Up @@ -327,7 +339,8 @@ def segment(im: PIL.Image.Image,
reading_order_fn=reading_order_fn,
text_direction=text_direction,
suppl_obj=suppl_obj,
topline=net.user_metadata['topline'] if 'topline' in net.user_metadata else False)
topline=net.user_metadata['topline'] if 'topline' in net.user_metadata else False,
raise_on_error=raise_on_error)

if len(rets['cls_map']['baselines']) > 1:
script_detection = True
Expand Down
3 changes: 2 additions & 1 deletion kraken/kraken.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ def segmenter(legacy, model, text_direction, scale, maxcolseps, black_colseps,
pad=pad,
mask=mask)
else:
res = blla.segment(im, text_direction, mask=mask, model=model, device=device)
res = blla.segment(im, text_direction, mask=mask, model=model, device=device,
raise_on_error=ctx.meta['raise_failed'])
except Exception:
if ctx.meta['raise_failed']:
raise
Expand Down
7 changes: 6 additions & 1 deletion kraken/lib/segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,8 @@ def calculate_polygonal_environment(im: PIL.Image.Image = None,
suppl_obj: Sequence[Sequence[Tuple[int, int]]] = None,
im_feats: np.ndarray = None,
scale: Tuple[int, int] = None,
topline: bool = False):
topline: bool = False,
raise_on_error: bool = False):
"""
Given a list of baselines and an input image, calculates a polygonal
environment around each baseline.
Expand All @@ -666,6 +667,8 @@ def calculate_polygonal_environment(im: PIL.Image.Image = None,
be offset upwards, if set to True, baselines are on the
top and will be offset downwards. If set to None, no
offset will be applied.
raise_on_error: Raises error instead of logging them when they are
not-blocking
Returns:
List of lists of coordinates. If no polygonization could be compute for
a baseline `None` is returned instead.
Expand Down Expand Up @@ -727,6 +730,8 @@ def calculate_polygonal_environment(im: PIL.Image.Image = None,
im_feats,
bounds))
except Exception as e:
if raise_on_error:
raise
logger.warning(f'Polygonizer failed on line {idx}: {e}')
polygons.append(None)

Expand Down