server.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 = os.getenv('USE_CUDA') == 'gpu'
  23. print(f'use gpu: {use_gpu}')
  24. # 初始化模型
  25. table_engine = PPStructure(layout=False,
  26. table=True,
  27. use_gpu=use_gpu,
  28. show_log=True,
  29. det_model_dir="./det_table_1014",
  30. table_model_dir="./SLAnet_1004")
  31. table_engine1 = PPStructure(layout=False,
  32. table=True,
  33. use_gpu=use_gpu,
  34. show_log=True,
  35. det_model_dir="./det_table_1012",
  36. able_model_dir="./SLAnet_1004")
  37. class TableInfo(BaseModel):
  38. image: str
  39. det: str
  40. @app.get("/ping")
  41. def ping():
  42. return 'pong!'
  43. @app.post("/ocr_system/table")
  44. @web_try()
  45. def table(image: TableInfo):
  46. img = base64_to_np(image.image)
  47. if image.det == 'no':
  48. res = table_engine(img)
  49. elif image.det == 'yes':
  50. res = table_engine1(img)
  51. return res[0]['res']
  52. if __name__ == '__main__':
  53. import uvicorn
  54. import argparse
  55. parser = argparse.ArgumentParser()
  56. parser.add_argument('--host', default='0.0.0.0')
  57. parser.add_argument('--port', default=8080)
  58. opt = parser.parse_args()
  59. app_str = 'server:app' # make the app string equal to whatever the name of this file is
  60. uvicorn.run(app_str, host=opt.host, port=int(opt.port), reload=True)