util.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import cv2
  2. import numpy as np
  3. class ResizeImage(object):
  4. def __init__(self, resize_short=None):
  5. self.resize_short = resize_short
  6. def __call__(self, img):
  7. img_h, img_w = img.shape[:2]
  8. percent = float(self.resize_short) / min(img_w, img_h)
  9. w = int(round(img_w * percent))
  10. h = int(round(img_h * percent))
  11. return cv2.resize(img, (w, h))
  12. class CropImage(object):
  13. def __init__(self, size):
  14. if type(size) is int:
  15. self.size = (size, size)
  16. else:
  17. self.size = size
  18. def __call__(self, img):
  19. w, h = self.size
  20. img_h, img_w = img.shape[:2]
  21. w_start = (img_w - w) // 2
  22. h_start = (img_h - h) // 2
  23. w_end = w_start + w
  24. h_end = h_start + h
  25. return img[h_start:h_end, w_start:w_end, :]
  26. class ToTensor(object):
  27. def __init__(self):
  28. pass
  29. def __call__(self, img):
  30. img = img.transpose((2, 0, 1))
  31. return img
  32. class NormalizeImage(object):
  33. def __init__(self, scale=None, mean=None, std=None):
  34. self.scale = np.float32(scale if scale is not None else 1.0 / 255.0)
  35. mean = mean if mean is not None else [0.485, 0.456, 0.406]
  36. std = std if std is not None else [0.229, 0.224, 0.225]
  37. shape = (1, 1, 3)
  38. self.mean = np.array(mean).reshape(shape).astype('float32')
  39. self.std = np.array(std).reshape(shape).astype('float32')
  40. def __call__(self, img):
  41. return (img.astype('float32') * self.scale - self.mean) / self.std