fastapi
十分好用方便
- post json参数规定
## 规定scene取值
class SceneEnum(str, Enum):
quexian = "QueXian"
caizhi = "CaiZhi"
yunyanshou = "YunYanShou"
## 规定mode取值
class ModeEnum(str, Enum):
mean = "mean"
single_max = "single_max"
double_max = "double_max"
triple_max = "triple_max"
quadra_max = "quadra_max"
all_ = "all"
class DetectItem(BaseModel):
imageData: str
scene: List[SceneEnum]
# 设置非必需并提供默认值
mode: Optional[ModeEnum] = ModeEnum.single_max
# 设置取值范围
blurThresh: confloat(ge=0, le=1)
lightThresh: confloat(ge=0, le=1)
darkThresh: confloat(ge=0, le=1)
detectThresh: Optional[Dict[str, float]] = {}
- post函数
@app.post("/seg/detect")
@app.post("/seg/detect/")
async def detect(item: DetectItem):
response = {
"Result": "OK",
"ErrorMsg": "",
"RecInfo": []
}
image = model.base64str2mat(item.imageData)
if not image.size:
response["Result"] = "FAILED"
response["ErrorMsg"] = "Decode Image Error"
return response
result = model.process(
item.scene, image, item.detectThresh,
item.blurThresh, item.lightThresh,
item.darkThresh, item.mode)
for r in result:
response["RecInfo"].append({
"left" : r.left,
"top" : r.top,
"right" : r.right,
"bottom" : r.bottom,
"label" : r.class_label,
"code" : r.val_code,
"confidence" : r.confidence
})
return response
网友评论