[AWS]AWS Rekognition using Python臉部特徵擷取

AWS Rekognition可以辨識照片中的物件、場景及臉部特徵,臉部特徵包含性別、是否有戴眼鏡、眼睛及嘴巴的位置座標。這次將使用的是偵測臉部特徵的API。

環境:
IDE: PyCharm, python 2.7
OS: mac OS Sierra

步驟:

  1. 註冊Amazon帳號:https://aws.amazon.com/tw/
  2. 建立一個IAM User並取得Access Key(基於安全上的考量,不使用Root建立Access Key):
    1. 登入AWS
    2. 右上角Account name
    3. My Security Credentials
    4. User
    5. Add User
    6. 紀錄Access Key(此Key絕對不能公開)
  3. 安裝boto3(boto3是給python的AWS SDK):
    sudo pip install boto3
  4. 建立一個python檔並貼上以下程式碼並將英文大寫部分更改為自己的:
  5. import boto3
    
    client = boto3.client(
       'rekognition',
       aws_access_key_id = 'YOUR_ACCESS_KEY',
       aws_secret_access_key = 'YOUR_SECRET_ACCESS_KEY',
       region_name = 'YOUR_REGION_NAME'
    )
    
    p = open("IMAGE_PATH", 'rb')
    
    face_features = client.detect_faces(Image={
       'Bytes':bytearray(p.read())
       }, Attributes=['ALL']
    )
    
    p.close() 
    
    print face_features
  6. 執行之後就會傳回辨識結果囉!大概長得像這樣:
  7. {  
       u'FaceDetails':[  
          {  
             u'Confidence':99.90534210205078,
             u'Eyeglasses':{  
                u'Confidence':99.999267578125,
                u'Value':False
             },
             u'Sunglasses':{  
                u'Confidence':99.97903442382812,
                u'Value':False
             },
             u'Gender':{  
                u'Confidence':100.0,
                u'Value':u'Female'
             },
             u'Landmarks':[  
                {  
                   u'Y':0.29652801156044006,
                   u'X':0.4198256731033325,
                   u'Type':u'eyeLeft'
                },
                {  
                   u'Y':0.29341962933540344,
                   u'X':0.5960872769355774,
                   u'Type':u'eyeRight'
                },
                {  
                   u'Y':0.3648611009120941,
                   u'X':0.519914448261261,
                   u'Type':u'nose'
                },
                {  
                   u'Y':0.4211627244949341,
                   u'X':0.45355224609375,
                   u'Type':u'mouthLeft'
                },
                {  
                   u'Y':0.4189545810222626,
                   u'X':0.5715887546539307,
                   u'Type':u'mouthRight'
                },
                {  
                   u'Y':0.2932279407978058,
                   u'X':0.423244833946228,
                   u'Type':u'leftPupil'
                },
                {  
                   u'Y':0.2887040078639984,
                   u'X':0.5888978838920593,
                   u'Type':u'rightPupil'
                },
                {  
                   u'Y':0.26639482378959656,
                   u'X':0.34561267495155334,
                   u'Type':u'leftEyeBrowLeft'
                },
                {  
                   u'Y':0.2544657289981842,
                   u'X':0.40450137853622437,
                   u'Type':u'leftEyeBrowRight'
                },
                {  
                   u'Y':0.2627829611301422,
                   u'X':0.46859216690063477,
                   u'Type':u'leftEyeBrowUp'
                },
                {  
                   u'Y':0.26147201657295227,
                   u'X':0.5543619990348816,
                   u'Type':u'rightEyeBrowLeft'
                },
                {  
                   u'Y':0.2506832182407379,
                   u'X':0.6015408635139465,
                   u'Type':u'rightEyeBrowRight'
                },
                {  
                   u'Y':0.25464701652526855,
                   u'X':0.6478369235992432,
                   u'Type':u'rightEyeBrowUp'
                },
                {  
                   u'Y':0.2980021834373474,
                   u'X':0.381369411945343,
                   u'Type':u'leftEyeLeft'
                },
                {  
                   u'Y':0.2984420359134674,
                   u'X':0.45687806606292725,
                   u'Type':u'leftEyeRight'
                },
                {  
                   u'Y':0.2849346995353699,
                   u'X':0.4199672043323517,
                   u'Type':u'leftEyeUp'
                },
                {  
                   u'Y':0.3064272105693817,
                   u'X':0.42038610577583313,
                   u'Type':u'leftEyeDown'
                },
                {  
                   u'Y':0.29792413115501404,
                   u'X':0.5583877563476562,
                   u'Type':u'rightEyeLeft'
                },
                {  
                   u'Y':0.29137128591537476,
                   u'X':0.6318506598472595,
                   u'Type':u'rightEyeRight'
                },
                {  
                   u'Y':0.2819582223892212,
                   u'X':0.5944773554801941,
                   u'Type':u'rightEyeUp'
                },
                {  
                   u'Y':0.3036530315876007,
                   u'X':0.5986653566360474,
                   u'Type':u'rightEyeDown'
                },
                {  
                   u'Y':0.3782326579093933,
                   u'X':0.48892658948898315,
                   u'Type':u'noseLeft'
                },
                {  
                   u'Y':0.3783911466598511,
                   u'X':0.5455747246742249,
                   u'Type':u'noseRight'
                },
                {  
                   u'Y':0.4099391996860504,
                   u'X':0.5140194892883301,
                   u'Type':u'mouthUp'
                },
                {  
                   u'Y':0.4421052038669586,
                   u'X':0.5142521262168884,
                   u'Type':u'mouthDown'
                }
             ],
             u'Pose':{  
                u'Yaw':6.750066757202148,
                u'Roll':-2.3479645252227783,
                u'Pitch':-1.6525852680206299
             },
             u'Emotions':[  
                {  
                   u'Confidence':85.22260284423828,
                   u'Type':u'CALM'
                },
                {  
                   u'Confidence':7.931999683380127,
                   u'Type':u'HAPPY'
                },
                {  
                   u'Confidence':2.901512384414673,
                   u'Type':u'SURPRISED'
                }
             ],
             u'AgeRange':{  
                u'High':38,
                u'Low':20
             },
             u'EyesOpen':{  
                u'Confidence':99.99998474121094,
                u'Value':True
             },
             u'BoundingBox':{  
                u'Width':0.5249999761581421,
                u'Top':0.15222221612930298,
                u'Left':0.24500000476837158,
                u'Height':0.3499999940395355
             },
             u'Smile':{  
                u'Confidence':69.94091796875,
                u'Value':True
             },
             u'MouthOpen':{  
                u'Confidence':99.92326354980469,
                u'Value':False
             },
             u'Quality':{  
                u'Sharpness':99.99671173095703,
                u'Brightness':41.9484748840332
             },
             u'Mustache':{  
                u'Confidence':99.78399658203125,
                u'Value':False
             },
             u'Beard':{  
                u'Confidence':99.99549102783203,
                u'Value':False
             }
          }
       ],
       
       u'OrientationCorrection':u'ROTATE_0'
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: