All Products
Search
Document Center

Content Moderation:OCR

Last Updated:Jul 31, 2023

This topic describes how to use Content Moderation SDK for Python to perform optical character recognition (OCR). This way, you can recognize text in images.

Prerequisites

  • Python dependencies are installed. For more information, see Installation.

    Note

    You must use the required Python version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.

  • The Extension.Uploader utility class is downloaded and imported into your project.

Submit synchronous OCR tasks

Operation

Description

Supported region

ImageSyncScanRequest

Submits synchronous OCR tasks with the scenes parameter set to ocr to recognize text in images.

  • cn-shanghai

  • cn-beijing

  • cn-shenzhen

  • ap-southeast-1

Sample code

#coding=utf-8
# The following code provides an example on how to call the ImageSyncScanRequest operation to submit synchronous OCR tasks and obtain the moderation results in real time. 
from aliyunsdkcore import client
from aliyunsdkgreen.request.v20180509 import ImageSyncScanRequest
from aliyunsdkgreen.request.extension import HttpContentHelper
import json
import uuid

# Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
# Common ways to obtain environment variables:
# Obtain the AccessKey ID of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of your RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
clt = client.AcsClient("We recommend that you obtain the AccessKey ID of your RAM user from environment variables", "We recommend that you obtain the AccessKey secret of your RAM user from environment variables", "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://example.com/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 begins when the request is initiated and ends upon moderation of the last image. 
# In most cases, 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 is extended. 
# In this example, a single image is moderated. 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)
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)