LoginSignup
15
12

More than 5 years have passed since last update.

PythonでMicrosoftの顔認識APIをさわってみたときのメモ

Last updated at Posted at 2015-11-12

公式Documentsをもとに、自分なりにコードを書いたときのメモ

※Subscription keyの入手方法については既に多くの方が記事にされていらっしゃるので、以下の方の記事などを参考に。
RでMicrosoft Face APIsを実行する

環境(執筆時点での最新版)

  • OS: Mac OS X El Capitan (10.11)
  • python: 2.7.10
  • package: projectoxford==0.1.3

やってみる

とりあえず必要なパッケージをインストール。
requestsなどでもできるがprojectoxfordのほうが簡潔に書けるのでこちらを採用!

$ pip install projectoxford

あとはsubscription keyと画像を用意して、プログラムを書く。

face_api.py
import json, requests
from projectoxford import Client


client = Client('<your_subscription_key>')
res = {
    "url": "<image_url>",
    # or
    #'path': '</local/path/to/your_image>',
    'analyzesFaceLandmarks': True,
    'analyzesAge': True,
    'analyzesGender': True,
    'analyzesHeadPose': True,
}

result = client.face.detect(res)
print json.dumps(result, indent=4)

#detectの他に、groping, identify, verify, similarなど

ちなみに結果はこんな感じ。

$ python face_api.py

[
    {
        "attributes": {
            "gender": "male", 
            "age": 35, 
            "headPose": {
                "yaw": -7.7, 
                "roll": -5.7, 
                "pitch": 0.0
            }
        }, 
        "faceId": "************************", 
        "faceRectangle": {
            "width": 99, 
            "top": 80, 
            "height": 99, 
            "left": 201
        }, 
        "faceLandmarks": {
            "underLipTop": {
                "y": 160.5, 
                "x": 250.2
            }, 
            "noseTip": {
                "y": 129.7, 
                "x": 247.1
            }, 
            "upperLipBottom": {
                "y": 149.8, 
                "x": 249.5
            }, 
            "noseLeftAlarTop": {
                "y": 124.6, 
                "x": 240.2
            }, 
            "eyebrowLeftOuter": {
                "y": 107.8, 
                "x": 208.7
            }, 
            "eyeLeftBottom": {
                "y": 112.8, 
                "x": 226.7
            }, 
            "pupilLeft": {
                "y": 110.1, 
                "x": 228.2
            }, 
            "upperLipTop": {
                "y": 145.1, 
                "x": 249.1
            }, 
            "eyeLeftInner": {
                "y": 110.3, 
                "x": 234.0
            }, 
            "eyeRightInner": {
                "y": 107.0, 
                "x": 262.8
            }, 
            "eyeLeftTop": {
                "y": 108.5, 
                "x": 227.0
            }, 
            "mouthLeft": {
                "y": 155.5, 
                "x": 233.0
            }, 
            "noseRightAlarTop": {
                "y": 122.7, 
                "x": 258.3
            }, 
            "eyebrowRightInner": {
                "y": 101.1, 
                "x": 253.9
            }, 
            "noseLeftAlarOutTip": {
                "y": 133.7, 
                "x": 234.8
            }, 
            "noseRightAlarOutTip": {
                "y": 130.4, 
                "x": 263.9
            }, 
            "noseRootRight": {
                "y": 109.6, 
                "x": 253.9
            }, 
            "eyeLeftOuter": {
                "y": 112.4, 
                "x": 219.9
            }, 
            "underLipBottom": {
                "y": 166.9, 
                "x": 251.3
            }, 
            "eyeRightTop": {
                "y": 102.8, 
                "x": 269.8
            }, 
            "eyeRightOuter": {
                "y": 105.1, 
                "x": 276.1
            }, 
            "noseRootLeft": {
                "y": 110.9, 
                "x": 240.6
            }, 
            "eyebrowRightOuter": {
                "y": 96.1, 
                "x": 286.1
            }, 
            "eyeRightBottom": {
                "y": 107.0, 
                "x": 270.2
            }, 
            "eyebrowLeftInner": {
                "y": 104.1, 
                "x": 234.6
            }, 
            "mouthRight": {
                "y": 150.6, 
                "x": 272.5
            }, 
            "pupilRight": {
                "y": 104.4, 
                "x": 270.5
            }
        }
    }
]
15
12
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
12