"docs/source/en/api/schedulers/euler_ancestral.md" did not exist on "4274a3a9150de0a4941c60eaa70e748571ceb4a0"
poly_nms.py 4.04 KB
Newer Older
zhiminzhang0830's avatar
zhiminzhang0830 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# copyright (c) 2022 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import numpy as np
zhiminzhang0830's avatar
zhiminzhang0830 committed
16
from shapely.geometry import Polygon
zhiminzhang0830's avatar
zhiminzhang0830 committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35


def points2polygon(points):
    """Convert k points to 1 polygon.

    Args:
        points (ndarray or list): A ndarray or a list of shape (2k)
            that indicates k points.

    Returns:
        polygon (Polygon): A polygon object.
    """
    if isinstance(points, list):
        points = np.array(points)

    assert isinstance(points, np.ndarray)
    assert (points.size % 2 == 0) and (points.size >= 8)

    point_mat = points.reshape([-1, 2])
zhiminzhang0830's avatar
zhiminzhang0830 committed
36
    return Polygon(point_mat)
zhiminzhang0830's avatar
zhiminzhang0830 committed
37
38


zhiminzhang0830's avatar
zhiminzhang0830 committed
39
def poly_intersection(poly_det, poly_gt, buffer=0.0001):
zhiminzhang0830's avatar
zhiminzhang0830 committed
40
41
42
43
44
45
46
47
48
    """Calculate the intersection area between two polygon.

    Args:
        poly_det (Polygon): A polygon predicted by detector.
        poly_gt (Polygon): A gt polygon.

    Returns:
        intersection_area (float): The intersection area between two polygons.
    """
zhiminzhang0830's avatar
zhiminzhang0830 committed
49
50
    assert isinstance(poly_det, Polygon)
    assert isinstance(poly_gt, Polygon)
zhiminzhang0830's avatar
zhiminzhang0830 committed
51

zhiminzhang0830's avatar
zhiminzhang0830 committed
52
53
54
55
    if buffer == 0:
        poly_inter = poly_det & poly_gt
    else:
        poly_inter = poly_det.buffer(buffer) & poly_gt.buffer(buffer)
zhiminzhang0830's avatar
zhiminzhang0830 committed
56
    return poly_inter.area, poly_inter
zhiminzhang0830's avatar
zhiminzhang0830 committed
57
58
59
60
61
62
63
64
65
66
67
68


def poly_union(poly_det, poly_gt):
    """Calculate the union area between two polygon.

    Args:
        poly_det (Polygon): A polygon predicted by detector.
        poly_gt (Polygon): A gt polygon.

    Returns:
        union_area (float): The union area between two polygons.
    """
zhiminzhang0830's avatar
zhiminzhang0830 committed
69
70
    assert isinstance(poly_det, Polygon)
    assert isinstance(poly_gt, Polygon)
zhiminzhang0830's avatar
zhiminzhang0830 committed
71

zhiminzhang0830's avatar
zhiminzhang0830 committed
72
73
    area_det = poly_det.area
    area_gt = poly_gt.area
zhiminzhang0830's avatar
zhiminzhang0830 committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    area_inters, _ = poly_intersection(poly_det, poly_gt)
    return area_det + area_gt - area_inters


def valid_boundary(x, with_score=True):
    num = len(x)
    if num < 8:
        return False
    if num % 2 == 0 and (not with_score):
        return True
    if num % 2 == 1 and with_score:
        return True

    return False


def boundary_iou(src, target):
    """Calculate the IOU between two boundaries.

    Args:
       src (list): Source boundary.
       target (list): Target boundary.

    Returns:
       iou (float): The iou between two boundaries.
    """
    assert valid_boundary(src, False)
    assert valid_boundary(target, False)
    src_poly = points2polygon(src)
    target_poly = points2polygon(target)

    return poly_iou(src_poly, target_poly)


def poly_iou(poly_det, poly_gt):
    """Calculate the IOU between two polygons.

    Args:
        poly_det (Polygon): A polygon predicted by detector.
        poly_gt (Polygon): A gt polygon.

    Returns:
        iou (float): The IOU between two polygons.
    """
zhiminzhang0830's avatar
zhiminzhang0830 committed
118
119
    assert isinstance(poly_det, Polygon)
    assert isinstance(poly_gt, Polygon)
zhiminzhang0830's avatar
zhiminzhang0830 committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    area_inters, _ = poly_intersection(poly_det, poly_gt)
    area_union = poly_union(poly_det, poly_gt)
    if area_union == 0:
        return 0.0
    return area_inters / area_union


def poly_nms(polygons, threshold):
    assert isinstance(polygons, list)

    polygons = np.array(sorted(polygons, key=lambda x: x[-1]))

    keep_poly = []
    index = [i for i in range(polygons.shape[0])]

    while len(index) > 0:
        keep_poly.append(polygons[index[-1]].tolist())
        A = polygons[index[-1]][:-1]
        index = np.delete(index, -1)
        iou_list = np.zeros((len(index), ))
        for i in range(len(index)):
            B = polygons[index[i]][:-1]
            iou_list[i] = boundary_iou(A, B)
        remove_index = np.where(iou_list > threshold)
        index = np.delete(index, remove_index)

zhiminzhang0830's avatar
zhiminzhang0830 committed
146
    return keep_poly