AI超清 V2

描述

本接口用于投递图像 AI 超清 V2 任务。

图片要求

  • 图片格式:JPG、PNG。
  • 图片大小:建议 10 MB 以内。

调用 URL

    正式环境:https://openapi.meitu.com
    任务提交接口:https://openapi.meitu.com/api/v1/sdk/sync/push
    任务名称(task):/v1/Ultra_High_Definition_V2/478332
    任务类型(task_type):formula

调用方法

POST

Content-Type: application/json

权限

开放平台接口签名

请求参数

请求体恰好包含以下五个顶层字段:

是否必选参数名类型参数说明
必选paramsstring推理参数;值为序列化后的 JSON 字符串
必选init_imagesobject[]图片参数列表
必选taskstring固定 /v1/Ultra_High_Definition_V2/478332
必选task_typestring固定 formula
可选sync_timeoutint默认 30-1 表示不等待。同步等待超时后返回状态 9,需使用查询接口获取结果

init_images 图片参数

是否必选参数名类型参数说明
必选urlstring图片地址
必选profileobject属性信息描述

profile 属性信息描述

是否必选参数名类型参数说明
必选media_profilesobject图片的相关属性信息描述

media_profiles 图片属性信息

是否必选参数名类型参数说明
必选media_data_typestring默认 urlurl 表示通过 URL 传入图片

params 是 JSON 字符串,反序列化后的结构如下:

是否必选字段类型说明
可选rsp_media_typestring公共响应媒体类型,默认 urlurl 表示结果媒体数据为图片 URL。源文未定义其他取值
必选parameterobjectAI 超清 V2 算法参数对象

rsp_media_typeparameter 同级,所有算法字段仅放在 parameter 内。

parameter 算法参数详情

是否必选参数名类型参数说明
可选sr_numint放大倍数;2 表示高清、效果优先,4 表示超清、分辨率优先,默认 2
可选area_sizeintsr_num=2 时需传 1920sr_num=4 时无需传值,默认 2560
可选save_photo_formatint保存图片格式,默认 11 表示 JPG 编码,2 表示 PNG 编码

输入值示例

{
  "task": "/v1/Ultra_High_Definition_V2/478332",
  "task_type": "formula",
  "init_images": [
    {
      "url": "https://wheeai.meitudata.com/static/666162c4139073547bhMUcLeee3093.jpeg",
      "profile": {
        "media_profiles": {
          "media_data_type": "url"
        }
      }
    }
  ],
  "params": "{\"rsp_media_type\":\"url\",\"parameter\":{\"area_size\":1920,\"sr_num\":2}}",
  "sync_timeout": 30
}

返回值说明

注意,生成的结果图 24 小时后会定期清理,请及时下载保存。
字段类型说明
request_idstring请求标识
trace_idstring链路追踪标识
codeint业务状态码,0 表示请求受理成功
error_codeint错误码,成功时为 0
messagestring业务信息或错误信息
tipsany补充提示,可为 null
dataobject/null任务状态和算法结果;请求未进入任务处理流程时可为 null

data 字段说明

字段类型说明
statusint-1 未找到任务;0 创建成功;1 执行中;2 任务失败;9 需使用查询接口查询;10 任务成功
msgstring任务状态说明
resultobject算法结果
progressnumber任务进度,例如 0.10.851
predict_elapsedint预计耗时,毫秒
create_timeint64创建时间戳,毫秒
task_idstring任务 ID
custom_task_idstring客户自定义任务 ID
trace_idstring链路追踪标识
client_infostring客户端信息
init_imagesobject[]/null输入媒体回显

成功或待查询时 result 字段

字段类型说明
idstring任务 ID,可用于查询任务状态
urlsstring[]结果图片 URL 列表

任务失败时 result 字段

字段类型说明
idstring任务 ID
ErrorCodeint算法错误代码
ErrorMsgstring算法错误信息
Datastring/null具体错误信息;无数据时为 null

返回值示例

请求成功返回示例(status=10)

Response Status:200

content-type 为 application/json; charset=utf-8

