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

preprocess.py 4.5 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import numpy as np
  2. import cv2
  3. import numbers
  4. import logging
  5. class Filter:
  6. # 根据需要添加其它函数
  7. def imnormalize(self, img, mean, std, to_rgb=True):
  8. """Inplace normalize an image with mean and std.
  9. Args:
  10. img (ndarray): Image to be normalized.
  11. mean (ndarray): The mean to be used for normalize.
  12. std (ndarray): The std to be used for normalize.
  13. to_rgb (bool): Whether to convert to rgb.
  14. Returns:
  15. ndarray: The normalized image.
  16. """
  17. # cv2 inplace normalization does not accept uint8
  18. #assert img.dtype != np.uint8
  19. mean = np.float64(mean.reshape(1, -1))
  20. stdinv = 1 / np.float64(std.reshape(1, -1))
  21. if to_rgb:
  22. cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img) # inplace
  23. cv2.subtract(img, mean, img) # inplace
  24. cv2.multiply(img, stdinv, img) # inplace
  25. return img
  26. def impad(self, img,
  27. *,
  28. shape=None,
  29. padding=None,
  30. pad_val=0,
  31. padding_mode='constant'):
  32. assert (shape is not None) ^ (padding is not None)
  33. if shape is not None:
  34. padding = (0, 0, shape[1] - img.shape[1], shape[0] - img.shape[0])
  35. # check pad_val
  36. if isinstance(pad_val, tuple):
  37. assert len(pad_val) == img.shape[-1]
  38. elif not isinstance(pad_val, numbers.Number):
  39. raise TypeError('pad_val must be a int or a tuple. '
  40. f'But received {type(pad_val)}')
  41. # check padding
  42. if isinstance(padding, tuple) and len(padding) in [2, 4]:
  43. if len(padding) == 2:
  44. padding = (padding[0], padding[1], padding[0], padding[1])
  45. elif isinstance(padding, numbers.Number):
  46. padding = (padding, padding, padding, padding)
  47. else:
  48. raise ValueError('Padding must be a int or a 2, or 4 element tuple.'
  49. f'But received {padding}')
  50. # check padding mode
  51. assert padding_mode in ['constant', 'edge', 'reflect', 'symmetric']
  52. border_type = {
  53. 'constant': cv2.BORDER_CONSTANT,
  54. 'edge': cv2.BORDER_REPLICATE,
  55. 'reflect': cv2.BORDER_REFLECT_101,
  56. 'symmetric': cv2.BORDER_REFLECT
  57. }
  58. logging.error("padding...")
  59. logging.error(padding)
  60. self.pad_x = padding[0]
  61. self.pad_y = padding[1]
  62. img = cv2.copyMakeBorder(
  63. img,
  64. padding[1],
  65. padding[3],
  66. padding[0],
  67. padding[2],
  68. border_type[padding_mode],
  69. value=pad_val)
  70. return img
  71. # 可选提供init函数
  72. def init(self, params):
  73. self.means = params['normal.means']
  74. self.stds = params['normal.stds']
  75. self.resize_width = params['resize.width']
  76. self.resize_height = params['resize.height']
  77. # 必须提供filter函数
  78. def filter(self, inputs, meta_list):
  79. images_data = inputs['INPUT']
  80. images = []
  81. for image_data, meta in zip(images_data, meta_list):
  82. image_data = np.asarray(bytearray(image_data[0]), dtype="uint8")
  83. image = cv2.imdecode(image_data, cv2.IMREAD_COLOR)
  84. h, w, _ = image.shape
  85. img_scale = (self.resize_width, self.resize_height)
  86. max_long_edge = max(img_scale)
  87. max_short_edge = min(img_scale)
  88. scale_factor = min(max_long_edge / max(h, w), max_short_edge / min(h, w))
  89. meta['scale_factor'] = scale_factor
  90. scale_w = int(w * float(scale_factor) + 0.5)
  91. scale_h = int(h * float(scale_factor) + 0.5)
  92. image = cv2.resize(image, (scale_w, scale_h))
  93. image = np.asarray(image).astype(np.float32)
  94. mean = np.array(self.means, dtype=np.float32)
  95. std = np.array(self.stds, dtype=np.float32)
  96. image = self.imnormalize(image, mean, std)
  97. divisor = 32
  98. pad_h = int(np.ceil(image.shape[0] / divisor)) * divisor
  99. pad_w = int(np.ceil(image.shape[1] / divisor)) * divisor
  100. image = self.impad(image, shape=(pad_h, pad_w), pad_val=0)
  101. image = np.asarray(image).astype(np.float32)
  102. image = np.transpose(image, [2, 0, 1])
  103. images.append(image)
  104. return {'input': images}
  105. # 可选提供finalize函数
  106. def finalize(self):
  107. pass

No Description

Contributors (2)