12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- from fastapi import FastAPI, Request
- from fastapi.middleware.cors import CORSMiddleware
- from pydantic import BaseModel
- from sx_utils.sximage import *
- from sx_utils.sxtime import sxtimeit
- from sx_utils.sxweb import web_try
- from stamp.stamp_parser import d_stamp
- app = FastAPI()
- origins = ["*"]
- # CORS 跨源资源共享
- app.add_middleware(
- CORSMiddleware,
- allow_origins=origins,
- allow_credentials=True,
- allow_methods=["*"],
- allow_headers=["*"],
- )
- # Get 健康检查
- @app.get("/ping")
- def ping():
- return "pong!"
- # 解析传入的 json对象
- class Stamp(BaseModel):
- image: str
- # Post 接口
- # 计算耗时
- # 异常处理
- @app.post("/stamp")
- @sxtimeit
- @web_try()
- # 传入=> base64码 -> np
- # 返回=> 检测到到结果 -> (conf, angle, parser, image_type)
- def blfe(request: Request, stamp: Stamp):
- image = base64_to_np(stamp.image)
- return d_stamp(image)
- if __name__ == '__main__':
- import uvicorn
- import argparse
- parser = argparse.ArgumentParser()
- parser.add_argument('--host', default='0.0.0.0')
- parser.add_argument('--port', default=8081)
- opt = parser.parse_args()
- app_str = 'stamp_server:app' # make the app string equal to whatever the name of this file is
- uvicorn.run(app_str, host=opt.host, port=int(opt.port), reload=True)
|