stamp_server.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from fastapi import FastAPI, Request
  2. from fastapi.middleware.cors import CORSMiddleware
  3. from pydantic import BaseModel
  4. from sx_utils.sximage import *
  5. from sx_utils.sxtime import sxtimeit
  6. from sx_utils.sxweb import web_try
  7. from stamp.stamp_parser import d_stamp
  8. app = FastAPI()
  9. origins = ["*"]
  10. # CORS 跨源资源共享
  11. app.add_middleware(
  12. CORSMiddleware,
  13. allow_origins=origins,
  14. allow_credentials=True,
  15. allow_methods=["*"],
  16. allow_headers=["*"],
  17. )
  18. # Get 健康检查
  19. @app.get("/ping")
  20. def ping():
  21. return "pong!"
  22. # 解析传入的 json对象
  23. class Stamp(BaseModel):
  24. image: str
  25. # Post 接口
  26. # 计算耗时
  27. # 异常处理
  28. @app.post("/stamp")
  29. @sxtimeit
  30. @web_try()
  31. # 传入=> base64码 -> np
  32. # 返回=> 检测到到结果 -> (conf, angle, parser, image_type)
  33. def blfe(request: Request, stamp: Stamp):
  34. image = base64_to_np(stamp.image)
  35. return d_stamp(image)
  36. if __name__ == '__main__':
  37. import uvicorn
  38. import argparse
  39. parser = argparse.ArgumentParser()
  40. parser.add_argument('--host', default='0.0.0.0')
  41. parser.add_argument('--port', default=8081)
  42. opt = parser.parse_args()
  43. app_str = 'stamp_server:app' # make the app string equal to whatever the name of this file is
  44. uvicorn.run(app_str, host=opt.host, port=int(opt.port), reload=True)