Git Product home page Git Product logo

calculatecaptcha_recognition's Introduction

CalculateCaptcha_Recognition

使用pytorch训练和识别计算验证码

#pytorch验证码识别

1.环境配置

下载代码

git clone https://github.com/fupinglee/CalculateCaptcha_Recognition

安装依赖

cd CalculateCaptcha_Recognition
pip install -r requirements_gpu.txt # gpu环境 

如果没有GPU环境的话使用pip install -r requirements.txt

2.样本生成

https://github.com/fupinglee/Calculate_Captcha/releases

有两个版本,v1.0是老版本的验证码生成,v1.1是新版本验证码的生成,差别不是很大。 可以两种验证码都生成一些,然后合并训练,也可以只要一种,识别率96%以上。

生成训练验和测试证码,这里训练验证码2000张,测试200张

java -jar  CalculateCaptcha.jar -n 2000 -p train
java -jar  CalculateCaptcha.jar -n 200 -p test

然后将datasets放在CalculateCaptcha_Recognition目录下

3.训练

python3 train.py

训练图片

可以用GPU训练,没条件就CPU训练,耗时不会太多。 刚开始训练正确率很快就能达到100%

4.识别测试

python3 test.py

批量测试

test()

单个样本测试

test_pic('datasets/test/xxxx.jpg')

网络下载新的验证码进行验证

test_net("http://demo.ruoyi.vip/captcha/captchaImage?type=math&s=0.39236748354325024")

5.pytorch模型转换为onnx

python to_onnx.py

参考将PyTorch模型转换为ONNX

然后使用onnx进行验证码识别

if __name__ == '__main__':
    path = "datasets/test/0+8=?_69146590872302eb7f65d52074da94a7.jpg"
    onnxFile = 'mathcode.onnx'

    img = Image.open(path)
    trans = transforms.Compose([
        transforms.Resize((60, 160)),
        # transforms.Grayscale(),
        transforms.ToTensor()
    ])
    img_tensor = trans(img)
    img_tensor = img_tensor.reshape(1, 3, 60, 160)  # 1张图片 1 灰色
    ort_session = onnxruntime.InferenceSession(onnxFile)

    modelInputName = ort_session.get_inputs()[0].name
    # onnx 网络输出
    onnx_out = ort_session.run(None, {modelInputName: to_numpy(img_tensor)})
    onnx_out = torch.tensor(np.array(onnx_out))
    onnx_out = onnx_out.view(-1, common.captcha_array.__len__())
    print(vec2Text(onnx_out))

执行python onnx_test.py

calculatecaptcha_recognition's People

Contributors

fupinglee avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.