MCPcopy Create free account
hub / github.com/PyImageSearch/imutils / non_max_suppression

Function non_max_suppression

imutils/object_detection.py:4–65  ·  view source on GitHub ↗
(boxes, probs=None, overlapThresh=0.3)

Source from the content-addressed store, hash-verified

2import numpy as np
3
4def non_max_suppression(boxes, probs=None, overlapThresh=0.3):
5 # if there are no boxes, return an empty list
6 if len(boxes) == 0:
7 return []
8
9 # if the bounding boxes are integers, convert them to floats -- this
10 # is important since we'll be doing a bunch of divisions
11 if boxes.dtype.kind == "i":
12 boxes = boxes.astype("float")
13
14 # initialize the list of picked indexes
15 pick = []
16
17 # grab the coordinates of the bounding boxes
18 x1 = boxes[:, 0]
19 y1 = boxes[:, 1]
20 x2 = boxes[:, 2]
21 y2 = boxes[:, 3]
22
23 # compute the area of the bounding boxes and grab the indexes to sort
24 # (in the case that no probabilities are provided, simply sort on the
25 # bottom-left y-coordinate)
26 area = (x2 - x1 + 1) * (y2 - y1 + 1)
27 idxs = y2
28
29 # if probabilities are provided, sort on them instead
30 if probs is not None:
31 idxs = probs
32
33 # sort the indexes
34 idxs = np.argsort(idxs)
35
36 # keep looping while some indexes still remain in the indexes list
37 while len(idxs) > 0:
38 # grab the last index in the indexes list and add the index value
39 # to the list of picked indexes
40 last = len(idxs) - 1
41 i = idxs[last]
42 pick.append(i)
43
44 # find the largest (x, y) coordinates for the start of the bounding
45 # box and the smallest (x, y) coordinates for the end of the bounding
46 # box
47 xx1 = np.maximum(x1[i], x1[idxs[:last]])
48 yy1 = np.maximum(y1[i], y1[idxs[:last]])
49 xx2 = np.minimum(x2[i], x2[idxs[:last]])
50 yy2 = np.minimum(y2[i], y2[idxs[:last]])
51
52 # compute the width and height of the bounding box
53 w = np.maximum(0, xx2 - xx1 + 1)
54 h = np.maximum(0, yy2 - yy1 + 1)
55
56 # compute the ratio of overlap
57 overlap = (w * h) / area[idxs[:last]]
58
59 # delete all indexes from the index list that have overlap greater
60 # than the provided overlap threshold
61 idxs = np.delete(idxs, np.concatenate(([last],

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…