智能媒体管理(IMM) Python SDK DEMO

SDK For Python

本文以Python为示例

安装 Python 环境

IMM Service 的Python SDK目前只支持 Python 2.6.x, 2.7.x。

请按以下步骤安装Python:

完成Python安装后,运行python -V确认Python版本。

  • 下载并安装 Python 的包管理工具 pip。

完成 pip 安装后,你可以运行pip -V确认 pip 是否安装成功和查看当前 pip 版本。

安装 Imm Service Python SDK

  • 使用 pip 安装(推荐)

    pip install aliyun-python-sdk-core # 安装阿里云 SDK 核心库
    pip install aliyun-python-sdk-imm # 安装管理 IMM 的库
  • 下载 GithHub 源码

    git clone https://github.com/aliyun/aliyun-openapi-python-sdk.git
    # 安装阿里云 SDK 核心库
    cd aliyun-python-sdk-core
    python setup.py install
    # 安装阿里云 IMM SDK
    cd aliyun-python-sdk-imm
    python setup.py install

使用Python SDK

初始化客户端

使用之前需要,您需要先获取 Region ID、AccessKey ID 和 AccessKey Secret。

# -*- coding: utf8 -*-
import json
import time

from aliyunsdkcore.client import AcsClient

client = AcsClient(
   "<your-access-key-id>", 
   "<your-access-key-secret>",
   "<your-region-id>"
)

创建Project

根据您的需求,选择Project类型。本示例目的是展示关键参数,详情参考Project文档

注意事项:

  • 测试时Project的BillingType请设置为ByUsage方式,本文所有示例代码BillingType均使用ByUsage。
  • 不能创建名称相同的Project。

代码示例

from aliyunsdkimm.request.v20170906 import PutProjectRequest

project = "python-sdk-demo-doc" #设置Project的名字
project_type = "DocStarter"  #图片标准型
project_cu  = 1
project_billing_type = 'ByUsage'

createReq = PutProjectRequest.PutProjectRequest()
createReq.set_Project(project) 
createReq.set_Type(project_type)
createReq.set_Type(project_cu)
createReq.set_BillingType(project_billing_type)

response = client.do_action_with_exception(createReq)

print response

输出结果

{
  "CU": 1 ,
  "Type": "DocStarter",
  "CreateTime": "2018-10-30T03:11:23Z",
  "RequestId": "4F048F9F-D622-4B96-9286-AEA234BF1480",
  "ModifyTime": "2018-10-30T03:11:23Z",
  "Project": "python-sdk-demo-doc",
  "BillingType": "ByUsage",
  "Endpoint": "imm.cn-beijing.aliyuncs.com",
  "ServiceRole": ""
}

创建具体的服务

文档管理操作

文档同步转换

本示例目的是展示关键参数,其余参数具体请参考ConvertOfficeFormat

代码示例

from aliyunsdkimm.request.v20170906 import ConvertOfficeFormatRequest

project = "python-sdk-demo-doc" #演示用project已创建,您需要自己创建
sync_srcUri = "oss://co-user-cn-beijing/zqh/input/WORD/5页Word.docx" #您文档资源的位置
sync_tgtUri = "oss://co-user-cn-beijing/zqh/output/PDF/" #您要输出的位置
tgt_type = "png"   #转化类型

createReq = ConvertOfficeFormatRequest.ConvertOfficeFormatRequest()
createReq.set_Project(project)
createReq.set_SrcUri(sync_srcUri)
createReq.set_TgtUri(sync_tgtUri )
createReq.set_TgtType(tgt_type)

response = client.do_action_with_exception(createReq)

print response

输出结果

{
  "PageCount": 5,
  "RequestId": "84302ED0-4823-4623-8AED-6E4129DA9733"
}

文档异步转换

创建转换任务

本示例目的是展示关键参数,其余参数具体请参考CreateOfficeConversionTask

代码示例

