使用pytorch模型学习框架easyocr模块识别行程码图片文字并使用Flask Web返回指定信息json字符串
关注「WeiyiGeek」公众号
设为「特别关注」每天带你玩转网络安全运维、应用开发、物联网IOT学习!
本章目录:
使用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
实践使用
步骤 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, requestapp = 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)continueresult_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", "行程": "重庆市"}
例如,获取多个行程码图片信息,我本地浏览器访问http://127.0.0.1:8000/tools/ocr?file=/img/
地址,将会返回如下图所示结果。
入坑出坑
问题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
往期相关文章
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快速颁发及自动续签泛域名证书实践指南
欢迎各位志同道合的朋友一起学习交流,如文章有误请在下方留下您宝贵的经验知识,个人邮箱地址【master#weiyigeek.top】
或者个人公众号【WeiyiGeek】
联系我。
更多文章来源于【WeiyiGeek Blog 个人博客 - 为了能到远方,脚下的每一步都不能少 】
个人主页: 【 https://weiyigeek.top】
博客地址: 【 https://blog.weiyigeek.top 】
专栏书写不易,如果您觉得这个专栏还不错的,请给这篇专栏 【点个赞、投个币、收个藏、关个注,转个发,留个言】(人间六大情),这将对我的肯定,谢谢!。
echo "【点个赞】,动动你那粗壮的拇指或者芊芊玉手,亲!"
printf("%s", "【投个币】,万水千山总是情,投个硬币行不行,亲!")
fmt.Printf("【收个藏】,阅后即焚不吃灰,亲!")
console.info("【转个发】,让更多的志同道合的朋友一起学习交流,亲!")
System.out.println("【关个注】,后续浏览查看不迷路哟,亲!")
cout << "【留个言】,文章写得好不好、有没有错误,一定要留言哟,亲! " << endl;
更多网络安全、系统运维、应用开发、全栈文章,尽在【个人博客 - https://blog.weiyigeek.top】站点,谢谢支持!
帅哥、美女、大佬们点个【赞+在看】吧!
使用pytorch模型学习框架easyocr模块识别行程码图片文字并使用Flask Web返回指定信息json字符串相关推荐
- pytorch深度学习框架—torch.nn模块(一)
pytorch深度学习框架-torch.nn模块 torch.nn模块中包括了pytorch中已经准备好的层,方便使用者调用构建的网络.包括了卷积层,池化层,激活函数层,循环层,全连接层. 卷积层 p ...
- pytorch深度学习框架--gpu和cpu的选择
pytorch深度学习框架–gpu和cpu的选择 基于pytorch框架,最近实现了一个简单的手写数字识别的程序,我安装的pytorch是gpu版(你也可以安装cpu版本的,根据个人需要),这里我介绍 ...
- 2021-7-26 pytorch深度学习框架学习
1. Pytorch深度学习框架
- 人工智能:PyTorch深度学习框架介绍
目录 1.PyTorch 2.PyTorch常用的工具包 3.PyTorch特点 4.PyTorch不足之处 今天给大家讲解一下PyTorch深度学习框架的一些基础知识,希望对大家理解PyTorch有 ...
- Python模块学习 - 用tinify模块压缩和优化图片
Python模块学习 - 用tinify模块压缩和优化图片 tinify模块 功能描述:TinyPNG和TinyJPG网站提供了压缩和优化.png和.jpg格式图片的功能.虽然可以很轻松地使用网页版进 ...
- Java调用百度OCR文字识别API实现图片文字识别软件
java_baidu_ocr Java调用百度OCR文字识别API实现图片文字识别软件 这是一款小巧方便,强大的文字识别软件,由Java编写,配上了窗口界面 调用了百度ocr文字识别API 识别精度高 ...
- java ocr api_Java调用百度OCR文字识别API实现图片文字识别软件
Java调用百度OCR文字识别API实现图片文字识别软件 原创isinple 发布于2019-01-06 13:35:59 阅读数 1296 收藏 展开 java_baidu_ocr Java调用百度 ...
- pytorch深度学习框架——实现病虫害图像分类
一.pytorch框架 1.1.概念 PyTorch是一个开源的Python机器学习库,基于Torch,用于自然语言处理等应用程序. 2017年1月,由Facebook人工智能研究院(FAIR)基于T ...
- 【深度学习】基于PyTorch深度学习框架的序列图像数据装载器
作者 | Harsh Maheshwari 编译 | VK 来源 | Towards Data Science 如今,深度学习和机器学习算法正在统治世界.PyTorch是最常用的深度学习框架之一,用于 ...
最新文章
- exec不同文件l怎么汇总_ABAQUS常见问题汇总 - 2.0版.doc
- const的用法,特别是用在函数前面与后面的区别!
- IPFS中国社区丨最简单全面介绍IPFS
- linux定时任务实例,linux定时任务访问url实例
- SAP CRM呼叫中心polling and C4C notification polling
- redhat python3.4安装步骤
- python怎么矩阵的秩_python – 从numpy或matlab中的满秩非矩形矩阵中获取可逆方阵...
- OpenCV-字典法实现数字识别(尺寸归一化+图像差值)
- 阿里云云计算 42 CDN中的常用名词
- 联想y7000 Linux显卡驱动,联想Y7000安装ubuntu1804.6双系统和显卡驱动(一)
- 01. Couchbase简介-CouchBase从0到50
- 暗战 惠普再掀市场风云
- 我的世界血量显示的服务器,Minecraft|世纪之都|服务器mod:拔刀剑 工业2 高级太阳能 血量显示 Nei 聊天泡泡等...
- 计算机在档案管理出现的问题,浅议档案管理中存在的问题及解决措施_档案管理员资格证...
- android 设置路由器,192.168.1.1路由器设置手机登陆
- oracle dbms_utility.get_time,dbms_utility如何使用?
- Python识别二维码获取电子发票基本信息
- VLC对视频流做翻转、旋转
- yii2授权之ACF
- 论文多次查重会影响重复率吗?
热门文章
- 非上市公司股权激励方案(珍藏版)
- 三维vr全景摄影展示满足产品720立体浏览
- 全国大学生软件测试大赛Web应用测试(五)Jmeter性能测试环境配置
- 高定价预计将降低iPhone XS的需求
- IT桔子 - 千里马俱乐部
- 作为一名大数据工程师你需要掌握Spark深度学习
- 导出期刊对应格式的参考_中文参考文献怎么一键导出正确格式?写作必看!
- c语言编写数据存储的游戏,c语言经典小程序和c语言编写的小游戏带注释(自动保存的).doc...
- 如何提高内存卡的读写速度
- 幼儿园数学目标_幼儿园大班数学计划