1 Star 0 Fork 17

gowy111/pytorch-captcha-recognition

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
one_hot_encoding.py 1.26 KB
一键复制 编辑 原始数据 按行查看 历史
coolcooldee 提交于 2018-10-23 17:27 . test
# -*- 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))
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/gowy111/pytorch-captcha-recognition.git
git@gitee.com:gowy111/pytorch-captcha-recognition.git
gowy111
pytorch-captcha-recognition
pytorch-captcha-recognition
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385