from aliyunsdkimm.request.v20170906 import CreateOfficeConversionTaskRequest

project = "python-sdk-demo-doc" #演示用project已创建,您需要自己创建
async_srcUri = "oss://co-user-cn-beijing/zqh/input/WORD/5页Word.docx" #您文档资源的位置
async_tgtUri = "oss://co-user-cn-beijing/zqh/output/PDF/" #您要输出的位置
tgt_type = "png"   #转化类型

createReq = CreateOfficeConversionTaskRequest.CreateOfficeConversionTaskRequest()
createReq.set_Project(project)
createReq.set_SrcUri(async_srcUri)
createReq.set_TgtUri(async_tgtUri )
createReq.set_TgtType(tgt_type)

response = client.do_action_with_exception(createReq)

print response

输出结果

{
  "Status": "Running",
  "CreateTime": "2018-10-30T03:49:06.345Z",
  "RequestId": "D1BA4308-CA2F-4F93-B3C6-B30188855138",
  "Percent": 0,
  "TaskId": "2ef08a22-6cff-4204-a1f5-38473d66596b",
  "TgtLoc": "oss://co-user-cn-beijing/zqh/input/WORD/5页Word.docx"
}
获取转换状态

本示例目的是展示关键参数,其余参数具体请参考GetOfficeConversionTask

代码示例

from aliyunsdkimm.request.v20170906 import GetOfficeConversionTaskRequest

project = "python-sdk-demo-doc" #演示用project已创建,您需要自己创建
task_id = "2ef08a22-6cff-4204-a1f5-38473d66596b"

createReq = GetOfficeConversionTaskRequest.GetOfficeConversionTaskRequest()
createReq.set_Project(project)
createReq.set_TaskId(task_id )

输出结果

{
  "FailDetail": {
    "Code": "NoError"
  },
  "NotifyTopicName": "",
  "TaskId": "2ef08a22-6cff-4204-a1f5-38473d66596b",
  "NotifyEndpoint": "",
  "PageCount": 5,
  "Status": "Finished",
  "TgtType": "png",
  "FinishTime": "2018-10-30T03:49:07.651Z",
  "RequestId": "D200F0EF-F3E7-4A18-B2A7-E7BBBA8E10EC",
  "CreateTime": "2018-10-30T03:49:06.345Z",
  "SrcUri": "oss://co-user-cn-beijing/zqh/input/WORD/5页Word.docx",
  "Percent": 100,
  "TgtUri": "oss://co-user-cn-beijing/zqh/input/WORD/5页Word.docx"
}

图片管理操作

Face检测

本示例目的是展示关键参数,其余参数具体请参考DetectFace

代码示例

from aliyunsdkimm.request.v20170906 import DetectFaceRequest

project = "python-sdk-demo-photo" #演示用project已创建,您需要自己创建
srcUri = '["oss://co-user-cn-beijing/zqh/input/FaceGroup/500/0.bmp"]' #您图片资源的位置

createReq = DetectFaceRequest.DetectFaceRequest()
createReq.set_Project(project)
createReq.set_SrcUris(srcUri)

response = client.do_action_with_exception(createReq)

print response

输出结果

