ops.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import cv2
  15. import math
  16. import numpy as np
  17. from PIL import Image, ImageEnhance
  18. def normalize(im, mean, std):
  19. im = im / 255.0
  20. im -= mean
  21. im /= std
  22. return im
  23. def permute(im, to_bgr=False):
  24. im = np.swapaxes(im, 1, 2)
  25. im = np.swapaxes(im, 1, 0)
  26. if to_bgr:
  27. im = im[[2, 1, 0], :, :]
  28. return im
  29. def resize_long(im, long_size=224, interpolation=cv2.INTER_LINEAR):
  30. value = max(im.shape[0], im.shape[1])
  31. scale = float(long_size) / float(value)
  32. resized_width = int(round(im.shape[1] * scale))
  33. resized_height = int(round(im.shape[0] * scale))
  34. im = cv2.resize(
  35. im, (resized_width, resized_height), interpolation=interpolation)
  36. return im
  37. def resize(im, target_size=608, interp=cv2.INTER_LINEAR):
  38. if isinstance(target_size, list) or isinstance(target_size, tuple):
  39. w = target_size[0]
  40. h = target_size[1]
  41. else:
  42. w = target_size
  43. h = target_size
  44. im = cv2.resize(im, (w, h), interpolation=interp)
  45. return im
  46. def random_crop(im,
  47. crop_size=224,
  48. lower_scale=0.08,
  49. lower_ratio=3. / 4,
  50. upper_ratio=4. / 3):
  51. scale = [lower_scale, 1.0]
  52. ratio = [lower_ratio, upper_ratio]
  53. aspect_ratio = math.sqrt(np.random.uniform(*ratio))
  54. w = 1. * aspect_ratio
  55. h = 1. / aspect_ratio
  56. bound = min((float(im.shape[0]) / im.shape[1]) / (h**2),
  57. (float(im.shape[1]) / im.shape[0]) / (w**2))
  58. scale_max = min(scale[1], bound)
  59. scale_min = min(scale[0], bound)
  60. target_area = im.shape[0] * im.shape[1] * np.random.uniform(
  61. scale_min, scale_max)
  62. target_size = math.sqrt(target_area)
  63. w = int(target_size * w)
  64. h = int(target_size * h)
  65. i = np.random.randint(0, im.shape[0] - h + 1)
  66. j = np.random.randint(0, im.shape[1] - w + 1)
  67. im = im[i:i + h, j:j + w, :]
  68. im = cv2.resize(im, (crop_size, crop_size))
  69. return im
  70. def center_crop(im, crop_size=224):
  71. height, width = im.shape[:2]
  72. w_start = (width - crop_size) // 2
  73. h_start = (height - crop_size) // 2
  74. w_end = w_start + crop_size
  75. h_end = h_start + crop_size
  76. im = im[h_start:h_end, w_start:w_end, :]
  77. return im
  78. def horizontal_flip(im):
  79. if len(im.shape) == 3:
  80. im = im[:, ::-1, :]
  81. elif len(im.shape) == 2:
  82. im = im[:, ::-1]
  83. return im
  84. def vertical_flip(im):
  85. if len(im.shape) == 3:
  86. im = im[::-1, :, :]
  87. elif len(im.shape) == 2:
  88. im = im[::-1, :]
  89. return im
  90. def bgr2rgb(im):
  91. return im[:, :, ::-1]
  92. def brightness(im, brightness_lower, brightness_upper):
  93. brightness_delta = np.random.uniform(brightness_lower, brightness_upper)
  94. im = ImageEnhance.Brightness(im).enhance(brightness_delta)
  95. return im
  96. def contrast(im, contrast_lower, contrast_upper):
  97. contrast_delta = np.random.uniform(contrast_lower, contrast_upper)
  98. im = ImageEnhance.Contrast(im).enhance(contrast_delta)
  99. return im
  100. def saturation(im, saturation_lower, saturation_upper):
  101. saturation_delta = np.random.uniform(saturation_lower, saturation_upper)
  102. im = ImageEnhance.Color(im).enhance(saturation_delta)
  103. return im
  104. def hue(im, hue_lower, hue_upper):
  105. hue_delta = np.random.uniform(hue_lower, hue_upper)
  106. im = np.array(im.convert('HSV'))
  107. im[:, :, 0] = im[:, :, 0] + hue_delta
  108. im = Image.fromarray(im, mode='HSV').convert('RGB')
  109. return im
  110. def rotate(im, rotate_lower, rotate_upper):
  111. rotate_delta = np.random.uniform(rotate_lower, rotate_upper)
  112. im = im.rotate(int(rotate_delta))
  113. return im
  114. def resize_padding(im, max_side_len=2400):
  115. '''
  116. resize image to a size multiple of 32 which is required by the network
  117. :param im: the resized image
  118. :param max_side_len: limit of max image size to avoid out of memory in gpu
  119. :return: the resized image and the resize ratio
  120. '''
  121. h, w, _ = im.shape
  122. resize_w = w
  123. resize_h = h
  124. # limit the max side
  125. if max(resize_h, resize_w) > max_side_len:
  126. ratio = float(
  127. max_side_len) / resize_h if resize_h > resize_w else float(
  128. max_side_len) / resize_w
  129. else:
  130. ratio = 1.
  131. resize_h = int(resize_h * ratio)
  132. resize_w = int(resize_w * ratio)
  133. resize_h = resize_h if resize_h % 32 == 0 else (resize_h // 32 - 1) * 32
  134. resize_w = resize_w if resize_w % 32 == 0 else (resize_w // 32 - 1) * 32
  135. resize_h = max(32, resize_h)
  136. resize_w = max(32, resize_w)
  137. im = cv2.resize(im, (int(resize_w), int(resize_h)))
  138. #im = cv2.resize(im, (512, 512))
  139. ratio_h = resize_h / float(h)
  140. ratio_w = resize_w / float(w)
  141. _ratio = np.array([ratio_h, ratio_w]).reshape(-1, 2)
  142. return im, _ratio