AI Earth ——开发者模式案例9:OpenAPI调用AI识别能力

OpenAPI 调用 AI 识别能力¶

初始化环境¶

阿里云账号的 AccessKey ID 和 AccessKey Secret 可以从 AccessKey 管理 获取。

AccessKey ID 和 AccessKey Secret 属于您的个人敏感信息,请妥善保管,谨防泄露。

import time
from Tea.exceptions import TeaException
from alibabacloud_tea_openapi import models
from alibabacloud_aiearth_engine20220609.models import *
from alibabacloud_aiearth_engine20220609.client import Client

config = models.Config(
    # 您的AccessKey ID,
    access_key_id='*请替换*',
    # 您的AccessKey Secret,
    access_key_secret='*请替换*',
    # 地域ID
    region_id='cn-hangzhou',
    # 访问的域名
    endpoint='aiearth-engine.cn-hangzhou.aliyuncs.com'
)

client = Client(config)

提交AI解译任务¶

data_id 的获取,可以通过 项目数据 的引用或 我的数据 的引用获取。

支持的AI解译任务类型(即 app 参数)、置信度阈值等,请参考 OpenAPI 指南

try:
    createAIJobRequest = CreateAIJobRequest()
    createAIJobRequest.job_name = '*请替换*'
    createAIJobRequest.app = '*请替换*'
    createAIJobRequest.area_threshold = 0
    createAIJobRequest.confidence = 10
    createAIJobRequestInputs = CreateAIJobRequestInputs()
    createAIJobRequestInputs.idx = 1
    createAIJobRequestInputsSrc = CreateAIJobRequestInputsSrc()
    createAIJobRequestInputsSrc.data_id = '*请替换*'
    createAIJobRequestInputs.src = createAIJobRequestInputsSrc
    createAIJobRequest.inputs = [createAIJobRequestInputs]
    aijob: CreateAIJobResponse = client.create_aijob(createAIJobRequest)
    print(aijob.body)
    jobId = aijob.body.jobs[0].job_id
except TeaException as e:
    # 打印整体的错误输出
    print(e)
    # 打印错误码
    print(e.code)
    # 打印错误信息,错误信息中包含
    print(e.message)
    # 打印服务端返回的具体错误内容
    print(e.data)

查询 AI 解译任务¶

try:
    getJobsRequest = GetJobsRequest()
    getJobsRequest.job_ids = [jobId]
    jobs: GetJobsResponse = client.get_jobs(getJobsRequest)
    print(jobs.body)
except TeaException as e:
    # 打印整体的错误输出
    print(e)
    # 打印错误码
    print(e.code)
    # 打印错误信息,错误信息中包含
    print(e.message)
    # 打印服务端返回的具体错误内容
    print(e.data)

停止 AI 解译任务¶

try:
    deleteJobsRequest = DeleteJobsRequest()
    deleteJobsRequest.job_ids = [jobId]
    del_jobs: DeleteJobsResponse = client.delete_jobs(deleteJobsRequest)
    print(del_jobs.body)
except TeaException as e:
    # 打印整体的错误输出
    print(e)
    # 打印错误码
    print(e.code)
    # 打印错误信息,错误信息中包含
    print(e.message)
    # 打印服务端返回的具体错误内容
    print(e.data)

你可能感兴趣的:(AI,Earth,人工智能,ai,openapi,api,python)