天天看點

智能媒體管理(IMM) Python SDK DEMOSDK For Python安裝 Python 環境安裝 Imm Service Python SDK使用Python SDK

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分組

建立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"
}           

繼續閱讀