{
  "request_id": "req_1234567890",
  "trace_id": "trace_1234567890",
  "code": 0,
  "error_code": 0,
  "message": "success",
  "tips": null,
  "data": {
    "status": 10,
    "msg": "success",
    "result": {
      "id": "50309bd5-a827-4125-bc96-62039c93770b",
      "urls": [
        "https://obs.mtlab.meitu.com/mtopen/rF5GIhp5ReLKgLV91CKj5BO1q2FTLMmc/MTY5Mjg1MzIwMA==/1c39ef92-04f1-40b4-5b7e-8fcfca0c11ea.png"
      ]
    },
    "progress": 1,
    "predict_elapsed": 0,
    "create_time": 1692853200000,
    "task_id": "50309bd5-a827-4125-bc96-62039c93770b",
    "custom_task_id": "",
    "trace_id": "trace_1234567890",
    "client_info": "",
    "init_images": null
  }
}

需要查询返回示例(status=9)

data.status9 时,使用查询接口和返回的任务 ID 查询结果。

Response Status:200

content-type 为 application/json; charset=utf-8

{
  "request_id": "req_1234567890",
  "trace_id": "trace_1234567890",
  "code": 0,
  "error_code": 0,
  "message": "success",
  "tips": null,
  "data": {
    "status": 9,
    "msg": "processing",
    "result": {
      "id": "task_1234567890"
    },
    "progress": 0,
    "predict_elapsed": 10000,
    "create_time": 1692853200000,
    "task_id": "task_1234567890",
    "custom_task_id": "",
    "trace_id": "trace_1234567890",
    "client_info": "",
    "init_images": null
  }
}

任务失败返回示例(status=2)

Response Status:400

content-type 为 application/json; charset=utf-8

{
  "request_id": "req_1234567890",
  "trace_id": "trace_1234567890",
  "code": 20001,
  "error_code": 20001,
  "message": "PROCESS_ERROR",
  "tips": null,
  "data": {
    "status": 2,
    "msg": "PROCESS_ERROR",
    "result": {
      "id": "task_1234567890",
      "ErrorCode": 20001,
      "ErrorMsg": "PROCESS_ERROR",
      "Data": null
    },
    "progress": 1,
    "predict_elapsed": 0,
    "create_time": 1692853200000,
    "task_id": "task_1234567890",
    "custom_task_id": "",
    "trace_id": "trace_1234567890",
    "client_info": "",
    "init_images": null
  }
}

请求未进入任务处理流程的失败示例

源文还给出了以下请求失败返回;此时 datanull,没有任务状态。

Response Status:400

content-type 为 application/json; charset=utf-8

{
  "request_id": "req_1234567890",
  "trace_id": "trace_1234567890",
  "code": 20008,
  "error_code": 20008,
  "message": "UNSUITABLE_IMAGE",
  "tips": null,
  "data": null
}

当前 API 特有的错误代码与信息

ErrorCode 状态代码错误信息说明
20001PROCESS_ERROR处理错误

源文的请求失败示例还使用了 20008UNSUITABLE_IMAGE),但未将其列入当前 API 特有错误码表。

通用的错误代码与信息

详见 API 错误码

SDK 调用示例

以下示例均对最终实际发送的完整 JSON body 进行签名;body 恰好包含 tasktask_typeinit_imagesparamssync_timeout 五个顶层字段。

Python

import json

import requests
from sign_sdk import sign

def api_call_example():
    key = "your_api_key"
    secret = "your_api_secret"
    url = "https://openapi.meitu.com/api/v1/sdk/sync/push"
    method = "POST"
    headers = {
        "Content-Type": "application/json",
        sign.HeaderHost: "openapi.meitu.com",
    }
    inner_params = {
        "rsp_media_type": "url",
        "parameter": {
            "area_size": 1920,
            "sr_num": 2,
        },
    }
    payload = {
        "task": "/v1/Ultra_High_Definition_V2/478332",
        "task_type": "formula",
        "init_images": [
            {
                "url": "https://wheeai.meitudata.com/static/666162c4139073547bhMUcLeee3093.jpeg",
                "profile": {
                    "media_profiles": {"media_data_type": "url"},
                },
            }
        ],
        "params": json.dumps(inner_params, ensure_ascii=False),
        "sync_timeout": 30,
    }
    body = json.dumps(payload, ensure_ascii=False)

    signer = sign.Signer(key, secret)
    signed_request = signer.sign(url, method, headers, body)
    response = requests.Session().send(signed_request)
    print(f"Status: {response.status_code}")
    print(f"Response: {response.text}")

