AWS rekognitionを使用すると簡単に画像の解析を行うことができます。
Amazon Rekognition とは - Amazon Rekognition
Lambda関数でrekognitionを使用する
S3のPutObjectをイベントリソースにしたLambda関数の例。S3にアップロードされた画像を解析します。
import json import urllib import boto3 import os import logging logger = logging.getLogger() logger.setLevel(logging.INFO) s3 = boto3.client('s3') rekognition = boto3.client('rekognition', 'us-east-1') dynamodb = boto3.resource('dynamodb', region_name='ap-northeast-1') table = dynamodb.Table(os.getenv('TABLE_NAME')) def lambda_handler(event, context): bucket = event['Records'][0]['s3']['bucket']['name'] key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], 'utf8') obj = s3.get_object(Bucket=bucket, Key=key) body = obj['Body'].read() # 画像内に含まれるlabel(大まかなオブジェクトの意味。(human | text | outdoor | etc ...)) labels = rekognition.detect_labels(Image={'Bytes':body}, MinConfidence=75) # 顔識別 faces = rekognition.detect_faces(Image={'Bytes':body}, Attributes=['ALL']) rekognized_label = {} rekognized_label['Labels'] = labels['Labels'] rekognized_label['FaceDetails'] = faces['FaceDetails'] logging.info(json.dumps(rekognized_label)) return
以下のように簡単に利用することができる。
サンプルデータと解析結果
試しにこんな画像を解析してみると...

