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)
| 91 | return dict_character |
| 92 | |
| 93 | def encode(self, text): |
| 94 | """convert text-label into text-index. |
| 95 | input: |
| 96 | text: text labels of each image. [batch_size] |
| 97 | |
| 98 | output: |
| 99 | text: concatenated text index for CTCLoss. |
| 100 | [sum(text_lengths)] = [text_index_0 + text_index_1 + ... + text_index_(n - 1)] |
| 101 | length: length of each text. [batch_size] |
| 102 | """ |
| 103 | if len(text) == 0: |
| 104 | return None, None, None |
| 105 | if self.lower: |
| 106 | text = text.lower() |
| 107 | text_node = [0 for _ in range(self.num_character)] |
| 108 | text_node[0] = 1 |
| 109 | text_list = [] |
| 110 | ch_order = [] |
| 111 | order = 1 |
| 112 | for char in text: |
| 113 | if char not in self.dict: |
| 114 | continue |
| 115 | text_list.append(self.dict[char]) |
| 116 | text_node[self.dict[char]] += 1 |
| 117 | ch_order.append( |
| 118 | [self.dict[char], text_node[self.dict[char]], order]) |
| 119 | order += 1 |
| 120 | |
| 121 | no_ch_order = [] |
| 122 | for char in self.character: |
| 123 | if char not in text: |
| 124 | no_ch_order.append([self.dict[char], 1, 0]) |
| 125 | random.shuffle(no_ch_order) |
| 126 | ch_order = ch_order + no_ch_order |
| 127 | ch_order = ch_order[:self.max_text_len + 1] |
| 128 | |
| 129 | if len(text_list) == 0 or len(text_list) > self.max_text_len: |
| 130 | return None, None, None |
| 131 | return text_list, text_node, ch_order.sort() |
| 132 | |
| 133 | def encodech(self, text): |
| 134 | """convert text-label into text-index. |