MCPcopy Create free account
hub / github.com/chibohe/text_recognition_toolbox / encode

Method encode

character.py:53–71  ·  view source on GitHub ↗

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: l

(self, text)

Source from the content-addressed store, hash-verified

51 self.char_num = len(self.character)
52
53 def encode(self, text):
54 """convert text-label into text-index.
55 input:
56 text: text labels of each image. [batch_size]
57 output:
58 text: concatenated text index for CTCLoss.
59 [sum(text_lengths)] = [text_index_0 + text_index_1 + ... + text_index_(n - 1)]
60 length: length of each text. [batch_size]
61 """
62 length = [len(s) for s in text]
63 # text = ''.join(text)
64 # text = [self.dict[char] for char in text]
65 d = []
66 batch_max_length = max(length)
67 for s in text:
68 t = [self.dict[char] for char in s]
69 t.extend([0] * (batch_max_length - len(s)))
70 d.append(t)
71 return (torch.tensor(d, dtype=torch.long), torch.tensor(length, dtype=torch.long))
72
73 def decode(self, preds, raw=False):
74 """ convert text-index into text-label. """

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected