当前位置: 首页 > article >正文

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。


http://www.kler.cn/news/305795.html

相关文章:

  • C++11的部分新特性
  • Vue路由:Vue router
  • 使用ESP8266和OLED屏幕实现一个小型电脑性能监控
  • 优化深度学习模型训练过程:提升PASCAL VOC 2012数据集上Deeplabv3+模型训练效率的策略
  • 【leetcode-python】最接近的三数之和
  • Acrobat 9 安装教程
  • Redis入门2
  • 驾校预约学习系统的设计与实现
  • 关于决策树的一些介绍
  • 让孩子们动手又动脑,用学优马电子积木,探索电路的奥秘
  • 计算机毕业设计Python深度学习垃圾邮件分类检测系统 朴素贝叶斯算法 机器学习 人工智能 数据可视化 大数据毕业设计 Python爬虫 知识图谱 文本分类
  • Visual Studio安装教程
  • 如何使用ssm实现流浪动物救助站+vue
  • JVM基础概念
  • 深度学习基础--卷积网络
  • Vue安装及环境配置【图解版】
  • leetcode 2576.求出最多标记下标
  • C# 开发教程-中级教程
  • IEEE 754浮点数表示
  • 18062 二维数组每行中的最大值
  • k8s环境配置
  • 【Unity】简易而又实用的抽卡算法
  • 机器学习特征构建与特征筛选
  • NC字典树的实现
  • 深入理解 Redis 的文件事件处理器
  • 暗界正方形之谜
  • 【YashanDB知识库】单机升级典型问题及应急措施
  • Spring3-IoC1-IoC容器、基于xml管理bean
  • 【SSRF漏洞】——http协议常见绕过
  • 【React】React18.2.0核心源码解读