Image and text translation
Update:
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
-
Request URL: https://translate.ilivedata.com/api/v1/image/translate
-
HTTP request Header: Accept: application/json;charset=UTF-8
-
Request method: POST
-
Request Header
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, avif; 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
-
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 (~).
-
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 (/)
-
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)。
-
Converting the results of the previous step to a BASE64 string
-
Put the BASE64 string into the Authorization of HTTP request Header
-
Example of signature
-
Below is an example of appId & secretKey.
appId=***
secrectKey=*****
-
Below is an example request.
Request Json:
{
"type": "2",
"userId": "test",
"target": "zh-CN",
"image": "T2dnUwACAAAAAAAAAAAd8pVTAAAAAGsIvpMBE..."
}
Generate CanonicalizedQueryString:
b3ad8e9d16439ccd5e91924d2516bf9592975003f69beff44e56cddf47bd3118
-
Generate StringToSign
POST\n
translate.ilivedata.com\n
api/v1/image/translate\n
b3ad8e9d16439ccd5e91924d2516bf9592975003f69beff44e56cddf47bd3118
X-AppId:***
X-TimeStamp:2020-07-31T07:59:03Z
-
Signatures from HMAC calculation
*****
- HTTP response:
Content-Type: application/json;charset=UTF-8
Response fields:
Field
Type
Description
errorCode
Number
Error code; 0 indicates success
errorMessage
String
Error message
code
Number
Status: 0 = success, 1 = image download failed, 2 = invalid image format, 3 = other error
taskId
String
Unique translation task identifier
ocr
String[]
Text recognized in the image
ocrDetail
OcrDetail[]
Recognized text and its coordinates
translateResult
TranslateResult[]
Translation results
OcrDetail structure:
Field
Type
Description
text
String
Recognized text
coordinate
Number[][]
Relative coordinates of the four vertices of the text bounding rectangle, in the range (0, 1)
TranslateResult structure:
Field
Type
Description
source
String
Source language of the image text
target
String
Target language
sourceText
String
Original image text
targetText
String
Translated text
- 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": "迈克尔"
}
]
}
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
-
Request URL: https://translate.ilivedata.com/api/v1/image/translate
-
HTTP request Header: Accept: application/json;charset=UTF-8
-
Request method: POST
-
Request Header
| 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, avif; 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
-
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 (~).
-
Constructs the signed string
StringToSign("\n" stands for ASCII newline character):StringToSign = HTTPMethod + "\n" + HostHeaderInLowercase + "\n" + HTTPRequestURI + "\n" + CanonicalizedQueryString <got from previous step>The
HTTPRequestURIis the absolute path to the request URI, without the request string. If theHTTPRequestURIis empty, also keep a forward slash (/) -
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)。
-
Converting the results of the previous step to a BASE64 string
-
Put the BASE64 string into the
Authorizationof HTTP request Header
-
-
Example of signature
-
Below is an example of
appId&secretKey.appId=*** secrectKey=***** -
Below is an example request.
Request Json:
{
"type": "2",
"userId": "test",
"target": "zh-CN",
"image": "T2dnUwACAAAAAAAAAAAd8pVTAAAAAGsIvpMBE..."
}
Generate CanonicalizedQueryString: b3ad8e9d16439ccd5e91924d2516bf9592975003f69beff44e56cddf47bd3118
-
Generate
StringToSignPOST\n translate.ilivedata.com\n api/v1/image/translate\n b3ad8e9d16439ccd5e91924d2516bf9592975003f69beff44e56cddf47bd3118 X-AppId:*** X-TimeStamp:2020-07-31T07:59:03Z -
Signatures from HMAC calculation
*****
- HTTP response:
Content-Type: application/json;charset=UTF-8
Response fields:
| Field | Type | Description |
|---|---|---|
| errorCode | Number | Error code; 0 indicates success |
| errorMessage | String | Error message |
| code | Number | Status: 0 = success, 1 = image download failed, 2 = invalid image format, 3 = other error |
| taskId | String | Unique translation task identifier |
| ocr | String[] | Text recognized in the image |
| ocrDetail | OcrDetail[] | Recognized text and its coordinates |
| translateResult | TranslateResult[] | Translation results |
OcrDetail structure:
| Field | Type | Description |
|---|---|---|
| text | String | Recognized text |
| coordinate | Number[][] | Relative coordinates of the four vertices of the text bounding rectangle, in the range (0, 1) |
TranslateResult structure:
| Field | Type | Description |
|---|---|---|
| source | String | Source language of the image text |
| target | String | Target language |
| sourceText | String | Original image text |
| targetText | String | Translated text |
- 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": "迈克尔"
}
]
}