PaddleOCR Docker CPU版 一键部署运行

文章描述了如何通过构建镜像部署Paddle_OCR模型,提供测试代码示例,包括初始化API、发送包含Base64编码图像的POST请求,以及解析OCR结果。
摘要由CSDN通过智能技术生成

构建了一个镜像,直接部署运行即可使用

docker run -dp 8866:8866  --name paddle_ocr nowindandmoon/paddle_ocr:latest

测试代码

import requests
import base64
import json


class InvoiceOcr:
    def __init__(self, url):
        self.api_url = url if url else "http://xxxxxx:8866/predict/ocr_system"

    def post(self, parameters):
        headers = {
            'Content-type': 'application/json',
            'Accept': 'application/json'
        }

        try:
            response = requests.post(self.api_url, data=json.dumps(parameters), headers=headers)
            if response.status_code == 200:
                return response.text
            else:
                print(f"Error: {response.status_code}")
                return None
        except requests.exceptions.RequestException as e:
            print(f"Request exception: {e}")
            return None


def image_to_base64(path):
    with open(path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode("utf-8")


if __name__ == "__main__":
    api_url = "http://xxxxxx:8866/predict/ocr_system"
    ocr = InvoiceOcr(api_url)

    # 替换为你的图片路径
    base64_image = image_to_base64("./test.jpg")

    parameters = {
        "images": [base64_image]
    }

    result = ocr.post(parameters)

    print(result)
    result = json.loads(result)

    for item in result['results'][0]:
        print(item['text'])

效果

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值