1 Star 0 Fork 1

Barneys / pytorch_captcha_recognition

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
one_hot_encoding.py 1.26 KB
一键复制 编辑 原始数据 按行查看 历史
Barneys 提交于 2021-12-11 13:49 . 20211211
# -*- 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))
Python
1
https://gitee.com/Barneys/pytorch_captcha_recognition.git
git@gitee.com:Barneys/pytorch_captcha_recognition.git
Barneys
pytorch_captcha_recognition
pytorch_captcha_recognition
master

搜索帮助