You need to enable JavaScript to run this app.
导航

图片翻译API

最近更新时间2023.05.12 17:10:07

首次发布时间2021.02.23 10:42:10

接口描述

图片翻译是火山引擎基于行业领先的文字识别和翻译技术,提供多场景、多语种、高精度的整图识别+翻译服务。您只需传入图片,即可自动识别图片中的文字并进行翻译,支持实景回填将译文还原到原图相应位置上。

接口限制

  • 支持语言:可在语言支持列表中查看图片翻译支持的的源语言及目标语言。
  • 图片要求:
    • 支持的图片格式: jpg、png
    • 图片大小限制:4MB
    • 图片尺寸限制:4096*4096
    • 图片内文字的要求:图片内文字要清晰,不能为手写体

接口说明

  • 请求方法
    POST
  • 请求体
字段类型是否为必须项说明备注
TargetLanguageString目标语言对应的语言代码支持语种:sl, en, es, pt, fr, de, id, vi, nl, it, tr, ru, pl, fi, ro, cs, el, uk, sv, ms, no, sk, mk, lv, tl, mn, lt, hr, et, bs, da, bg, af, hu, zh-Hant, ja, ko, zh, th, hi, mr, te, ta, my, ml, km, kn, he, gu, bn, fa, ka。可在语言支持中查询对应的语言代码
ImageString图片二进制内容进行base64编码后的字符串
  • 返回体
字段类型说明备注
ResponseMetadataResponseMetadata通用字段
ImageString文字识别+翻译后文本回贴后图片的base64编码
TextBlocks[TextBlock]包含位置信息、识别文本、翻译文本等信息的文本块结果列表TextBlocks列表内具体字段信息见下表
  • TextBlock
字段类型说明备注
Points[Point]文本块坐标点Points的具体字段信息见下表
DetectedLanguageString检测到的语种
TextString识别内容
TranslationString翻译结果
ForeColor[int32]文字平均色RGBA
BackColor[int32]背景平均色RGBA
  • Point
字段类型说明备注
Xint32X坐标
Yint32Y坐标
  • 样例
    • 请求体
    {
        "Image": "iVBORw0KGgoAAAANSUhEUgA......yspVVQAAAAASUVORK5CYII=",
        "TargetLanguage": "zh"
    }
    
    • 返回体
    {
        "Image": "iVBORw0KGgoAAAANSUhEUgAA......IpHha3IAAAAASUVORK5CYII=",
        "TextBlocks":[
            {
                "Points": [
                    {"X":0, "Y":0},
                    {"X":100, "Y":0},
                    {"X":0, "Y":100},
                    {"X":100, "Y":100}
                ],
                "DetectedLanguage": "en",
                "Text": "Hello",
                "Translation": "你好",
                "ForeColor": [0, 0, 0, 0],
                "BackColor": [255, 255, 255, 255]
        ],
        "ResponseMetadata": {
            "RequestId":"20200708125936010014044066270A1763",
            "Action":"TranslateImage",
            "Version":"2020-07-01",
            "Service":"translate",
            "Region":"cn-north-1"}
    }
    

示例代码

  • 使用 Python

依赖 volc-sdk-python

import json
import requests
import base64
import time

from volcengine.ApiInfo import ApiInfo
from volcengine.Credentials import Credentials
from volcengine.ServiceInfo import ServiceInfo
from volcengine.base.Service import Service

# 示例图片url
img_url = 'https://s1.ax1x.com/2020/07/07/UAtqnf.png'

k_access_key = '密钥管理中的 AccessKey ID' # https://console.volcengine.com/iam/keymanage/
k_secret_key = '密钥管理中的 AccessKey Secret'
k_timeout = 5  # second
k_service_info = \
    ServiceInfo('open.volcengineapi.com',
                {'Content-Type': 'application/json'},
                Credentials(k_access_key, k_secret_key, 'translate', 'cn-north-1'),
                5,
                5)
k_query = {
    'Action': 'TranslateImage',
    'Version': '2020-07-01'
}
k_api_info = {
    'translate': ApiInfo('POST', '/', k_query, {}, {})
}
service = Service(k_service_info, k_api_info)

img_data = requests.get(img_url).content
img_base64 = str(base64.b64encode(img_data), encoding='utf8')
body = {
    'Image': img_base64,
    'TargetLanguage': "zh"}
res_data = service.json('translate', {}, json.dumps(body))
print(res_data)
  • Go
package main

import (
    "encoding/base64"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url"
    "time"

    "github.com/volcengine/volc-sdk-golang/base"
)

const (
    kAccessKey      = "密钥管理中的 AccessKey ID" // https://console.volcengine.com/iam/keymanage/
    kSecretKey      = "密钥管理中的 AccessKey Secret"
    kServiceVersion = "2020-07-01"

    // 示例图片url
    imgUrl = "https://s1.ax1x.com/2020/07/07/UAtqnf.png"
)

var (
    ServiceInfo = &base.ServiceInfo{
        Timeout: 5 * time.Second,
        Host:    "open.volcengineapi.com",
        Header: http.Header{
            "Accept": []string{"application/json"},
        },
        Credentials: base.Credentials{Region: base.RegionCnNorth1, Service: "translate"},
    }
    ApiInfoList = map[string]*base.ApiInfo{
        "TranslateImage": {
            Method: http.MethodPost,
            Path:   "/",
            Query: url.Values{
                "Action":  []string{"TranslateImage"},
                "Version": []string{kServiceVersion},
            },
        },
    }
)

func newClient() *base.Client {
    client := base.NewClient(ServiceInfo, ApiInfoList)
    client.SetAccessKey(kAccessKey)
    client.SetSecretKey(kSecretKey)
    return client
}

func main() {
    imgRes, _ := http.Get(imgUrl)
    defer imgRes.Body.Close()
    imgData, _ := ioutil.ReadAll(imgRes.Body)
    imgBase64 := base64.StdEncoding.EncodeToString(imgData)
    jsonBody := map[string]string{
        "Image": imgBase64,
        "TargetLanguage": "zh"}
    body, _ := json.Marshal(jsonBody)

    client := newClient()
    res, code, err := client.Json("TranslateImage", nil, string(body))
    if err != nil {
        panic(err)
    }
    fmt.Printf("%d %s\n", code, string(res))
}
//            byte[] input = new byte[10]; // read file
            String base64Image = Base64.getEncoder().encodeToString(input);

            TranslateImageRequest translateImageRequest = new TranslateImageRequest();
            translateImageRequest.setTargetLanguage("zh");
            translateImageRequest.setImage(base64Image);

            TranslateImageResponse translateImageResponse = translateService.translateImage(translateImageRequest);
            System.out.println(JSON.toJSONString(translateImageResponse.getResponseMetadata()));
            System.out.println(JSON.toJSONString(translateImageResponse.getResult()));
            byte[] output = Base64.getDecoder().decode(translateImageResponse.getImage());
            // write file