1
+ import numpy as np
2
+
3
+ # Felzenszwalb et al.
4
+ def non_max_suppression_slow (boxes , overlapThresh ):
5
+ # if there are no boxes, return an empty list
6
+ if len (boxes ) == 0 :
7
+ return boxes
8
+
9
+ # initialize the list of picked indexes
10
+ pick = []
11
+
12
+ # grab the coordinates of the bounding boxes
13
+ x1 = boxes [:,0 ]
14
+ y1 = boxes [:,1 ]
15
+ xw = boxes [:,2 ]
16
+ yh = boxes [:,3 ]
17
+ x2 = x1 + xw
18
+ y2 = y1 + yh
19
+
20
+ # compute the area of the bounding boxes and sort the bounding
21
+ # boxes by the bottom-right y-coordinate of the bounding box
22
+ area = xw * yh
23
+ idxs = np .argsort (boxes [:,4 ])
24
+
25
+ # keep looping while some indexes still remain in the indexes
26
+ # list
27
+ while len (idxs ) > 0 :
28
+ # grab the last index in the indexes list, add the index
29
+ # value to the list of picked indexes, then initialize
30
+ # the suppression list (i.e. indexes that will be deleted)
31
+ # using the last index
32
+ last = len (idxs ) - 1
33
+ i = idxs [last ]
34
+ pick .append (i )
35
+ suppress = [last ]
36
+
37
+ # loop over all indexes in the indexes list
38
+ for pos in xrange (0 , last ):
39
+ # grab the current index
40
+ j = idxs [pos ]
41
+
42
+ # find the largest (x, y) coordinates for the start of
43
+ # the bounding box and the smallest (x, y) coordinates
44
+ # for the end of the bounding box
45
+ xx1 = max (x1 [i ], x1 [j ])
46
+ yy1 = max (y1 [i ], y1 [j ])
47
+ xx2 = min (x2 [i ], x2 [j ])
48
+ yy2 = min (y2 [i ], y2 [j ])
49
+
50
+ # compute the width and height of the bounding box
51
+ w = max (0 , xx2 - xx1 )
52
+ h = max (0 , yy2 - yy1 )
53
+
54
+ # compute the ratio of overlap between the computed
55
+ # bounding box and the bounding box in the area list
56
+ overlap = float (w * h ) / min (area [j ], area [i ])
57
+
58
+ # if there is sufficient overlap, suppress the
59
+ # current bounding box
60
+ if overlap > overlapThresh :
61
+ suppress .append (pos )
62
+
63
+ # delete all indexes from the index list that are in the
64
+ # suppression list
65
+ idxs = np .delete (idxs , suppress )
66
+
67
+ # return only the bounding boxes that were picked
68
+ return boxes [pick ]
69
+
70
+ def non_max_suppression_fast (boxes , overlapThresh ):
71
+ # if there are no boxes, return an empty list
72
+ if len (boxes ) == 0 :
73
+ return boxes
74
+
75
+ # initialize the list of picked indexes
76
+ pick = []
77
+
78
+ # grab the coordinates of the bounding boxes
79
+ x1 = boxes [:,0 ]
80
+ y1 = boxes [:,1 ]
81
+ xw = boxes [:,2 ]
82
+ yh = boxes [:,3 ]
83
+ x2 = x1 + xw
84
+ y2 = y1 + yh
85
+
86
+ # compute the area of the bounding boxes and sort the bounding
87
+ # boxes by the bottom-right y-coordinate of the bounding box
88
+ area = xw * yh
89
+ idxs = np .argsort (boxes [:,4 ])
90
+
91
+ # keep looping while some indexes still remain in the indexes
92
+ # list
93
+ while len (idxs ) > 0 :
94
+ # grab the last index in the indexes list and add the
95
+ # index value to the list of picked indexes
96
+ last = len (idxs ) - 1
97
+ i = idxs [last ]
98
+ pick .append (i )
99
+
100
+ # find the largest (x, y) coordinates for the start of
101
+ # the bounding box and the smallest (x, y) coordinates
102
+ # for the end of the bounding box
103
+ xx1 = np .maximum (x1 [i ], x1 [idxs [:last ]])
104
+ yy1 = np .maximum (y1 [i ], y1 [idxs [:last ]])
105
+ xx2 = np .minimum (x2 [i ], x2 [idxs [:last ]])
106
+ yy2 = np .minimum (y2 [i ], y2 [idxs [:last ]])
107
+
108
+ # compute the width and height of the bounding box
109
+ w = np .maximum (0 , xx2 - xx1 )
110
+ h = np .maximum (0 , yy2 - yy1 )
111
+
112
+ # compute the ratio of overlap
113
+ overlap = (w * h ) / np .minimum (area [idxs [:last ]], area [i ])
114
+
115
+ # delete all indexes from the index list that have
116
+ idxs = np .delete (idxs , np .concatenate (([last ], np .where (overlap > overlapThresh )[0 ])))
117
+ return boxes [pick ]
118
+
119
+ def filter_bbox (bbox ):
120
+ xrng = [0.1 , 959.9 ]
121
+ yrng = [0.1 , 719.9 ]
122
+ #bbox[:, :4] = bbox[:, :4] / 1.5
123
+ x1 = bbox [:, 0 ]
124
+ y1 = bbox [:, 1 ]
125
+ x2 = bbox [:, 0 ] + bbox [:, 2 ]
126
+ y2 = bbox [:, 1 ] + bbox [:, 3 ]
127
+ keep = np .where ((x1 > xrng [0 ]) & (x2 < xrng [1 ]) & (y1 > yrng [0 ]) & (y2 < yrng [1 ]))[0 ]
128
+ return bbox [keep , :]
0 commit comments