server.py 1.7 KB

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