This topic describes how to use the Python SDK to call the optical character recognition (OCR) operation to detect text in common images and return the results in real time.


Before calling operations, make the following preparations:
  1. Create an AccessKey ID and AccessKey secret of your Alibaba Cloud account. For more information, see Create an AccessKey.
  2. Install Python dependencies. For more information, see Installation.

Submit synchronous OCR tasks

Operation Feature Region Description
ImageSyncScanRequest Sends synchronous OCR requests to detect text in images.
  • cn-shanghai: China (Shanghai)
  • cn-beijing: China (Beijing)
  • cn-shenzhen: China (Shenzhen)
  • ap-southeast-1: Singapore
  • ap-southeast-5: Indonesia (Jakarta)
This operation detects text in images after you specify ocr in the scenes parameter.
Sample code
# The OCR operation returns OCR results in real time.
from aliyunsdkcore import client
from aliyunsdkgreen.request.v20180509 import ImageSyncScanRequest
from aliyunsdkgreen.request.extension import HttpContentHelper
import json
import uuid

clt = client.AcsClient("Your AccessKey ID", "Your AccessKey secret","cn-shanghai")
request = ImageSyncScanRequest.ImageSyncScanRequest()
task = {"dataId": str(uuid.uuid1()),

# Create one task for each image to be moderated.
# If you moderate multiple images in a request, the total response time that the server spends processing the request starts from when the request is initiated to when the last image is moderated.
# Generally, the average response time of moderating multiple images in a request is longer than that of moderating a single image. The more images you submit at a time, the higher the probability that the average response time will be extended.
# The sample code uses a single image as an example. If you want to moderate multiple images at a time, create one task for each image to be moderated.
# The OCR expense equals the product of the number of images moderated and the moderation unit price.
request.set_content(HttpContentHelper.toValue({"tasks": [task],
                                               "scenes": ["ocr"]
response = clt.do_action_with_exception(request)
result = json.loads(response)
if 200 == result["code"]:
    taskResults = result["data"]
    for taskResult in taskResults:
        if (200 == taskResult["code"]):
            sceneResults = taskResult["results"]