图片内容提取

AI搜索开放平台支持通过SDK的方式调用图片内容提取服务。

前提条件

已开通AI搜索开放平台服务,具体操作请参见开通服务

已通过API-KEY完成身份鉴权,获取鉴权信息请参见获取API-KEY

参数说明

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
)

if __name__ == '__main__':
    # token配置,endpoint配置
    config = Config(bearer_token="替换为您的API-KEY",
                    # endpoint: 配置统一的请求入口 需要去掉http://
                    endpoint="替换API访问地址",
                    # 支持 protocol 配置 HTTPS/HTTP
                    protocol="http")
    client = Client(config=config)
    # URL模式
    document = CreateImageAnalyzeTaskRequestDocument(
        url="https://help-static-aliyun-doc.aliyuncs.com/assets/img/zh-CN/6802494071/p756843.png",
    )

    # 本地模式,需要额外指定file_name
    # file_path = "path/to/xxx.jpg"
    # document = CreateImageAnalyzeTaskRequestDocument(
    #     content=base64.b64encode(open(file_path,'rb').read()).decode(),
    #     file_name=os.path.basename(file_path)
    # )

    request = CreateImageAnalyzeTaskRequest(document=document)
    response: CreateImageAnalyzeTaskResponse = client.create_image_analyze_task(
        "default", "ops-image-analyze-ocr-001", request)
    task_id = response.body.result.task_id
    print("task_id: " + task_id)
    request = GetImageAnalyzeTaskStatusRequest(task_id=task_id)
    while True:
        # default:替换工作空间名称, ops-image-analyze-ocr-001: 服务id
        response: GetImageAnalyzeTaskStatusResponse = client.get_image_analyze_task_status(
            "default", "ops-image-analyze-ocr-001", request)
        status = response.body.result.status
        print("status: " + status)
        if status == "PENDING":
            time.sleep(5)
        elif status == "SUCCESS":
            data = response.body.result.data
            usage = response.body.usage
            print("content:\n" + data.content + "\n")
            print("usage: " + str(usage))
            break
        else:
            print("error: " + response.body.result.error)
            break