preprocessing.py
Go to the documentation of this file.
1 # vim: expandtab:ts=4:sw=4
2 import numpy as np
3 import cv2
4 
5 
6 def non_max_suppression(boxes, max_bbox_overlap, scores=None):
7  """Suppress overlapping detections.
8 
9  Original code from [1]_ has been adapted to include confidence score.
10 
11  .. [1] http://www.pyimagesearch.com/2015/02/16/
12  faster-non-maximum-suppression-python/
13 
14  Examples
15  --------
16 
17  >>> boxes = [d.roi for d in detections]
18  >>> scores = [d.confidence for d in detections]
19  >>> indices = non_max_suppression(boxes, max_bbox_overlap, scores)
20  >>> detections = [detections[i] for i in indices]
21 
22  Parameters
23  ----------
24  boxes : ndarray
25  Array of ROIs (x, y, width, height).
26  max_bbox_overlap : float
27  ROIs that overlap more than this values are suppressed.
28  scores : Optional[array_like]
29  Detector confidence score.
30 
31  Returns
32  -------
33  List[int]
34  Returns indices of detections that have survived non-maxima suppression.
35 
36  """
37  if len(boxes) == 0:
38  return []
39 
40  boxes = boxes.astype(np.float)
41  pick = []
42 
43  x1 = boxes[:, 0]
44  y1 = boxes[:, 1]
45  x2 = boxes[:, 2] + boxes[:, 0]
46  y2 = boxes[:, 3] + boxes[:, 1]
47 
48  area = (x2 - x1 + 1) * (y2 - y1 + 1)
49  if scores is not None:
50  idxs = np.argsort(scores)
51  else:
52  idxs = np.argsort(y2)
53 
54  while len(idxs) > 0:
55  last = len(idxs) - 1
56  i = idxs[last]
57  pick.append(i)
58 
59  xx1 = np.maximum(x1[i], x1[idxs[:last]])
60  yy1 = np.maximum(y1[i], y1[idxs[:last]])
61  xx2 = np.minimum(x2[i], x2[idxs[:last]])
62  yy2 = np.minimum(y2[i], y2[idxs[:last]])
63 
64  w = np.maximum(0, xx2 - xx1 + 1)
65  h = np.maximum(0, yy2 - yy1 + 1)
66 
67  overlap = (w * h) / area[idxs[:last]]
68 
69  idxs = np.delete(
70  idxs, np.concatenate(
71  ([last], np.where(overlap > max_bbox_overlap)[0])))
72 
73  return pick
def non_max_suppression(boxes, max_bbox_overlap, scores=None)
Definition: preprocessing.py:6


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Mon May 3 2021 03:03:27