文档
模特图智能抠图请求接口

提交模特图片分割任务

1. 接口地址

https://e.hidreamai.com/api-pub/ec/v3/segment/model

2. 请求方式

POST

3. 请求参数

Header

参数类型是否必填说明
Authorizationstring客户授权Token
Content-Typestring使用json传参
X-accept-languagestring语言偏好,支持['en', 'zh'],en:英文,zh:中文,默认为"zh"

Body

参数类型是否必填说明
imagestring待分割的图片,支持传入图片的base64值/URL地址

说明:

curl示例

curl -H 'Authorization: Bearer {USER_Authorization}' \
     -H 'Content-Type: application/json' \
     -X POST \
     -d '{
        "image": "https://img0.baidu.com/it/u=3162963441,624224718&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=800",
     }' \
     -v 'https://e.hidreamai.com/api-pub/ec/v3/segment/model'
 

Python示例

import requests
import json
import uuid
 
USER_Authorization = <token>  # 获取方式详见接口文档https://www.hidreamai.com/docs/pixeling/user/token.html
 
 
# 请求头
headers = {
    'Authorization': f'Bearer {USER_Authorization}',
    'Content-Type': 'application/json',
}
 
# 请求体参数
data = {
    "image": "https://img0.baidu.com/it/u=3162963441,624224718&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=800"
}
 
# 发送POST请求
url = 'https://e.hidreamai.com/api-pub/ec/v3/segment/model'
response = requests.post(url, headers=headers, json=data)
 
# 处理响应
if response.status_code == 200:
    if response.json()['code'] == 0:
        task_id = response.json()['result']['task_id']
        print(task_id)
    else:
        print(response.json()['message'])
else:
    print(response.status_code)

4. 返回参数

参数类型说明
codeint返回状态码
messagestring返回状态信息
request_idstring本次请求的ID
resultdict返回结果

其中返回结果 result字段具体为:

参数类型说明
idx_matstring分割的结果图片
maskslist模特身体各个模块的图片
show_indiceslist模特图模特衣服的图片索引,根据索引在masks中查找图片
special_indicesdict模特图特殊区域的图片索引,根据索引在masks中查找图片

示例:

{
    "code": 0,
    "message": "Success",
    "request_id": "3ef8e6d5-9723-43e2-872d-58b7542ac034",
    "result": {
        "idx_mat": "p_72d43c72-ad4d-11ef-8658-d62439723af9",
        "masks": [
            "p_72d8f5d2-ad4d-11ef-8658-d62439723af9",
            "p_72e07f64-ad4d-11ef-8658-d62439723af9",
            "p_72e6538a-ad4d-11ef-8658-d62439723af9",
            "p_72eab556-ad4d-11ef-8658-d62439723af9",
            "p_72edf89c-ad4d-11ef-8658-d62439723af9",
            "p_72f2da1a-ad4d-11ef-8658-d62439723af9",
            "p_72f8614c-ad4d-11ef-8658-d62439723af9",
            "p_72fc8f92-ad4d-11ef-8658-d62439723af9"
        ],
        "show_indices": [2, 3, 4, 7],
        "special_indices": {
            "background": 0,
            "face": 6,
            "hair": 1,
            "skin": 5
        },
    }
}

附:如何处理分割结果为生图的输入参数mask

python示例:

import base64
import time
from io import BytesIO
 
import requests
from PIL import Image
 
 
def get_img_bytes_from_server(image_url):
    response = requests.get(image_url)
    image_base64 = base64.b64encode(response.content).decode("utf-8")
    img_bytes = base64.b64decode(image_base64)
    return img_bytes
 
 
def process_masks(masks: list, idx_mat: str):
    # 使用idx_mat作为结果输出图
    with Image.open(BytesIO(get_img_bytes_from_server(idx_mat))) as img:
        width, height = img.size
    result_image = Image.new("RGBA", (width, height), (255, 255, 255, 255))
    result_pixels = result_image.load()
 
    for image_path in masks:
        with Image.open(BytesIO(get_img_bytes_from_server(image_path))) as img:
            img = img.convert("RGBA")
            pixels = img.load()
 
            special_point = (1, 1, 1, 255)
            for x in range(width):
                for y in range(height):
                    if pixels[x, y] == special_point:
                        # 背景图片上设置黑色像素点
                        result_pixels[x, y] = special_point
 
    # 保存整合后的图片
    result_image.save("./mask_result.png")
    result_image.close()
    print(f"整合后的图片已保存")
 
 
segment_result = {
    "idx_mat": "https://storage.hidreamai.com/image/p_72d43c72-ad4d-11ef-8658-d62439723af9.png",
    "masks": [
        "https://storage.hidreamai.com/image/p_72d8f5d2-ad4d-11ef-8658-d62439723af9.png",
        "https://storage.hidreamai.com/image/p_72e07f64-ad4d-11ef-8658-d62439723af9.png",
        "https://storage.hidreamai.com/image/p_72e6538a-ad4d-11ef-8658-d62439723af9.png",
        "https://storage.hidreamai.com/image/p_72eab556-ad4d-11ef-8658-d62439723af9.png",
        "https://storage.hidreamai.com/image/p_72edf89c-ad4d-11ef-8658-d62439723af9.png",
        "https://storage.hidreamai.com/image/p_72f2da1a-ad4d-11ef-8658-d62439723af9.png",
        "https://storage.hidreamai.com/image/p_72f8614c-ad4d-11ef-8658-d62439723af9.png",
        "https://storage.hidreamai.com/image/p_72fc8f92-ad4d-11ef-8658-d62439723af9.png",
    ],
    "show_indices": [2, 3, 4, 7],
    "special_indices": {"background": 0, "face": 6, "hair": 1, "skin": 5},
}
 
if __name__ == '__main__':
 
    masks = [segment_result["masks"][idx] for idx in segment_result["show_indices"]]
    idx_mat = segment_result["idx_mat"]
    process_masks(masks, idx_mat)