label_ops.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. from __future__ import unicode_literals
  18. import numpy as np
  19. import string
  20. class ClsLabelEncode(object):
  21. def __init__(self, label_list, **kwargs):
  22. self.label_list = label_list
  23. def __call__(self, data):
  24. label = data['label']
  25. if label not in self.label_list:
  26. return None
  27. label = self.label_list.index(label)
  28. data['label'] = label
  29. return data
  30. class DetLabelEncode(object):
  31. def __init__(self, **kwargs):
  32. pass
  33. def __call__(self, data):
  34. import json
  35. label = data['label']
  36. label = json.loads(label)
  37. nBox = len(label)
  38. boxes, txts, txt_tags = [], [], []
  39. for bno in range(0, nBox):
  40. box = label[bno]['points']
  41. txt = label[bno]['transcription']
  42. boxes.append(box)
  43. txts.append(txt)
  44. if txt in ['*', '###']:
  45. txt_tags.append(True)
  46. else:
  47. txt_tags.append(False)
  48. boxes = self.expand_points_num(boxes)
  49. boxes = np.array(boxes, dtype=np.float32)
  50. txt_tags = np.array(txt_tags, dtype=np.bool)
  51. data['polys'] = boxes
  52. data['texts'] = txts
  53. data['ignore_tags'] = txt_tags
  54. return data
  55. def order_points_clockwise(self, pts):
  56. rect = np.zeros((4, 2), dtype="float32")
  57. s = pts.sum(axis=1)
  58. rect[0] = pts[np.argmin(s)]
  59. rect[2] = pts[np.argmax(s)]
  60. diff = np.diff(pts, axis=1)
  61. rect[1] = pts[np.argmin(diff)]
  62. rect[3] = pts[np.argmax(diff)]
  63. return rect
  64. def expand_points_num(self, boxes):
  65. max_points_num = 0
  66. for box in boxes:
  67. if len(box) > max_points_num:
  68. max_points_num = len(box)
  69. ex_boxes = []
  70. for box in boxes:
  71. ex_box = box + [box[-1]] * (max_points_num - len(box))
  72. ex_boxes.append(ex_box)
  73. return ex_boxes
  74. class BaseRecLabelEncode(object):
  75. """ Convert between text-label and text-index """
  76. def __init__(self,
  77. max_text_length,
  78. character_dict_path=None,
  79. character_type='ch',
  80. use_space_char=False):
  81. support_character_type = [
  82. 'ch', 'en', 'EN_symbol', 'french', 'german', 'japan', 'korean',
  83. 'EN', 'it', 'xi', 'pu', 'ru', 'ar', 'ta', 'ug', 'fa', 'ur', 'rs',
  84. 'oc', 'rsc', 'bg', 'uk', 'be', 'te', 'ka', 'chinese_cht', 'hi',
  85. 'mr', 'ne'
  86. ]
  87. assert character_type in support_character_type, "Only {} are supported now but get {}".format(
  88. support_character_type, character_type)
  89. self.max_text_len = max_text_length
  90. self.beg_str = "sos"
  91. self.end_str = "eos"
  92. if character_type == "en":
  93. self.character_str = "0123456789abcdefghijklmnopqrstuvwxyz"
  94. dict_character = list(self.character_str)
  95. elif character_type == "EN_symbol":
  96. # same with ASTER setting (use 94 char).
  97. self.character_str = string.printable[:-6]
  98. dict_character = list(self.character_str)
  99. elif character_type in support_character_type:
  100. self.character_str = ""
  101. assert character_dict_path is not None, "character_dict_path should not be None when character_type is {}".format(
  102. character_type)
  103. with open(character_dict_path, "rb") as fin:
  104. lines = fin.readlines()
  105. for line in lines:
  106. line = line.decode('utf-8').strip("\n").strip("\r\n")
  107. self.character_str += line
  108. if use_space_char:
  109. self.character_str += " "
  110. dict_character = list(self.character_str)
  111. self.character_type = character_type
  112. dict_character = self.add_special_char(dict_character)
  113. self.dict = {}
  114. for i, char in enumerate(dict_character):
  115. self.dict[char] = i
  116. self.character = dict_character
  117. def add_special_char(self, dict_character):
  118. return dict_character
  119. def encode(self, text):
  120. """convert text-label into text-index.
  121. input:
  122. text: text labels of each image. [batch_size]
  123. output:
  124. text: concatenated text index for CTCLoss.
  125. [sum(text_lengths)] = [text_index_0 + text_index_1 + ... + text_index_(n - 1)]
  126. length: length of each text. [batch_size]
  127. """
  128. if len(text) == 0 or len(text) > self.max_text_len:
  129. return None
  130. if self.character_type == "en":
  131. text = text.lower()
  132. text_list = []
  133. for char in text:
  134. if char not in self.dict:
  135. # logger = get_logger()
  136. # logger.warning('{} is not in dict'.format(char))
  137. continue
  138. text_list.append(self.dict[char])
  139. if len(text_list) == 0:
  140. return None
  141. return text_list
  142. class CTCLabelEncode(BaseRecLabelEncode):
  143. """ Convert between text-label and text-index """
  144. def __init__(self,
  145. max_text_length,
  146. character_dict_path=None,
  147. character_type='ch',
  148. use_space_char=False,
  149. **kwargs):
  150. super(CTCLabelEncode,
  151. self).__init__(max_text_length, character_dict_path,
  152. character_type, use_space_char)
  153. def __call__(self, data):
  154. text = data['label']
  155. text = self.encode(text)
  156. if text is None:
  157. return None
  158. data['length'] = np.array(len(text))
  159. text = text + [0] * (self.max_text_len - len(text))
  160. data['label'] = np.array(text)
  161. return data
  162. def add_special_char(self, dict_character):
  163. dict_character = ['blank'] + dict_character
  164. return dict_character
  165. class AttnLabelEncode(BaseRecLabelEncode):
  166. """ Convert between text-label and text-index """
  167. def __init__(self,
  168. max_text_length,
  169. character_dict_path=None,
  170. character_type='ch',
  171. use_space_char=False,
  172. **kwargs):
  173. super(AttnLabelEncode,
  174. self).__init__(max_text_length, character_dict_path,
  175. character_type, use_space_char)
  176. def add_special_char(self, dict_character):
  177. self.beg_str = "sos"
  178. self.end_str = "eos"
  179. dict_character = [self.beg_str] + dict_character + [self.end_str]
  180. return dict_character
  181. def __call__(self, data):
  182. text = data['label']
  183. text = self.encode(text)
  184. if text is None:
  185. return None
  186. if len(text) >= self.max_text_len:
  187. return None
  188. data['length'] = np.array(len(text))
  189. text = [0] + text + [len(self.character) - 1] + [0] * (self.max_text_len
  190. - len(text) - 2)
  191. data['label'] = np.array(text)
  192. return data
  193. def get_ignored_tokens(self):
  194. beg_idx = self.get_beg_end_flag_idx("beg")
  195. end_idx = self.get_beg_end_flag_idx("end")
  196. return [beg_idx, end_idx]
  197. def get_beg_end_flag_idx(self, beg_or_end):
  198. if beg_or_end == "beg":
  199. idx = np.array(self.dict[self.beg_str])
  200. elif beg_or_end == "end":
  201. idx = np.array(self.dict[self.end_str])
  202. else:
  203. assert False, "Unsupport type %s in get_beg_end_flag_idx" \
  204. % beg_or_end
  205. return idx
  206. class SRNLabelEncode(BaseRecLabelEncode):
  207. """ Convert between text-label and text-index """
  208. def __init__(self,
  209. max_text_length=25,
  210. character_dict_path=None,
  211. character_type='en',
  212. use_space_char=False,
  213. **kwargs):
  214. super(SRNLabelEncode,
  215. self).__init__(max_text_length, character_dict_path,
  216. character_type, use_space_char)
  217. def add_special_char(self, dict_character):
  218. dict_character = dict_character + [self.beg_str, self.end_str]
  219. return dict_character
  220. def __call__(self, data):
  221. text = data['label']
  222. text = self.encode(text)
  223. char_num = len(self.character)
  224. if text is None:
  225. return None
  226. if len(text) > self.max_text_len:
  227. return None
  228. data['length'] = np.array(len(text))
  229. text = text + [char_num - 1] * (self.max_text_len - len(text))
  230. data['label'] = np.array(text)
  231. return data
  232. def get_ignored_tokens(self):
  233. beg_idx = self.get_beg_end_flag_idx("beg")
  234. end_idx = self.get_beg_end_flag_idx("end")
  235. return [beg_idx, end_idx]
  236. def get_beg_end_flag_idx(self, beg_or_end):
  237. if beg_or_end == "beg":
  238. idx = np.array(self.dict[self.beg_str])
  239. elif beg_or_end == "end":
  240. idx = np.array(self.dict[self.end_str])
  241. else:
  242. assert False, "Unsupport type %s in get_beg_end_flag_idx" \
  243. % beg_or_end
  244. return idx