実現したいこと
AWSのRekognition(画像解析サービス)から返される解析結果の中から、指定の項目を出力したいと考えています。
初歩的な質問で恐縮ですがどのような処理を記述をすればよいかご教示いただけないでしょうか。
希望する出力の形
AgeRange : Low: 26, High: 43
Smile : true, 99.25891876220703%
Eyeglasses : true, 99.99998474121094%
Rekognitionの解析結果
json
1{ "FaceDetails": [ { "BoundingBox": { "Width": 0.2618750035762787, "Height": 0.3930581510066986, "Left": 0.14937500655651093, "Top": 0.13414634764194489 }, "AgeRange": { "Low": 26, "High": 43 }, "Smile": { "Value": true, "Confidence": 99.25891876220703 }, "Eyeglasses": { "Value": true, "Confidence": 99.99998474121094 }, "Sunglasses": { "Value": true, "Confidence": 98.9379653930664 }, "Gender": { "Value": "Female", "Confidence": 100 }, "Beard": { "Value": false, "Confidence": 99.9868392944336 }, "Mustache": { "Value": false, "Confidence": 98.99354553222656 }, "EyesOpen": { "Value": true, "Confidence": 96.80782318115234 }, "MouthOpen": { "Value": true, "Confidence": 86.21734619140625 }, "Emotions": [ { "Type": "HAPPY", "Confidence": 99.55255889892578 }, { "Type": "SAD", "Confidence": 0.5614995956420898 }, { "Type": "CALM", "Confidence": 0.46750307083129883 } ], "Landmarks": [ { "Type": "eyeLeft", "X": 0.2425708919763565, "Y": 0.29896146059036255 }, { "Type": "eyeRight", "X": 0.3258243203163147, "Y": 0.2869422733783722 }, { "Type": "nose", "X": 0.29795974493026733, "Y": 0.33913564682006836 }, { "Type": "mouthLeft", "X": 0.2424982190132141, "Y": 0.4403011202812195 }, { "Type": "mouthRight", "X": 0.3285549283027649, "Y": 0.42653071880340576 }, { "Type": "leftPupil", "X": 0.2514769434928894, "Y": 0.2966562509536743 }, { "Type": "rightPupil", "X": 0.3344457149505615, "Y": 0.2878323793411255 }, { "Type": "leftEyeBrowLeft", "X": 0.20776528120040894, "Y": 0.24588149785995483 }, { "Type": "leftEyeBrowUp", "X": 0.23486393690109253, "Y": 0.22838211059570312 }, { "Type": "leftEyeBrowRight", "X": 0.26713693141937256, "Y": 0.24221475422382355 }, { "Type": "rightEyeBrowLeft", "X": 0.29768621921539307, "Y": 0.24332448840141296 }, { "Type": "rightEyeBrowUp", "X": 0.324931800365448, "Y": 0.22636346518993378 }, { "Type": "rightEyeBrowRight", "X": 0.35234054923057556, "Y": 0.23396380245685577 }, { "Type": "leftEyeLeft", "X": 0.22536121308803558, "Y": 0.3036869764328003 }, { "Type": "leftEyeRight", "X": 0.25859442353248596, "Y": 0.2963939905166626 }, { "Type": "leftEyeUp", "X": 0.24218744039535522, "Y": 0.2921774685382843 }, { "Type": "leftEyeDown", "X": 0.24354742467403412, "Y": 0.3046663999557495 }, { "Type": "rightEyeLeft", "X": 0.30785858631134033, "Y": 0.2880838215351105 }, { "Type": "rightEyeRight", "X": 0.3429398238658905, "Y": 0.2862967848777771 }, { "Type": "rightEyeUp", "X": 0.32599735260009766, "Y": 0.28063637018203735 }, { "Type": "rightEyeDown", "X": 0.3260765075683594, "Y": 0.29300010204315186 }, { "Type": "noseLeft", "X": 0.2778167128562927, "Y": 0.38191738724708557 }, { "Type": "noseRight", "X": 0.31011009216308594, "Y": 0.3755253553390503 }, { "Type": "mouthUp", "X": 0.2913239300251007, "Y": 0.4082499146461487 }, { "Type": "mouthDown", "X": 0.29487860202789307, "Y": 0.4634847044944763 } ], "Pose": { "Roll": -7.02242374420166, "Yaw": 17.866071701049805, "Pitch": 14.642864227294922 }, "Quality": { "Brightness": 41.43524169921875, "Sharpness": 99.98487854003906 }, "Confidence": 99.82343292236328 } ] }
実行処理
python3 です。
python
1from __future__ import print_function 2 3import boto3 4from decimal import Decimal 5import json 6import urllib 7 8print('Loading function') 9 10s3 = boto3.client('s3') 11rekognition = boto3.client('rekognition') 12 13 14# --------------- Helper Functions to call Rekognition APIs ------------------ 15 16 17def detect_faces(bucket, key): 18 response = rekognition.detect_faces( 19 Image={ 20 "S3Object": { 21 "Bucket": bucket, 22 "Name": key 23 } 24 }, 25 Attributes=['ALL'] 26 ) 27 return response 28 29 30#def detect_labels(bucket, key): 31# response = rekognition.detect_labels(Image={"S3Object": {"Bucket": bucket, "Name": key}}) 32 33 # Sample code to write response to DynamoDB table 'MyTable' with 'PK' as Primary Key. 34 # Note: role used for executing this Lambda function should have write access to the table. 35 #table = boto3.resource('dynamodb').Table('MyTable') 36 #labels = [{'Confidence': Decimal(str(label_prediction['Confidence'])), 'Name': label_prediction['Name']} for label_prediction in response['Labels']] 37 #table.put_item(Item={'PK': key, 'Labels': labels}) 38# return response 39 40 41#def index_faces(bucket, key): 42 # Note: Collection has to be created upfront. Use CreateCollection API to create a collecion. 43 #rekognition.create_collection(CollectionId='BLUEPRINT_COLLECTION') 44 #response = rekognition.index_faces(Image={"S3Object": {"Bucket": bucket, "Name": key}}, CollectionId="BLUEPRINT_COLLECTION") 45 #return response 46 47 48# --------------- Main handler ------------------ 49 50 51def lambda_handler(event, context): 52 '''Demonstrates S3 trigger that uses 53 Rekognition APIs to detect faces, labels and index faces in S3 Object. 54 ''' 55 #print("Received event: " + json.dumps(event, indent=2)) 56 57 # Get the object from the event 58 bucket = event['Records'][0]['s3']['bucket']['name'] 59 key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8') 60 try: 61 # Calls rekognition DetectFaces API to detect faces in S3 object 62 response = detect_faces(bucket, key) 63 64 # Calls rekognition DetectLabels API to detect labels in S3 object 65 #response = detect_labels(bucket, key) 66 67 # Calls rekognition IndexFaces API to detect faces in S3 object and index faces into specified collection 68 #response = index_faces(bucket, key) 69 70 # Print response to console. 71 ※ここに指定の項目を出力する処理を記述したいです。 72 73 74 return response 75 except Exception as e: 76 print(e) 77 print("Error processing object {} from bucket {}. ".format(key, bucket) + 78 "Make sure your object and bucket exist and your bucket is in the same region as this function.") 79 raise e
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/14 07:18
2018/05/14 07:24
2018/05/14 07:44