使用pytorch模型学习框架easyocr模块识别行程码图片文字并使用Flask Web返回指定信息json字符串

关注「WeiyiGeek」公众号

设为「特别关注」每天带你玩转网络安全运维、应用开发、物联网IOT学习!

1312485496917b380a2a705ba45f0c39.png


本章目录:

使用pytorch模型学习框架easyocr模块行识别程码图片

  • 安装部署

  • 实践使用

  • 入坑出坑


原文地址: https://www.weiyigeek.top

前言简述

描述: 公司有业务需求做一个行程码识别, 当前是调用某云的文字识别接口来识别行程码, 而其按照调用次数进行计费, 所以为了节约成本就要Python参考了Github上大佬的们项目, 截取部分函数,并使用Flask Web 框架进行封装,从而实现通过网页进行请求调用,并返回JSON字符串。

项目地址: https://github.com/JaidedAI/EasyOCR


使用pytorch模型学习框架easyocr模块识别行程码图片

安装部署

环境依赖

  • Python 建议 3.8 以上版本 (原本我的环境是Python 3.7安装时各种稀奇古怪的错误都出来,不得已abandon放弃)

  • flask 模块

  • torch 、torchvision 模块

  • easyocr 模块

安装流程
步骤 01.flask 和 easyocr及其依赖模块的安装。

pip install flask -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
pip install easyocr -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

步骤 02.为了防止使用时长时间拉取训练模型,我们可以手动下载模型并安装到指定位置,下载地址: https://www.jaided.ai/easyocr/modelhub/

# 主要下载以下模型
english_g2 : https://github.com/JaidedAI/EasyOCR/releases/download/v1.3/english_g2.zip
zh_sim_g2 : https://github.com/JaidedAI/EasyOCR/releases/download/v1.3/zh_sim_g2.zip
CRAFT : https://github.com/JaidedAI/EasyOCR/releases/download/pre-v1.1.6/craft_mlt_25k.zip

# 模型安装位置
# windows
C:\Users\WeiyiGeek\.EasyOCR\model

# Linux
/home/weiyigeek/.EasyOCR\model

ce413e97d540b78be9bd6c33806c7770.png

实践使用

  • 步骤 01.项目路径以及图片路径 D:\Study\Project

PS D:\Study\Project> ls
    目录: D:\Study\Project
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2022/5/25     15:59                img
d-----         2022/5/25     17:07                upfile
-a----         2022/5/25     19:34           3966 index.py
  • 步骤 02.基于Flask web框架下进行调用EasyOCR执行图片文字识别的python代码.

# -*- coding: utf-8 -*-
# ####################################################################
# Author: WeiyiGeek
# Description: 基于easyocr实现大数据通信行程卡图片识别信息获取-Flask项目。
# Time: 2022年5月25日 17点31分
# ====================================================================
# 环境依赖与模块安装, 建议 Python 3.8.x 的环境下进行
# pip install flask
# pip install easyocr
# #####################################################################
import re
import os
import glob
import json
import easyocr
from flask import Flask, jsonify, request

app = Flask(__name__)

# 项目与行程码图片
HOMEDIR=r"D:\Study\Project"

# 使用easyocr模块中的Reader方法, 设置识别中英文两种语言
reader = easyocr.Reader(['ch_sim', 'en'], gpu=False) 

def information_filter(text_str,file_path):
  """
  函数说明: 提出ocr识别的行程码
  参数值:字符串,文件名称
  返回值:有效信息组成的字典
  """
  # 健康码字段
  re_healthcode = re.compile('请收下(.{,2})行程卡')
  healthcode = re_healthcode.findall(text_str)[0]
  # 电话字段
  re_phone = re.compile('[0-9]{3}\*{4}[0-9]{4}')
  phone_str = re_phone.findall(text_str)[0]
  # 日期字段
  re_data = re.compile('2022\.[0-1][0-9]\.[0-3][0-9]')
  data_str = re_data.findall(text_str)[0]
  # 时间字段
  re_time = re.compile('[0-9][0-9]:[0-9][0-9]:[0-9][0-9]')
  time_str = re_time.findall(text_str)[0]
  # 地区城市字段
  citys_re = re.compile('到达或途经:(.+)结果包含')
  citys_str = citys_re.findall(text_str)[0].strip().split('(')[0]
  result_dic = {"filename": file_path ,"类型": healthcode, "电话": phone_str, "日期": data_str, "时间": time_str, "行程": citys_str}
  print(result_dic)
  return result_dic

