Skip to content

Commit

Permalink
Self-intersection handling (#961)
Browse files Browse the repository at this point in the history
Co-authored-by: fatih <[email protected]>
  • Loading branch information
sergiev and fcakyon authored Nov 5, 2023
1 parent e161468 commit 3061811
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions sahi/utils/coco.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from typing import Dict, List, Optional, Set, Union

import numpy as np
from shapely import MultiPolygon
from shapely.validation import make_valid
from tqdm import tqdm

from sahi.utils.file import is_colab, load_json, save_json
Expand Down Expand Up @@ -223,6 +225,12 @@ def __init__(

def get_sliced_coco_annotation(self, slice_bbox: List[int]):
shapely_polygon = box(slice_bbox[0], slice_bbox[1], slice_bbox[2], slice_bbox[3])
samp = self._shapely_annotation.multipolygon
if not samp.is_valid:
valid = make_valid(samp)
if not isinstance(valid, MultiPolygon):
valid = MultiPolygon([valid])
self._shapely_annotation.multipolygon = valid
intersection_shapely_annotation = self._shapely_annotation.get_intersection(shapely_polygon)
return CocoAnnotation.from_shapely_annotation(
intersection_shapely_annotation,
Expand Down

0 comments on commit 3061811

Please sign in to comment.