python简单搭建EASYOCR识别系统

当前个人部署服务器,输入图片识别http://43.139.194.12/

即可访问,注意:图片太大ocr不支持会报错

环境以及代码如下:

1.安装EasyOCR,pip install easyocr

可参考:适合小白的几个入门级Python ocr识别库_起不好名字就不起了的博客-CSDN博客_ddddocr库textact,muggleocr,paddle,ddddocrhttps://blog.csdn.net/qq_38017966/article/details/118724459

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



    
    图片识别


身份证识别

选择一个文件上传:

常用文字识别

选择一个文件上传:

content.html




    
    Title


身份证识别结果


姓 名:{{ my_int.姓名 }}
性 别:{{ my_int.性别 }}
民 族:{{ my_int.民族 }}
出 生:{{ my_int.出生日期 }}
住 址:{{ my_int.住址 }}
公民身份号码:{{ my_int.身份号码 }}

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启动服务

python简单搭建EASYOCR识别系统_第1张图片

你可能感兴趣的:(easyocr+opencv,python)