{
  "SrcUris": [
    "oss://co-user-cn-beijing/zqh/input/FaceGroup/500/0.bmp"
  ],
  "RequestId": "21404A97-8625-44DB-8C7A-E7F33A9EDEF4",
  "SuccessDetails": [
    {
      "SrcUri": "oss://co-user-cn-beijing/zqh/input/FaceGroup/500/0.bmp",
      "Faces": [
        {
          "FaceAttribute": {
            "Blur": {
              "Blurness": {
                "Value": 12.217,
                "Threshold": 50
              }
            },
            "FaceQuality": {
              "Value": 100,
              "Threshold": 70.1
            },
            "HeadPose": {
              "RollAngle": 2.934,
              "PitchAngle": 10.529,
              "YawAngle": -3.145
            },
            "Age": {
              "Value": 21
            },
            "EyeStatus": {
              "RightEyeStatus": {
                "DarkGlasses": 0,
                "NoGlassEyeClose": 0.001,
                "Occlusion": 0.049,
                "NormalGlassEyeOpen": 0.106,
                "NormalGlassEyeClose": 0.001,
                "NoGlassEyeOpen": 99.842
              },
              "LeftEyeStatus": {
                "DarkGlasses": 0.014,
                "NoGlassEyeClose": 0.006,
                "Occlusion": 0.504,
                "NormalGlassEyeOpen": 4.954,
                "NormalGlassEyeClose": 0.004,
                "NoGlassEyeOpen": 94.517
              }
            },
            "Gender": {
              "Value": "Male"
            }
          },
          "FaceRectangle": {
            "Top": 17,
            "Height": 89,
            "Width": 89,
            "Left": 4
          },
          "FaceId": "66690675bd0bc2599170fc63e7f8dbd8"
        }
      ],
      "PhotoId": "77f6e9ce630b83956192e8bd4af3cf39"
    }
  ],
  "FailDetails": []
}

Tag检测

本示例目的是展示关键参数,其余参数具体请参考DetectTag

代码示例

from aliyunsdkimm.request.v20170906 import DetectTagRequest

project = "python-sdk-demo-photo" #演示用project已创建,您需要自己创建
srcUri = '["oss://co-user-cn-beijing/zqh/input/TagSet/baseball.jpg"]' #您图片资源的位置

createReq = DetectTagRequest.DetectTagRequest()
createReq.set_Project(project)
createReq.set_SrcUris(srcUri)

response = client.do_action_with_exception(createReq)

print response

输出结果

{
  "SuccessNum": "1",
  "RequestId": "6A3188E0-BC89-4AD5-9348-851F37F5AF95",
  "SuccessDetails": [
    {
      "Tags": [
        {
          "ParentTagId": "11",
          "TagScore": "0.8965469",
          "TagLevel": "2",
          "TagId": "0",
          "ParentTagName": "运动",
          "TagName": "其他"
        },
        {
          "ParentTagId": "10",
          "TagScore": "0.7168899",
          "TagLevel": "2",
          "TagId": "655",
          "ParentTagName": "其他",
          "TagName": "棒球守备位置"
        },
        {
          "ParentTagId": "11",
          "TagScore": "0.55166656",
          "TagLevel": "2",
          "TagId": "50",
          "ParentTagName": "运动",
          "TagName": "球赛"
        },
        {
          "ParentTagId": "10",
          "TagScore": "0.53409994",
          "TagLevel": "2",
          "TagId": "940",
          "ParentTagName": "其他",
          "TagName": "场地"
        },
        {
          "ParentTagId": "0",
          "TagScore": "0.5216745",
          "TagLevel": "2",
          "TagId": "159",
          "ParentTagName": "人物",
          "TagName": "比赛者"
        },
        {
          "ParentTagId": "0",
          "TagScore": "0.5185509",
          "TagLevel": "2",
          "TagId": "65",
          "ParentTagName": "人物",
          "TagName": "足球运动员"
        },
        {
          "ParentTagId": "0",
          "TagScore": "0.5162505",
          "TagLevel": "2",
          "TagId": "54",
          "ParentTagName": "人物",
          "TagName": "运动员"
        },
        {
          "ParentTagId": "0",
          "TagScore": "0.5147973",
          "TagLevel": "2",
          "TagId": "133",
          "ParentTagName": "人物",
          "TagName": "棒球运动员"
        },
        {
          "ParentTagId": "10",
          "TagScore": "0.5040618",
          "TagLevel": "2",
          "TagId": "453",
          "ParentTagName": "其他",
          "TagName": "球游戏"
        }
      ],
      "SrcUri": "oss://co-user-cn-beijing/zqh/input/TagSet/baseball.jpg"
    }
  ],
  "FailDetails": []
}

