图片切割
功能描述
图片切割是一项将图片中的不同物体或区域精确分离出来的技术。与传统图片处理不同,图片切割能识别图片中每个像素属于哪个物体,从而实现精细的对象分离,返回每个物体的轮廓或掩码信息。
支持模型
如何调用接口
前提准备
- 获取 API Token(访问模力方舟控制台创建)
- 一张需要处理的图片(本地文件或网络图片URL)
python调用(示例)
import requests
API_URL = "https://api.moark.com/v1/images/segmentation"
API_TOKEN = "<your_api_token>" # 替换为您的真实Token
headers = {
"Authorization": f"Bearer {API_TOKEN}"
}
def cut_image(image_path, prompt=""):
data = {
"model": "sam3",
"prompt": prompt
}
if image_path.startswith(("http://", "https://")):
# 网络图片:直接传递URL
data["image"] = image_path
response = requests.post(API_URL, headers=headers, data=data)
else:
# 本地图片:通过文件上传
with open(image_path, "rb") as image_file:
files = {
"image": (image_path, image_file, "image/jpeg")
}
response = requests.post(API_URL, headers=headers, files=files, data=data)
return response.json()
# 示例1:切割本地图片中的人物
result = cut_image("path/to/your/photo.jpg", prompt="person")
print(f"找到 {result['num_segments']} 个人物")
# 示例2:切割网络图片中的所有物体(不使用prompt)
result2 = cut_image("https://example.com/image.jpg")
print(f"识别到 {result2['num_segments']} 个物体")