convert text-label into text-index. input: text: text labels of each image. [batch_size] output: text: concatenated text index for CTCLoss. [sum(text_lengths)] = [text_index_0 + text_index_1 + ... + text_index_(n - 1)] length:
(self, text)
| 131 | return text_list, text_node, ch_order.sort() |
| 132 | |
| 133 | def encodech(self, text): |
| 134 | """convert text-label into text-index. |
| 135 | input: |
| 136 | text: text labels of each image. [batch_size] |
| 137 | |
| 138 | output: |
| 139 | text: concatenated text index for CTCLoss. |
| 140 | [sum(text_lengths)] = [text_index_0 + text_index_1 + ... + text_index_(n - 1)] |
| 141 | length: length of each text. [batch_size] |
| 142 | """ |
| 143 | if len(text) == 0: |
| 144 | return None, None, None |
| 145 | if self.lower: |
| 146 | text = text.lower() |
| 147 | text_node_dict = {} |
| 148 | text_node_dict.update({0: 1}) |
| 149 | character_index = [_ for _ in range(self.num_character)] |
| 150 | text_list = [] |
| 151 | for char in text: |
| 152 | if char not in self.dict: |
| 153 | continue |
| 154 | i_c = self.dict[char] |
| 155 | text_list.append(i_c) |
| 156 | |
| 157 | if i_c in text_node_dict.keys(): |
| 158 | text_node_dict[i_c] += 1 |
| 159 | else: |
| 160 | text_node_dict.update({i_c: 1}) |
| 161 | for ic in list(text_node_dict.keys()): |
| 162 | character_index.remove(ic) |
| 163 | none_char_index = random.sample(character_index, |
| 164 | 37 - len(list(text_node_dict.keys()))) |
| 165 | for ic in none_char_index: |
| 166 | text_node_dict[ic] = 0 |
| 167 | |
| 168 | text_node_index = sorted(text_node_dict) |
| 169 | |
| 170 | text_node_num = [text_node_dict[k] for k in text_node_index] |
| 171 | if len(text_list) == 0 or len(text_list) > self.max_text_len: |
| 172 | return None, None, None |
| 173 | return text_list, text_node_index, text_node_num |