fix(pre_commit): 🎨 auto format pre-commit hooks
This commit is contained in:
parent
d8679d9c46
commit
eefa05cf01
|
|
@ -10,8 +10,8 @@ import numpy.typing as npt
|
|||
from supervision.dataset.utils import (
|
||||
approximate_mask_with_polygons,
|
||||
map_detections_class_id,
|
||||
mask_to_rle,
|
||||
rle_to_mask,
|
||||
mask_to_rle
|
||||
)
|
||||
from supervision.detection.core import Detections
|
||||
from supervision.detection.utils import polygon_to_mask
|
||||
|
|
@ -107,9 +107,10 @@ def coco_annotations_to_detections(
|
|||
return Detections(xyxy=xyxy, class_id=np.asarray(class_ids, dtype=int))
|
||||
|
||||
|
||||
def _mask_has_holes(mask: np.ndarray)-> bool:
|
||||
_, hierarchy = cv2.findContours(mask.astype(np.uint8), cv2.RETR_CCOMP,
|
||||
cv2.CHAIN_APPROX_SIMPLE)
|
||||
def _mask_has_holes(mask: np.ndarray) -> bool:
|
||||
_, hierarchy = cv2.findContours(
|
||||
mask.astype(np.uint8), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE
|
||||
)
|
||||
parent_countour_index = 3
|
||||
for h in hierarchy[0]:
|
||||
if h[parent_countour_index] != -1:
|
||||
|
|
@ -117,9 +118,9 @@ def _mask_has_holes(mask: np.ndarray)-> bool:
|
|||
return False
|
||||
|
||||
|
||||
def _mask_has_multiple_segments(mask: np.ndarray)-> bool:
|
||||
def _mask_has_multiple_segments(mask: np.ndarray) -> bool:
|
||||
number_of_labels, _ = cv2.connectedComponents(mask.astype(np.uint8), connectivity=4)
|
||||
return number_of_labels > 2
|
||||
return number_of_labels > 2
|
||||
|
||||
|
||||
def detections_to_coco_annotations(
|
||||
|
|
@ -136,21 +137,26 @@ def detections_to_coco_annotations(
|
|||
segmentation = []
|
||||
iscrowd = 0
|
||||
if mask is not None:
|
||||
iscrowd = _mask_has_holes(mask = mask) or \
|
||||
_mask_has_multiple_segments(mask = mask)
|
||||
iscrowd = _mask_has_holes(mask=mask) or _mask_has_multiple_segments(
|
||||
mask=mask
|
||||
)
|
||||
|
||||
if iscrowd:
|
||||
segmentation = {"counts": mask_to_rle(mask=mask),
|
||||
"size": list(mask.shape[:2])}
|
||||
segmentation = {
|
||||
"counts": mask_to_rle(mask=mask),
|
||||
"size": list(mask.shape[:2]),
|
||||
}
|
||||
else:
|
||||
segmentation = [list(
|
||||
approximate_mask_with_polygons(
|
||||
mask=mask,
|
||||
min_image_area_percentage=min_image_area_percentage,
|
||||
max_image_area_percentage=max_image_area_percentage,
|
||||
approximation_percentage=approximation_percentage,
|
||||
)[0].flatten()
|
||||
)] # multicomponent masks supported only for rle format
|
||||
segmentation = [
|
||||
list(
|
||||
approximate_mask_with_polygons(
|
||||
mask=mask,
|
||||
min_image_area_percentage=min_image_area_percentage,
|
||||
max_image_area_percentage=max_image_area_percentage,
|
||||
approximation_percentage=approximation_percentage,
|
||||
)[0].flatten()
|
||||
)
|
||||
] # multicomponent masks supported only for rle format
|
||||
coco_annotation = {
|
||||
"id": annotation_id,
|
||||
"image_id": image_id,
|
||||
|
|
|
|||
|
|
@ -10,8 +10,8 @@ from supervision.dataset.formats.coco import (
|
|||
classes_to_coco_categories,
|
||||
coco_annotations_to_detections,
|
||||
coco_categories_to_classes,
|
||||
detections_to_coco_annotations,
|
||||
group_coco_annotations_by_image_id,
|
||||
detections_to_coco_annotations
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -463,40 +463,46 @@ def test_build_coco_class_index_mapping(
|
|||
"detections, image_id, annotation_id, expected_result, exception",
|
||||
[
|
||||
(
|
||||
Detections(xyxy=np.array([[0, 0, 100, 100]], dtype=np.float32),
|
||||
class_id=np.array([0], dtype=int)),
|
||||
0,
|
||||
0,
|
||||
[mock_cock_coco_annotation(category_id=0, bbox=(0, 0, 100, 100), area=100 * 100)],
|
||||
DoesNotRaise(),
|
||||
), # no segmentation mask
|
||||
# (
|
||||
# Detections(
|
||||
# xyxy=np.array([[0, 0, 5, 5]], dtype=np.float32),
|
||||
# class_id=np.array([0], dtype=int),
|
||||
# mask=np.array(
|
||||
# [
|
||||
# [
|
||||
# [1, 1, 1, 0, 0],
|
||||
# [1, 1, 1, 0, 0],
|
||||
# [1, 1, 1, 1, 1],
|
||||
# [1, 1, 1, 1, 1],
|
||||
# [1, 1, 1, 1, 1],
|
||||
# ]
|
||||
# ]
|
||||
# ),
|
||||
# ),
|
||||
# 0,
|
||||
# 0,
|
||||
# [mock_cock_coco_annotation(
|
||||
# category_id=0,
|
||||
# bbox=(0, 0, 5, 5),
|
||||
# area=5 * 5,
|
||||
# segmentation=[[0, 0, 2, 0, 2, 2, 4, 2, 4, 4, 0, 4]])],
|
||||
# DoesNotRaise(),
|
||||
# ), # segmentation mask in single component,no holes in mask, expects polygon mask
|
||||
(
|
||||
Detections(
|
||||
Detections(
|
||||
xyxy=np.array([[0, 0, 100, 100]], dtype=np.float32),
|
||||
class_id=np.array([0], dtype=int),
|
||||
),
|
||||
0,
|
||||
0,
|
||||
[
|
||||
mock_cock_coco_annotation(
|
||||
category_id=0, bbox=(0, 0, 100, 100), area=100 * 100
|
||||
)
|
||||
],
|
||||
DoesNotRaise(),
|
||||
), # no segmentation mask
|
||||
# (
|
||||
# Detections(
|
||||
# xyxy=np.array([[0, 0, 5, 5]], dtype=np.float32),
|
||||
# class_id=np.array([0], dtype=int),
|
||||
# mask=np.array(
|
||||
# [
|
||||
# [
|
||||
# [1, 1, 1, 0, 0],
|
||||
# [1, 1, 1, 0, 0],
|
||||
# [1, 1, 1, 1, 1],
|
||||
# [1, 1, 1, 1, 1],
|
||||
# [1, 1, 1, 1, 1],
|
||||
# ]
|
||||
# ]
|
||||
# ),
|
||||
# ),
|
||||
# 0,
|
||||
# 0,
|
||||
# [mock_cock_coco_annotation(
|
||||
# category_id=0,
|
||||
# bbox=(0, 0, 5, 5),
|
||||
# area=5 * 5,
|
||||
# segmentation=[[0, 0, 2, 0, 2, 2, 4, 2, 4, 4, 0, 4]])],
|
||||
# DoesNotRaise(),
|
||||
# ), # segmentation mask in single component,no holes in mask, expects polygon mask
|
||||
(
|
||||
Detections(
|
||||
xyxy=np.array([[0, 0, 5, 5]], dtype=np.float32),
|
||||
class_id=np.array([0], dtype=int),
|
||||
mask=np.array(
|
||||
|
|
@ -511,21 +517,24 @@ def test_build_coco_class_index_mapping(
|
|||
]
|
||||
),
|
||||
),
|
||||
0,
|
||||
0,
|
||||
[mock_cock_coco_annotation(
|
||||
category_id=0,
|
||||
bbox=(0, 0, 5, 5),
|
||||
area=5 * 5,
|
||||
segmentation={
|
||||
0,
|
||||
0,
|
||||
[
|
||||
mock_cock_coco_annotation(
|
||||
category_id=0,
|
||||
bbox=(0, 0, 5, 5),
|
||||
area=5 * 5,
|
||||
segmentation={
|
||||
"size": [5, 5],
|
||||
"counts": [0, 3, 2, 3, 2, 3, 5, 2, 3, 2],
|
||||
},
|
||||
iscrowd=True, )],
|
||||
DoesNotRaise(),
|
||||
), # segmentation mask with 2 components, no holes in mask, expects RLE mask
|
||||
(
|
||||
Detections(
|
||||
iscrowd=True,
|
||||
)
|
||||
],
|
||||
DoesNotRaise(),
|
||||
), # segmentation mask with 2 components, no holes in mask, expects RLE mask
|
||||
(
|
||||
Detections(
|
||||
xyxy=np.array([[0, 0, 5, 5]], dtype=np.float32),
|
||||
class_id=np.array([0], dtype=int),
|
||||
mask=np.array(
|
||||
|
|
@ -540,19 +549,22 @@ def test_build_coco_class_index_mapping(
|
|||
]
|
||||
),
|
||||
),
|
||||
0,
|
||||
0,
|
||||
[mock_cock_coco_annotation(
|
||||
category_id=0,
|
||||
bbox=(0, 0, 5, 5),
|
||||
area=5 * 5,
|
||||
segmentation={
|
||||
0,
|
||||
0,
|
||||
[
|
||||
mock_cock_coco_annotation(
|
||||
category_id=0,
|
||||
bbox=(0, 0, 5, 5),
|
||||
area=5 * 5,
|
||||
segmentation={
|
||||
"size": [5, 5],
|
||||
"counts": [2, 10, 2, 3, 2, 6],
|
||||
},
|
||||
iscrowd=True, )],
|
||||
DoesNotRaise(),
|
||||
) # segmentation mask in single component, with holes in mask, expects RLE mask
|
||||
iscrowd=True,
|
||||
)
|
||||
],
|
||||
DoesNotRaise(),
|
||||
), # segmentation mask in single component, with holes in mask, expects RLE mask
|
||||
],
|
||||
)
|
||||
def test_detections_to_coco_annotations(
|
||||
|
|
@ -560,7 +572,8 @@ def test_detections_to_coco_annotations(
|
|||
image_id: int,
|
||||
annotation_id: int,
|
||||
expected_result: List[Dict],
|
||||
exception: Exception) -> None:
|
||||
exception: Exception,
|
||||
) -> None:
|
||||
with exception:
|
||||
result, _ = detections_to_coco_annotations(
|
||||
detections=detections, image_id=image_id, annotation_id=annotation_id
|
||||
|
|
|
|||
Loading…
Reference in New Issue