__init__.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import copy
  2. import io
  3. import cv2
  4. import numpy as np
  5. from PIL import Image
  6. from importlib import import_module
  7. MODULE_MAPPING = {
  8. 'DetResizeForTest': '.db_resize_for_test',
  9. 'CopyPaste': '.crop_paste',
  10. 'IaaAugment': '.iaa_augment',
  11. 'EastRandomCropData': '.crop_resize',
  12. 'DetLabelEncode': '.db_label_encode',
  13. 'MakeBorderMap': '.db_label_encode',
  14. 'MakeShrinkMap': '.db_label_encode',
  15. }
  16. class NormalizeImage(object):
  17. """normalize image such as substract mean, divide std"""
  18. def __init__(self, scale=None, mean=None, std=None, order='chw', **kwargs):
  19. if isinstance(scale, str):
  20. scale = eval(scale)
  21. self.scale = np.float32(scale if scale is not None else 1.0 / 255.0)
  22. mean = mean if mean is not None else [0.485, 0.456, 0.406]
  23. std = std if std is not None else [0.229, 0.224, 0.225]
  24. shape = (3, 1, 1) if order == 'chw' else (1, 1, 3)
  25. self.mean = np.array(mean).reshape(shape).astype('float32')
  26. self.std = np.array(std).reshape(shape).astype('float32')
  27. def __call__(self, data):
  28. img = data['image']
  29. from PIL import Image
  30. if isinstance(img, Image.Image):
  31. img = np.array(img)
  32. assert isinstance(img,
  33. np.ndarray), "invalid input 'img' in NormalizeImage"
  34. data['image'] = (img.astype('float32') * self.scale -
  35. self.mean) / self.std
  36. return data
  37. class ToCHWImage(object):
  38. """convert hwc image to chw image"""
  39. def __init__(self, **kwargs):
  40. pass
  41. def __call__(self, data):
  42. img = data['image']
  43. from PIL import Image
  44. if isinstance(img, Image.Image):
  45. img = np.array(img)
  46. data['image'] = img.transpose((2, 0, 1))
  47. return data
  48. class KeepKeys(object):
  49. def __init__(self, keep_keys, **kwargs):
  50. self.keep_keys = keep_keys
  51. def __call__(self, data):
  52. data_list = []
  53. for key in self.keep_keys:
  54. data_list.append(data[key])
  55. return data_list
  56. def transform(data, ops=None):
  57. """transform."""
  58. if ops is None:
  59. ops = []
  60. for op in ops:
  61. data = op(data)
  62. if data is None:
  63. return None
  64. return data
  65. class DecodeImage(object):
  66. """decode image."""
  67. def __init__(self,
  68. img_mode='RGB',
  69. channel_first=False,
  70. ignore_orientation=False,
  71. **kwargs):
  72. self.img_mode = img_mode
  73. self.channel_first = channel_first
  74. self.ignore_orientation = ignore_orientation
  75. def __call__(self, data):
  76. img = data['image']
  77. assert type(img) is bytes and len(
  78. img) > 0, "invalid input 'img' in DecodeImage"
  79. img = np.frombuffer(img, dtype='uint8')
  80. if self.ignore_orientation:
  81. img = cv2.imdecode(
  82. img, cv2.IMREAD_IGNORE_ORIENTATION | cv2.IMREAD_COLOR)
  83. else:
  84. img = cv2.imdecode(img, 1)
  85. if img is None:
  86. return None
  87. if self.img_mode == 'GRAY':
  88. img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
  89. elif self.img_mode == 'RGB':
  90. assert img.shape[2] == 3, 'invalid shape of image[%s]' % (
  91. img.shape)
  92. img = img[:, :, ::-1]
  93. if self.channel_first:
  94. img = img.transpose((2, 0, 1))
  95. data['image'] = img
  96. return data
  97. class DecodeImagePIL(object):
  98. """decode image."""
  99. def __init__(self, img_mode='RGB', **kwargs):
  100. self.img_mode = img_mode
  101. def __call__(self, data):
  102. img = data['image']
  103. assert type(img) is bytes and len(
  104. img) > 0, "invalid input 'img' in DecodeImage"
  105. img = data['image']
  106. buf = io.BytesIO(img)
  107. img = Image.open(buf).convert('RGB')
  108. if self.img_mode == 'Gray':
  109. img = img.convert('L')
  110. elif self.img_mode == 'BGR':
  111. img = np.array(img)[:, :, ::-1] # 将图片转为numpy格式,并将最后一维通道倒序
  112. img = Image.fromarray(np.uint8(img))
  113. data['image'] = img
  114. return data
  115. def dynamic_import(class_name):
  116. module_path = MODULE_MAPPING.get(class_name)
  117. if not module_path:
  118. raise ValueError(f'Unsupported class: {class_name}')
  119. module = import_module(module_path, package=__package__)
  120. return getattr(module, class_name)
  121. def create_operators(op_param_list, global_config=None):
  122. ops = []
  123. for op_info in op_param_list:
  124. op_name = list(op_info.keys())[0]
  125. param = copy.deepcopy(op_info[op_name]) or {}
  126. if global_config:
  127. param.update(global_config)
  128. if op_name in globals():
  129. op_class = globals()[op_name]
  130. else:
  131. op_class = dynamic_import(op_name)
  132. ops.append(op_class(**param))
  133. return ops