# Flask 路由 - 首页
@app.route('/')
@app.route('/index')
def Index():
  return "<h4 style='text-algin:center'>https://blog.weiyigeek.top</h4><script>window.location.href='https://blog.weiyigeek.top'</script>"

# Flask 路由
@app.route('/tools/ocr',methods=["GET"])
def Travelcodeocr():
  """
  请求路径: /tools/ocr
  请求参数: ?file=test.png
  """
  filename = request.args.get("file")
  dirname = request.args.get("dir")
  if (filename):
    img_path = os.path.join(HOMEDIR, filename)
    print(img_path)  # 打印路径
    if (os.path.exists(img_path)):
      text = reader.readtext(img_path, detail=0) 
      text_str = "".join(text)
      try:
        result_dic = information_filter(text_str,os.path.basename(img_path))
      except Exception as err:
        print(err)
        return json.dumps({"status":"err", "img": filename}).encode('utf-8'), 200, {"Content-Type":"application/json"}
      return json.dumps(result_dic, ensure_ascii=False).encode('utf-8'), 200, {"Content-Type":"application/json"}
    else:
      return jsonify({"status": "err","msg": "文件"+img_path+"路径不存在!"})
  elif (dirname and os.path.join(HOMEDIR+dirname)):
    result_dic_all = []
    result_dic_err = []
    img_path_all = glob.glob(HOMEDIR+dirname+"\*.png")      # 支持正则匹配
    for img_path in img_path_all:
      print(img_path)  # 打印路径
      text = reader.readtext(img_path, detail=0)  # 支持图片路径和url,返回列表
      text_str = "".join(text)
      try:
        result_dic = information_filter(text_str,os.path.basename(img_path))
      except Exception as err:
        print(img_path,"-->>",err) # 错误输出
        result_dic_err.append(img_path)
        continue
      result_dic_all.append(result_dic)
    print(result_dic_err)
    return json.dumps(result_dic_all, ensure_ascii=False).encode('utf-8'), 200, {"Content-Type":"application/json"}
  else:
    return jsonify({"status": "err","msg": "请求参数有误!"})

# Flask 程序入口
if __name__ == '__main__':
  app.run(host='0.0.0.0',port=8000,debug=True)
  • 步骤 03.运行该脚本并使用浏览进行指定行程码图片路径以及识别提取。

python .\index.py
  # Using CPU. Note: This module is much faster with a GPU.
  # * Serving Flask app 'index' (lazy loading)
  # * Environment: production
  #   WARNING: This is a development server. Do not use it in a production deployment.
  #   Use a production WSGI server instead.
  # * Debug mode: on
  # * Running on all addresses (0.0.0.0)
  #   WARNING: This is a development server. Do not use it in a production deployment.
  # * Running on http://127.0.0.1:8000
  # * Running on http://10.20.172.106:8000 (Press CTRL+C to quit)
  # * Restarting with stat
  # Using CPU. Note: This module is much faster with a GPU.
  # * Debugger is active!
  # * Debugger PIN: 115-313-307

温馨提示: 从上面的Python脚本中可以看出我们可使用file参数指定图片路径或者使用dir参数指定行程码图片存放目录。
例如,获取单个行程码图片信息,我本地浏览器访问http://127.0.0.1:8000/tools/ocr?file=img/00e336dbde464c809ef1f6ea568d4621.png地址,将会返回如下JSON字符串。

