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

Invalid bounding polygon computed exception during segmentation #632

Open
fattynoparents opened this issue Jul 31, 2024 · 8 comments
Open

Comments

@fattynoparents
Copy link

fattynoparents commented Jul 31, 2024

I'm trying to segment a bunch of images, however the segment command fails at one of the images with the following error message:

ERROR    Failed processing /home/user/images/189097.jpg: Invalid bounding polygon computed: 
Self-intersection[224.964516129032 302.148387096774]

What could cause this?
Thanks in advance.

Traceback:

╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
│ /path/to/kraken/bin/kraken:8 in <module>                                                    │
│                                                                                                  │
│   5 from kraken.kraken import cli                                                                │
│   6 if __name__ == '__main__':                                                                   │
│   7 │   sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])                         │
│ ❱ 8 │   sys.exit(cli())                                                                          │
│   9                                                                                              │
│                                                                                                  │
│ /path/to/kraken/lib/python3.11/site-packages/click/core.py:1157 in __call__                 │
│                                                                                                  │
│ /path/to/kraken/lib/python3.11/site-packages/click/core.py:1078 in main                     │
│                                                                                                  │
│ /path/to/kraken/lib/python3.11/site-packages/click/core.py:1720 in invoke                   │
│                                                                                                  │
│ /path/to/kraken/lib/python3.11/site-packages/click/core.py:1657 in _process_result          │
│                                                                                                  │
│ /path/to/kraken/lib/python3.11/site-packages/click/core.py:783 in invoke                    │
│                                                                                                  │
│ /path/to/kraken/lib/python3.11/site-packages/kraken/kraken.py:431 in process_pipeline       │
│                                                                                                  │
│   428 │   │   │   │   if len(fc) - 2 == idx:                                                     │
│   429 │   │   │   │   │   ctx.meta['last_process'] = True                                        │
│   430 │   │   │   │   with threadpool_limits(limits=ctx.meta['threads']):                        │
│ ❱ 431 │   │   │   │   │   task(input=input, output=output)                                       │
│   432 │   │   except Exception as e:                                                             │
│   433 │   │   │   logger.error(f'Failed processing {io_pair[0]}: {str(e)}')                      │
│   434 │   │   │   if ctx.meta['raise_failed']:                                                   │
│                                                                                                  │
│ /path/to/kraken/lib/python3.11/site-packages/kraken/kraken.py:154 in segmenter              │
│                                                                                                  │
│   151 │   │   │   │   │   │   │   │     pad=pad,                                                 │
│   152 │   │   │   │   │   │   │   │     mask=mask)                                               │
│   153 │   │   else:                                                                              │
│ ❱ 154 │   │   │   res = blla.segment(im, text_direction, mask=mask, model=models, device=devic   │
│   155 │   │   │   │   │   │   │      raise_on_error=ctx.meta['raise_failed'], autocast=ctx.met   │
│   156 │   except Exception:                                                                      │
│   157 │   │   if ctx.meta['raise_failed']:                                                       │
│                                                                                                  │
│ /path/to/kraken/lib/python3.11/site-packages/kraken/blla.py:342 in segment                  │
│                                                                                                  │
│   339 │   │   suppl_obj = scale_regions([x.boundary for x in suppl_obj], 1/rets['scale'])        │
│   340 │   │   line_regs = scale_regions([x.boundary for x in line_regs], 1/rets['scale'])        │
│   341 │   │                                                                                      │
│ ❱ 342 │   │   _lines = vec_lines(**rets,                                                         │
│   343 │   │   │   │   │   │      regions=line_regs,                                              │
│   344 │   │   │   │   │   │      text_direction=text_direction,                                  │
│   345 │   │   │   │   │   │      suppl_obj=suppl_obj,                                            │
│                                                                                                  │
│ /path/to/kraken/lib/python3.11/site-packages/kraken/blla.py:229 in vec_lines                │
│                                                                                                  │
│   226 │   │   for reg_idx, reg_pol in enumerate(reg_pols):                                       │
│   227 │   │   │   if is_in_region(bl_ls, reg_pol):                                               │
│   228 │   │   │   │   suppl_obj.append(regions[reg_idx])                                         │
│ ❱ 229 │   │   pol = calculate_polygonal_environment(baselines=[bl[1]],                           │
│   230 │   │   │   │   │   │   │   │   │   │   │     im_feats=im_feats,                           │
│   231 │   │   │   │   │   │   │   │   │   │   │     suppl_obj=suppl_obj,                         │
│   232 │   │   │   │   │   │   │   │   │   │   │     topline=topline,                             │
│                                                                                                  │
│ /path/to/kraken/lib/python3.11/site-packages/kraken/lib/segmentation.py:770 in              │
│ calculate_polygonal_environment                                                                  │
│                                                                                                  │
│    767 │   │   │                                                                                 │
│    768 │   │   │   env_up, env_bottom = _calc_roi(line, bounds, baselines[:idx] + baselines[idx  │
│    769 │   │   │                                                                                 │
│ ❱  770 │   │   │   polygons.append(_extract_patch(env_up,                                        │
│    771 │   │   │   │   │   │   │   │   │   │      env_bottom,                                    │
│    772 │   │   │   │   │   │   │   │   │   │      line.astype('int'),                            │
│    773 │   │   │   │   │   │   │   │   │   │      offset_line.astype('int'),                     │
│                                                                                                  │
│ /path/to/kraken/lib/python3.11/site-packages/kraken/lib/segmentation.py:620 in              │
│ _extract_patch                                                                                   │
│                                                                                                  │
│    617 │   │   polygon = np.concatenate(([end_points[-1]], upper_seam, [end_points[0]], bottom_  │
│    618 │   │   polygon = geom.Polygon(polygon)                                                   │
│    619 │   if not polygon.is_valid:                                                              │
│ ❱  620 │   │   raise Exception(f'Invalid bounding polygon computed: {explain_validity(polygon)}  │
│    621 │   polygon = np.array(roi_polygon.intersection(polygon).boundary.coords, dtype=int)      │
│    622 │   return polygon                                                                        │
│    623
@mittagessen
Copy link
Owner

mittagessen commented Jul 31, 2024 via email

@fattynoparents
Copy link
Author

fattynoparents commented Jul 31, 2024

Does it actually fail or does it just print this message and ignore the line?

Yes it fails and stops processing other images.

Usually polygonization fails for broken lines, e.g. short line fragments, very high curvature, things like that.

That's weird because I have much worse images that kraken processes fine. If I attach the image, will it help you to understand why this could happen?

EDIT: Now that I think a bit more about it, I remember that the previous dev version worked fine, that is it skipped unsuccessful images and continued the work on other images.

@fattynoparents
Copy link
Author

Hi, did you manage to reproduce the error? Do you think it should be easy to fix?

@fattynoparents
Copy link
Author

Getting a new exception now, and the segment command stops executing:

TopologyException: side location conflict at 1345.5620723362658 1028.5092864125122. This can occur if the input geometry is invalid.
[08/15/24 11:58:28] ERROR    Failed processing /images/29022.jpg: No Shapely geometry can be created from   kraken.py:433
                             null value

@fattynoparents
Copy link
Author

Any news on fixing this?

@mittagessen
Copy link
Owner

Sorry, it's vacation time in France so as a good assimilating foreigner I've been off all of August.

Could you give me the image it happens on? And try the latest main branch as a bug in the polygonizer got fixed that caused weird bounding polygons (which are prone to triggering these kinds of shapely errors).

@fattynoparents
Copy link
Author

Oh, sorry that I got on you, hope you had a good rest :)

I use the latest dev version (I guess?) 5.2.9.dev6, get the exception:

TopologyException: side location conflict at 1345.5620723362658 1028.5092864125122. This can occur if the input geometry is invalid.
[08/28/24 08:14:39] ERROR    Failed processing /images/29022.jpg: No Shapely geometry can be  created from null value

on this image:
image

@fattynoparents
Copy link
Author

fattynoparents commented Sep 6, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants