detection.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # vim: expandtab:ts=4:sw=4
  2. import numpy as np
  3. class Detection(object):
  4. """
  5. This class represents a bounding box detection in a single image.
  6. Parameters
  7. ----------
  8. tlwh : array_like
  9. Bounding box in format `(x, y, w, h)`.
  10. confidence : float
  11. Detector confidence score.
  12. feature : array_like
  13. A feature vector that describes the object contained in this image.
  14. Attributes
  15. ----------
  16. tlwh : ndarray
  17. Bounding box in format `(top left x, top left y, width, height)`.
  18. confidence : ndarray
  19. Detector confidence score.
  20. class_name : ndarray
  21. Detector class.
  22. feature : ndarray | NoneType
  23. A feature vector that describes the object contained in this image.
  24. """
  25. def __init__(self, tlwh, confidence, class_name, feature):
  26. self.tlwh = np.asarray(tlwh, dtype=np.float)
  27. self.confidence = float(confidence)
  28. self.class_name = class_name
  29. self.feature = np.asarray(feature, dtype=np.float32)
  30. def get_class(self):
  31. return self.class_name
  32. def to_tlbr(self):
  33. """Convert bounding box to format `(min x, min y, max x, max y)`, i.e.,
  34. `(top left, bottom right)`.
  35. """
  36. ret = self.tlwh.copy()
  37. ret[2:] += ret[:2]
  38. return ret
  39. def to_xyah(self):
  40. """Convert bounding box to format `(center x, center y, aspect ratio,
  41. height)`, where the aspect ratio is `width / height`.
  42. """
  43. ret = self.tlwh.copy()
  44. ret[:2] += ret[2:] / 2
  45. ret[2] /= ret[3]
  46. return ret