forked from dee1024/pytorch-captcha-recognition
-
Notifications
You must be signed in to change notification settings - Fork 1
/
one_hot_encoding.py
46 lines (42 loc) · 1.26 KB
/
one_hot_encoding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# -*- coding: UTF-8 -*-
import numpy as np
import captcha_setting
def encode(text):
vector = np.zeros(captcha_setting.ALL_CHAR_SET_LEN * captcha_setting.MAX_CAPTCHA, dtype=float)
def char2pos(c):
if c =='_':
k = 62
return k
k = ord(c)-48
if k > 9:
k = ord(c) - 65 + 10
if k > 35:
k = ord(c) - 97 + 26 + 10
if k > 61:
raise ValueError('error')
return k
for i, c in enumerate(text):
idx = i * captcha_setting.ALL_CHAR_SET_LEN + char2pos(c)
vector[idx] = 1.0
return vector
def decode(vec):
char_pos = vec.nonzero()[0]
text=[]
for i, c in enumerate(char_pos):
char_at_pos = i #c/63
char_idx = c % captcha_setting.ALL_CHAR_SET_LEN
if char_idx < 10:
char_code = char_idx + ord('0')
elif char_idx <36:
char_code = char_idx - 10 + ord('A')
elif char_idx < 62:
char_code = char_idx - 36 + ord('a')
elif char_idx == 62:
char_code = ord('_')
else:
raise ValueError('error')
text.append(chr(char_code))
return "".join(text)
if __name__ == '__main__':
e = encode("BK7H")
print(decode(e))