server.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import json
  2. from base64 import b64decode
  3. import cv2
  4. import numpy as np
  5. from fastapi import FastAPI, Request
  6. from fastapi.middleware.cors import CORSMiddleware
  7. from pydantic import BaseModel
  8. from paddleocr import PaddleOCR, PPStructure
  9. from sx_utils.sxweb import *
  10. from sx_utils.sximage import *
  11. import os
  12. # 初始化app
  13. app = FastAPI()
  14. origins = ["*"]
  15. app.add_middleware(
  16. CORSMiddleware,
  17. allow_origins=origins,
  18. allow_credentials=True,
  19. allow_methods=["*"],
  20. allow_headers=["*"],
  21. )
  22. use_gpu = False
  23. if os.getenv('USE_CUDA') == 'gpu':
  24. use_gpu = True
  25. print(f'use gpu: {use_gpu}')
  26. # 初始化模型
  27. table_engine = PPStructure(layout=False, table=True, show_log=True, table_model_dir="./tabel_ocr_infer")
  28. class TableInfo(BaseModel):
  29. image: str
  30. @app.get("/ping")
  31. def ping():
  32. return 'pong!'
  33. @app.post("/ocr_system/table")
  34. @web_try()
  35. def table(image: TableInfo):
  36. img = base64_to_np(image.image)
  37. res = table_engine(img)
  38. return res[0]['res']
  39. if __name__ == '__main__':
  40. import uvicorn
  41. import argparse
  42. parser = argparse.ArgumentParser()
  43. parser.add_argument('--host', default='0.0.0.0')
  44. parser.add_argument('--port', default=8080)
  45. opt = parser.parse_args()
  46. app_str = 'server:app' # make the app string equal to whatever the name of this file is
  47. uvicorn.run(app_str, host=opt.host, port=int(opt.port), reload=True)