こんな結果が返されます(^_^)
cf24ac4d73a9
{
"Labels": [
{
"Name": "Human",
"Confidence": 99.48844146728516,
"Instances": [],
"Parents": []
},
{
"Name": "Person",
"Confidence": 99.48844146728516,
"Instances": [
{
"BoundingBox": {
"Width": 0.20328140258789062,
"Height": 0.38710319995880127,
"Left": 0.36973658204078674,
"Top": 0.6079409718513489
},
"Confidence": 99.48844146728516
},
{
"BoundingBox": {
"Width": 0.27378788590431213,
"Height": 0.5127241611480713,
"Left": 0.6623964309692383,
"Top": 0.4001222252845764
},
"Confidence": 69.43467712402344
}
],
"Parents": []
},
{
"Name": "Comics",
"Confidence": 98.47220611572266,
"Instances": [],
"Parents": [
{
"Name": "Book"
}
]
},
{
"Name": "Manga",
"Confidence": 98.47220611572266,
"Instances": [],
"Parents": [
{
"Name": "Comics"
},
{
"Name": "Book"
}
]
},
{
"Name": "Book",
"Confidence": 98.47220611572266,
"Instances": [],
"Parents": []
}
],
"FaceDetails": [
{
"BoundingBox": {
"Width": 0.07516269385814667,
"Height": 0.09448358416557312,
"Left": 0.8122304677963257,
"Top": 0.477640300989151
},
"AgeRange": {
"Low": 52,
"High": 70
},
"Smile": {
"Value": false,
"Confidence": 54.83989715576172
},
"Eyeglasses": {
"Value": false,
"Confidence": 53.24875259399414
},
"Sunglasses": {
"Value": false,
"Confidence": 54.3681755065918
},
"Gender": {
"Value": "Male",
"Confidence": 54.61311340332031
},
"Beard": {
"Value": true,
"Confidence": 50.3885612487793
},
"Mustache": {
"Value": false,
"Confidence": 54.03059005737305
},
"EyesOpen": {
"Value": true,
"Confidence": 54.86281967163086
},
"MouthOpen": {
"Value": false,
"Confidence": 53.010009765625
},
"Emotions": [
{
"Type": "ANGRY",
"Confidence": 45.84453582763672
},
{
"Type": "FEAR",
"Confidence": 49.08755874633789
},
{
"Type": "SAD",
"Confidence": 48.044490814208984
},
{
"Type": "CALM",
"Confidence": 45.60464096069336
},
{
"Type": "HAPPY",
"Confidence": 45.03826904296875
},
{
"Type": "CONFUSED",
"Confidence": 45.1912956237793
},
{
"Type": "SURPRISED",
"Confidence": 46.129150390625
},
{
"Type": "DISGUSTED",
"Confidence": 45.060054779052734
}
],
"Landmarks": [
{
"Type": "eyeLeft",
"X": 0.8781353235244751,
"Y": 0.5331472754478455
},
{
"Type": "eyeRight",
"X": 0.869394063949585,
"Y": 0.5533623099327087
},
{
"Type": "mouthLeft",
"X": 0.8543952703475952,
"Y": 0.5070979595184326
},
{
"Type": "mouthRight",
"X": 0.8477988243103027,
"Y": 0.5230692625045776
},
{
"Type": "nose",
"X": 0.8716346025466919,
"Y": 0.5304551720619202
},
{
"Type": "leftEyeBrowLeft",
"X": 0.8835707902908325,
"Y": 0.5290471315383911
},
{
"Type": "leftEyeBrowRight",
"X": 0.8868613839149475,
"Y": 0.5454819202423096
},
{
"Type": "leftEyeBrowUp",
"X": 0.8883397579193115,
"Y": 0.5390796065330505
},
{
"Type": "rightEyeBrowLeft",
"X": 0.8814401030540466,
"Y": 0.5579567551612854
},
{
"Type": "rightEyeBrowRight",
"X": 0.8670448660850525,
"Y": 0.5670304298400879
},
{
"Type": "rightEyeBrowUp",
"X": 0.877813458442688,
"Y": 0.5652103424072266
},
{
"Type": "leftEyeLeft",
"X": 0.8783277273178101,
"Y": 0.5284242033958435
},
{
"Type": "leftEyeRight",
"X": 0.8757176995277405,
"Y": 0.5359859466552734
},
{
"Type": "leftEyeUp",
"X": 0.8795347809791565,
"Y": 0.5335092544555664
},
{
"Type": "leftEyeDown",
"X": 0.8771616220474243,
"Y": 0.53129643201828
},
{
"Type": "rightEyeLeft",
"X": 0.8699447512626648,
"Y": 0.5485483407974243
},
{
"Type": "rightEyeRight",
"X": 0.8652085661888123,
"Y": 0.5564696788787842
},
{
"Type": "rightEyeUp",
"X": 0.8697998523712158,
"Y": 0.554349958896637
},
{
"Type": "rightEyeDown",
"X": 0.8675624132156372,
"Y": 0.5517592430114746
},
{
"Type": "noseLeft",
"X": 0.864964485168457,
"Y": 0.5220027565956116
},
{
"Type": "noseRight",
"X": 0.8609451055526733,
"Y": 0.5307477712631226
},
{
"Type": "mouthUp",
"X": 0.8589581251144409,
"Y": 0.5209534168243408
},
{
"Type": "mouthDown",
"X": 0.850980818271637,
"Y": 0.5131813287734985
},
{
"Type": "leftPupil",
"X": 0.8781353235244751,
"Y": 0.5331472754478455
},
{
"Type": "rightPupil",
"X": 0.869394063949585,
"Y": 0.5533623099327087
},
{
"Type": "upperJawlineLeft",
"X": 0.8650891780853271,
"Y": 0.5151005387306213
},
{
"Type": "midJawlineLeft",
"X": 0.8431430459022522,
"Y": 0.49400797486305237
},
{
"Type": "chinBottom",
"X": 0.8355048298835754,
"Y": 0.5003891587257385
},
{
"Type": "midJawlineRight",
"X": 0.8251460790634155,
"Y": 0.5319800972938538
},
{
"Type": "upperJawlineRight",
"X": 0.8428035974502563,
"Y": 0.5624011158943176
}
],
"Pose": {
"Roll": 118.93660736083984,
"Yaw": -44.56156921386719,
"Pitch": 20.28704833984375
},
"Quality": {
"Brightness": 85.13558197021484,
"Sharpness": 89.85481262207031
},
"Confidence": 99.96990203857422
},
{
"BoundingBox": {
"Width": 0.04701037332415581,
"Height": 0.07499895244836807,
"Left": 0.4147825241088867,
"Top": 0.6886023283004761
},
"AgeRange": {
"Low": 22,
"High": 34
},
"Smile": {
"Value": false,
"Confidence": 50.31565475463867
},
"Eyeglasses": {
"Value": false,
"Confidence": 50.11409378051758
},
"Sunglasses": {
"Value": false,
"Confidence": 50.2744026184082
},
"Gender": {
"Value": "Male",
"Confidence": 50.42430114746094
},
"Beard": {
"Value": true,
"Confidence": 50.027400970458984
},
"Mustache": {
"Value": false,
"Confidence": 50.360809326171875
},
"EyesOpen": {
"Value": true,
"Confidence": 50.451690673828125
},
"MouthOpen": {
"Value": false,
"Confidence": 50.06884765625
},
"Emotions": [
{
"Type": "ANGRY",
"Confidence": 49.627830505371094
},
{
"Type": "SAD",
"Confidence": 49.56283187866211
},
{
"Type": "FEAR",
"Confidence": 50.061988830566406
},
{
"Type": "CALM",
"Confidence": 49.60093688964844
},
{
"Type": "DISGUSTED",
"Confidence": 49.54352569580078
},
{
"Type": "HAPPY",
"Confidence": 49.54714584350586
},
{
"Type": "CONFUSED",
"Confidence": 49.520606994628906
},
{
"Type": "SURPRISED",
"Confidence": 49.535133361816406
}
],
"Landmarks": [
{
"Type": "eyeLeft",
"X": 0.4284121096134186,
"Y": 0.7184935212135315
},
{
"Type": "eyeRight",
"X": 0.43668362498283386,
"Y": 0.7126566767692566
},
{
"Type": "mouthLeft",
"X": 0.4349792003631592,
"Y": 0.7283509373664856
},
{
"Type": "mouthRight",
"X": 0.4419858157634735,
"Y": 0.7235278487205505
},
{
"Type": "nose",
"X": 0.43629977107048035,
"Y": 0.722704291343689
},
{
"Type": "leftEyeBrowLeft",
"X": 0.4241586923599243,
"Y": 0.7180318236351013
},
{
"Type": "leftEyeBrowRight",
"X": 0.4287092387676239,
"Y": 0.7148158550262451
},
{
"Type": "leftEyeBrowUp",
"X": 0.4261903762817383,
"Y": 0.7157451510429382
},
{
"Type": "rightEyeBrowLeft",
"X": 0.4335206151008606,
"Y": 0.7113913893699646
},
{
"Type": "rightEyeBrowRight",
"X": 0.43840110301971436,
"Y": 0.7075032591819763
},
{
"Type": "rightEyeBrowUp",
"X": 0.4355311989784241,
"Y": 0.7088116407394409
},
{
"Type": "leftEyeLeft",
"X": 0.42720815539360046,
"Y": 0.7193988561630249
},
{
"Type": "leftEyeRight",
"X": 0.43036019802093506,
"Y": 0.7175643444061279
},
{
"Type": "leftEyeUp",
"X": 0.42846548557281494,
"Y": 0.7181983590126038
},
{
"Type": "leftEyeDown",
"X": 0.4290151298046112,
"Y": 0.7190854549407959
},
{
"Type": "rightEyeLeft",
"X": 0.43517008423805237,
"Y": 0.7140955924987793
},
{
"Type": "rightEyeRight",
"X": 0.43798673152923584,
"Y": 0.7115782499313354
},
{
"Type": "rightEyeUp",
"X": 0.4364575743675232,
"Y": 0.7124010920524597
},
{
"Type": "rightEyeDown",
"X": 0.4369019567966461,
"Y": 0.7133592367172241
},
{
"Type": "noseLeft",
"X": 0.43508270382881165,
"Y": 0.7241132259368896
},
{
"Type": "noseRight",
"X": 0.4380292296409607,
"Y": 0.7219674587249756
},
{
"Type": "mouthUp",
"X": 0.43793272972106934,
"Y": 0.7257525324821472
},
{
"Type": "mouthDown",
"X": 0.4395196735858917,
"Y": 0.728694498538971
},
{
"Type": "leftPupil",
"X": 0.4284121096134186,
"Y": 0.7184935212135315
},
{
"Type": "rightPupil",
"X": 0.43668362498283386,
"Y": 0.7126566767692566
},
{
"Type": "upperJawlineLeft",
"X": 0.4227452874183655,
"Y": 0.7198435068130493
},
{
"Type": "midJawlineLeft",
"X": 0.43062615394592285,
"Y": 0.7304227352142334
},
{
"Type": "chinBottom",
"X": 0.4420109987258911,
"Y": 0.733522355556488
},
{
"Type": "midJawlineRight",
"X": 0.44504693150520325,
"Y": 0.7198812961578369
},
{
"Type": "upperJawlineRight",
"X": 0.44071856141090393,
"Y": 0.7067651748657227
}
],
"Pose": {
"Roll": -31.231826782226562,
"Yaw": -7.7481231689453125,
"Pitch": -28.088232040405273
},
"Quality": {
"Brightness": 82.80461120605469,
"Sharpness": 86.86019134521484
},
"Confidence": 99.26252746582031
}
]
}
リンク
