Use the AI Search Open Platform SDK to extract text and content from images. The service processes images asynchronously — you submit a task, then poll for the result.
Prerequisites
Before you begin, make sure you have:
An activated AI Search Open Platform — see Activate AI Search Open Platform
An API key for authentication — see Manage API key
How it works
Image content extraction uses an asynchronous task model:
Submit an image to the service by calling
create_image_analyze_task. The image can be a publicly accessible URL or a base64-encoded local file.Receive a
task_idfrom the response.Poll
get_image_analyze_task_statuswith thetask_iduntil the task completes.Read the extracted text from
result.data.contentin the final response.
Task status values:
| Status | Meaning | Action |
|---|---|---|
PENDING | Task is still processing | Wait 5 seconds, then poll again |
SUCCESS | Task completed | Read result.data.content and usage |
| Other | Task failed | Read result.error for the error details |
The request body cannot exceed 8 MB. For full parameter details, see Image content extraction API.
Extract image content
The following examples use the Python SDK (alibabacloud_searchplat20240529). Both examples call create_image_analyze_task to submit the task and get_image_analyze_task_status to retrieve the result.
Replace the placeholder values before running:
| Placeholder | Description | Example |
|---|---|---|
<your-api-key> | Your API key | your-api-key |
<your-endpoint> | Your API endpoint (without http://) | your-instance.alicloudapi.com |
<your-workspace> | Your workspace name | default |
<your-service-id> | Your image analysis service ID | ops-image-analyze-ocr-001 |
URL mode
Use URL mode when the image is hosted at a publicly accessible URL.
import time
from alibabacloud_tea_openapi.models import Config
from alibabacloud_searchplat20240529.client import Client
from alibabacloud_searchplat20240529.models import (
CreateImageAnalyzeTaskRequestDocument,
CreateImageAnalyzeTaskRequest,
CreateImageAnalyzeTaskResponse,
GetImageAnalyzeTaskStatusRequest,
GetImageAnalyzeTaskStatusResponse,
)
# Configure the client
config = Config(
bearer_token="<your-api-key>",
endpoint="<your-endpoint>", # Do not include http://
protocol="http",
)
client = Client(config=config)
# Submit the image by URL
document = CreateImageAnalyzeTaskRequestDocument(
url="https://help-static-aliyun-doc.aliyuncs.com/assets/img/zh-CN/6802494071/p756843.png",
)
request = CreateImageAnalyzeTaskRequest(document=document)
response: CreateImageAnalyzeTaskResponse = client.create_image_analyze_task(
"<your-workspace>", "<your-service-id>", request
)
task_id = response.body.result.task_id
print("task_id:", task_id)
# Poll until complete
poll_request = GetImageAnalyzeTaskStatusRequest(task_id=task_id)
while True:
response: GetImageAnalyzeTaskStatusResponse = client.get_image_analyze_task_status(
"<your-workspace>", "<your-service-id>", poll_request
)
status = response.body.result.status
print("status:", status)
if status == "PENDING":
time.sleep(5)
elif status == "SUCCESS":
print("content:\n" + response.body.result.data.content)
print("usage:", response.body.usage)
break
else:
print("error:", response.body.result.error)
breakLocal mode
Use local mode when the image is on your local filesystem. The file is base64-encoded and sent as part of the request body.
import base64
import os
import time
from alibabacloud_tea_openapi.models import Config
from alibabacloud_searchplat20240529.client import Client
from alibabacloud_searchplat20240529.models import (
CreateImageAnalyzeTaskRequestDocument,
CreateImageAnalyzeTaskRequest,
CreateImageAnalyzeTaskResponse,
GetImageAnalyzeTaskStatusRequest,
GetImageAnalyzeTaskStatusResponse,
)
# Configure the client
config = Config(
bearer_token="<your-api-key>",
endpoint="<your-endpoint>", # Do not include http://
protocol="http",
)
client = Client(config=config)
# Submit the image from a local file
file_path = "path/to/your/image.jpg"
with open(file_path, "rb") as f:
encoded = base64.b64encode(f.read()).decode()
document = CreateImageAnalyzeTaskRequestDocument(
content=encoded,
file_name=os.path.basename(file_path),
)
request = CreateImageAnalyzeTaskRequest(document=document)
response: CreateImageAnalyzeTaskResponse = client.create_image_analyze_task(
"<your-workspace>", "<your-service-id>", request
)
task_id = response.body.result.task_id
print("task_id:", task_id)
# Poll until complete
poll_request = GetImageAnalyzeTaskStatusRequest(task_id=task_id)
while True:
response: GetImageAnalyzeTaskStatusResponse = client.get_image_analyze_task_status(
"<your-workspace>", "<your-service-id>", poll_request
)
status = response.body.result.status
print("status:", status)
if status == "PENDING":
time.sleep(5)
elif status == "SUCCESS":
print("content:\n" + response.body.result.data.content)
print("usage:", response.body.usage)
break
else:
print("error:", response.body.result.error)
breakWhat's next
Image content extraction API reference — full list of request parameters and response fields
Manage API key — create, rotate, or revoke your API keys