当前个人部署服务器,输入图片识别
http://39.107.76.144/即可访问,注意:图片太大ocr不支持会报错
环境以及代码如下:
1.安装EasyOCR,pip install easyocr
import easyocr
#设置识别中英文两种语言
reader = easyocr.Reader(['ch_sim','en'], gpu = False) # need to run only once to load model into memory
result = reader.readtext(r"d:\Desktop\4A34A16F-6B12-4ffc-88C6-FC86E4DF6912.png", detail = 0)
print(result)
初次运行需要在线下载检测模型和识别模型,建议在网速好点的环境运行
2.安装flask,pip install flask
3.前端代码
index.html
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>图片识别</title>
</head>
<body>
<h3>身份证识别</h3>
选择一个文件上传: <br />
<form action="/Id_card/" method="post" enctype="multipart/form-data">
<label for="">请上传图片:</label>
<input type="file" name="image"><br>
<input type="submit" value="提交">
</form>
<h3>常用文字识别</h3>
选择一个文件上传: <br />
<form action="/getImg/" method="post" enctype="multipart/form-data">
<label for="">请上传图片:</label>
<input type="file" name="image"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
content.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
身份证识别结果
<br>姓 名:{{ my_int.姓名 }}
<br>性 别:{{ my_int.性别 }}
<br>民 族:{{ my_int.民族 }}
<br>出 生:{{ my_int.出生日期 }}
<br>住 址:{{ my_int.住址 }}
<br>公民身份号码:{{ my_int.身份号码 }}
</body>
</html>
4.后端代码
newflask.py
# 导入flask以及相关子模块(安装方式:pip install flask)
from flask import Flask, render_template, request
import os
import easyocr
import re
# 获取项目当前绝对路径
# 比如我的项目在桌面上存放则得到——"C:\Users\asus\Desktop\shou"
basedir = os.path.abspath(os.path.dirname(__file__))
print(basedir)
# 实例
app = Flask(__name__)
r = easyocr.Reader(['ch_sim', "en"], gpu=True)
# 在根路由下返回上面的表单页面
@app.route('/', methods=['GET'])
def index():
return render_template("indexx.html")
# 身份证识别
@app.route('/Id_card/', methods=['GET', 'POST'])
def id_card():
# 通过表单中name值获取图片
#imgData = request.files["image"]
imgData = request.files.get("image")
# 设置图片要保存到的路径
path = basedir + "/static/"
# 获取图片名称及后缀名
imgName = imgData.filename
# 图片path和名称组成图片的保存路径
file_path = path + imgName
# 保存图片
imgData.save(file_path)
# url是图片的路径
# url = '/static/upload/img/' + imgName
res = r.readtext(file_path, detail=0)
print(res)
preprocess = {
"姓名Info": ".*?姓名.*",
"性别Info": ".*?性别.*",
"民族Info": ".*?(?:民族|民蔟).*",
"出生日期Info": ".*?出生.*",
"住址Info": ".*?(?:住址|任址|址).*",
"身份号码Info": ".*?(?:公民身份).*",
}
predict = {
"姓名": ".*?(?:姓名)(.*)",
"性别": ".*?(?:性别)(.*?)(?:民族|.族|民.).*",
"民族": ".*?(?:民族|民蔟)(.*)",
"出生日期": ".*?(?:出生)(.*)",
"住址": ".*?(?:住址|任址|址)(.*)",
"身份号码": ".*?(?:公民身份.{2})(.*)",
}
txt_list = []
for i in res:
txt_list.append(i.replace(" ",""))
pre_index = []
for i in range(len(txt_list)):
value = txt_list[i]
for k, v in preprocess.items():
try:
c = re.search(v, value).group(0)
if value == c:
print("打上预标签", k, c)
# 记录打上标签的index
pre_index.append((k, i))
# pre_index[k]=i
# pre_index[k]=i
except Exception as e:
pass
print(pre_index)
n = len(txt_list)
result = {}
for i in range(len(pre_index)):
k = pre_index[i][0]
if i == len(pre_index) - 1:
new_combine = "".join(txt_list[pre_index[i][1]:])
print(new_combine)
try:
key = k.replace("Info", "")
pattern = predict[key]
d, f = re.search(pattern, new_combine).group(0, 1)
print("核心抽取", key, f)
result[key] = f
except Exception as e:
pass
else:
if pre_index[i][1]==pre_index[i+1][1]:
new_combine = "".join(txt_list[pre_index[i][1]])
else:
new_combine = "".join(txt_list[pre_index[i][1]:pre_index[i + 1][1]])
# new_combine = "".join(txt_list[pre_index[i][1]:pre_index[i][1]])
pattern = "\^|\『|\』|,|,|。|\.$|#|\&|“|"
# print(value)
new_combine = re.sub(pattern, pattern.split("|")[-1], new_combine)
print(new_combine)
try:
key = k.replace("Info", "")
pattern = predict[key]
if key == "性别":
try:
d, f = re.search(pattern, new_combine).group(0, 1)
print("核心抽取", key, f)
result[key] = f
except Exception as e:
pattern = ".*?(?:性别)(.*)"
d, f = re.search(pattern, new_combine).group(0, 1)
print("核心抽取", key, f)
result[key] = f
elif key == "出生日期":
d, f = re.search(pattern, new_combine).group(0, 1)
pattern = ".$"
# print(value)
f = re.sub(pattern, "日", f)
print("核心抽取", key, f)
result[key] = f
else:
d, f = re.search(pattern, new_combine).group(0, 1)
print("核心抽取", key, f)
result[key] = f
except Exception as e:
pass
# try:
# key = k.replace("Info", "")
# pattern = predict[key]
# d, f = re.search(pattern, new_combine).group(0, 1)
# print("核心抽取", key, f)
# result[key] = f
# except Exception as e:
# pass
print(result)
# return '{}'.format(res)
# return render_template("content.html", my_str=res, my_int=result)
return render_template("content.html", my_int=result)
# return '{}-{}'.format(res,result)
# return result
# 常用文字识别
@app.route('/getImg/', methods=['GET', 'POST'])
def getImg():
# 通过表单中name值获取图片
imgData = request.files["image"]
# 设置图片要保存到的路径
path = basedir + "/static/"
# 获取图片名称及后缀名
imgName = imgData.filename
# 图片path和名称组成图片的保存路径
file_path = path + imgName
# 保存图片
imgData.save(file_path)
# url是图片的路径
# url = '/static/upload/img/' + imgName
res = r.readtext(file_path, detail=0)
return '{}'.format(res)
# return result
if __name__ == '__main__':
# app.run(host='127.0.0.1', port=8181)
app.run(host='0.0.0.0', port=80)
5.运行newflask.py启动服务