文生图
描述
本接口用于云端处理文生图的请求,具体效果请看以下文档详情。
注意,算法生成的图片/视频,请及时下载保存,服务侧会定期清理。最小保留时间为1小时版本
1.0
更新日志
调用 URL
正式环境:https://openapi.meitu.com/whee/business/text2image.json
签名验证
调用方法
POST
request 的头的类型应包含:Content-Type:application/json
权限
使用前需提前申请 AccessKey 和 SecretKey。
请求参数
| 是否必选 | 参数名 | 类型 | 参数说明 |
|---|---|---|---|
| 必选 | params | string | 文生图推理参数 |
| 必选 | task | string | txt2img |
| 必选 | task_type | string | inference |
| 可选 | task_id | string | 任务id,保证业务唯一 |
params 推理参数是JSON字符串,其结构说明如下
| 是否必选 | 字段 | 类型 | 说明 |
|---|---|---|---|
| 必选 | model | string | 模型,可用模型从API接口中获取。 获取文档 |
| 必选 | prompt | string | 正向提示词及风格模型设置,(prompt:weight) 格式控制文本权重,<lora:model:weight> 配置风格模型及其权重,可用风格模型从API接口获取。 获取文档 示例:girl,(cartoon:1.1),<lora:074594b2d60230282482d176d28c7c2e_1.0:0.8> 说明:普通描述词girl, 1.1比例权重描述词cartoon, lora使用诗意水墨并设置0.8强度 |
| 可选 | seed | int | 随机数种子,默认-1 。 正数或-1。 当seed值为正数时,生成的图片内容稳定相关,-1时内容有随机差异。 |
| 必选 | width | int | 宽度,默认768,可用尺寸由API接口获取。 获取文档 |
| 必选 | height | int | 高度,默认1024,可用尺寸由API接口获取。 获取文档 |
| 可选 | negative_prompt | string | 负向提示词 |
| 可选 | alwayson_scripts | object | 插件配置 |
| 可选 | steps | int | 采样步骤。生成图片所需的步骤。默认50。 |
| 可选 | cfg_scale | float64 | 提示词相关性(输入的prompt和negative prompt对生成结果的影响程度系数),[1-20],步进0.5,默认5。 |
| 可选 | batch_size | int | 生成图片数量,不传参默认生成4张,传参范围1到4。 |
| 可选 | adetailer_face | int | 面部修复插件:1开启,2关闭。默认开启。 |
| 可选 | controlnet_units | object[] | 画面控制插件,可用于控制画面的生成,不用则不传 |
controlnet_units 画面控制参数,结构说明如下
| 是否必选 | 参数名 | 类型 | 参数说明 |
|---|---|---|---|
| 必选 | input_image | string | 图片地址,不传或传空则无效 |
| 可选 | module | string | 预处理器,可用预处理器从API接口中获取,默认为该列表的第一个。 获取文档 |
| 可选 | weight | float64 | 权重,代表使用画面控制生成图片时被应用的权重占比,权重越大,效果越明显,值范围为0-1,推荐值在0.4到0.6之间 |
| 可选 | guidance_start | float64 | 引导介入时机,值越大,画面控制引导介入的时间越晚,效果越不明显,值范围为0-1,推荐值为0-0.1之间 |
| 可选 | guidance_end | float64 | 引导结束时机,值越大,画面控制引导持续时间越长,效果越明显,值范围为0-1,推荐值为0.5-0.7之间 |
alwayson_scripts 插件参数说明
ADetailer 插件(人脸修复)
"alwayson_scripts": {
"ADetailer": {
"args": [
true,
{
"ad_cfg_scale": 6,
"ad_confidence": 0.3,
"ad_denoising_strength": 0.3,
"ad_dilate_erode": 4,
"ad_enable_face_generation": true,
"ad_inpaint_height": -1,
"ad_inpaint_width": -1,
"ad_mask_blur": 4,
"ad_mask_max_ratio": 0.15,
"ad_mask_min_ratio": 0,
"ad_model": "face_yolov8m.pt",
"ad_negative_prompt": "",
"ad_prompt": "",
"ad_steps": 30
}
]
}
}| 是否必选 | 参数名 | 类型 | 参数说明 |
|---|---|---|---|
| 必须 | args | 数组 | 插件参数 |
args 参数说明
| 是否必选 | 参数名 | 类型 | 参数说明 |
|---|---|---|---|
| 必须 | 数组第一项 | bool | 是否启用插件 |
| 可选 | 数组第二项 config | object | 插件配置参数 |
config 参数说明
| 是否必选 | 参数名 | 类型 | 参数说明 |
|---|---|---|---|
| 可选 | ad_dilate_erode | int | 用于对检测出的细节面积进行扩大(为正数时),或者缩小(为负数时)默认 4 |
| 可选 | ad_confidence | float | 检测细节信息(人脸、手部)时的置信度,默认 0.3 |
| 可选 | ad_cfg_scale | int | 控制修复图与prompt的相关性 0-10 |
| 可选 | ad_denoising_strength | double | 0-1,控制修复图相比于原图的变化程度,越大变化越大 |
| 可选 | ad_enable_face_generation | bool | true、false,控制是否使用人脸引导 |
| 可选 | ad_inpaint_height | int | 默认-1, 控制输入给生成模型的图片宽度,默认和原图宽度一样 |
| 可选 | ad_inpaint_width | int | 默认-1, 控制输入给生成模型的图片高度,默认和原图高度一样 |
| 可选 | ad_mask_blur | int | 0-64,用于当修复后图片和原图进行融合时缓解边缘拼接过于锐利的情况,越小拼接边缘越明显,默认4 |
| 可选 | ad_mask_max_ratio | float | 1.0 检测细节信息时的最小面积 |
| 可选 | ad_mask_min_ratio | float | 0.0 检测细节信息时的最大面积 |
| 可选 | ad_model | string | 人脸修复模型,当前仅支持 “face_yolov8m.pt” |
| 可选 | ad_negative_prompt | string | 人脸修复相关逆向提示词 |
| 可选 | ad_prompt | string | 人脸修复相关提示词 |
| 可选 | ad_steps | int | 人脸修复迭代步数,值越大修复力越大,速度越慢,默认30 |
{
"ad_cfg_scale": 6,
"ad_denoising_strength": 0.3,
"ad_enable_face_generation": true,
"ad_inpaint_height": -1,
"ad_inpaint_width": -1,
"ad_mask_blur": 4,
"ad_mask_max_ratio": 0.15,
"ad_mask_min_ratio": 0,
"ad_model": "face_yolov8m.pt",
"ad_negative_prompt": "",
"ad_prompt": "",
"ad_steps": 30
}
输入值示例
{
"params": "{\"model\":\"miracle-vision_v3.1-realistic1.zip\",\"prompt\":\"girl,(cartoon:1.1),<lora:074594b2d60230282482d176d28c7c2e_1.0:0.8>\",\"seed\":-1,\"width\":768,\"height\":1024,\"negative_prompt\":\"Malformed Limbs\"}",
"task": "txt2img",
"task_type": "inference"
}返回值说明
成功返回值说明
| 字段 | 类型 | 参数说明 |
|---|---|---|
| data | Object | 返回的内容 |
| message | String | 错误信息 |
| code | Number | 错误代码 |
data 中单个元素的字段说明
| 字段 | 类型 | 参数说明 |
|---|---|---|
| status | Number | 状态码,-1没有找到任务,0任务创建成功,1 任务执行中,2任务失败,9 任务状态待查询 通过status接口查询,10任务成功。得到状态9时,需要通过Status 接口查询任务状态。 |
| msg | String | 多媒体文件的相关属性信息描述 |
| result | Object | 结果 |
| progress | Number | 任务进度,例如:0.1 、0.85、1。 |
result 中单个元素的字段说明
| 字段 | 类型 | 参数说明 |
|---|---|---|
| id | String | 任务ID |
| urls | String[] | 结果图片地址 |
失败返回值说明
| 字段 | 类型 | 说明 |
|---|---|---|
| data | String | 返回的内容 |
| message | String | 错误信息 |
| code | Int | 错误代码 |
返回值示例
请求成功返回示例
Response Status:200
content-type 为 application/json; charset=utf-8
{
"code": 0,
"message":"",
"data": {
"status": 10,
"result": {
"id": "50309bd5-a827-4125-bc96-62039c93770b",
"urls": ["https://obs.mtlab.meitu.com/mtopen/rF5GIhp5ReLKgLV91CKj5BO1q2FTLMmc/MTY5Mjg1MzIwMA==/1c39ef92-04f1-40b4-5b7e-8fcfca0c11ea.png"]
},
"progress": 1
}
}请求失败返回示例
Response Status:400
content-type 为 application/json; charset=utf-8
{
"code": 20008,
"message": "UNSUITABLE_IMAGE",
"data": null
}当前 API 特有的错误代码与信息
| ErrorCode状态代码 | 错误信息 | 说明 |
|---|---|---|
| 20001 | PROCESS_ERROR | 处理错误 |
| 21007 | JSON_ERROR | json内容错误 |
通用的错误代码与信息
详见 API 错误码
调用示例
curl -vX POST "https://openapi.meitu.com/whee/business/text2image.json" -d @p.json --header "Content-Type: application/json" --header 'Authorization:Bearer YourSignAuthCode' --header 'X-SDK-Date:20230824T102323Z'示例Demo
GO
package main
import (
"fmt"
"io"
"net/http"
"github.com/mtlab/api/signer"
)
func main() {
key := "your api_key"
secret := "your api_secret"
sign := signer.NewSigner(key, secret)
url := "https://openapi.meitu.com/whee/business/text2image.json"
method := "POST"
headers := make(http.Header)
headers.Set(signer.HeaderHost, "openapi.meitu.com")
headers.Set("Content-Type", "application/json")
body:=`{
"params": "{\"model\":\"miracle-vision_v3.1-realistic1.zip\",\"prompt\":\"girl,(cartoon:1.1),<lora:074594b2d60230282482d176d28c7c2e_1.0:0.8>\",\"seed\":-1,\"width\":768,\"height\":1024,\"negative_prompt\":\"Malformed Limbs\"}",
"task": "txt2img",
"task_type": "inference"
}`
req, err := sign.Sign(url, method, headers, body)
if err != nil {
fmt.Println("Failed to sign request:", err)
return
}
// Use the signed request to make the actual HTTP request
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
fmt.Println("Failed to send request:", err)
return
}
defer resp.Body.Close()
body1, err1 := io.ReadAll(resp.Body)
if err1 != nil {
fmt.Println("读取响应失败:", err1)
return
}
fmt.Println("GetResponse=>",resp.StatusCode,string(body1),resp)
}
PHP
<html>
<head>
<title>PHP test</title>
</head>
<body>
<pre>
<?php
require 'signer.php';
$signer = new Signer("your api key","your api secret");
$url = 'https://openapi.meitu.com/whee/business/text2image.json';
$method = 'POST';
$headers = [
'Content-Type' => 'application/json'
];
$body = '{
"params": "{\"model\":\"miracle-vision_v3.1-realistic1.zip\",\"prompt\":\"girl,(cartoon:1.1),<lora:074594b2d60230282482d176d28c7c2e_1.0:0.8>\",\"seed\":-1,\"width\":768,\"height\":1024,\"negative_prompt\":\"Malformed Limbs\"}",
"task": "txt2img",
"task_type": "inference"
}';
$curl = $signer->sign($url, $method, $headers, $body);
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($status == 0) {
echo curl_error($curl);
} else {
echo $status . "\n";
echo $response;
}
curl_close($curl);
?>
</pre>
</body>
</html>
python
import json
import requests
from sign_sdk import sign
key = "your api key"
secret = "your api secret"
url = "https://openapi.meitu.com/whee/business/text2image.json"
method = "POST"
headers = {
"Content-Type": "application/json",
sign.HeaderHost: "openapi.meitu.com",
}
body = json.dumps({"params": "{\"model\": \"miracle-vision_v3.1-realistic1.zip\", \"prompt\": \"\\u5973\\u5b69\\uff0c\\u53ef\\u7231\\uff0c\\u6d01\\u767d\\u7684\\u76ae\\u80a4\\uff0c\\u4f4e\\u80f8,\\u9501\\u9aa8\\uff0c<lora:7aa93dca8909a4f27ac4282c60b7b264_1.0:0.8>\", \"width\": 512, \"height\": 1024, \"negative_prompt\": \"EasyNegative\"}", "task": "txt2img", "task_type": "inference"})
signer = sign.Signer(key, secret)
signed_request = signer.sign(url, method, headers, body)
session = requests.Session()
response = session.send(signed_request,verify=False)
print(response.status_code)
print(response.text)
def fetchResult(taskId,item):
url = f"https://openapi.meitu.com/api/v1/sdk/status?task_id={taskId}"
key = "your api key"
secret = "your api secret"
method = "GET"
headers = {
"Content-Type": "application/json",
sign.HeaderHost: "openapi.meitu.com"
}
body = ""
try:
signer = sign.Signer(key, secret)
signed_request = signer.sign(url,method,headers,body)
session = requests.Session()
response = session.send(signed_request,verify=False)
print(response.status_code)
print(response.text)
except Exception as ex:
print(ex)
fetchResult("mtlab-f3db4716-ed68-4f36-9eb6-a06b558aabec",None)