QRCodes检测

本示例目的是展示关键参数,其余参数具体请参考DetectQRCodes

代码示例

from aliyunsdkimm.request.v20170906 import DetectQRCodesRequest

project = "python-sdk-demo-photo" #演示用project已创建,您需要自己创建
srcUri = '["oss://co-user-cn-beijing/zqh/input/TagSet/qrcode.png"]' #您图片资源的位置

createReq = DetectQRCodesRequest.DetectQRCodesRequest()
createReq.set_Project(project)
createReq.set_SrcUris(srcUri)

response = client.do_action_with_exception(createReq)

print response

运行结果

{
  "RequestId": "21B97DEA-A3A9-4473-90E5-D527CC0F7287",
  "SuccessDetails": [
    {
      "SrcUri": "oss://co-user-cn-beijing/zqh/input/qrcode.png",
      "QRCodes": [
        {
          "QRCodesRectangle": {
            "Top": 11,
            "Height": 280,
            "Width": 280,
            "Left": 11
          },
          "Content": "二维码又称二维条码,常见的二维码为QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据类型。"
        }
      ]
    }
  ],
  "FailDetails": []
}

Face分组

分组需要Project为PhotoProfessional类型。

创建FaceSet

代码示例

from aliyunsdkimm.request.v20170906 import CreateFaceSetRequest

project = "python-sdk-demo-professional"  # 演示用project已创建,您需要自己创建

createReq = CreateFaceSetRequest.CreateFaceSetRequest()
createReq.set_Project(project)

response = client.do_action_with_exception(createReq)

print response

运行结果

{
  "Status": "Running",
  "Photos": 0,
  "RequestId": "006AF2E5-B1E9-4A8A-89F1-E4FE229C57AE",
  "CreateTime": "2018-10-30T06:58:35.556Z",
  "Faces": 0,
  "ModifyTime": "2018-10-30T06:58:35.556Z",
  "SetId": "FACE-99a292da-32e3-49cb-aa35-e6a01604a744"
}
进行IndexFace人脸检测

本示例目的是展示关键参数,其余参数具体请参考IndexFace

代码示例

from aliyunsdkimm.request.v20170906 import IndexFaceRequest

project = "python-sdk-demo-professional" #演示用project已创建,您需要自己创建
srcUri = '["oss://co-user-cn-beijing/zqh/input/FaceGroup/500/0.bmp"]' #您图片资源的位置

createReq = IndexFaceRequest.IndexFaceRequest()
createReq.set_Project(project)
createReq.set_SrcUris(srcUri)

response = client.do_action_with_exception(createReq)

print response

运行结果

{
  "SrcUris": [
    "oss://co-user-cn-beijing/zqh/input/FaceGroup/500/0.bmp"
  ],
  "RequestId": "14888AFD-E6D5-418D-B629-4E23A89C7022",
  "SuccessDetails": [
    {
      "SrcUri": "oss://co-user-cn-beijing/zqh/input/FaceGroup/500/0.bmp",
      "Faces": [
        {
          "FaceAttribute": {
            "Blur": {
              "Blurness": {
                "Value": 12.217,
                "Threshold": 50
              }
            },
            "FaceQuality": {
              "Value": 100,
              "Threshold": 70.1
            },
            "HeadPose": {
              "RollAngle": 2.934,
              "PitchAngle": 10.529,
              "YawAngle": -3.145
            },
            "Age": {
              "Value": 21
            },
            "EyeStatus": {
              "RightEyeStatus": {
                "DarkGlasses": 0,
                "NoGlassEyeClose": 0.001,
                "Occlusion": 0.049,
                "NormalGlassEyeOpen": 0.106,
                "NormalGlassEyeClose": 0.001,
                "NoGlassEyeOpen": 99.842
              },
              "LeftEyeStatus": {
                "DarkGlasses": 0.014,
                "NoGlassEyeClose": 0.006,
                "Occlusion": 0.504,
                "NormalGlassEyeOpen": 4.954,
                "NormalGlassEyeClose": 0.004,
                "NoGlassEyeOpen": 94.517
              }
            },
            "Gender": {
              "Value": "Male"
            }
          },
          "FaceRectangle": {
            "Top": 17,
            "Height": 89,
            "Width": 89,
            "Left": 4
          },
          "FaceId": "66690675bd0bc2599170fc63e7f8dbd8"
        }
      ],
      "PhotoId": "77f6e9ce630b83956192e8bd4af3cf39"
    }
  ],
  "FailDetails": [],
  "SetId": "FACE-99a292da-32e3-49cb-aa35-e6a01604a744"
}
进行GroupFace人脸分组

