server.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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,
  28. table=True,
  29. use_gpu=use_gpu,
  30. show_log=True,
  31. table_model_dir="./tabel_ocr_infer")
  32. class TableInfo(BaseModel):
  33. image: str
  34. @app.get("/ping")
  35. def ping():
  36. return 'pong!'
  37. @app.post("/ocr_system/table")
  38. @web_try()
  39. def table(image: TableInfo):
  40. img = base64_to_np(image.image)
  41. res = table_engine(img)
  42. return res[0]['res']
  43. if __name__ == '__main__':
  44. import uvicorn
  45. import argparse
  46. parser = argparse.ArgumentParser()
  47. parser.add_argument('--host', default='0.0.0.0')
  48. parser.add_argument('--port', default=8080)
  49. opt = parser.parse_args()
  50. app_str = 'server:app' # make the app string equal to whatever the name of this file is
  51. uvicorn.run(app_str, host=opt.host, port=int(opt.port), reload=True)