比较图像并保存在dynamodb aws

时间:2018-06-05 11:37:45

标签: python amazon-web-services amazon-s3 aws-lambda

嗨我想写一个lambda函数,它会起作用。我在s3桶中有两个文件夹。在第一个框中有“所有者”,第二个有随机图片。我想将所有图片与所有者进行比较,然后在每个图片中保存带有所有者名称的dynamodb。 Atm我在面部检测的API中迷失了,做了这样的事情

    BUCKET = "ais-django"
KEY = "20180530105812.jpeg"
FEATURES_BLACKLIST = ("Landmarks", "Emotions", "Pose", "Quality", "BoundingBox", "Confidence")


def detect_faces(bucket, key, attributes=['ALL'], region="eu-west-1"):
    rekognition = boto3.client("rekognition", region)
    response = rekognition.detect_faces(
        Image={
            "S3Object": {
                "Bucket": bucket,
                "Name": key,
            }
        },
        Attributes=attributes,
    )
    return response['FaceDetails']


for face in detect_faces(BUCKET, KEY):
    print
    "Face ({Confidence}%)".format(**face)
    # emotions
    for emotion in face['Emotions']:
        print
        "  {Type} : {Confidence}%".format(**emotion)
    # quality
    for quality, value in face['Quality'].iteritems():
        print
        "  {quality} : {value}".format(quality=quality, value=value)
    # facial features
    for feature, data in face.iteritems():
        if feature not in FEATURES_BLACKLIST:
            print
            "  {feature}({data[Value]}) : {data[Confidence]}%".format(feature=feature, data=data)

1 个答案:

答案 0 :(得分:1)

您可以使用Rekognition客户端的compare_faces操作。这是一个反映操作的伪代码(注意:此代码未经过测试,仅用于显示概念)。您可以根据需要调整相似度阈值。

client = boto3.client('rekognition', region_name='eu-west-1')
keyNamesInsideRandomFolder=['1.jpg','2.jpg']
for key in keyNamesInsideRandomFolder:
    response = client.detect_faces(
        'S3Object': {
           'Bucket': "bucketname",
           'Name': "randomfolder/"+key
        }
    )
    faceDetails = response['FaceDetails']
    hasFace = len(faceDetails) > 0
    if hasFace:
        response = client.compare_faces(
            SimilarityThreshold=90,
            SourceImage={
                'S3Object': {
                    'Bucket': "bucketname",
                    'Name': "ownerfolder/ownerimage.jpg"
                }
            },
            TargetImage={
                'S3Object': {
                    'Bucket': "bucketname",
                    'Name': "randomfolder/"+key
                },
            }
        )
        faceMatch= response['FaceMatches']
        similarity = faceMatch['Similarity']
        if similarity>90:
             #write to dynamodb

编辑:要从具有前缀/文件夹'random'的文件夹中获取对象列表,请使用s3客户端的list_objects操作。

response = client.list_objects(
    Bucket='bucketname',
    Prefix='random'
)
numberofobjects=len(response['Contents'])
keyNamesInsideRandomFolder=[]
for x in range(1, numberofobjects):
    keyNamesInsideRandomFolder.append(response['Contents'][x]['Key'])

注意:响应['Contents'] [x] ['Key']返回对象的键名和前缀。例如。如果您在随机文件夹中有一个文件名为img.jpg的图像,则返回“random / img.jpg”。注意我从1开始for循环,因为作为响应返回的第一个元素只是文件夹的键名,在这种情况下是“random /”。