分组需要多张图片,请执行两次或以上IndexFace操作。

本示例目的是展示关键参数,其余参数具体请参考GroupFace

代码示例

from aliyunsdkimm.request.v20170906 import GroupFacesRequest


project = "python-sdk-demo-professional"
set_id = "FACE-99a292da-32e3-49cb-aa35-e6a01604a744"

createReq = GroupFacesRequest.GroupFacesRequest()
createReq.set_Project(project)
createReq.set_SetId(set_id)

response = client.do_action_with_exception(createReq)

print response

运行结果

{
  "Groups": [
    {
      "UnGroupReason": "FaceNoSimilar",
      "GroupId": "0",
      "FaceId": "fffbf28d706c2230ba29a1b2ee271744"
    },
    {
      "UnGroupReason": "FaceNoSimilar",
      "GroupId": "0",
      "FaceId": "5c1212c756047570ffd0839386b5c2bd"
    },
    {
      "UnGroupReason": "FaceNoSimilar",
      "GroupId": "0",
      "FaceId": "66690675bd0bc2599170fc63e7f8dbd8"
    },
    {
      "UnGroupReason": "FaceNoSimilar",
      "GroupId": "0",
      "FaceId": "dadddf6bdd41d02aadc9fc4f2b54ca2b"
    }
  ],
  "RequestId": "AC452D2A-F2B2-48AD-83B4-A6E03D38066F",
  "HasMore": 0,
  "SetId": "FACE-99a292da-32e3-49cb-aa35-e6a01604a744"
}

Tag分组

分组需要Project为PhotoProfessional类型。

创建TagSet

代码示例

from aliyunsdkimm.request.v20170906 import CreateTagSetRequest

project = "python-sdk-demo-professional"  # 演示用project已创建,您需要自己创建

createReq = CreateTagSetRequest.CreateTagSetRequest()
createReq.set_Project(project)

response = client.do_action_with_exception(createReq)

print response

运行结果

{
  "Status": "Running",
  "Photos": 0,
  "RequestId": "FD93E872-1437-4A78-8C15-CBD83B9CE152",
  "CreateTime": "2018-10-30T07:33:15.365Z",
  "ModifyTime": "2018-10-30T07:33:15.365Z",
  "SetId": "TAG-9d79cc61-0f2b-4d4d-931a-377bcc641878"
}
进行IndexTag图片检测

本示例目的是展示关键参数,其余参数具体请参考IndexTag

代码示例

from aliyunsdkimm.request.v20170906 import IndexTagRequest

project = "python-sdk-demo-professional" #演示用project已创建,您需要自己创建
srcUri = '["oss://co-user-cn-beijing/zqh/input/FaceGroup/500/0.bmp"]' #您图片资源的位置
set_id = 'TAG-9d79cc61-0f2b-4d4d-931a-377bcc641878'

createReq = IndexTagRequest.IndexTagRequest()
createReq.set_Project(project)
createReq.set_SrcUris(srcUri)
createReq.set_SetId(set_id )

response = client.do_action_with_exception(createReq)

print response

