Pythonとboto3を使ってAWS Rekognitionで画像から顔認識する

AWS CLIを使う方法もありますが今回はPythonとboto3を用いて画像を認識させました。

#detect-face.py

import boto3
import json

if __name__ == "__main__":
    photo='{image_name}'
    bucket='{bucket_name}'
    client=boto3.client('rekognition')

    response = client.detect_faces(Image={'S3Object':{'Bucket':bucket,'Name':photo}},Attributes=['ALL'])

    print('Detected faces for ' + photo)
    for faceDetail in response['FaceDetails']:
       print(json.dumps(faceDetail, indent=4, sort_keys=True))

というファイルを作成し、

python detect-face.py

を実行すると

{
    "FaceDetails": [
        {
            "BoundingBox": {
                "Width": 0.17604286968708038, 
                "Top": 0.24878467619419098, 
                "Left": -0.02678913064301014, 
                "Height": 0.2630826532840729
            }, 
            "Landmarks": [
                {
                    "Y": 0.33396294713020325, 
                    "X": 0.047480691224336624, 
                    "Type": "eyeLeft"
                }, 
                {
                    "Y": 0.37172257900238037, 
                    "X": 0.09598125517368317, 
                    "Type": "eyeRight"
                }, 
                {
                    "Y": 0.3975467383861542, 
                    "X": 0.07194872200489044, 
                    "Type": "nose"
                }, 
                {
                    "Y": 0.42218661308288574, 
                    "X": 0.020532911643385887, 
                    "Type": "mouthLeft"
                }, 
                {
                    "Y": 0.45728546380996704, 
                    "X": 0.07432971894741058, 
                    "Type": "mouthRight"
                }
            ], 
            "Pose": {
                "Yaw": 19.644346237182617, 
                "Roll": 25.670103073120117, 
                "Pitch": 11.709482192993164
            }, 
            "Quality": {
                "Sharpness": 99.68138885498047, 
                "Brightness": 41.849143981933594
            }, 
            "Confidence": 99.99942016601562
        }, 
...

このように解析を行うことができます。