You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
142 lines
3.3 KiB
Python
142 lines
3.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
请求数据模型
|
|
定义 API 请求的数据结构
|
|
"""
|
|
|
|
from typing import Optional
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
|
class ROIParams(BaseModel):
|
|
"""
|
|
ROI (感兴趣区域) 参数
|
|
使用归一化坐标 (0.0 ~ 1.0)
|
|
"""
|
|
|
|
x: float = Field(
|
|
default=0.0,
|
|
ge=0.0,
|
|
le=1.0,
|
|
description="ROI 左上角 X 坐标 (归一化)",
|
|
)
|
|
y: float = Field(
|
|
default=0.0,
|
|
ge=0.0,
|
|
le=1.0,
|
|
description="ROI 左上角 Y 坐标 (归一化)",
|
|
)
|
|
width: float = Field(
|
|
default=1.0,
|
|
gt=0.0,
|
|
le=1.0,
|
|
description="ROI 宽度 (归一化)",
|
|
)
|
|
height: float = Field(
|
|
default=1.0,
|
|
gt=0.0,
|
|
le=1.0,
|
|
description="ROI 高度 (归一化)",
|
|
)
|
|
|
|
|
|
class OCRRequestParams(BaseModel):
|
|
"""
|
|
OCR 请求参数 (用于 multipart/form-data)
|
|
"""
|
|
|
|
lang: str = Field(
|
|
default="ch",
|
|
description="识别语言,支持 'ch'(中文), 'en'(英文) 等",
|
|
)
|
|
use_gpu: bool = Field(
|
|
default=False,
|
|
description="是否使用 GPU 加速",
|
|
)
|
|
drop_score: float = Field(
|
|
default=0.5,
|
|
ge=0.0,
|
|
le=1.0,
|
|
description="置信度阈值,低于此值的结果将被过滤",
|
|
)
|
|
roi_x: Optional[float] = Field(
|
|
default=None,
|
|
ge=0.0,
|
|
le=1.0,
|
|
description="ROI 左上角 X 坐标 (归一化)",
|
|
)
|
|
roi_y: Optional[float] = Field(
|
|
default=None,
|
|
ge=0.0,
|
|
le=1.0,
|
|
description="ROI 左上角 Y 坐标 (归一化)",
|
|
)
|
|
roi_width: Optional[float] = Field(
|
|
default=None,
|
|
gt=0.0,
|
|
le=1.0,
|
|
description="ROI 宽度 (归一化)",
|
|
)
|
|
roi_height: Optional[float] = Field(
|
|
default=None,
|
|
gt=0.0,
|
|
le=1.0,
|
|
description="ROI 高度 (归一化)",
|
|
)
|
|
return_annotated_image: bool = Field(
|
|
default=False,
|
|
description="是否返回标注后的图片 (Base64)",
|
|
)
|
|
|
|
def has_roi(self) -> bool:
|
|
"""检查是否设置了 ROI 参数"""
|
|
return all(
|
|
v is not None
|
|
for v in [self.roi_x, self.roi_y, self.roi_width, self.roi_height]
|
|
)
|
|
|
|
def get_roi(self) -> Optional[ROIParams]:
|
|
"""获取 ROI 参数对象"""
|
|
if not self.has_roi():
|
|
return None
|
|
return ROIParams(
|
|
x=self.roi_x,
|
|
y=self.roi_y,
|
|
width=self.roi_width,
|
|
height=self.roi_height,
|
|
)
|
|
|
|
|
|
class OCRRequestBase64(BaseModel):
|
|
"""
|
|
OCR 请求 (Base64 JSON 格式)
|
|
"""
|
|
|
|
image_base64: str = Field(
|
|
...,
|
|
description="Base64 编码的图片数据,支持 Data URL 格式",
|
|
)
|
|
lang: str = Field(
|
|
default="ch",
|
|
description="识别语言,支持 'ch'(中文), 'en'(英文) 等",
|
|
)
|
|
use_gpu: bool = Field(
|
|
default=False,
|
|
description="是否使用 GPU 加速",
|
|
)
|
|
drop_score: float = Field(
|
|
default=0.5,
|
|
ge=0.0,
|
|
le=1.0,
|
|
description="置信度阈值,低于此值的结果将被过滤",
|
|
)
|
|
roi: Optional[ROIParams] = Field(
|
|
default=None,
|
|
description="ROI 区域参数",
|
|
)
|
|
return_annotated_image: bool = Field(
|
|
default=False,
|
|
description="是否返回标注后的图片 (Base64)",
|
|
)
|