Image and text translation

1. Overview of services

For pictures containing text, combine OCR image and text recognition technology and machine translation to identify the text content in the picture and translate it into the specified language

2. Apply for services

Image Translation API uses a complete flow and self-application model. Click on “Login” at the top of LiveData’s official website (https://www.ilivedata.com), and sign up following the instructions. Choose AI Live Translation Service and create an application. A pid and a service key will be sent to you.

You can also activate other services on the Management Console - Overview Page.

3. Access Method

Header Value Description
Content-Type application/json  
Accept application/json  
X-AppId Application ID or Project ID  
X-TimeStamp Request time in UTC format For example: 2010-01-31T23:59:59Z.  (for more information, go to http://www.w3.org/TR/xmlschema-2/#dateTime).
Authorization  Signature Token  Check Authentication section below
  • Request parameter
Parameter Required/Optional Type Description
type Required Number 1: Image URL
2: String value of Image encoded by BASE64
image Required String Image content, if type=1, then the value is the image URL, if type=2, then the value is the image BASE64 value. Support format:jpg, png, bmp, gif, webp, tiff, heic; Image max size: 10M
target Required String Set target language for translation as one of the ISO 639-1 language codes listed in Language Support.
  • Request signature:

When the user requests the Translation API, the request can be signed with the appId and secretKey. When the API receives the request with the signature information, it will verify the signature using the same algorithm, and if the signature is found to be inconsistent, the API will return 401 error to the user. If the API validation signature is consistent and the user corresponding to the appId has permission to operate the requested resource, the request succeeds, otherwise the API returns 401 error.

  • Request Header to send a signature via HTTP

Method: Add a header named Authorization in the request, whose value is the signature’s value. For example:

	Authorization: *****
  • Signature calculation method

    1. Canonicalized Query String:

      a. As signatures require uniqueness, including order, they need to be sorted by parameter name.

      b. The following rules are used to encode URLs for parameter names and values. For more information, see RFC 3986.

      * A-Z, a-z, 0-9, minus sign (-), underscore (_), full stop (.), and tilde (~) are not coded.
      * The other characters are encoded in the format %XY, where XY is the hexadecimal representation of the character corresponding to ASCII code and the letter is capitalized.For example, the double quotes (") correspond to the code %22
      * For extended UTF-8 characters, encoded in the format of %XY%ZA...
      * English spaces ( ) are to be encoded as %20 instead of the plus sign (+).
      

      c. Use the equal sign character (=) to separate the parameter name from the parameter value, even if the parameter value is null. Use arguments and value pairs separated from the character (&). Join parameters and their values to form a long string with no spaces between them. Spaces are allowed, but they must be URL encoded as %20. In the concatenated string, the period character (.) is not escaped. RFC 3986 treats period characters as unreserved characters and therefore does not URL encode them.

      d. Libraries that generally support URL encoding (such as java.net.URLEncoder in Java) are encoded according to the “application/x-www-form-urlencoded” MIME type rule. In practice, this method can be directly used for coding. Replace the plus sign (+) in the encoded string with %20, the asterisk (*) with %2A and %7E with tilde (~).

    2. Constructs the signed string StringToSign ("\n" stands for ASCII newline character):

          StringToSign = HTTPMethod + "\n" + 
                         HostHeaderInLowercase + "\n" + 
                         HTTPRequestURI + "\n" + 
                         CanonicalizedQueryString <got from previous step>
      

      The HTTPRequestURI is the absolute path to the request URI, without the request string. If the HTTPRequestURI is empty, also keep a forward slash (/)

    3. The hash-based message authentication code (HMAC) is created using the HMAC-SHA256 protocol and the signature is calculated.

      StringToSign as the signature string, secretKey as the secret key and SHA256 as the hash algorithm.

      For more information about HMAC, see HMAC: Hash Keys for Message Authentication (https://tools.ietf.org/html/rfc2104)。

    4. Converting the results of the previous step to a BASE64 string

    5. Put the BASE64 string into the Authorization of HTTP request Header

  • Example of signature

  1. Below is an example of appId & secretKey.

     appId=***
     secrectKey=*****
    
  2. Below is an example request.

Request Json:

{
    "type": "2",
    "userId": "test",
    "target": "zh-CN",
    "image": "T2dnUwACAAAAAAAAAAAd8pVTAAAAAGsIvpMBE..."
}

Generate CanonicalizedQueryString: b3ad8e9d16439ccd5e91924d2516bf9592975003f69beff44e56cddf47bd3118

  1. Generate StringToSign

     POST\n
     translate.ilivedata.com\n
     api/v1/image/translate\n
     b3ad8e9d16439ccd5e91924d2516bf9592975003f69beff44e56cddf47bd3118
     X-AppId:***
     X-TimeStamp:2020-07-31T07:59:03Z
    
  2. Signatures from HMAC calculation

     *****
    
  • HTTP response:

Content-Type: application/json;charset=UTF-8

具体参数说明:

字段名 类型 描述
errorCode Number 错误码,0表示成功
errorMessage String 错误消息
code Number 状态码,定义为:0:检测成功,1:图片下载失败,2:图片格式错误,3:其他
taskId String 翻译任务唯一标识
ocr String[] 识别出的图文列表
ocrDetail OcrDetail[] 识别出的图文详细信息列表 含有文本内容和
translateResult TranslateResult[] 翻译结果

OcrDetail结构:

字段名 类型 描述
text String 文本内容
coordinate Number[][] 文本对应的一组坐标, 分别是矩形文本框四个顶点的相对坐标, 范围在(0,1)

TranslateResult结构:

字段名 类型 描述
source String 图文语种
target String 翻译目标语种
sourceText String 图文内容
targetText String 翻译结果
  • HTTP response:
{
    "errorCode": 0,
    "code": 0,
    "ocr": [ "MICHAEL" ],
    "ocrDetail": [
      {
        "coordinate": [
            [0.8256130790190735,0.9365079365079365],
            [0.9346049046321526,0.9365079365079365],
            [0.9346049046321526,0.981859410430839],
            [0.8256130790190735,0.981859410430839]
        ],
        "text": "MICHAEL"
      }
    ],
    "taskId": "Tencent-ap_867488e27ab14c138180479992ab114b_1719829148272",
    "translateResult": [
      {
        "source": "en",
        "target": "zh-CN",
        "sourceText": "MICHAEL",
        "targetText": "迈克尔"
      }
    ]
}