【问题标题】:Download image from ImageID and collection using Amazon Rekognition使用 Amazon Rekognition 从 ImageID 和集合下载图像
【发布时间】:2023-03-16 17:08:01
【问题描述】:

我正在使用 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
  }
}

【问题讨论】:

    标签: amazon-web-services amazon-s3 aws-sdk amazon-rekognition


    【解决方案1】:

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

    来自Adding Faces to a Collection - Amazon Rekognition

    对于检测到的每个人脸,Amazon Rekognition 都会提取人脸特征并将特征信息存储在数据库中。此外,该命令存储在指定人脸集合中检测到的每个人脸的元数据。 Amazon Rekognition 不存储实际的图像字节数。

    您将原始图像存储在 Amazon S3 中的想法很好。但是,您可以通过在对search_faces_by_image() 的响应中使用BoundingBox 信息来改进它。

    当 Amazon Rekognition 将人脸添加到人脸集合时,它会搜索输入图像中最大的人脸。这张脸的位置由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))
    

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

    【讨论】:

    • 哇。那太棒了。非常感谢约翰。
    • 是的。 Rekognition 提到不保留原始图像。我想知道,在这种情况下 ImageId 的意义是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    • 1970-01-01
    相关资源
    最近更新 更多