1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- import json
- from base64 import b64decode
- import cv2
- import numpy as np
- from fastapi import FastAPI, Request
- from fastapi.middleware.cors import CORSMiddleware
- from pydantic import BaseModel
- from paddleocr import PaddleOCR, PPStructure
- from sx_utils.sxweb import *
- from sx_utils.sximage import *
- import os
- # 初始化app
- app = FastAPI()
- origins = ["*"]
- app.add_middleware(
- CORSMiddleware,
- allow_origins=origins,
- allow_credentials=True,
- allow_methods=["*"],
- allow_headers=["*"],
- )
- use_gpu = os.getenv('USE_CUDA') == 'gpu'
- print(f'use gpu: {use_gpu}')
- # 初始化模型
- table_engine = PPStructure(layout=False,
- table=True,
- use_gpu=use_gpu,
- show_log=True,
- det_model_dir="./det_table_1014",
- table_model_dir="./SLAnet_1004")
- table_engine1 = PPStructure(layout=False,
- table=True,
- use_gpu=use_gpu,
- show_log=True,
- det_model_dir="./det_table_1012",
- able_model_dir="./SLAnet_1004")
- class TableInfo(BaseModel):
- image: str
- det: str
- @app.get("/ping")
- def ping():
- return 'pong!'
- @app.post("/ocr_system/table")
- @web_try()
- def table(image: TableInfo):
- img = base64_to_np(image.image)
- if image.det == 'no':
- res = table_engine(img)
- elif image.det == 'yes':
- res = table_engine1(img)
- return res[0]['res']
- if __name__ == '__main__':
- import uvicorn
- import argparse
- parser = argparse.ArgumentParser()
- parser.add_argument('--host', default='0.0.0.0')
- parser.add_argument('--port', default=8080)
- opt = parser.parse_args()
- app_str = 'server:app' # make the app string equal to whatever the name of this file is
- uvicorn.run(app_str, host=opt.host, port=int(opt.port), reload=True)
|