Image content extraction

更新时间:
复制 MD 格式

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:

How it works

Image content extraction uses an asynchronous task model:

  1. 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.

  2. Receive a task_id from the response.

  3. Poll get_image_analyze_task_status with the task_id until the task completes.

  4. Read the extracted text from result.data.content in the final response.

Task status values:

StatusMeaningAction
PENDINGTask is still processingWait 5 seconds, then poll again
SUCCESSTask completedRead result.data.content and usage
OtherTask failedRead 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:

PlaceholderDescriptionExample
<your-api-key>Your API keyyour-api-key
<your-endpoint>Your API endpoint (without http://)your-instance.alicloudapi.com
<your-workspace>Your workspace namedefault
<your-service-id>Your image analysis service IDops-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)
        break

Local 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)
        break

What's next