feat(image): 抠图增加人物白色描边(need_contour + contour_color + contour_size + need_crop_background)
- provider: 增加 need_contour/contour_color/contour_size/need_crop_background 参数 - service: 默认 scene=human,human/product 场景自动启用白色描边 + 裁剪背景 - adapter: 透传新参数到 provider - API: scene 默认值改为 human - 前端: removeBackground 默认 scene 改为 human
This commit is contained in:
@@ -66,6 +66,10 @@ class VolcengineMediakitAdapter(PlatformAdapter, SyncCapable):
|
||||
result = await self.provider.remove_background(
|
||||
image_url=payload["image_url"],
|
||||
scene=payload.get("scene", "general"),
|
||||
need_contour=payload.get("need_contour", False),
|
||||
contour_color=payload.get("contour_color", "#FFFFFF"),
|
||||
contour_size=payload.get("contour_size", 10),
|
||||
need_crop_background=payload.get("need_crop_background", False),
|
||||
)
|
||||
data = result.get("data", {})
|
||||
return AdapterResponse(
|
||||
|
||||
@@ -88,13 +88,31 @@ class VolcengineMediakitProvider:
|
||||
self,
|
||||
image_url: str,
|
||||
scene: str = "general",
|
||||
need_contour: bool = False,
|
||||
contour_color: str = "#FFFFFF",
|
||||
contour_size: int = 10,
|
||||
need_crop_background: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
"""同步抠图,返回原始 JSON
|
||||
|
||||
Args:
|
||||
image_url: 原始图片 URL
|
||||
scene: 场景类型
|
||||
need_contour: 是否为主体生成描边(仅 human/product 场景生效)
|
||||
contour_color: 描边颜色,十六进制 RGB 格式
|
||||
contour_size: 描边宽度(px),范围 [1, 100]
|
||||
need_crop_background: 是否裁剪透明背景到刚好包裹主体
|
||||
|
||||
Returns:
|
||||
{"code": 0, "message": "Success", "data": {"image_url": "https://..."}}
|
||||
"""
|
||||
payload = {"image_url": image_url, "scene": scene}
|
||||
payload: dict[str, Any] = {"image_url": image_url, "scene": scene}
|
||||
if need_contour:
|
||||
payload["need_contour"] = True
|
||||
payload["contour_color"] = contour_color
|
||||
payload["contour_size"] = max(1, min(100, contour_size))
|
||||
if need_crop_background:
|
||||
payload["need_crop_background"] = True
|
||||
|
||||
try:
|
||||
response = await self.client.post(
|
||||
|
||||
@@ -61,7 +61,7 @@ class RemoveBackgroundRequest(BaseModel):
|
||||
"""抠图请求"""
|
||||
|
||||
image_url: str = Field(..., description="原始图片 URL")
|
||||
scene: str = Field(default="general", description="场景类型:general(通用)或 product(商品)")
|
||||
scene: str = Field(default="human", description="场景类型:general(通用)、human(人物,默认白色描边)或 product(商品)")
|
||||
|
||||
|
||||
# ── Endpoints ──
|
||||
|
||||
@@ -31,7 +31,7 @@ class VolcengineMediakitService:
|
||||
"""火山引擎 MediaKit 服务封装"""
|
||||
|
||||
# 支持的场景
|
||||
SUPPORTED_SCENES = {"general", "product"}
|
||||
SUPPORTED_SCENES = {"general", "human", "product"}
|
||||
|
||||
def __init__(self, gateway: PlatformGateway):
|
||||
self.gateway = gateway
|
||||
@@ -39,13 +39,13 @@ class VolcengineMediakitService:
|
||||
async def remove_background(
|
||||
self,
|
||||
image_url: str,
|
||||
scene: str = "general",
|
||||
scene: str = "human",
|
||||
) -> RemoveBackgroundResult:
|
||||
"""同步抠图
|
||||
|
||||
Args:
|
||||
image_url: 原始图片 URL
|
||||
scene: 场景类型,"general"(通用)或 "product"(商品)
|
||||
scene: 场景类型,"general"(通用)、"human"(人物)或 "product"(商品)
|
||||
|
||||
Returns:
|
||||
RemoveBackgroundResult: 包含抠图结果图片 URL
|
||||
@@ -60,9 +60,15 @@ class VolcengineMediakitService:
|
||||
if scene not in self.SUPPORTED_SCENES:
|
||||
raise ValueError(f"不支持的场景: {scene},可选: {self.SUPPORTED_SCENES}")
|
||||
|
||||
# 人物/商品场景默认启用白色描边 + 裁剪背景
|
||||
enable_contour = scene in ("human", "product")
|
||||
payload = {
|
||||
"image_url": image_url,
|
||||
"scene": scene,
|
||||
"need_contour": enable_contour,
|
||||
"contour_color": "#FFFFFF",
|
||||
"contour_size": 10,
|
||||
"need_crop_background": enable_contour,
|
||||
}
|
||||
|
||||
response = await self.gateway.call_sync(
|
||||
|
||||
@@ -33,7 +33,7 @@ export async function uploadImage(file: File): Promise<string> {
|
||||
/**
|
||||
* AI 抠图(火山引擎 MediaKit)
|
||||
*/
|
||||
export async function removeBackground(imageUrl: string, scene = 'general'): Promise<string> {
|
||||
export async function removeBackground(imageUrl: string, scene = 'human'): Promise<string> {
|
||||
const result = await client.post<{ url: string }>('/image/remove-background', {
|
||||
imageUrl,
|
||||
scene,
|
||||
|
||||
Reference in New Issue
Block a user