4
4
5
5
import numpy as np
6
6
from scipy .optimize import linear_sum_assignment
7
+ from shapely .geometry import Polygon
7
8
8
9
from nucleus .annotation import BoxAnnotation , PolygonAnnotation
9
10
from nucleus .prediction import BoxPrediction , PolygonPrediction
10
11
11
12
from .base import ScalarResult
12
13
from .errors import PolygonAnnotationTypeError
13
- from .geometry import GeometryPolygon , polygon_intersection_area
14
14
15
15
BoxOrPolygonPrediction = TypeVar (
16
16
"BoxOrPolygonPrediction" , BoxPrediction , PolygonPrediction
27
27
)
28
28
29
29
30
- def polygon_annotation_to_geometry (
30
+ def polygon_annotation_to_shape (
31
31
annotation : BoxOrPolygonAnnotation ,
32
- ) -> GeometryPolygon :
32
+ ) -> Polygon :
33
33
if isinstance (annotation , BoxAnnotation ):
34
34
xmin = annotation .x - annotation .width / 2
35
35
xmax = annotation .x + annotation .width / 2
36
36
ymin = annotation .y - annotation .height / 2
37
37
ymax = annotation .y + annotation .height / 2
38
- points = [(xmin , ymin ), (xmax , ymin ), (xmax , ymax ), (xmin , ymax )]
39
- return GeometryPolygon (points = points , is_rectangle = True )
40
- elif isinstance (annotation , PolygonAnnotation ):
41
- return GeometryPolygon (
42
- points = [(point .x , point .y ) for point in annotation .vertices ],
43
- is_rectangle = False ,
38
+ return Polygon (
39
+ [(xmin , ymin ), (xmax , ymin ), (xmax , ymax ), (xmin , ymax )]
44
40
)
41
+ elif isinstance (annotation , PolygonAnnotation ):
42
+ return Polygon ([(point .x , point .y ) for point in annotation .vertices ])
45
43
else :
46
44
raise PolygonAnnotationTypeError ()
47
45
48
46
49
- def _iou (annotation : GeometryPolygon , prediction : GeometryPolygon ) -> float :
50
- intersection = polygon_intersection_area ( annotation , prediction )
47
+ def _iou (annotation : Polygon , prediction : Polygon ) -> float :
48
+ intersection = annotation . intersection ( prediction ). area
51
49
union = annotation .area + prediction .area - intersection
52
50
return intersection / max (union , sys .float_info .epsilon )
53
51
54
52
55
53
def _iou_matrix (
56
- annotations : List [GeometryPolygon ], predictions : List [GeometryPolygon ]
54
+ annotations : List [Polygon ], predictions : List [Polygon ]
57
55
) -> np .ndarray :
58
56
iou_matrix = np .empty ((len (predictions ), len (annotations )))
59
57
for i , prediction in enumerate (predictions ):
@@ -80,13 +78,9 @@ def _iou_assignments_for_same_reference_id(
80
78
len (reference_ids ) <= 1
81
79
), "Expected annotations and predictions to have same reference ID."
82
80
83
- # Convert annotation and predictions to GeometryPolygon objects
84
- polygon_annotations = list (
85
- map (polygon_annotation_to_geometry , annotations )
86
- )
87
- polygon_predictions = list (
88
- map (polygon_annotation_to_geometry , predictions )
89
- )
81
+ # Convert annotation and predictions to shapely.geometry.Polygon objects
82
+ polygon_annotations = list (map (polygon_annotation_to_shape , annotations ))
83
+ polygon_predictions = list (map (polygon_annotation_to_shape , predictions ))
90
84
91
85
# Compute IoU matrix and set IoU values below the threshold to 0.
92
86
iou_matrix = _iou_matrix (polygon_annotations , polygon_predictions )
0 commit comments