使用 Amazon Rekognition 从 ImageID 和集合下载图像

Download image from ImageID and collection using Amazon Rekognition

我正在使用 AWS Rekognition 构建面部识别应用程序。我还想向用户显示与我的输入图像匹配的图像。当我们为图像建立索引时,AWS 是否也会保存图像?如果是,我怎样才能得到那个图像?

如果没有,我正在考虑这样做。我可以使用与 ExternalImageId 相同的名称将图像保存到 S3 中,以便在检测到人脸时,我从 Rekognition 读取外部 ID 并从 S3 中获取它。

如果有比这更好的方法,请告诉我。

我正在使用以下代码为集合中的图像编制索引:

import boto3
from PIL import Image
import io
import time

image1 = 'images/haad2.jpg'

client = boto3.client('rekognition')
image = Image.open(image1)

stream = io.BytesIO()
image.save(stream, format="JPEG")
image_binary = stream.getvalue()

response = client.index_faces(
    Image={
        'Bytes': image_binary
    },
    CollectionId='root_faces_data',
    ExternalImageId=str(time.time()),
    DetectionAttributes=[
        'ALL',
    ]
)

print(response)

以及下面的代码来查看集合中是否存在人脸:

import boto3
import io
from PIL import Image

client = boto3.client('rekognition')

image1 = 'images/haad2.jpg'

client = boto3.client('rekognition')
image = Image.open(image1)

stream = io.BytesIO()
image.save(stream, format="JPEG")
image_binary = stream.getvalue()

response = client.search_faces_by_image(
    CollectionId='root_faces_data',
    Image={
        'Bytes': image_binary
    },
    MaxFaces=10,
    FaceMatchThreshold=90
)

print(response)

这是search_faces_by_image的输出:

{
  'SearchedFaceBoundingBox': {
    'Width': 0.2646464705467224,
    'Height': 0.39817628264427185,
    'Left': 0.3186868727207184,
    'Top': 0.23252280056476593
  },
  'SearchedFaceConfidence': 99.9957275390625,
  'FaceMatches': [
    {
      'Similarity': 99.98405456542969,
      'Face': {
        'FaceId': '5bc98595-7d30-4447-b430-4c0fd8f1b926',
        'BoundingBox': {
          'Width': 0.2646459937095642,
          'Height': 0.39817601442337036,
          'Left': 0.31868699193000793,
          'Top': 0.23252299427986145
        },
        'ImageId': '8e631731-4a0c-513d-be32-dbfe3ae5e813',
        'ExternalImageId': '1534576206.8314612',
        'Confidence': 99.9957046508789
      }
    }
  ],
  'FaceModelVersion': '3.0',
  'ResponseMetadata': {
    'RequestId': 'eca4bea6-a2b5-11e8-9345-a5eddf19f47f',
    'HTTPStatusCode': 200,
    'HTTPHeaders': {
      'content-type': 'application/x-amz-json-1.1',
      'date': 'Sat, 18 Aug 2018 07:12:09 GMT',
      'x-amzn-requestid': 'eca4bea6-a2b5-11e8-9345-a5eddf19f47f',
      'content-length': '553',
      'connection': 'keep-alive'
    },
    'RetryAttempts': 0
  }
}

没有。将人脸添加到 Face Collection 时,不会保留原始图像。

来自Adding Faces to a Collection - Amazon Rekognition

For each face detected, Amazon Rekognition extracts facial features and stores the feature information in a database. In addition, the command stores metadata for each face that's detected in the specified face collection. Amazon Rekognition doesn't store the actual image bytes.

您将原始图像存储在 Amazon S3 中的想法很好。但是,您可以通过在对 search_faces_by_image().

的响应中使用 BoundingBox 信息来改进它

当 Amazon Rekognition 将人脸添加到人脸 Collection 时,它会搜索输入图像中最大的人脸。这张脸的位置由BoundingBox.

提供

在存储原始图像之前,您应该使用 BoundingBox 尺寸裁剪图像。请注意,这些是相对于维度大小的,因此您需要使用类似:

(int(face['BoundingBox']['Left'] * width),
 int(face['BoundingBox']['Top'] * height)),
(int((face['BoundingBox']['Left'] + face['BoundingBox']['Width']) * width),
 int((face['BoundingBox']['Top'] + face['BoundingBox']['Height']) * height))

结果是您的所有图像都将聚焦在面部本身上。