0

嗨,在 aws 上,我有两个文件夹 1 是老板,老板的所有图像都在其中并使用 indexfacesApi 进行索引,现在我想修改此代码以使用文件夹“事件”中的所有图像并存储在新表中。喜欢在camparision之后,我得到了3张老板名字myboss的照片

所以在新的数据库条目中

image1 我的老板 image4 我的老板

其他老板也是一样的情况。ATM使用这个

import boto3
import io
from PIL import Image

rekognition = boto3.client('rekognition', region_name='eu-west-1')
dynamodb = boto3.client('dynamodb', region_name='eu-west-1')

image = Image.open("group1.jpeg")
stream = io.BytesIO()
image.save(stream,format="JPEG")
image_binary = stream.getvalue()


response = rekognition.search_faces_by_image(
        CollectionId='family_collection',
        Image={'Bytes':image_binary}                                       
        )

for match in response['FaceMatches']:
    print (match['Face']['FaceId'],match['Face']['Confidence'])

    face = dynamodb.get_item(
        TableName='family_collection',  
        Key={'RekognitionId': {'S': match['Face']['FaceId']}}
        )

    if 'Item' in face:
        print (face['Item']['FullName']['S'])
    else:
        print ('no match found in person lookup')
4

1 回答 1

0

此示例可以帮助您,这将获取Event/文件夹中的所有图像,然后与 rekognition 中的集合 ID进行比较,如果找到匹配项,则该图像将保存在新文件夹结果中,并从集合相关数据库中检索名称将结果存储在dynamodb的新表中

from __future__ import print_function

import boto3
import io
import uuid
import urllib
from PIL import Image
import datetime
from pprint import pprint


def lambda_handler(event, context):
    rekognition = boto3.client('rekognition')
    dynamodb = boto3.client('dynamodb')
    s3 = boto3.resource('s3')
    s3client=boto3.client('s3')

    Bucket=s3.Bucket('ais-django');
    obj = s3.Object('ais-django','Event/')

    i = datetime.datetime.now()
    ptr= i.strftime("%d-%m-%y     %H:%M:%S (UTC)")

    for obj in Bucket.objects.filter(Prefix='Event/'):
        filename=obj.key
        if filename.endswith(".jpeg") or filename.endswith(".jpg") or filename.endswith(".png"):
            key=str(filename)
            bucket='ais-django'
            print(key)

            local = '/tmp/'+ptr+'.jpeg'

            s3client.download_file(bucket,key, local)

            image = Image.open(local)
            stream = io.BytesIO()
            image.save(stream,format="JPEG")

            image_binary = stream.getvalue()
            response = rekognition.detect_faces(
                Image={'Bytes':image_binary}
                )


            all_faces=response['FaceDetails']

            boxes = []
            image_width = image.size[0]
            image_height = image.size[1]

            for face in all_faces:
                box=face['BoundingBox']
                x1 = int(box['Left'] * image_width) * 0.9
                y1 = int(box['Top'] * image_height) * 0.9
                x2 = int(box['Left'] * image_width + box['Width'] * image_width) * 1.10
                y2 = int(box['Top'] * image_height + box['Height']  * image_height) * 1.10
                image_crop = image.crop((x1,y1,x2,y2))

                stream = io.BytesIO()
                image_crop.save(stream,format="JPEG")
                image_crop_binary = stream.getvalue()

                response = rekognition.search_faces_by_image(
                    CollectionId='family_collection',
                    Image={'Bytes':image_crop_binary}
                    )

                if len(response['FaceMatches']) > 0:

                    for match in response['FaceMatches']:
                        face = dynamodb.get_item(
                            TableName='family_collection',
                            Key={'RekognitionId': {'S': match['Face']['FaceId']}}
                            )
                        if 'Item' in face:
                            person = face['Item']['FullName']['S']
                            print(person);
                            ktr='results/'+person+'.jpg'
                            s3client.upload_file(local, 'ais-django', ktr,ExtraArgs={'ContentType': 'image/jpeg','ACL': 'public-read'})
                            dynamodb.put_item(
                                TableName='results',
                                Item={
                                    'image': {'S': key},
                                    'FullName': {'S': person},
                                    'date': {'S': ptr}
                                }
                            )                               
                else:
                    print('null')

用你的替换bucketnamecollection-id name 、dynamodb 表名

于 2018-06-07T11:18:12.777 回答