Files
FiDA_Python/src/routers/seg_furniture.py

59 lines
2.0 KiB
Python
Raw Normal View History

2026-03-27 14:41:13 +08:00
import json
import logging
import requests
from fastapi import APIRouter
from src.core.config import settings
from src.schemas.response_template import ResponseModel
from src.schemas.san_furniture import SAMRequestModel
router = APIRouter(prefix="/canvas", tags=["Furniture Canvas"])
logger = logging.getLogger(__name__)
@router.post("/seg_anything")
async def seg_anything(request_data: SAMRequestModel):
"""
**Segment Anything 交互式分割接口**
通过传入图片路径和点击的点坐标返回分割后的掩码数据
### 参数说明:
- **user_id**:用户id 用于存储分割图
- **image_path**: 图片在服务器或云端的相对路径
- **type**: 推理类型
- **box**: 框选矩形点位信息
- **points**: 交互点的坐标列表每个点为 [x, y] 像素格式
- **labels**: 坐标点的属性标签必须与 points 长度一致
- 1: **前景点** (代表想要分割出的区域)
- 0: **背景点** (代表想要排除的区域)
### 请求体示例:
```json
point
{
"user_id": 1,
"image_path": "aida-users/89/sketch/4e8fe37d-7068-400a-ac94-c01647fa5f6f.png",
"type":"point",
"points": [[310, 403], [493, 375], [261, 266], [404, 484]],
"labels": [1, 1, 0, 1]
}
box
{
"user_id": 1,
"image_path": "aida-users/89/sketch/4e8fe37d-7068-400a-ac94-c01647fa5f6f.png",
"type":"box",
"box": [350, 286, 544, 520]
}
```
"""
try:
logger.info(f"seg_anything request item is : @@@@@@:{json.dumps(request_data.dict(), indent=4)}")
data = requests.post(f"http://{settings.SEG_ANYTHING}/predict", json=request_data.dict())
logger.info(f"seg_anything response @@@@@@:{json.dumps(json.loads(data.content), indent=4)}")
return ResponseModel(data=json.loads(data.content))
except Exception as e:
logger.warning(f"seg_anything Run Exception @@@@@@:{e}")