Meta Byte Track
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

iou_matching.py 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # vim: expandtab:ts=4:sw=4
  2. from __future__ import absolute_import
  3. import numpy as np
  4. from yolox.deepsort_tracker import linear_assignment
  5. def iou(bbox, candidates):
  6. """Computer intersection over union.
  7. Parameters
  8. ----------
  9. bbox : ndarray
  10. A bounding box in format `(top left x, top left y, width, height)`.
  11. candidates : ndarray
  12. A matrix of candidate bounding boxes (one per row) in the same format
  13. as `bbox`.
  14. Returns
  15. -------
  16. ndarray
  17. The intersection over union in [0, 1] between the `bbox` and each
  18. candidate. A higher score means a larger fraction of the `bbox` is
  19. occluded by the candidate.
  20. """
  21. bbox_tl, bbox_br = bbox[:2], bbox[:2] + bbox[2:]
  22. candidates_tl = candidates[:, :2]
  23. candidates_br = candidates[:, :2] + candidates[:, 2:]
  24. tl = np.c_[np.maximum(bbox_tl[0], candidates_tl[:, 0])[:, np.newaxis],
  25. np.maximum(bbox_tl[1], candidates_tl[:, 1])[:, np.newaxis]]
  26. br = np.c_[np.minimum(bbox_br[0], candidates_br[:, 0])[:, np.newaxis],
  27. np.minimum(bbox_br[1], candidates_br[:, 1])[:, np.newaxis]]
  28. wh = np.maximum(0., br - tl)
  29. area_intersection = wh.prod(axis=1)
  30. area_bbox = bbox[2:].prod()
  31. area_candidates = candidates[:, 2:].prod(axis=1)
  32. return area_intersection / (area_bbox + area_candidates - area_intersection)
  33. def iou_cost(tracks, detections, track_indices=None,
  34. detection_indices=None):
  35. """An intersection over union distance metric.
  36. Parameters
  37. ----------
  38. tracks : List[deep_sort.track.Track]
  39. A list of tracks.
  40. detections : List[deep_sort.detection.Detection]
  41. A list of detections.
  42. track_indices : Optional[List[int]]
  43. A list of indices to tracks that should be matched. Defaults to
  44. all `tracks`.
  45. detection_indices : Optional[List[int]]
  46. A list of indices to detections that should be matched. Defaults
  47. to all `detections`.
  48. Returns
  49. -------
  50. ndarray
  51. Returns a cost matrix of shape
  52. len(track_indices), len(detection_indices) where entry (i, j) is
  53. `1 - iou(tracks[track_indices[i]], detections[detection_indices[j]])`.
  54. """
  55. if track_indices is None:
  56. track_indices = np.arange(len(tracks))
  57. if detection_indices is None:
  58. detection_indices = np.arange(len(detections))
  59. cost_matrix = np.zeros((len(track_indices), len(detection_indices)))
  60. for row, track_idx in enumerate(track_indices):
  61. if tracks[track_idx].time_since_update > 1:
  62. cost_matrix[row, :] = linear_assignment.INFTY_COST
  63. continue
  64. bbox = tracks[track_idx].to_tlwh()
  65. candidates = np.asarray(
  66. [detections[i].tlwh for i in detection_indices])
  67. cost_matrix[row, :] = 1. - iou(bbox, candidates)
  68. return cost_matrix