This topic describes how to call the synchronous image detection API to recognize text using a custom Optical Character Recognition (OCR) template. Custom OCR templates allow you to identify specific text fields in images. When you create an OCR template, you can specify the text to recognize and retrieve the results in a key-value format.
Usage notes
The /green/image/scan API performs synchronous image detection.
You can call this API to create a synchronous image detection task. For information about how to construct an HTTP request, see Request structure. You can also use an SDK to make API calls. For more information, see SDK overview.
You have created a custom template in the Content Moderation console. For more information, see Custom OCR templates.
- Billing information:
You are charged for calling this operation. For more information about the billing methods, see
- Detection timeout:
The maximum response time that is allowed for a synchronous moderation request is 6 seconds. If the moderation is not completed within 6 seconds, a timeout error is returned. If you do not require moderation results in real time, you can send asynchronous moderation requests. In most cases, we recommend that you send synchronous moderation requests because synchronous moderation operations are easier to call. We recommend that you set the timeout period to 6 seconds for calling synchronous moderation operations.
- Return result:
In general, moderation results are returned within 1 second after you send a synchronous moderation request. The time may increase in special scenarios where a large number of requests are to be processed in the system, the size of images is large, or the images contain a large number of words. The speed of OCR is inversely relevant to the number of words in the images. If the images to be moderated contain a large number of words, we recommend that you send asynchronous moderation requests.
- Image requirements:
The URLs of images must be HTTP or HTTPS URLs.
The images must be in PNG, JPG, JPEG, BMP, GIF, or WEBP format.
An image can be up to 20 MB in size. The limit for the image size is applicable to both synchronous and asynchronous moderation operations.
,高度或者宽度不能超过30,000像素(px),且图像总像素不超过2.5亿(px)
Note其中,GIF格式的图片,图像总像素不超过4,194,304(px),高度或者宽度不能超过30,000像素(px)。
The duration for downloading an image is limited to 3 seconds. If an image fails to be downloaded within 3 seconds, a timeout error is returned.
We recommend that you submit images of at least 256 × 256 pixels to ensure the moderation effect.
The response time of an operation for moderating images varies based on the duration for downloading these images. Make sure that you use a stable and reliable storage service to store the images to be moderated. We recommend that you use Object Storage Service (OSS) or Content Delivery Network (CDN).
QPS limits
You can call this operation up to 10 times per second per account. If the number of calls per second exceeds the limit, throttling is triggered. As a result, your business may be affected. We recommend that you take note of the limit when you call this operation.
Request parameters
| Name | Type | Required | Example | Description |
| bizType | String | No | default | The business scenario. You can create a business scenario in the 内容安全控制台. For more information, see Customize policies for machine-assisted moderation. |
| scenes | StringArray | Yes | ["ocr"] | The detection scenario. The only valid value is ocr. |
| tasks | JSONArray | Yes | The detection objects. Each element in the JSON array is a detection task struct. You can submit up to 100 elements to detect 100 pieces of content at a time. To submit 100 elements, you must increase the number of concurrent tasks to 100 or more. For a description of the struct for each element, see task. | |
| extras | JSONObject | Yes | {"card":"template","templateId":"xxx"} | Specifies the OCR template to apply. The format is {"card":"template","templateId":"xxx"}. For templateId, enter the ID of the template that you created in the Content Moderation console. For more information about custom OCR templates, see Custom OCR templates. |
| Name | Type | Required | Example | Description |
| dataId | String | No | est_data_xxxx | The ID of the moderation object. The ID can contain letters, digits, underscores (_), hyphens (-), and periods (.). It can be up to 128 characters in length. This ID uniquely identifies your business data. |
| url | String | Yes | https://aliyundoc.com/test_image_xxxx.png | The URL of the image to be detected. |
Returned Data
| Name | Type | Example | Description |
| code | Integer | 200 | The returned HTTP status code. For more information, see Common error codes. |
| msg | String | OK | The response message for the request. |
| dataId | String | test_data_xxxx | The ID of the moderation object. Note If you set the dataId parameter in the moderation request, the value of the dataId request parameter is returned here. |
| taskId | String | imgCjxO0DeXTC7phcds6yrEm-1q**** | The ID of the detection task. |
| url | String | http://aliyundoc.com/test_image_xxxx.png | The URL of the detection object. |
| extras | JSONObject | XXX | If you set the extras parameter in the moderation request, the value of the extras request parameter is returned here. Note This parameter may be subject to changes. Use the latest value of this parameter. |
| results | JSONArray | The results. If the call is successful (code=200), the response contains one or more elements. Each element is a struct. For a description of the struct, see result. |
| Name | Type | Example | Description |
| scene | String | ocr | The detection scenario. The only valid value is ocr. |
| label | String | ocr | The classification of the detection result. Valid values:
|
| suggestion | String | review | The recommended action for the user. Valid values:
|
| rate | Float | 99.91 | In the OCR text and image recognition scenario, you can ignore this return value. |
| customizeOcrInfo | JSONArray | The information recognized from the custom OCR template. For more information, see customizeOcrInfo. Note This field is present only if the extras request parameter is set to {"card":"template"}. |
| Name | Type | Example | Description |
| ocrInfo | Array | [{"Date of Birth":"1981.08.03"},{"Validity Period":"2012.12.12-2022.12.11"}] | Each struct in the array is a recognized key:value field. |
Examples
http(s)://[Endpoint]/green/image/scan
&<Common request parameters>
{
"scenes": [
"ocr"
],
"extras": {
"card": "template",
"templateId": "xxx"
},
"tasks": [
{
"dataId": "test_data_xxxx",
"url": "https://aliyundoc.com/test_image_xxxx.png"
}
]
}{
"msg": "OK",
"code": 200,
"data": [
{
"msg": "OK",
"code": 200,
"dataId": "test_data_xxxx",
"extras": {
},
"results": [
{
"rate": 99.91,
"suggestion": "review",
"customizeOcrInfo": {
"ocrInfo": [
{
"Date of Birth": "1981.08.03"
},
{
"Validity Period": "2012.12.12-2022.12.11"
}
]
},
"label": "ocr",
"scene": "ocr"
}
],
"taskId": "imgCjxO0DeXTC7phcds6yrEm-1q****",
"url": "http://aliyundoc.com/test_image_xxxx.png"
}
],
"requestId": "8ADA8439-4AD7-49BE-8496-2D57F7FB0387"
}