if __name__ == "__main__":
    api_call_example()

Go

package main

import (
	"fmt"
	"io"
	"net/http"

	"github.com/mtlab/api/signer"
)

func main() {
	key := "your_api_key"
	secret := "your_api_secret"
	signObj := signer.NewSigner(key, secret)
	url := "https://openapi.meitu.com/api/v1/sdk/sync/push"
	method := http.MethodPost
	headers := make(http.Header)
	headers.Set(signer.HeaderHost, "openapi.meitu.com")
	headers.Set("Content-Type", "application/json")

	body := `{
  "task": "/v1/Ultra_High_Definition_V2/478332",
  "task_type": "formula",
  "init_images": [{
    "url": "https://wheeai.meitudata.com/static/666162c4139073547bhMUcLeee3093.jpeg",
    "profile": {
      "media_profiles": {"media_data_type": "url"}
    }
  }],
  "params": "{\"rsp_media_type\":\"url\",\"parameter\":{\"area_size\":1920,\"sr_num\":2}}",
  "sync_timeout": 30
}`

	req, err := signObj.Sign(url, method, headers, body)
	if err != nil {
		fmt.Println("Failed to sign request:", err)
		return
	}
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		fmt.Println("Failed to send request:", err)
		return
	}
	defer resp.Body.Close()
	responseBody, err := io.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Read response failed:", err)
		return
	}
	fmt.Println("Response:", resp.StatusCode, string(responseBody))
}

PHP

<?php
require 'signer.php';

$key = 'your_api_key';
$secret = 'your_api_secret';
$signer = new Signer($key, $secret);
$url = 'https://openapi.meitu.com/api/v1/sdk/sync/push';
$method = 'POST';
$headers = ['Content-Type' => 'application/json'];

$innerParams = json_encode([
    'rsp_media_type' => 'url',
    'parameter' => [
        'area_size' => 1920,
        'sr_num' => 2,
    ],
]);
$body = json_encode([
    'task' => '/v1/Ultra_High_Definition_V2/478332',
    'task_type' => 'formula',
    'init_images' => [
        [
            'url' => 'https://wheeai.meitudata.com/static/666162c4139073547bhMUcLeee3093.jpeg',
            'profile' => [
                'media_profiles' => ['media_data_type' => 'url'],
            ],
        ],
    ],
    'params' => $innerParams,
    'sync_timeout' => 30,
]);

$curl = $signer->sign($url, $method, $headers, $body);
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($status === 0) {
    echo 'Error: ' . curl_error($curl);
} else {
    echo "Status: {$status}\n";
    echo "Response: {$response}\n";
}
curl_close($curl);
?>

Java

package com.meitu.openai.common;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) throws Exception {
        Signer signer = new Signer("your_api_key", "your_api_secret");
        String url = "https://openapi.meitu.com/api/v1/sdk/sync/push";
        String method = "POST";

        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put(Signer.HeaderHost, "openapi.meitu.com");

        String body = "{\n" +
                "  \"task\": \"/v1/Ultra_High_Definition_V2/478332\",\n" +
                "  \"task_type\": \"formula\",\n" +
                "  \"init_images\": [{\n" +
                "    \"url\": \"https://wheeai.meitudata.com/static/666162c4139073547bhMUcLeee3093.jpeg\",\n" +
                "    \"profile\": {\n" +
                "      \"media_profiles\": {\"media_data_type\": \"url\"}\n" +
                "    }\n" +
                "  }],\n" +
                "  \"params\": \"{\\\"rsp_media_type\\\":\\\"url\\\",\\\"parameter\\\":{\\\"area_size\\\":1920,\\\"sr_num\\\":2}}\",\n" +
                "  \"sync_timeout\": 30\n" +
                "}";

        Map<String, String> signedHeaders = signer.sign(url, method, headers, body);
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setRequestMethod(method);
        for (Map.Entry<String, String> entry : signedHeaders.entrySet()) {
            connection.setRequestProperty(entry.getKey(), entry.getValue());
        }
        connection.setDoOutput(true);
        connection.getOutputStream().write(body.getBytes(StandardCharsets.UTF_8));

        int status = connection.getResponseCode();
        InputStream stream = status >= 400
                ? connection.getErrorStream()
                : connection.getInputStream();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            System.out.println("Response: " + status + " " + response);
        }
    }
}