python 大模型验证码识别
一、注册账号https://huggingface.co/
方便下载大模型,并配置Token
进入国内镜像站:https://hf-mirror.com/
我们现在以验证码识别为例:webapi.py
安装依赖:
https://webapi.py/
1. 安装依赖
pip install -U huggingface_hub
pip install transformers datasets torch
#pip uninstall torch
#conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
2. 设置环境变量
Linux
export HF_ENDPOINT=https://hf-mirror.com
Windows Powershell
$env:HF_ENDPOINT = "https://hf-mirror.com"
3.1 下载模型
huggingface-cli download --token 自已上面生成的token --resume-download zenoda/trocr-captcha-killer --local-dir trocr-captcha-killer
打开:https://hf-mirror.com/zenoda/trocr-captcha-killer/tree/main
打开:https://hf-mirror.com/zenoda/trocr-captcha-killer 复制运行代码
注意要将:cuda改成cpu
运行完整代码
from flask import Flask,request
from werkzeug.utils import secure_filename
import uuid
from transformers import VisionEncoderDecoderModel, TrOCRProcessor
from PIL import Image
import requests
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
@app.post('/upload')
def file_upload():
f=request.files['file']
uuid1 =str(uuid.uuid1())
path=uuid1 + f.filename
f.save(f"{secure_filename(f.filename)}")
processor = TrOCRProcessor.from_pretrained("D:/工作空间/练习空间/PythonProjects/pythonProject1/trocr-captcha-killer")
model = VisionEncoderDecoderModel.from_pretrained("D:/工作空间/练习空间/PythonProjects/pythonProject1/trocr-captcha-killer")
#model.to('cuda')
model.to('cpu')
url = 'https://hf-mirror.com/datasets/zenoda/trocr-captcha-killer/resolve/main/106-1688354008849.png'
image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
#generated_ids = model.generate(processor(image, return_tensors="pt").pixel_values.to('cuda'))
generated_ids = model.generate(processor(image, return_tensors="pt").pixel_values.to('cpu'))
predictText = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(predictText)
return '上传成功!'
if __name__ == '__main__':
app.run()
上面图片验证码识别是:106。