paddle v4 hubserving 部署

HubServing则提供了模型的部署和在线预测服务。

环境准备:https://github.com/PaddlePaddle/PaddleOCR/tree/release/2.7/deploy/hubserving#24-启动服务

服务器启动命令

复制代码
hub serving start -c deploy/hubserving/ocr_system/config.json

客户端请求

python tools/test_hubserving.py --server_url=server_url --image_dir=image_path

复制代码
import base64
import sys
import time
import requests
import json
import asyncio
import aiohttp

import pandas as pd
from sqlalchemy import create_engine, text
import time


from PIL import Image
from io import BytesIO
import cv2
import numpy as np
import io

time1 = time.time()



def read_data():
    conn = create_engine('mysql+pymysql://xxx:xxx@xxx.xxx.xx.xx:3306/x?charset=uxxxtf8')
    connection = conn.connect()
    page = 0
    sql_select = f"SELECT * from xj_zsjh_png LIMIT {page * 10}, 100"  # 1000条100s  
    results = pd.read_sql(sql=text(sql_select), con=connection)
    out = json.loads(results.to_json(orient='records'))
    img_strs_list = [x['bas64_str'] for x in out]
    return img_strs_list


def cv2_to_base64(image):
    return base64.b64encode(image).decode('utf8')

def save_data(results, file_name):
    df = pd.DataFrame([[i['text'] for i in x] for x in results])

    df.to_csv(file_name, index=False)

def process_image_(img_str):
    binary = base64.b64decode(img_str)
    image = Image.open(BytesIO(binary))

    # 创建一个新的RGB图像,将Alpha通道设置为0
    rgb_image = Image.new('RGB', image.size, (255, 255, 255))
    # 将RGBA图像的颜色信息复制到RGB图像
    rgb_image.paste(image, (0, 0), mask=image)
    # 裁剪图片
    left = 535
    top = 0
    right = left + 240
    bottom = image.size[1]
    cropped_image = rgb_image.crop((left, top, right, bottom))

    # 创建一个BytesIO对象
    image_bytes = io.BytesIO()
    # 将图像保存到BytesIO对象中
    cropped_image.save(image_bytes, format='JPEG')
    return image_bytes






def main2():
    results = []
    img_strs_list = read_data()
    for i, img_str in enumerate(img_strs_list):
        image_bytes = process_image_(img_str)
        data = {'images': [cv2_to_base64(image_bytes.getvalue())]}
        headers = {
            'Content-Type': 'application/json'
        }
        response = requests.post("http://192.168.0.189:8868/predict/ocr_system", data=json.dumps(data), headers=headers)


        if response.status_code == 200:
            res = response.json()["results"][0]
            results.append(res)
        else:
            print('Error:', response.status_code)
    save_data(results, 'normal.csv')

async def process_image(img_str):
    image_bytes = process_image_(img_str)
    headers = {
        'Content-Type': 'application/json'
    }
    # 发送 OCR 请求
    data = {'images': [cv2_to_base64(image_bytes.getvalue())]}
    async with aiohttp.ClientSession() as session:
        async with session.post("http://192.168.0.189:8868/predict/ocr_system", data=json.dumps(data),
                                headers=headers) as response:
            if response.status == 200:
                res = (await response.json())["results"][0]
                return res
            else:
                print(f'Error: {response.status}')
                return None


async def process_images(img_strs_list):
    tasks = []
    sem = asyncio.Semaphore(1)  # 限制并发数为5 当创建过多session时就会报错
    async with sem:
        for img_str in img_strs_list:
            task = asyncio.create_task(process_image(img_str))
            tasks.append(task)
        results = await asyncio.gather(*tasks)

    return results


# 在主函数中调用异步任务
async def main():
    img_strs_list = read_data()
    results = await process_images(img_strs_list)
    save_data(results, 'async.csv')


# asyncio.run(main()) #100条 8.666
main2() #100条 9.667 # 96.832
print(f'当前页 共花费--> ', round(time.time() - time1, 3), '\n') # 1.813

(异步与非异步结果差不多)

结果

相关推荐
Yao.Li3 天前
踩坑 5090 编译构建 Paddle 源码
人工智能·深度学习·飞桨·paddle
满怀冰雪5 天前
19-图像数据处理:PaddleVision Transform 实战
人工智能·深度学习·计算机视觉·paddle
满怀冰雪6 天前
20-卷积神经网络基础:用 Paddle 构建 CNN
人工智能·深度学习·cnn·paddle
满怀冰雪12 天前
15-Paddle 高层 API 入门:paddle.Model 的训练与评估流程
人工智能·python·深度学习·机器学习·paddle
molihuan17 天前
最新 Paddle-Lite Android平台编译
android·ocr·paddle·推理·端侧·paddle lite
满怀冰雪19 天前
08-Paddle 神经网络层入门:Linear、激活函数与 Sequential
神经网络·机器学习·paddle
蓝创工坊Blue Foundry20 天前
PaddleOCR 本地部署教程:小模型 OCR 如何完成字段提取到 Excel
pdf·自动化·ocr·excel·paddlepaddle·paddle
满怀冰雪20 天前
09-使用 paddle.nn 构建第一个多层感知机
python·深度学习·神经网络·paddle
蓝创工坊Blue Foundry20 天前
个人藏书太多怎么整理?用 OCR 字段提取汇总成电子书目
pdf·ocr·excel·文心一言·paddlepaddle·paddle
山石满棠23 天前
基于python313环境构建paddle-ocr镜像
ocr·paddle