文档简介
本文介绍图像局部重绘模型的输入输出参数。图像局部重绘是根据用户提供原始图片和局部涂抹图片,再加上文字描述,然后在涂抹区域生成文字描述的内容,涂抹区域外没有变化。
相关指南:图像局部重绘
模型概览
模型名 | 模型简介 | 免费额度 | 计费单价 | 限流(含主账号与RAM子账号) | |
任务下发接口QPS限制 | 同时处理中任务数量 | ||||
wanx-x-painting | 通义万相-图像局部重绘是基于自研的Composer组合生成框架的AI绘画创作大模型后置处理链路,能够根据用户输入的原始图片、随意涂抹图中局部区域和prompt提示词文字内容,生成符合语义描述的多样化风格的局部重绘图像。通过知识重组与可变维度扩散模型,加速收敛并提升最终生成图片的效果, 布局自然、细节丰富、画面细腻、结果逼真。 | 免费额度:500张 领取方式:开通阿里云百炼大模型服务后,自动发放 有效期:180天 | 限时免费 | 2 | 1 |
前提条件
您需要已获取API Key并配置API Key到环境变量。如果通过SDK调用,还需要安装DashScope SDK。
HTTP调用
为了减少等待时间并且避免请求超时,服务采用异步方式提供。您需要发起两个请求:
创建任务:首先发送一个请求创建文生图任务,该请求会返回任务ID。
根据任务ID查询结果:使用上一步获得的任务ID,查询模型生成的结果。
创建任务
POST https://dashscope.aliyuncs.com/api/v1/services/aigc/image2image/image-synthesis
请求头(Headers) | curl
|
Content-Type string 必选 请求内容类型。固定为 | |
Authorization string 必选 推荐您使用百炼API-Key,也可填DashScope API-Key。例如:Bearer d1xxx2a。 | |
X-DashScope-Async string 必选 是否开启异步处理。必须开启异步处理,设置为 | |
请求体(Request Body) | |
model string 必选 调用模型。 | |
input object 必选 输入图像的基本信息,比如图像URL地址。 | |
parameters object 可选 图像处理参数。 |
响应 | 成功响应
异常响应
|
output dict 任务输出信息。 | |
code string 请求失败,表示错误码,成功时返回参数中不会包含该参数。 | |
message string 请求失败,表示失败详细信息,成功时返回参数中不会包含该参数。 | |
request_id string 请求唯一标识。可用于请求明细溯源和问题排查。 |
根据任务ID查询结果
GET https://dashscope.aliyuncs.com/api/v1/tasks/{task_id}
请求头(Headers) | 获取任务结果
|
Authorization string 必选 推荐使用百炼API-Key,也可填DashScope API-Key。例如:Bearer d1xxx2a。 | |
URL路径参数(Path parameters) | |
task_id string 必选 任务id。 |
响应 | 任务执行成功对于本模型,任务状态和结果将保留24小时,生成图像的URL地址有效期也为24小时,需要您在有效期内及时转存生成图像,24小时之后,任务连同结果一起从系统中清除。
任务执行中
任务执行失败
任务部分失败
|
output object 调用结果信息,对于千问模型,包含输出text。 | |
request_id string 请求唯一标识。可用于请求明细溯源和问题排查。 | |
usage object 输出信息统计。 |
(可选)配置域名白名单,允许访问图片链接
根据上述查询接口的响应,可以看到返回了图片OSS链接,如https://dashscope-result-xx.oss-cn-xxxx.aliyuncs.com/xxx.png
。图片OSS链接允许公开访问,您可以使用此链接查看或者下载图片。
需要注意的是,如果您的业务对安全性要求较高,无法随意访问外网链接,您需要单独配置外网访问白名单。请将以下域名添加到您的白名单中,以便顺利访问图片链接。
dashscope-result-bj.oss-cn-beijing.aliyuncs.com
dashscope-result-hz.oss-cn-hangzhou.aliyuncs.com
dashscope-result-sh.oss-cn-shanghai.aliyuncs.com
dashscope-result-wlcb.oss-cn-wulanchabu.aliyuncs.com
dashscope-result-zjk.oss-cn-zhangjiakou.aliyuncs.com
dashscope-result-sz.oss-cn-shenzhen.aliyuncs.com
dashscope-result-hy.oss-cn-heyuan.aliyuncs.com
dashscope-result-cd.oss-cn-chengdu.aliyuncs.com
dashscope-result-gz.oss-cn-guangzhou.aliyuncs.com
SDK调用
请先确认已安装最新版SDK:安装SDK。
此API支持Python SDK和Java SDK。SDK接口的入参和出参可参考HTTP调用。
由于本模型请求处理时间较长,服务采用异步方式提供,SDK进行了封装。您既可以按异步方式调用,也可以按照同步方式调用。
Python
同步调用
from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
from dashscope import ImageSynthesis
prompt = "a dog wearing red glasses"
model = "wanx-x-painting"
task = "image2image"
extra_input = {
"base_image_url": "http://synthesis-source.oss-accelerate.aliyuncs.com/lingji/validation/mask2img/demo/source3.jpg",
"mask_image_url": "http://synthesis-source.oss-accelerate.aliyuncs.com/lingji/validation/mask2img/demo/glasses.png"
}
print('----sync call, please wait a moment----')
rsp = ImageSynthesis.call(model=model,
prompt=prompt,
n=1,
size='1024*1024',
task=task,
extra_input=extra_input)
if rsp.status_code == HTTPStatus.OK:
print(rsp.output)
# save file to current directory
for result in rsp.output.results:
file_name = PurePosixPath(unquote(urlparse(result.url).path)).parts[-1]
with open('./%s' % file_name, 'wb+') as f:
f.write(requests.get(result.url).content)
else:
print('sync_call Failed, status_code: %s, code: %s, message: %s' %
(rsp.status_code, rsp.code, rsp.message))
异步调用
from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
from dashscope import ImageSynthesis
prompt = "a dog wearing red glasses"
model = "wanx-x-painting"
task = "image2image"
extra_input = {
"base_image_url": "http://synthesis-source.oss-accelerate.aliyuncs.com/lingji/validation/mask2img/demo/source3.jpg",
"mask_image_url": "http://synthesis-source.oss-accelerate.aliyuncs.com/lingji/validation/mask2img/demo/glasses.png"
}
def async_call():
print('----create task----')
task_info = create_async_task()
print('----wait task done then save image----')
wait_async_task(task_info)
# 创建异步任务
def create_async_task():
rsp = ImageSynthesis.async_call(model=model,
prompt=prompt,
n=1,
size='1024*1024',
task=task,
extra_input=extra_input)
print(rsp)
if rsp.status_code == HTTPStatus.OK:
print(rsp.output)
else:
print('create_async_task Failed, status_code: %s, code: %s, message: %s' %
(rsp.status_code, rsp.code, rsp.message))
return rsp
# 等待异步任务结束
def wait_async_task(task):
rsp = ImageSynthesis.wait(task)
print(rsp)
if rsp.status_code == HTTPStatus.OK:
print(rsp.output.task_status)
# save file to current directory
for result in rsp.output.results:
file_name = PurePosixPath(unquote(urlparse(result.url).path)).parts[-1]
with open('./%s' % file_name, 'wb+') as f:
f.write(requests.get(result.url).content)
else:
print('Failed, status_code: %s, code: %s, message: %s' %
(rsp.status_code, rsp.code, rsp.message))
if __name__ == '__main__':
async_call()
Java
同步调用
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisParam;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import java.util.HashMap;
public class Main {
public void syncCall() {
String task = "image2image";
ImageSynthesis imageSynthesis = new ImageSynthesis(task);
ImageSynthesisParam param = genImageSynthesis();
ImageSynthesisResult result = null;
try {
System.out.println("---sync call, please wait a moment----");
result = imageSynthesis.call(param);
} catch (ApiException | NoApiKeyException e){
throw new RuntimeException(e.getMessage());
}
System.out.println(JsonUtils.toJson(result));
}
private ImageSynthesisParam genImageSynthesis(){
HashMap<String,Object> extraInputMap = new HashMap<>();
extraInputMap.put("base_image_url", "http://synthesis-source.oss-accelerate.aliyuncs.com/lingji/validation/mask2img/demo/source3.jpg");
extraInputMap.put("mask_image_url", "http://synthesis-source.oss-accelerate.aliyuncs.com/lingji/validation/mask2img/demo/glasses.png");
String prompt = "a dog wearing red glasses";
String model = "wanx-x-painting";
return ImageSynthesisParam.builder()
.model(model)
.prompt(prompt)
.n(1)
.size("1024*1024")
.extraInputs(extraInputMap)
.build();
}
public static void main(String[] args){
Main text2Image = new Main();
text2Image.syncCall();
}
}
异步调用
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisParam;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import java.util.HashMap;
public class Main {
public void asyncCall() {
System.out.println("---create task----");
String taskId = this.createAsyncTask();
System.out.println("---wait task done then return image url----");
this.waitAsyncTask(taskId);
}
/**
* 创建异步任务
* @return taskId
*/
public String createAsyncTask() {
String task = "image2image";
ImageSynthesis imageSynthesis = new ImageSynthesis(task);
ImageSynthesisParam param = genImageSynthesis();
ImageSynthesisResult result = null;
try {
result = imageSynthesis.asyncCall(param);
} catch (ApiException | NoApiKeyException e){
throw new RuntimeException(e.getMessage());
}
String taskId = result.getOutput().getTaskId();
System.out.println("taskId=" + taskId);
return taskId;
}
private ImageSynthesisParam genImageSynthesis(){
String prompt = "a dog wearing red glasses";
String model = "wanx-x-painting";
HashMap<String,Object> extraInputMap = new HashMap<>();
extraInputMap.put("base_image_url", "http://synthesis-source.oss-accelerate.aliyuncs.com/lingji/validation/mask2img/demo/source3.jpg");
extraInputMap.put("mask_image_url", "http://synthesis-source.oss-accelerate.aliyuncs.com/lingji/validation/mask2img/demo/glasses.png");
return ImageSynthesisParam.builder()
.model(model)
.prompt(prompt)
.n(1)
.size("1024*1024")
.extraInputs(extraInputMap)
.build();
}
/**
* 等待异步任务结束
* @param taskId 任务id
* */
public void waitAsyncTask(String taskId) {
ImageSynthesis imageSynthesis = new ImageSynthesis();
ImageSynthesisResult result = null;
try {
// If you have set the DASHSCOPE_API_KEY in the system environment variable, the apiKey can be null.
result = imageSynthesis.wait(taskId, null);
} catch (ApiException | NoApiKeyException e){
throw new RuntimeException(e.getMessage());
}
System.out.println(JsonUtils.toJson(result.getOutput()));
System.out.println(JsonUtils.toJson(result.getUsage()));
}
public static void main(String[] args){
Main text2Image = new Main();
text2Image.asyncCall();
}
}
错误码
如果模型调用失败并返回报错信息,请参见错误码进行解决。