如何使用python将图像中的面保存到AWS Rekognion上的集合中
我已经创建了一个集合名称,现在我想将图像添加到集合中,这样我就可以像比较面一样对它们运行函数。问题是,我不知道如何保存到我在运行诸如detect_faces之类的函数时创建的特定集合。AWS文档没有显示将CollectionId添加到这些函数中的语法,以将我检测到的面保存到集合中,以便以后使用它们进行比较。这就是将带有元数据的图像上载到s3的方式,每个图像应仅包含一个面,并提供一个名称作为元数据如何使用python将图像中的面保存到AWS Rekognion上的集合中,python,collections,amazon-rekognition,Python,Collections,Amazon Rekognition,我已经创建了一个集合名称,现在我想将图像添加到集合中,这样我就可以像比较面一样对它们运行函数。问题是,我不知道如何保存到我在运行诸如detect_faces之类的函数时创建的特定集合。AWS文档没有显示将CollectionId添加到这些函数中的语法,以将我检测到的面保存到集合中,以便以后使用它们进行比较。这就是将带有元数据的图像上载到s3的方式,每个图像应仅包含一个面,并提供一个名称作为元数据 import boto3 s3 = boto3.resource('s3') # Get lis
import boto3
s3 = boto3.resource('s3')
# Get list of objects for indexing
images=[('image01.jpeg','Albert Einstein'),
('image02.jpeg','Candy'),
('image03.jpeg','Armstrong'),
('image04.jpeg','Ram'),
('image05.jpeg','Peter'),
('image06.jpeg','Shashank')
]
# Iterate through list to upload objects to S3
for image in images:
file = open(image[0],'rb')
object = s3.Object('rekognition-pictures','index/'+ image[0])
ret = object.put(Body=file,
Metadata={'FullName':image[1]}
)
现在,您必须使用lambda函数进行索引,该函数将完成您的工作,并将面存储到您的rekognition集合,以及将每个面的名称存储在dynamodb表中,您可以在以后使用其他api(如compare faces)时引用这两个表
from __future__ import print_function
import boto3
from decimal import Decimal
import json
import urllib
print('Loading function')
dynamodb = boto3.client('dynamodb')
s3 = boto3.client('s3')
rekognition = boto3.client('rekognition')
# --------------- Helper Functions ------------------
def index_faces(bucket, key):
response = rekognition.index_faces(
Image={"S3Object":
{"Bucket": bucket,
"Name": key}},
CollectionId="family_collection")
return response
def update_index(tableName,faceId, fullName):
response = dynamodb.put_item(
TableName=tableName,
Item={
'RekognitionId': {'S': faceId},
'FullName': {'S': fullName}
}
)
# --------------- Main handler ------------------
def lambda_handler(event, context):
# Get the object from the event
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(
event['Records'][0]['s3']['object']['key'].encode('utf8'))
try:
# Calls Amazon Rekognition IndexFaces API to detect faces in S3 object
# to index faces into specified collection
response = index_faces(bucket, key)
# Commit faceId and full name object metadata to DynamoDB
if response['ResponseMetadata']['HTTPStatusCode'] == 200:
faceId = response['FaceRecords'][0]['Face']['FaceId']
ret = s3.head_object(Bucket=bucket,Key=key)
personFullName = ret['Metadata']['fullname']
update_index('family_collection',faceId,personFullName)
# Print response to console
print(response)
return response
except Exception as e:
print(e)
print("Error processing object {} from bucket {}. ".format(key, bucket))
raise e
这就是您将带元数据的图像上载到s3的方式,每个图像应仅包含一个面,并提供一个名称作为元数据
import boto3
s3 = boto3.resource('s3')
# Get list of objects for indexing
images=[('image01.jpeg','Albert Einstein'),
('image02.jpeg','Candy'),
('image03.jpeg','Armstrong'),
('image04.jpeg','Ram'),
('image05.jpeg','Peter'),
('image06.jpeg','Shashank')
]
# Iterate through list to upload objects to S3
for image in images:
file = open(image[0],'rb')
object = s3.Object('rekognition-pictures','index/'+ image[0])
ret = object.put(Body=file,
Metadata={'FullName':image[1]}
)
现在,您必须使用lambda函数进行索引,该函数将完成您的工作,并将面存储到您的rekognition集合,以及将每个面的名称存储在dynamodb表中,您可以在以后使用其他api(如compare faces)时引用这两个表
from __future__ import print_function
import boto3
from decimal import Decimal
import json
import urllib
print('Loading function')
dynamodb = boto3.client('dynamodb')
s3 = boto3.client('s3')
rekognition = boto3.client('rekognition')
# --------------- Helper Functions ------------------
def index_faces(bucket, key):
response = rekognition.index_faces(
Image={"S3Object":
{"Bucket": bucket,
"Name": key}},
CollectionId="family_collection")
return response
def update_index(tableName,faceId, fullName):
response = dynamodb.put_item(
TableName=tableName,
Item={
'RekognitionId': {'S': faceId},
'FullName': {'S': fullName}
}
)
# --------------- Main handler ------------------
def lambda_handler(event, context):
# Get the object from the event
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(
event['Records'][0]['s3']['object']['key'].encode('utf8'))
try:
# Calls Amazon Rekognition IndexFaces API to detect faces in S3 object
# to index faces into specified collection
response = index_faces(bucket, key)
# Commit faceId and full name object metadata to DynamoDB
if response['ResponseMetadata']['HTTPStatusCode'] == 200:
faceId = response['FaceRecords'][0]['Face']['FaceId']
ret = s3.head_object(Bucket=bucket,Key=key)
personFullName = ret['Metadata']['fullname']
update_index('family_collection',faceId,personFullName)
# Print response to console
print(response)
return response
except Exception as e:
print(e)
print("Error processing object {} from bucket {}. ".format(key, bucket))
raise e