MCPcopy Create free account
hub / github.com/DocsaidLab/MRZScanner / TextDecoder

Class TextDecoder

mrzscanner/utils.py:44–78  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

42
43
44class TextDecoder:
45
46 def __init__(
47 self,
48 *,
49 chars_dict: Dict[str, int],
50 decode_mode: Optional[Union[DecodeMode, str, int]] = DecodeMode.Default
51 ):
52 self.chars_dict = chars_dict
53 self.chars = {v: k for k, v in self.chars_dict.items()}
54 self.decode_mode = DecodeMode.obj_to_enum(decode_mode)
55
56 def decode(self, encode: List[np.ndarray]) -> List[List[str]]:
57 encode = np.array(encode)
58 if self.decode_mode == DecodeMode.CTC:
59 masks = (encode != np.roll(encode, 1)) & (encode != 0)
60 elif self.decode_mode in [DecodeMode.Default, DecodeMode.Normal]:
61 masks = []
62 for row in encode:
63 eos_index = np.where(row == self.chars_dict["<EOS>"])[0]
64 if eos_index.size > 0:
65 mask = np.zeros_like(row, dtype=bool)
66 mask[:eos_index[0]] = True
67 else:
68 mask = np.ones_like(row, dtype=bool)
69 mask = mask & (row != self.chars_dict["<PAD>"])
70 masks.append(mask)
71
72 chars_list = [''.join([self.chars[idx] for idx in e[m]])
73 for e, m in zip(encode, masks)]
74
75 return chars_list
76
77 def __call__(self, *args, **kwargs) -> List[List[str]]:
78 return self.decode(*args, **kwargs)

Callers 8

test_textdecoder_defaultFunction · 0.90
test_textdecoder_no_eosFunction · 0.90
test_textdecoder_ctcFunction · 0.90
test_textdecoder_callFunction · 0.90
__init__Method · 0.85
__init__Method · 0.85

Calls

no outgoing calls

Tested by 6

test_textdecoder_defaultFunction · 0.72
test_textdecoder_no_eosFunction · 0.72
test_textdecoder_ctcFunction · 0.72
test_textdecoder_callFunction · 0.72