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

Before you begin

Before you call operations, make the following preparations:

Submit synchronous OCR tasks

Operation Description Supported region
ImageSyncScanRequest Sends synchronous OCR requests to detect text in images after you set the scenes parameter to ocr.
  • cn-shanghai
  • cn-beijing
  • cn-shenzhen
  • ap-southeast-1
  • ap-southeast-5
Sample code
#coding=utf-8
# The following code provides an example on how to call the synchronous operation and return the moderation result in real time:
from aliyunsdkcore import client
from aliyunsdkgreen.request.v20180509 import ImageSyncScanRequest
from aliyunsdkgreen.request.extension import HttpContentHelper
import json
import uuid

# Use the AccessKey pair of your Alibaba Cloud account.
clt = client.AcsClient("yourAccessKeyId", "yourAccessKeySecret","cn-shanghai")
# The request object cannot be reused. You must create a request object for each request.
request = ImageSyncScanRequest.ImageSyncScanRequest()
request.set_accept_format('JSON')
task = {"dataId": str(uuid.uuid1()),
         "url":"https://xxx/test.jpg"
        }

print(task)
# 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.
# In this example, a single image is moderated. If you need 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)
print(response)
result = json.loads(response)
if 200 == result["code"]:
    taskResults = result["data"]
    for taskResult in taskResults:
        if (200 == taskResult["code"]):
            sceneResults = taskResult["results"]
            print(sceneResults)