boxes_utils.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import numpy as np
  2. def assert_and_normalize_shape(x, length):
  3. """
  4. Args:
  5. x: ndarray
  6. length: int
  7. """
  8. if x.ndim == 0:
  9. return x
  10. elif x.ndim == 1:
  11. if len(x) == 1:
  12. return x
  13. elif len(x) == length:
  14. return x
  15. else:
  16. raise ValueError('Incompatible shape!')
  17. elif x.ndim == 2:
  18. if x.shape == (1, 1):
  19. return np.squeeze(x, axis=-1)
  20. elif x.shape == (length, 1):
  21. return np.squeeze(x, axis=-1)
  22. else:
  23. raise ValueError('Incompatible shape!')
  24. else:
  25. raise ValueError('Incompatible ndim!')
  26. def normalize_boxes(boxes, dtype=np.float32, copy=False, support_extra=True):
  27. boxes = np.array(boxes, dtype=dtype, copy=copy)
  28. assert boxes.ndim in [1, 2]
  29. last_dimension = boxes.shape[-1]
  30. if support_extra:
  31. assert last_dimension >= 4
  32. else:
  33. assert last_dimension == 4
  34. if boxes.ndim == 1:
  35. boxes = np.expand_dims(boxes, axis=0)
  36. return boxes