ocr 文字识别 服务

发布时间 2023-08-24 18:57:38作者: __username

ocr 文字识别 服务

利用百度开源模型:地址:https://github.com/PaddlePaddle/PaddleHub/tree/develop/modules/image/text_recognition/chinese_ocr_db_crnn_mobile

一个开发web服务demo

from flask import Flask, render_template, request, jsonify
import requests, json

app = Flask(__name__)


@app.route('/', methods=['GET', 'POST'])
def index():
    processed_data = None

    if request.method == 'POST':
        try:
            json_data = request.get_json()  # 获取POST请求中的JSON数据

            if 'image_data' in json_data:
                # 假设用户发送的JSON中有一个名为 "image_data" 的字段
                image_data = json_data['image_data']  # 这是属于base64加密字段

                # 在这里可以对图像数据进行处理
                processed_data = process_image_data(image_data)

        except Exception as e:
            error_message = str(e)
            return jsonify({"error": error_message})

    if processed_data:
        return jsonify({"result": processed_data})
    else:
        return jsonify({"message": "No data processed."})


def process_image_data(image_data):
    # # 在这里进行图像数据处理的实际操作
    # # 这个函数是一个示例,根据实际需求来实现图像数据的处理逻
    data = {'images': [image_data]}
    headers = {"Content-type": "application/json"}
    url = "http://127.0.0.1:8866/predict/chinese_ocr_db_crnn_mobile"
    r = requests.post(url=url, headers=headers, json=data)

    # 打印预测结果
    print(r.json()["results"])
    r.close()

    return r.json()["results"]


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5522, debug=True)