Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

公開日 : 2018-03-11

Python 機械学習のためのMicrosoft Azure Face API活用② Microsoft Azure Face APIに、AV女優を学習させる

このエントリーをはてなブックマークに追加
とりあえずメモで。
PythonでMicrosoft Azure Face APIを利用して、画像の学習が成功したので、とりあえず動かしてみたときのコードを置いておく。







Subscription_keyは以下のページからとってきたキーのこと。







で、動かしたときのコードを保存しておきます。
人に見られるために書いていないので、ごりょうしょうください。

import requests
import json
import logging

BASE_URL = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/"
SUBSCRIPTION_KEY  = ${subscription_key}
GROUP_NAME = ${group_name}

def makeGroup():
    end_point = BASE_URL + "persongroups/" + GROUP_NAME
    payload = {
        "name": GROUP_NAME
    }
    headers = {
        "Ocp-Apim-Subscription-Key" :SUBSCRIPTION_KEY
    }
    r = requests.put(
        end_point,
        headers = headers,
        json = payload
    )
    print (r.text)

def makePerson(name):
    end_point = BASE_URL + "persongroups/" + GROUP_NAME + "/persons"
    headers = {
        "Ocp-Apim-Subscription-Key" :SUBSCRIPTION_KEY
    }
    payload = {
        "name": name
    }
    r = requests.post(
        end_point,
        headers = headers,
        json = payload
    )
    print (r.text)

def addFaceToPerson(personId, imageUrl):
    end_point = BASE_URL + "persongroups/" + GROUP_NAME + "/persons/" + personId  + "/persistedFaces"
    print(end_point)
    headers = {
        "Ocp-Apim-Subscription-Key" :SUBSCRIPTION_KEY
    }
    payload = {
        "url": imageUrl
    }
    r = requests.post(
        end_point,
        headers = headers,
        json = payload
    )
    print(r.text)

def trainGroup(groupId):
    end_point = BASE_URL + "persongroups/" + GROUP_NAME + "/train"
    headers = {
        "Ocp-Apim-Subscription-Key" :SUBSCRIPTION_KEY
    }
    r = requests.post(
        end_point,
        headers = headers,
    )
    print(r.text)

def detectFace(imageUrl):
    end_point = BASE_URL + "detect"
    headers = {
        "Ocp-Apim-Subscription-Key" :SUBSCRIPTION_KEY
    }
    payload = {
        "url": imageUrl
    }
    r = requests.post(
        end_point,
        json = payload,
        headers = headers
    )
    print(r.text)

def identify(faceId):
    end_point = BASE_URL + "identify"
    headers = {
        "Ocp-Apim-Subscription-Key" :SUBSCRIPTION_KEY
    }
    faceIds = [faceId]
    payload = {
        "faceIds" :faceIds,
        "personGroupId" :GROUP_NAME,
        #"maxNumOfCandidatesReturned" :maxNumOfCandidatesReturned
    }
    r = requests.post(
        end_point,
        json = payload,
        headers = headers
    )
    print(r.text)

if __name__ == '__main__':
    #makeGroup()
    #makePerson("testA")#{"personId":"9ce50e03-6a82-4e78-9fae-ec620acc07cb"}
    #addFaceToPerson("9ce50e03-6a82-4e78-9fae-ec620acc07cb","https://i.pinimg.com/236x/f8/95/93/f89593bf870d459f5c0152810d8aceec.jpg") #{"persistedFaceId":"ccc4777e-3712-4ec1-9f50-4f026615c00f"}
    #trainGroup(GROUP_NAME)
    #detectFace("https://d2dcan0armyq93.cloudfront.net/photo/odai/600/e84dea0bd656bca6b6b8e2fc06244598_600.jpg") #"faceId":"0e9b4951-0337-4aa6-b386-8ed5b1f47b96"
    identify("06459343-c748-4fe2-83c1-65bc1fca08bf")






























スポンサーリンク