こんにちは。
ITエンジニアのきゅうと申します。
今回は事前にお伝えした通り、AWS Rekognitionのメソッドの種類や機能概要などについて見ていきたいと思います。
ここにあるサンプルは、メソッドを使う部分のみ切り出したものになります。
探し求めていたものがあった場合は、下記の備忘録を参考にインスタンス化、接続、実行、データの取り出し・活用をしてみてください。
Image(画像分析)のメソッドについて
まずはRekognitionの画像分析(Image)と動画分析(Video)の内、画像分析のメソッドについて確認していきます。
1.detect_labels
detect_labelsメソッドは、画像から物体やイベント、テーマなどのラベルを検出することが出来ます。
画像プロパティには、前景や背景の色、画像の鮮明度、明るさ、コントラストなどの属性が含まれます。
detect_labelsのサンプルコード
session = boto3.Session(profile_name='profile-name')
client = session.client('rekognition')
response = client.detect_labels(Image={'S3Object':{'Bucket':bucket,'Name':photo}},
MaxLabels=10,
# Uncomment to use image properties and filtration settings
#Features=["GENERAL_LABELS", "IMAGE_PROPERTIES"],
#Settings={"GeneralLabels": {"LabelInclusionFilters":["Cat"]},
# "ImageProperties": {"MaxDominantColors":10}}
)
labels = [label['Name'] for label in response['Labels']]
detect_labelsの引数
"Image"= {
"S3Object": {
"Bucket": "bucket", #バケット名
"Name": "input.jpg" #キー(フォルダ+ファイル名)
}
}
detect_labelsの戻り値
{
"Labels": [
{
"Name": "Mobile Phone",
"Parents": [
{
"Name": "Phone"
}
],
"Aliases": [
{
"Name": "Cell Phone"
}
],
"Categories": [
{
"Name": "Technology and Computing"
}
],
"Confidence": 99.9364013671875,
"Instances": [
{
"BoundingBox": {
"Width": 0.26779675483703613,
"Height": 0.8562285900115967,
"Left": 0.3604024350643158,
"Top": 0.09245597571134567,
}
"Confidence": 99.9364013671875,
"DominantColors": [
{
"Red": 120,
"Green": 137,
"Blue": 132,
"HexCode": "3A7432",
"SimplifiedColor": "red",
"CssColor": "fuscia",
"PixelPercentage": 40.10
}
],
}
]
}
],
"ImageProperties": {
"Quality": {
"Brightness": 40,
"Sharpness": 40,
"Contrast": 24,
},
"DominantColors": [
{
"Red": 120,
"Green": 137,
"Blue": 132,
"HexCode": "3A7432",
"SimplifiedColor": "red",
"CssColor": "fuscia",
"PixelPercentage": 40.10
}
],
"Foreground": {
"Quality": {
"Brightness": 40,
"Sharpness": 40,
},
"DominantColors": [
{
"Red": 200,
"Green": 137,
"Blue": 132,
"HexCode": "3A7432",
"CSSColor": "",
"SimplifiedColor": "red",
"PixelPercentage": 30.70
}
],
}
"Background": {
"Quality": {
"Brightness": 40,
"Sharpness": 40,
},
"DominantColors": [
{
"Red": 200,
"Green": 137,
"Blue": 132,
"HexCode": "3A7432",
"CSSColor": "",
"SimplifiedColor": "Red",
"PixelPercentage": 10.20
}
],
},
},
"LabelModelVersion": "3.0"
}
2.detect_faces
detect_facesメソッドは、目、鼻、口などの主な顔の特徴を探して入力画像内の顔を検出するメソッドになります。
detect_facesのサンプルコード
session = boto3.Session(profile_name='profile-name',
region_name=region)
client = session.client('rekognition', region_name=region)
response = client.detect_faces(Image={'S3Object':{'Bucket':bucket,'Name':photo}},
Attributes=['ALL'])
print('Detected faces for ' + photo)
for faceDetail in response['FaceDetails']:
print('The detected face is between ' + str(faceDetail['AgeRange']['Low'])
+ ' and ' + str(faceDetail['AgeRange']['High']) + ' years old')
print('Here are the other attributes:')
print(json.dumps(faceDetail, indent=4, sort_keys=True))
# Access predictions for individual face details and print them
print("Gender: " + str(faceDetail['Gender']))
print("Smile: " + str(faceDetail['Smile']))
print("Eyeglasses: " + str(faceDetail['Eyeglasses']))
print("Face Occluded: " + str(faceDetail['FaceOccluded']))
print("Emotions: " + str(faceDetail['Emotions'][0]))
detect_facesの引数
{
"Image": {
"S3Object": {
"Bucket": "bucket",
"Name": "input.jpg"
}
},
"Attributes": [
"ALL"
]
}
detect_facesの戻り値
{
"FaceDetails": [
{
"BoundingBox": {
"Width": 0.7919622659683228,
"Height": 0.7510867118835449,
"Left": 0.08881539851427078,
"Top": 0.151064932346344
},
"AgeRange": {
"Low": 18,
"High": 26
},
"Smile": {
"Value": false,
"Confidence": 89.77348327636719
},
"Eyeglasses": {
"Value": true,
"Confidence": 99.99996948242188
},
"Sunglasses": {
"Value": true,
"Confidence": 93.65237426757812
},
"Gender": {
"Value": "Female",
"Confidence": 99.85968780517578
},
"Beard": {
"Value": false,
"Confidence": 77.52591705322266
},
"Mustache": {
"Value": false,
"Confidence": 94.48904418945312
},
"EyesOpen": {
"Value": true,
"Confidence": 98.57169342041016
},
"MouthOpen": {
"Value": false,
"Confidence": 74.33953094482422
},
"Emotions": [
{
"Type": "SAD",
"Confidence": 65.56403350830078
},
{
"Type": "CONFUSED",
"Confidence": 31.277774810791016
},
{
"Type": "DISGUSTED",
"Confidence": 15.553778648376465
},
{
"Type": "ANGRY",
"Confidence": 8.012762069702148
},
{
"Type": "SURPRISED",
"Confidence": 7.621500015258789
},
{
"Type": "FEAR",
"Confidence": 7.243380546569824
},
{
"Type": "CALM",
"Confidence": 5.8196024894714355
},
{
"Type": "HAPPY",
"Confidence": 2.2830512523651123
}
],
"Landmarks": [
{
"Type": "eyeLeft",
"X": 0.30225440859794617,
"Y": 0.41018882393836975
},
{
"Type": "eyeRight",
"X": 0.6439348459243774,
"Y": 0.40341562032699585
},
{
"Type": "mouthLeft",
"X": 0.343580037355423,
"Y": 0.6951127648353577
},
{
"Type": "mouthRight",
"X": 0.6306480765342712,
"Y": 0.6898072361946106
},
{
"Type": "nose",
"X": 0.47164231538772583,
"Y": 0.5763645172119141
},
{
"Type": "leftEyeBrowLeft",
"X": 0.1732882857322693,
"Y": 0.34452149271965027
},
{
"Type": "leftEyeBrowRight",
"X": 0.3655243515968323,
"Y": 0.33231860399246216
},
{
"Type": "leftEyeBrowUp",
"X": 0.2671719491481781,
"Y": 0.31669262051582336
},
{
"Type": "rightEyeBrowLeft",
"X": 0.5613729953765869,
"Y": 0.32813435792922974
},
{
"Type": "rightEyeBrowRight",
"X": 0.7665090560913086,
"Y": 0.3318614959716797
},
{
"Type": "rightEyeBrowUp",
"X": 0.6612788438796997,
"Y": 0.3082450032234192
},
{
"Type": "leftEyeLeft",
"X": 0.2416982799768448,
"Y": 0.4085965156555176
},
{
"Type": "leftEyeRight",
"X": 0.36943578720092773,
"Y": 0.41230902075767517
},
{
"Type": "leftEyeUp",
"X": 0.29974061250686646,
"Y": 0.3971870541572571
},
{
"Type": "leftEyeDown",
"X": 0.30360740423202515,
"Y": 0.42347756028175354
},
{
"Type": "rightEyeLeft",
"X": 0.5755768418312073,
"Y": 0.4081145226955414
},
{
"Type": "rightEyeRight",
"X": 0.7050536870956421,
"Y": 0.39924031496047974
},
{
"Type": "rightEyeUp",
"X": 0.642906129360199,
"Y": 0.39026668667793274
},
{
"Type": "rightEyeDown",
"X": 0.6423097848892212,
"Y": 0.41669243574142456
},
{
"Type": "noseLeft",
"X": 0.4122826159000397,
"Y": 0.5987403392791748
},
{
"Type": "noseRight",
"X": 0.5394935011863708,
"Y": 0.5960900187492371
},
{
"Type": "mouthUp",
"X": 0.478581964969635,
"Y": 0.6660456657409668
},
{
"Type": "mouthDown",
"X": 0.483366996049881,
"Y": 0.7497162818908691
},
{
"Type": "leftPupil",
"X": 0.30225440859794617,
"Y": 0.41018882393836975
},
{
"Type": "rightPupil",
"X": 0.6439348459243774,
"Y": 0.40341562032699585
},
{
"Type": "upperJawlineLeft",
"X": 0.11031254380941391,
"Y": 0.3980775475502014
},
{
"Type": "midJawlineLeft",
"X": 0.19301874935626984,
"Y": 0.7034031748771667
},
{
"Type": "chinBottom",
"X": 0.4939905107021332,
"Y": 0.8877836465835571
},
{
"Type": "midJawlineRight",
"X": 0.7990140914916992,
"Y": 0.6899225115776062
},
{
"Type": "upperJawlineRight",
"X": 0.8548634648323059,
"Y": 0.38160091638565063
}
],
"Pose": {
"Roll": -5.83309268951416,
"Yaw": -2.4244730472564697,
"Pitch": 2.6216139793395996
},
"Quality": {
"Brightness": 96.16363525390625,
"Sharpness": 95.51618957519531
},
"Confidence": 99.99872589111328,
"FaceOccluded": {
"Value": true,
"Confidence": 99.99726104736328
},
"EyeDirection": {
"Yaw": 16.299732,
"Pitch": -6.407457,
"Confidence": 99.968704
}
}
],
"ResponseMetadata": {
"RequestId": "8bf02607-70b7-4f20-be55-473fe1bba9a2",
"HTTPStatusCode": 200,
"HTTPHeaders": {
"x-amzn-requestid": "8bf02607-70b7-4f20-be55-473fe1bba9a2",
"content-type": "application/x-amz-json-1.1",
"content-length": "3409",
"date": "Wed, 26 Apr 2023 20:18:50 GMT"
},
"RetryAttempts": 0
}
}
3.compare_faces
compare_facesメソッドは、ソース画像内の最も大きい顔とターゲット画像内の顔を比較します。
compare_facesのサンプルコード
session = boto3.Session(profile_name='profile-name')
client = session.client('rekognition')
imageSource = open(sourceFile, 'rb')
imageTarget = open(targetFile, 'rb')
response = client.compare_faces(SimilarityThreshold=80,
SourceImage={'Bytes': imageSource.read()},
TargetImage={'Bytes': imageTarget.read()})
for faceMatch in response['FaceMatches']:
position = faceMatch['Face']['BoundingBox']
similarity = str(faceMatch['Similarity'])
print('The face at ' +
str(position['Left']) + ' ' +
str(position['Top']) +
' matches with ' + similarity + '% confidence')
imageSource.close()
imageTarget.close()
return len(response['FaceMatches'])
compare_facesの引数
{
"SourceImage": {
"Bytes": "/9j/4AAQSk2Q==..."
},
"TargetImage": {
"Bytes": "/9j/4O1Q==..."
},
"SimilarityThreshold": 70
}
compare_facesの戻り値
{
"FaceMatches": [{
"Face": {
"BoundingBox": {
"Width": 0.5521978139877319,
"Top": 0.1203877404332161,
"Left": 0.23626373708248138,
"Height": 0.3126954436302185
},
"Confidence": 99.98751068115234,
"Pose": {
"Yaw": -82.36799621582031,
"Roll": -62.13221740722656,
"Pitch": 0.8652129173278809
},
"Quality": {
"Sharpness": 99.99880981445312,
"Brightness": 54.49755096435547
},
"Landmarks": [{
"Y": 0.2996366024017334,
"X": 0.41685718297958374,
"Type": "eyeLeft"
},
{
"Y": 0.2658946216106415,
"X": 0.4414493441581726,
"Type": "eyeRight"
},
{
"Y": 0.3465650677680969,
"X": 0.48636093735694885,
"Type": "nose"
},
{
"Y": 0.30935320258140564,
"X": 0.6251809000968933,
"Type": "mouthLeft"
},
{
"Y": 0.26942989230155945,
"X": 0.6454493403434753,
"Type": "mouthRight"
}
]
},
"Similarity": 100.0
}],
"SourceImageOrientationCorrection": "ROTATE_90",
"TargetImageOrientationCorrection": "ROTATE_90",
"UnmatchedFaces": [{
"BoundingBox": {
"Width": 0.4890109896659851,
"Top": 0.6566604375839233,
"Left": 0.10989011079072952,
"Height": 0.278298944234848
},
"Confidence": 99.99992370605469,
"Pose": {
"Yaw": 51.51519012451172,
"Roll": -110.32493591308594,
"Pitch": -2.322134017944336
},
"Quality": {
"Sharpness": 99.99671173095703,
"Brightness": 57.23163986206055
},
"Landmarks": [{
"Y": 0.8288310766220093,
"X": 0.3133862614631653,
"Type": "eyeLeft"
},
{
"Y": 0.7632885575294495,
"X": 0.28091415762901306,
"Type": "eyeRight"
},
{
"Y": 0.7417283654212952,
"X": 0.3631140887737274,
"Type": "nose"
},
{
"Y": 0.8081989884376526,
"X": 0.48565614223480225,
"Type": "mouthLeft"
},
{
"Y": 0.7548204660415649,
"X": 0.46090251207351685,
"Type": "mouthRight"
}
]
}],
"SourceImageFace": {
"BoundingBox": {
"Width": 0.5521978139877319,
"Top": 0.1203877404332161,
"Left": 0.23626373708248138,
"Height": 0.3126954436302185
},
"Confidence": 99.98751068115234
}
}
4.detect_protective_equipment
detect_protective_equipmentメソッドは、画像内の人物の個人用保護具(PPE)を検出します。
個人用保護具(PPE)には、服は含まれません。
ここでいう個人用保護具(PPE)とは、顔、頭、左手、右手に装着されているカバー・・・、ですので、フェイスカバーやハンドカバー、ヘルメットなどが対象となります。
ですので、コロナ渦であればマスクをしているか否かの判断、交通調査的なアプリであれば、自転車に乗っている人がヘルメットをしているか否かの判断など需要があるかもしれませんが、ニッチすぎるので、今回、詳細は省かせて頂きます。
代わりに2つのサイトをご紹介させて頂きます
5.recognize_celebrities
recognize_celebritiesメソッドは、画像から有名人を検出します。
recognize_celebritiesメソッドについてもニッチなメソッドになり、ハッカソンでの使用率は低いと思われますので、参考サイトの御紹介に留めたいと思います。
6.get_celebrity_info
get_celebrity_infoメソッドは、有名人に関する情報を取得します。
ただし、有名人を識別するには有名人IDを必要とし、有名人IDは「5.recognize_celebrities」で取得できます。
こちらもニッチなメソッドになり、ハッカソンでの使用率は低いと思われますので、参考サイトの御紹介に留めたいと思います。
7.detect_moderation_labels
detect_moderation_labelsメソッドは、指定した画像内に不適切なコンテンツを検出します。
detect_moderation_labelsメソッドについてもニッチなメソッドになり、ハッカソンでの使用率は低いと思われますので、参考サイトの御紹介に留めたいと思います。
8.detect_text
detect_textメソッドは、指定した画像内に不適切なコンテンツを検出します。
detect_moderation_labelsのサンプルコード
session = boto3.Session(profile_name='default')
client = session.client('rekognition')
response = client.detect_text(Image={'S3Object': {'Bucket': bucket, 'Name': photo}})
textDetections = response['TextDetections']
print('Detected text\n----------')
for text in textDetections:
print('Detected text:' + text['DetectedText'])
print('Confidence: ' + "{:.2f}".format(text['Confidence']) + "%")
print('Id: {}'.format(text['Id']))
if 'ParentId' in text:
print('Parent Id: {}'.format(text['ParentId']))
print('Type:' + text['Type'])
print()
return len(textDetections)
detect_moderation_labelsの引数
{
"Image": {
"S3Object": {
"Bucket": "bucket",
"Name": "inputtext.jpg"
}
}
}
detect_moderation_labelsの戻り値
{
'TextDetections': [{'Confidence': 99.35693359375,
'DetectedText': "IT'S",
'Geometry': {'BoundingBox': {'Height': 0.09988046437501907,
'Left': 0.6684935688972473,
'Top': 0.18226495385169983,
'Width': 0.1461552083492279},
'Polygon': [{'X': 0.6684935688972473,
'Y': 0.1838926374912262},
{'X': 0.8141663074493408,
'Y': 0.18226495385169983},
{'X': 0.8146487474441528,
'Y': 0.28051772713661194},
{'X': 0.6689760088920593,
'Y': 0.2821454107761383}]},
'Id': 0,
'Type': 'LINE'},
{'Confidence': 99.6207275390625,
'DetectedText': 'MONDAY',
'Geometry': {'BoundingBox': {'Height': 0.11442459374666214,
'Left': 0.5566731691360474,
'Top': 0.3525116443634033,
'Width': 0.39574965834617615},
'Polygon': [{'X': 0.5566731691360474,
'Y': 0.353712260723114},
{'X': 0.9522717595100403,
'Y': 0.3525116443634033},
{'X': 0.9524227976799011,
'Y': 0.4657355844974518},
{'X': 0.5568241477012634,
'Y': 0.46693623065948486}]},
'Id': 1,
'Type': 'LINE'},
{'Confidence': 99.6160888671875,
'DetectedText': 'but keep',
'Geometry': {'BoundingBox': {'Height': 0.08314694464206696,
'Left': 0.6398131847381592,
'Top': 0.5267938375473022,
'Width': 0.2021435648202896},
'Polygon': [{'X': 0.640289306640625,
'Y': 0.5267938375473022},
{'X': 0.8419567942619324,
'Y': 0.5295097827911377},
{'X': 0.8414806723594666,
'Y': 0.609940767288208},
{'X': 0.6398131847381592,
'Y': 0.6072247624397278}]},
'Id': 2,
'Type': 'LINE'},
{'Confidence': 88.95134735107422,
'DetectedText': 'Smiling',
'Geometry': {'BoundingBox': {'Height': 0.4326171875,
'Left': 0.46289217472076416,
'Top': 0.5634765625,
'Width': 0.5371078252792358},
'Polygon': [{'X': 0.46289217472076416,
'Y': 0.5634765625},
{'X': 1.0, 'Y': 0.5634765625},
{'X': 1.0, 'Y': 0.99609375},
{'X': 0.46289217472076416,
'Y': 0.99609375}]},
'Id': 3,
'Type': 'LINE'},
{'Confidence': 99.35693359375,
'DetectedText': "IT'S",
'Geometry': {'BoundingBox': {'Height': 0.09988046437501907,
'Left': 0.6684935688972473,
'Top': 0.18226495385169983,
'Width': 0.1461552083492279},
'Polygon': [{'X': 0.6684935688972473,
'Y': 0.1838926374912262},
{'X': 0.8141663074493408,
'Y': 0.18226495385169983},
{'X': 0.8146487474441528,
'Y': 0.28051772713661194},
{'X': 0.6689760088920593,
'Y': 0.2821454107761383}]},
'Id': 4,
'ParentId': 0,
'Type': 'WORD'},
{'Confidence': 99.6207275390625,
'DetectedText': 'MONDAY',
'Geometry': {'BoundingBox': {'Height': 0.11442466825246811,
'Left': 0.5566731691360474,
'Top': 0.35251158475875854,
'Width': 0.39574965834617615},
'Polygon': [{'X': 0.5566731691360474,
'Y': 0.3537122905254364},
{'X': 0.9522718787193298,
'Y': 0.35251158475875854},
{'X': 0.9524227976799011,
'Y': 0.4657355546951294},
{'X': 0.5568241477012634,
'Y': 0.46693626046180725}]},
'Id': 5,
'ParentId': 1,
'Type': 'WORD'},
{'Confidence': 99.96778869628906,
'DetectedText': 'but',
'Geometry': {'BoundingBox': {'Height': 0.0625,
'Left': 0.6402802467346191,
'Top': 0.5283203125,
'Width': 0.08027780801057816},
'Polygon': [{'X': 0.6402802467346191,
'Y': 0.5283203125},
{'X': 0.7205580472946167,
'Y': 0.5283203125},
{'X': 0.7205580472946167,
'Y': 0.5908203125},
{'X': 0.6402802467346191,
'Y': 0.5908203125}]},
'Id': 6,
'ParentId': 2,
'Type': 'WORD'},
{'Confidence': 99.26438903808594,
'DetectedText': 'keep',
'Geometry': {'BoundingBox': {'Height': 0.0818721204996109,
'Left': 0.7344760298728943,
'Top': 0.5280686020851135,
'Width': 0.10748066753149033},
'Polygon': [{'X': 0.7349520921707153,
'Y': 0.5280686020851135},
{'X': 0.8419566750526428,
'Y': 0.5295097827911377},
{'X': 0.8414806127548218,
'Y': 0.6099407076835632},
{'X': 0.7344760298728943,
'Y': 0.6084995269775391}]},
'Id': 7,
'ParentId': 2,
'Type': 'WORD'},
{'Confidence': 88.95134735107422,
'DetectedText': 'Smiling',
'Geometry': {'BoundingBox': {'Height': 0.4326171875,
'Left': 0.46289217472076416,
'Top': 0.5634765625,
'Width': 0.5371078252792358},
'Polygon': [{'X': 0.46289217472076416,
'Y': 0.5634765625},
{'X': 1.0, 'Y': 0.5634765625},
{'X': 1.0, 'Y': 0.99609375},
{'X': 0.46289217472076416,
'Y': 0.99609375}]},
'Id': 8,
'ParentId': 3,
'Type': 'WORD'}],
'TextModelVersion': '3.0'}
Image(画像分析)のメソッド:その他のメソッドについて
ここからはハッカソンでは使用頻度が思いますので、メソッド名と概要だけに留めさせていただきます。
9.create_collection
create_collectionメソッドは、特定のリージョンにコレクションを作成します。
10.list_collections
list_collectionsメソッドは、アカウント内のコレクションID情報を返します。
11.describe_collection
describe_collectionメソッドは、特定のコレクションの情報を返します。
12.list_faces
list_facesメソッドは、特定のコレクションにある顔のメタデータを返します。
13.search_faces_by_image
search_faces_by_imageメソッドは、入力画像内の最も大きい顔と類似する顔をコレクションから検索します。
14.search_faces
search_facesメソッドは、指定した顔IDと類似する顔をコレクションから検索する。
15.delete_faces
delete_facesメソッドは、特定のコレクションから顔情報を削除します。
16.delete_collection
delete_collectionメソッドは、特定のコレクションを削除します。
17.tag_resource
tag_resourceメソッドは、特定のリソースにタグを追加します。
18.list_tags_for_resource
list_tags_for_resourceメソッドは、コレクションとカスタムモデルラベル内のタグリストを返します。
19.untag_resource
untag_resourceメソッドは、特定のリソースからタグを削除します。
20.can_paginate
untag_resourceメソッドは、各メソッドのページネーション有無を調べます。
21.get_paginator
get_paginatorメソッドは、メソッドに関するページネータを生成します。
22.generate_presigned_url
get_paginatorメソッドは、署名済みURLを返します。