2 from __future__
import absolute_import
4 from .
import linear_assignment
7 def iou(bbox, candidates):
8 """Computer intersection over union. 13 A bounding box in format `(top left x, top left y, width, height)`. 15 A matrix of candidate bounding boxes (one per row) in the same format 21 The intersection over union in [0, 1] between the `bbox` and each 22 candidate. A higher score means a larger fraction of the `bbox` is 23 occluded by the candidate. 26 bbox_tl, bbox_br = bbox[:2], bbox[:2] + bbox[2:]
27 candidates_tl = candidates[:, :2]
28 candidates_br = candidates[:, :2] + candidates[:, 2:]
30 tl = np.c_[np.maximum(bbox_tl[0], candidates_tl[:, 0])[:, np.newaxis],
31 np.maximum(bbox_tl[1], candidates_tl[:, 1])[:, np.newaxis]]
32 br = np.c_[np.minimum(bbox_br[0], candidates_br[:, 0])[:, np.newaxis],
33 np.minimum(bbox_br[1], candidates_br[:, 1])[:, np.newaxis]]
34 wh = np.maximum(0., br - tl)
36 area_intersection = wh.prod(axis=1)
37 area_bbox = bbox[2:].prod()
38 area_candidates = candidates[:, 2:].prod(axis=1)
39 return area_intersection / (area_bbox + area_candidates - area_intersection)
42 def iou_cost(tracks, detections, track_indices=None,
43 detection_indices=
None):
44 """An intersection over union distance metric. 48 tracks : List[deep_sort.track.Track] 50 detections : List[deep_sort.detection.Detection] 52 track_indices : Optional[List[int]] 53 A list of indices to tracks that should be matched. Defaults to 55 detection_indices : Optional[List[int]] 56 A list of indices to detections that should be matched. Defaults 62 Returns a cost matrix of shape 63 len(track_indices), len(detection_indices) where entry (i, j) is 64 `1 - iou(tracks[track_indices[i]], detections[detection_indices[j]])`. 67 if track_indices
is None:
68 track_indices = np.arange(len(tracks))
69 if detection_indices
is None:
70 detection_indices = np.arange(len(detections))
72 cost_matrix = np.zeros((len(track_indices), len(detection_indices)))
73 for row, track_idx
in enumerate(track_indices):
74 if tracks[track_idx].time_since_update > 1:
75 cost_matrix[row, :] = linear_assignment.INFTY_COST
78 bbox = tracks[track_idx].to_tlwh()
79 candidates = np.asarray([detections[i].tlwh
for i
in detection_indices])
80 cost_matrix[row, :] = 1. -
iou(bbox, candidates)
def iou(bbox, candidates)
def iou_cost(tracks, detections, track_indices=None, detection_indices=None)