{"filename": "00e336dbde464c809ef1f6ea568d4621.png", "类型": "绿色", "电话": "157****2966", "日期": "2022.05.25", "时间": "09:03:56", "行程": "重庆市"}

c1bceedacb87552731c0e944d2bbeace.png

例如,获取多个行程码图片信息,我本地浏览器访问http://127.0.0.1:8000/tools/ocr?file=/img/地址,将会返回如下图所示结果。

771f202f97ff612e3d0e2558320e07fd.png

入坑出坑

问题1.通过pip install 安装easyocr离线的whl包是报ERROR: No matching distribution found for torch

  • 错误信息:

pip install ./easyocr-1.4.2-py3-none-any.whl -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
ERROR: Could not find a version that satisfies the requirement torch (from easyocr) (from versions: none)
ERROR: No matching distribution found for torch
  • 解决办法: python.exe -m pip install --upgrade pip

问题2.在Python3.7的环境中安装easyocr依赖的torch模块的whl安装包报not a supported wheel on this platform.错误

  • 错误信息:

$ pip install torch-1.8.0+cpu-cp37-cp37m-win_amd64.whl -i https://pypi.tuna.tsinghua.edu.cn/simple/
WARNING: Requirement 'torch-1.8.0+cpu-cp37-cp37m-win_amd64.whl' looks like a filename, but the file does not exist
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
ERROR: torch-1.8.0+cpu-cp37-cp37m-win_amd64.whl is
  • 错误原因: 平台与下载的whl不符合, 此处我遇到的问题明显不是这个导致的,百度后我想是由于pip版本与python版本、以及系统平台联合导致。

  • 解决办法:

# 解决1.假如,你是linux你可以通过 https://download.pytorch.org/whl/torch_stable.html 找到所需版本。
文件名解释:cpu或显卡/文件名-版本号-python版本-应该是编译格式-平台-cpu类型(intel也选amd64)
# torch-1.8.0+cpu-cp37-cp37m-win_amd64.whl

# 解决2.将 torch-1.8.0+cpu-cp37-cp37m-win_amd64.whl 更名为 torch-1.8.0+cpu-cp37-cp37m-win32.whl

问题3.在执行调用torch模块的py脚本时报Error loading "D:\****\lib\site-packages\torch\lib\asmjit.dll" or one of its dependencies.错误

  • 错误信息:

Microsoft Visual C++ Redistributable is not installed, this may lead to the DLL load failure.
It can be downloaded at https://aka.ms/vs/16/release/vc_redist.x64.exe
Traceback (most recent call last):
.....
OSError: [WinError 193] <no description> Error loading "D:\Program Files (x86)\Python37-32\lib\site-packages\torch\lib\asmjit.dll" or one of its dependencies.
  • 解决办法: 在你的电脑上下载安装 https://aka.ms/vs/16/release/vc_redist.x64.exe 缺少的C++运行库,重启电脑。

问题4.在安装opencv_python_headless进行依赖模块安装时报ERROR: No matching distribution found for torchvision>=0.5错误

  • 错误信息:

Using cached https://mirrors.aliyun.com/pypi/packages/a4/0a/39b102047bcf3b1a58ee1cc83a9269b2a2c4c1ab3062a65f5292d8df6594/opencv_python_headless-4.5.4.60-cp37-cp37m-win32.whl (25.8 MB)
ERROR: Could not find a version that satisfies the requirement torchvision>=0.5 (from easyocr) (from versions: 0.1.6, 0.1.7, 0.1.8, 0.1.9, 0.2.0, 0.2.1, 0.2.2, 0.2.2.post2, 0.2.2.post3)
ERROR: No matching distribution found for torchvision>=0.5
  • 解决办法: 如果你的 python 版本为3.7.x,那么你只能安装 torch 1.5 和 torchvision0.6

本文至此完毕,更多技术文章,尽情期待下一章节!


原文地址: https://blog.weiyigeek.top


92fdcfa4085554f2cb965d47e01d4be2.png 往期相关文章bc636885ab2538cde89ea816ee27b136.png