运行结果

{
  "RequestId": "F296C6E7-2213-4934-910B-0436B9F1C71F",
  "SuccessDetails": [
    {
      "Tags": [
        {
          "ParentTagId": "11",
          "TagScore": "0.8965469",
          "TagLevel": "2",
          "TagId": "0",
          "ParentTagName": "运动",
          "TagName": "其他"
        },
        {
          "ParentTagId": "10",
          "TagScore": "0.7168899",
          "TagLevel": "2",
          "TagId": "655",
          "ParentTagName": "其他",
          "TagName": "棒球守备位置"
        },
        {
          "ParentTagId": "11",
          "TagScore": "0.55166656",
          "TagLevel": "2",
          "TagId": "50",
          "ParentTagName": "运动",
          "TagName": "球赛"
        },
        {
          "ParentTagId": "10",
          "TagScore": "0.53409994",
          "TagLevel": "2",
          "TagId": "940",
          "ParentTagName": "其他",
          "TagName": "场地"
        },
        {
          "ParentTagId": "0",
          "TagScore": "0.5216745",
          "TagLevel": "2",
          "TagId": "159",
          "ParentTagName": "人物",
          "TagName": "比赛者"
        },
        {
          "ParentTagId": "0",
          "TagScore": "0.5185509",
          "TagLevel": "2",
          "TagId": "65",
          "ParentTagName": "人物",
          "TagName": "足球运动员"
        },
        {
          "ParentTagId": "0",
          "TagScore": "0.5162505",
          "TagLevel": "2",
          "TagId": "54",
          "ParentTagName": "人物",
          "TagName": "运动员"
        },
        {
          "ParentTagId": "0",
          "TagScore": "0.5147973",
          "TagLevel": "2",
          "TagId": "133",
          "ParentTagName": "人物",
          "TagName": "棒球运动员"
        },
        {
          "ParentTagId": "10",
          "TagScore": "0.5040618",
          "TagLevel": "2",
          "TagId": "453",
          "ParentTagName": "其他",
          "TagName": "球游戏"
        }
      ],
      "SrcUri": "oss://co-user-cn-beijing/zqh/input/TagSet/baseball.jpg"
    }
  ],
  "FailDetails": [],
  "SetId": "TAG-9d79cc61-0f2b-4d4d-931a-377bcc641878",
  "SuccessIndexNum": "1"
}
列出数据集检测出的标签。

本示例目的是展示关键参数,其余参数具体请参考ListTagNames

代码示例

from aliyunsdkimm.request.v20170906 import ListTagNamesRequest

project = "python-sdk-demo-professional" #演示用project已创建,您需要自己创建
set_id = 'TAG-9d79cc61-0f2b-4d4d-931a-377bcc641878'

createReq = ListTagNamesRequest.ListTagNamesRequest()
createReq.set_Project(project)
createReq.set_SetId(set_id )

response = client.do_action_with_exception(createReq)

print response

运行结果

{
  "Tags": [
    {
      "Num": 1,
      "TagName": "场地"
    },
    {
      "Num": 1,
      "TagName": "女人"
    },
    {
      "Num": 1,
      "TagName": "晚餐"
    },
    {
      "Num": 1,
      "TagName": "棒球守备位置"
    },
    {
      "Num": 1,
      "TagName": "棒球运动员"
    },
    {
      "Num": 2,
      "TagName": "其他"
    },
    {
      "Num": 1,
      "TagName": "八宝饭"
    },
    {
      "Num": 1,
      "TagName": "午餐"
    },
    {
      "Num": 1,
      "TagName": "比赛者"
    },
    {
      "Num": 1,
      "TagName": "人"
    }
  ],
  "RequestId": "D59C1305-F01A-46DF-8E66-22A334F43F35"
}
上一篇:2016最新Java笔试题集锦


下一篇:Android官方开发文档Training系列课程中文版:OpenGL绘图之图形绘制