技術雑記帳兼日記帳

AWS、Python、Terraformの使い方をコッソリ

python Amazon Rekognition detect_facesその1

はじめに

Amazon Rekognitionでdetect_facesを使用してみる。
今回は書くことがないので短め

準備

下記の画像をdetect_facesで分析させる。
f:id:halhalhal1:20210422203926j:plain

下記のプログラムを用意する。

  • detect_faces.py
import boto3, sys, json

if len(sys.argv) != 2:
    exit()

client=boto3.client('rekognition')
#写真を読み出して、Byte変換してdetect_faces送信
file = open(sys.argv[1],'rb')
response = client.detect_faces(Image={'Bytes':file.read()},Attributes=['ALL'])

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

実行方法

python detect_faces.py 画像ファイル.jpg

実行結果

Detected faces for kurt-cobain-20191028-001.jpg

{
    "AgeRange": {
        "High": 34,
        "Low": 22
    },
    "Beard": {
        "Confidence": 77.95692443847656,
        "Value": true
    },
    "BoundingBox": {
        "Height": 0.23728753626346588,
        "Left": 0.36626124382019043,
        "Top": 0.07902675867080688,
        "Width": 0.10884405672550201
    },
    "Confidence": 99.9982681274414,
    "Emotions": [
        {
            "Confidence": 66.24143981933594,
            "Type": "CALM"
        },
        {
            "Confidence": 19.93850326538086,
            "Type": "CONFUSED"
        },
        {
            "Confidence": 4.765133380889893,
            "Type": "SAD"
        },
        {
            "Confidence": 3.4628701210021973,
            "Type": "SURPRISED"
        },
        {
            "Confidence": 3.388702869415283,
            "Type": "ANGRY"
        },
        {
            "Confidence": 0.9147283434867859,
            "Type": "FEAR"
        },
        {
            "Confidence": 0.8962799906730652,
            "Type": "DISGUSTED"
        },
        {
            "Confidence": 0.3923453986644745,
            "Type": "HAPPY"
        }
    ],
    "Eyeglasses": {
        "Confidence": 99.44938659667969,
        "Value": false
    },
    "EyesOpen": {
        "Confidence": 97.86561584472656,
        "Value": true
    },
    "Gender": {
        "Confidence": 99.65865325927734,
        "Value": "Male"
    },
    "Landmarks": [
        {
            "Type": "eyeLeft",
            "X": 0.39052438735961914,
            "Y": 0.16976229846477509
        },
********中略********
    ],
    "MouthOpen": {
        "Confidence": 69.85497283935547,
        "Value": false
    },
    "Mustache": {
        "Confidence": 91.35269165039062,
        "Value": false
    },
    "Pose": {
        "Pitch": 19.76197624206543,
        "Roll": 0.11792755126953125,
        "Yaw": -20.5476016998291
    },
    "Quality": {
        "Brightness": 93.4253921508789,
        "Sharpness": 86.86019134521484
    },
    "Smile": {
        "Confidence": 99.43907165527344,
        "Value": false
    },
    "Sunglasses": {
        "Confidence": 99.6850814819336,
        "Value": false
    }
}

まとめ

細かな解説は後日に。
これもBoundingBoxを付与したりしてみたい。