1.还不会部署高可用的kubernetes集群?看我手把手教你使用二进制部署v1.23.6的K8S集群实践(上)

2.还不会部署高可用的kubernetes集群?看我手把手教你使用二进制部署v1.23.6的K8S集群实践(下)

Jenkins Pipeline 流水线如何根据代仓库的 webhook 自动触发拉取提交的分支代码并构建?

如何在 Kubernetes 中进行 ingress-nginx 配置优化以及HTTP请求速率限制

如何配置Kubernetes仪表板dashboard支持http方式并使用ingress-nginx代理访问实践

在k8s集群中Kubernetes仪表板dashboard使用RABC机制限制指定用户针对指定名称空间中的资源进行管理实践

Let'sEncrypt快速颁发及自动续签泛域名证书实践指南

d12bb0a3f146e58d68aa41da210fab3b.png


欢迎各位志同道合的朋友一起学习交流,如文章有误请在下方留下您宝贵的经验知识,个人邮箱地址【master#weiyigeek.top】或者个人公众号【WeiyiGeek】联系我。

更多文章来源于【WeiyiGeek Blog 个人博客 - 为了能到远方,脚下的每一步都不能少 】

个人主页: 【 https://weiyigeek.top】

博客地址: 【 https://blog.weiyigeek.top 】

3df7cd15ea05d44afe0ab609e0c47304.png

专栏书写不易,如果您觉得这个专栏还不错的,请给这篇专栏 【点个赞、投个币、收个藏、关个注,转个发,留个言】(人间六大情),这将对我的肯定,谢谢!。

  • echo  "【点个赞】,动动你那粗壮的拇指或者芊芊玉手,亲!"

  • printf("%s", "【投个币】,万水千山总是情,投个硬币行不行,亲!")

  • fmt.Printf("【收个藏】,阅后即焚不吃灰,亲!")  

  • console.info("【转个发】,让更多的志同道合的朋友一起学习交流,亲!")

  • System.out.println("【关个注】,后续浏览查看不迷路哟,亲!")

  • cout << "【留个言】,文章写得好不好、有没有错误,一定要留言哟,亲! " << endl;

007f64d412b939f64416511ffe164929.gif

更多网络安全、系统运维、应用开发、全栈文章,尽在【个人博客 - https://blog.weiyigeek.top】站点,谢谢支持!

    帅哥、美女、大佬们点个【赞+在看】吧! 👇

↓👇↓ 点击【"阅读原文"】,即可获取更多知识!

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来回答你的问题。 人脸识别系统是一个非常常见和重要的AI应用,而PyTorch是一个非常流行且易于使用的深度学习框架,AlexNet则是一个经典的卷积神经网络模型,我们可以使用这两者来构建一个人脸识别系统。 下面是一个简单的人脸识别系统的实现步骤: 1. 收集人脸数据集:首先需要收集一个包含多张人脸图像的数据集,可以使用公开数据集如LFW或自行收集并标注数据集。 2. 数据预处理:将数据集进行预处理,包括人脸检测、对齐、裁剪和归一化等操作,使其符合模型的输入要求。 3. 构建模型使用PyTorch框架构建一个基于AlexNet的卷积神经网络模型,用于训练和识别人脸。模型的最后一层需要用于将输入图像映射为一个向量,这个向量将作为人脸的特征向量用于后续的识别操作。 4. 训练模型使用已经预处理好的数据集对模型进行训练,可以使用交叉熵损失函数和随机梯度下降算法进行优化。 5. 人脸识别使用训练好的模型对新的人脸图像进行识别,首先需要对新图像进行预处理,然后输入到训练好的模型中得到特征向量,最后将新的特征向量与已知的特征向量进行比较,找到最相似的人脸特征向量即可完成识别操作。 总之,使用PyTorch框架和AlexNet模型构建一个人脸识别系统需要进行数据预处理模型构建和训练等步骤,最终可以实现对新的人脸图像的识别操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值