文档
图片翻译结果接口

获取图片翻译结果

1. 接口地址

https://hidreamai.com/api-pub/ec/v3/image/translate/result

2. 请求方式

GET

3. 请求参数

Header

参数类型是否必填说明
Authorizationstring客户授权Token

Query Params

参数类型是否必填说明
task_idstring提交任务时返回的任务task_id
request_idstring请求的ID

curl示例:

curl -H 'Authorization: Bearer {USER_Authorization}' -X GET 
-v 'https://hidreamai.com/api-pub/ec/v3/image/translate/result?task_id=b2781982-5c57-11ee-bf2b-f6f8d60dadec'

python示例:

import time
import requests
 
# token获取方式详见 接口认证 api-key换取令牌小节
token = ''
 
# 请求头
headers = {
    'Authorization': f'Bearer {token}',
    'Content-Type': 'application/json'
}
 
params = {
    "task_id": "b2781982-5c57-11ee-bf2b-f6f8d60dadec"
}
 
url = f"https://hidreamai.com/api-pub/ec/v3/image/translate/result"
 
# 轮询获取任务进度
while True:
    response = requests.get(url, params=params, headers=headers)
    uncompleted_task = 0
    if response.status_code == 200:
        if response.json().get('code') == 0:
            sub_task_results = response.json().get('result').get('sub_task_results')
            for item in sub_task_results:
                if item.get('task_status') in (0, 2):
                    uncompleted_task += 1
                    print("当前子任务进度:", item.get('task_completion'))
            # 子任务全部完成,打印图片结果
            if uncompleted_task == 0:
                for item in sub_task_results:
                    print(item.get('image'))
                break
        else:
            # 接口异常
            print(response.json())
            break
    else:
        # http请求异常
        print(response.status_code)
        break
    time.sleep(3)
 

4. 返回参数

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

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

参数类型说明
task_idstring任务的task_id
sub_task_resultslist各个子任务的生成结果

其中sub_task_result字段具体为

参数类型说明
sub_task_idstring子任务的task_id
task_statusint子任务的状态,0: 等待中,1: 完成,2: 处理中,3: 失败,4: 未通过审核
task_completionfloat子任务的进度,取值范围为0-1的小数点后保留2位的小数
imagestring生成的图片的url, 如果需要提高分辨率可以使用.png替换.jpg, 也可使用?width=num来限制图片大小, eg.'https://storage.hidreamai.com/image/p_63f1670c-3a76-11ee-ad4f-eeed008884fa.jpg?width=512 (opens in a new tab)'

示例:

{
    "code": 0,
    "message": "请求成功",
    "request_id": "425d6094-3a73-11ee-85e2-0a4fb61b1c34",
    "result": {
        "sub_task_results": [
            {
                "image": "https://storage.hidreamai.com/image/p_aedd5d2e-5c5a-11ee-a955-9a295d03f023.jpg",
                "sub_task_id": "d932ac2b-4591-4fc3-8995-adf81a87ce53",
                "task_completion": 1,
                "task_status": 1
            }
        ],
        "task_id": "b2781982-5c57-11ee-bf2b-f6f8d60dadec"
    }
}