文档 AWS SDK 示例 GitHub 存储库中还有更多 S AWS DK 示例
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用 SDK for Python (Boto3) 的 Amazon Rekognition 示例
以下代码示例向您展示了如何使用 AWS SDK for Python (Boto3) 与 Amazon Rekognition 配合使用来执行操作和实现常见场景。
操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。
场景是向您演示如何通过在一个服务中调用多个函数或与其他 AWS 服务结合来完成特定任务的代码示例。
每个示例都包含一个指向完整源代码的链接,您可以从中找到有关如何在上下文中设置和运行代码的说明。
操作
以下代码示例演示如何使用 CompareFaces
。
有关更多信息,请参阅比较图像中的人脸。
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 class RekognitionImage: """ Encapsulates an Amazon Rekognition image. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, image, image_name, rekognition_client): """ Initializes the image object. :param image: Data that defines the image, either the image bytes or an Amazon S3 bucket and object key. :param image_name: The name of the image. :param rekognition_client: A Boto3 Rekognition client. """ self.image = image self.image_name = image_name self.rekognition_client = rekognition_client def compare_faces(self, target_image, similarity): """ Compares faces in the image with the largest face in the target image. :param target_image: The target image to compare against. :param similarity: Faces in the image must have a similarity value greater than this value to be included in the results. :return: A tuple. The first element is the list of faces that match the reference image. The second element is the list of faces that have a similarity value below the specified threshold. """ try: response = self.rekognition_client.compare_faces( SourceImage=self.image, TargetImage=target_image.image, SimilarityThreshold=similarity, ) matches = [ RekognitionFace(match["Face"]) for match in response["FaceMatches"] ] unmatches = [RekognitionFace(face) for face in response["UnmatchedFaces"]] logger.info( "Found %s matched faces and %s unmatched faces.", len(matches), len(unmatches), ) except ClientError: logger.exception( "Couldn't match faces from %s to %s.", self.image_name, target_image.image_name, ) raise else: return matches, unmatches
-
有关 API 的详细信息,请参阅适用CompareFaces于 Python 的AWS SDK (Boto3) API 参考。
-
以下代码示例演示如何使用 CreateCollection
。
有关更多信息,请参阅创建集合。
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 class RekognitionCollectionManager: """ Encapsulates Amazon Rekognition collection management functions. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, rekognition_client): """ Initializes the collection manager object. :param rekognition_client: A Boto3 Rekognition client. """ self.rekognition_client = rekognition_client def create_collection(self, collection_id): """ Creates an empty collection. :param collection_id: Text that identifies the collection. :return: The newly created collection. """ try: response = self.rekognition_client.create_collection( CollectionId=collection_id ) response["CollectionId"] = collection_id collection = RekognitionCollection(response, self.rekognition_client) logger.info("Created collection %s.", collection_id) except ClientError: logger.exception("Couldn't create collection %s.", collection_id) raise else: return collection
-
有关 API 的详细信息,请参阅适用CreateCollection于 Python 的AWS SDK (Boto3) API 参考。
-
以下代码示例演示如何使用 DeleteCollection
。
有关更多信息,请参阅删除集合。
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 class RekognitionCollection: """ Encapsulates an Amazon Rekognition collection. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, collection, rekognition_client): """ Initializes a collection object. :param collection: Collection data in the format returned by a call to create_collection. :param rekognition_client: A Boto3 Rekognition client. """ self.collection_id = collection["CollectionId"] self.collection_arn, self.face_count, self.created = self._unpack_collection( collection ) self.rekognition_client = rekognition_client @staticmethod def _unpack_collection(collection): """ Unpacks optional parts of a collection that can be returned by describe_collection. :param collection: The collection data. :return: A tuple of the data in the collection. """ return ( collection.get("CollectionArn"), collection.get("FaceCount", 0), collection.get("CreationTimestamp"), ) def delete_collection(self): """ Deletes the collection. """ try: self.rekognition_client.delete_collection(CollectionId=self.collection_id) logger.info("Deleted collection %s.", self.collection_id) self.collection_id = None except ClientError: logger.exception("Couldn't delete collection %s.", self.collection_id) raise
-
有关 API 的详细信息,请参阅适用DeleteCollection于 Python 的AWS SDK (Boto3) API 参考。
-
以下代码示例演示如何使用 DeleteFaces
。
有关更多信息,请参阅从集合中删除人脸。
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 class RekognitionCollection: """ Encapsulates an Amazon Rekognition collection. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, collection, rekognition_client): """ Initializes a collection object. :param collection: Collection data in the format returned by a call to create_collection. :param rekognition_client: A Boto3 Rekognition client. """ self.collection_id = collection["CollectionId"] self.collection_arn, self.face_count, self.created = self._unpack_collection( collection ) self.rekognition_client = rekognition_client @staticmethod def _unpack_collection(collection): """ Unpacks optional parts of a collection that can be returned by describe_collection. :param collection: The collection data. :return: A tuple of the data in the collection. """ return ( collection.get("CollectionArn"), collection.get("FaceCount", 0), collection.get("CreationTimestamp"), ) def delete_faces(self, face_ids): """ Deletes faces from the collection. :param face_ids: The list of IDs of faces to delete. :return: The list of IDs of faces that were deleted. """ try: response = self.rekognition_client.delete_faces( CollectionId=self.collection_id, FaceIds=face_ids ) deleted_ids = response["DeletedFaces"] logger.info( "Deleted %s faces from %s.", len(deleted_ids), self.collection_id ) except ClientError: logger.exception("Couldn't delete faces from %s.", self.collection_id) raise else: return deleted_ids
-
有关 API 的详细信息,请参阅适用DeleteFaces于 Python 的AWS SDK (Boto3) API 参考。
-
以下代码示例演示如何使用 DescribeCollection
。
有关更多信息,请参阅描述集合。
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 class RekognitionCollection: """ Encapsulates an Amazon Rekognition collection. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, collection, rekognition_client): """ Initializes a collection object. :param collection: Collection data in the format returned by a call to create_collection. :param rekognition_client: A Boto3 Rekognition client. """ self.collection_id = collection["CollectionId"] self.collection_arn, self.face_count, self.created = self._unpack_collection( collection ) self.rekognition_client = rekognition_client @staticmethod def _unpack_collection(collection): """ Unpacks optional parts of a collection that can be returned by describe_collection. :param collection: The collection data. :return: A tuple of the data in the collection. """ return ( collection.get("CollectionArn"), collection.get("FaceCount", 0), collection.get("CreationTimestamp"), ) def describe_collection(self): """ Gets data about the collection from the Amazon Rekognition service. :return: The collection rendered as a dict. """ try: response = self.rekognition_client.describe_collection( CollectionId=self.collection_id ) # Work around capitalization of Arn vs. ARN response["CollectionArn"] = response.get("CollectionARN") ( self.collection_arn, self.face_count, self.created, ) = self._unpack_collection(response) logger.info("Got data for collection %s.", self.collection_id) except ClientError: logger.exception("Couldn't get data for collection %s.", self.collection_id) raise else: return self.to_dict()
-
有关 API 的详细信息,请参阅适用DescribeCollection于 Python 的AWS SDK (Boto3) API 参考。
-
以下代码示例演示如何使用 DetectFaces
。
有关更多信息,请参阅检测图像中的人脸。
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 class RekognitionImage: """ Encapsulates an Amazon Rekognition image. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, image, image_name, rekognition_client): """ Initializes the image object. :param image: Data that defines the image, either the image bytes or an Amazon S3 bucket and object key. :param image_name: The name of the image. :param rekognition_client: A Boto3 Rekognition client. """ self.image = image self.image_name = image_name self.rekognition_client = rekognition_client def detect_faces(self): """ Detects faces in the image. :return: The list of faces found in the image. """ try: response = self.rekognition_client.detect_faces( Image=self.image, Attributes=["ALL"] ) faces = [RekognitionFace(face) for face in response["FaceDetails"]] logger.info("Detected %s faces.", len(faces)) except ClientError: logger.exception("Couldn't detect faces in %s.", self.image_name) raise else: return faces
-
有关 API 的详细信息,请参阅适用DetectFaces于 Python 的AWS SDK (Boto3) API 参考。
-
以下代码示例演示如何使用 DetectLabels
。
有关更多信息,请参阅检测图像中的标签。
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 class RekognitionImage: """ Encapsulates an Amazon Rekognition image. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, image, image_name, rekognition_client): """ Initializes the image object. :param image: Data that defines the image, either the image bytes or an Amazon S3 bucket and object key. :param image_name: The name of the image. :param rekognition_client: A Boto3 Rekognition client. """ self.image = image self.image_name = image_name self.rekognition_client = rekognition_client def detect_labels(self, max_labels): """ Detects labels in the image. Labels are objects and people. :param max_labels: The maximum number of labels to return. :return: The list of labels detected in the image. """ try: response = self.rekognition_client.detect_labels( Image=self.image, MaxLabels=max_labels ) labels = [RekognitionLabel(label) for label in response["Labels"]] logger.info("Found %s labels in %s.", len(labels), self.image_name) except ClientError: logger.info("Couldn't detect labels in %s.", self.image_name) raise else: return labels
-
有关 API 的详细信息,请参阅适用DetectLabels于 Python 的AWS SDK (Boto3) API 参考。
-
以下代码示例演示如何使用 DetectModerationLabels
。
有关更多信息,请参阅检测不适宜的图像。
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 class RekognitionImage: """ Encapsulates an Amazon Rekognition image. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, image, image_name, rekognition_client): """ Initializes the image object. :param image: Data that defines the image, either the image bytes or an Amazon S3 bucket and object key. :param image_name: The name of the image. :param rekognition_client: A Boto3 Rekognition client. """ self.image = image self.image_name = image_name self.rekognition_client = rekognition_client def detect_moderation_labels(self): """ Detects moderation labels in the image. Moderation labels identify content that may be inappropriate for some audiences. :return: The list of moderation labels found in the image. """ try: response = self.rekognition_client.detect_moderation_labels( Image=self.image ) labels = [ RekognitionModerationLabel(label) for label in response["ModerationLabels"] ] logger.info( "Found %s moderation labels in %s.", len(labels), self.image_name ) except ClientError: logger.exception( "Couldn't detect moderation labels in %s.", self.image_name ) raise else: return labels
-
有关 API 的详细信息,请参阅适用DetectModerationLabels于 Python 的AWS SDK (Boto3) API 参考。
-
以下代码示例演示如何使用 DetectText
。
有关更多信息,请参阅检测图像中的文本。
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 class RekognitionImage: """ Encapsulates an Amazon Rekognition image. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, image, image_name, rekognition_client): """ Initializes the image object. :param image: Data that defines the image, either the image bytes or an Amazon S3 bucket and object key. :param image_name: The name of the image. :param rekognition_client: A Boto3 Rekognition client. """ self.image = image self.image_name = image_name self.rekognition_client = rekognition_client def detect_text(self): """ Detects text in the image. :return The list of text elements found in the image. """ try: response = self.rekognition_client.detect_text(Image=self.image) texts = [RekognitionText(text) for text in response["TextDetections"]] logger.info("Found %s texts in %s.", len(texts), self.image_name) except ClientError: logger.exception("Couldn't detect text in %s.", self.image_name) raise else: return texts
-
有关 API 的详细信息,请参阅适用DetectText于 Python 的AWS SDK (Boto3) API 参考。
-
以下代码示例演示如何使用 DisassociateFaces
。
- 适用于 Python 的 SDK(Boto3)
-
from botocore.exceptions import ClientError import boto3 import logging logger = logging.getLogger(__name__) session = boto3.Session(profile_name='profile-name') client = session.client('rekognition') def disassociate_faces(collection_id, user_id, face_ids): """ Disassociate stored faces within collection to the given user :param collection_id: The ID of the collection where user and faces are stored. :param user_id: The ID of the user that we want to disassociate faces from :param face_ids: The list of face IDs to be disassociated from the given user :return: response of AssociateFaces API """ logger.info(f'Disssociating faces from user: {user_id}, {face_ids}') try: response = client.disassociate_faces( CollectionId=collection_id, UserId=user_id, FaceIds=face_ids ) print(f'- disassociated {len(response["DisassociatedFaces"])} faces') except ClientError: logger.exception("Failed to disassociate faces from the given user") raise else: print(response) return response def main(): face_ids = ["faceId1", "faceId2"] collection_id = "collection-id" user_id = "user-id" disassociate_faces(collection_id, user_id, face_ids) if __name__ == "__main__": main()
-
有关 API 的详细信息,请参阅适用DisassociateFaces于 Python 的AWS SDK (Boto3) API 参考。
-
以下代码示例演示如何使用 IndexFaces
。
有关更多信息,请参阅将人脸添加到集合中。
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 class RekognitionCollection: """ Encapsulates an Amazon Rekognition collection. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, collection, rekognition_client): """ Initializes a collection object. :param collection: Collection data in the format returned by a call to create_collection. :param rekognition_client: A Boto3 Rekognition client. """ self.collection_id = collection["CollectionId"] self.collection_arn, self.face_count, self.created = self._unpack_collection( collection ) self.rekognition_client = rekognition_client @staticmethod def _unpack_collection(collection): """ Unpacks optional parts of a collection that can be returned by describe_collection. :param collection: The collection data. :return: A tuple of the data in the collection. """ return ( collection.get("CollectionArn"), collection.get("FaceCount", 0), collection.get("CreationTimestamp"), ) def index_faces(self, image, max_faces): """ Finds faces in the specified image, indexes them, and stores them in the collection. :param image: The image to index. :param max_faces: The maximum number of faces to index. :return: A tuple. The first element is a list of indexed faces. The second element is a list of faces that couldn't be indexed. """ try: response = self.rekognition_client.index_faces( CollectionId=self.collection_id, Image=image.image, ExternalImageId=image.image_name, MaxFaces=max_faces, DetectionAttributes=["ALL"], ) indexed_faces = [ RekognitionFace({**face["Face"], **face["FaceDetail"]}) for face in response["FaceRecords"] ] unindexed_faces = [ RekognitionFace(face["FaceDetail"]) for face in response["UnindexedFaces"] ] logger.info( "Indexed %s faces in %s. Could not index %s faces.", len(indexed_faces), image.image_name, len(unindexed_faces), ) except ClientError: logger.exception("Couldn't index faces in image %s.", image.image_name) raise else: return indexed_faces, unindexed_faces
-
有关 API 的详细信息,请参阅适用IndexFaces于 Python 的AWS SDK (Boto3) API 参考。
-
以下代码示例演示如何使用 ListCollections
。
有关更多信息,请参阅列出集合。
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 class RekognitionCollectionManager: """ Encapsulates Amazon Rekognition collection management functions. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, rekognition_client): """ Initializes the collection manager object. :param rekognition_client: A Boto3 Rekognition client. """ self.rekognition_client = rekognition_client def list_collections(self, max_results): """ Lists collections for the current account. :param max_results: The maximum number of collections to return. :return: The list of collections for the current account. """ try: response = self.rekognition_client.list_collections(MaxResults=max_results) collections = [ RekognitionCollection({"CollectionId": col_id}, self.rekognition_client) for col_id in response["CollectionIds"] ] except ClientError: logger.exception("Couldn't list collections.") raise else: return collections
-
有关 API 的详细信息,请参阅适用ListCollections于 Python 的AWS SDK (Boto3) API 参考。
-
以下代码示例演示如何使用 ListFaces
。
有关更多信息,请参阅列出集合中的人脸。
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 class RekognitionCollection: """ Encapsulates an Amazon Rekognition collection. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, collection, rekognition_client): """ Initializes a collection object. :param collection: Collection data in the format returned by a call to create_collection. :param rekognition_client: A Boto3 Rekognition client. """ self.collection_id = collection["CollectionId"] self.collection_arn, self.face_count, self.created = self._unpack_collection( collection ) self.rekognition_client = rekognition_client @staticmethod def _unpack_collection(collection): """ Unpacks optional parts of a collection that can be returned by describe_collection. :param collection: The collection data. :return: A tuple of the data in the collection. """ return ( collection.get("CollectionArn"), collection.get("FaceCount", 0), collection.get("CreationTimestamp"), ) def list_faces(self, max_results): """ Lists the faces currently indexed in the collection. :param max_results: The maximum number of faces to return. :return: The list of faces in the collection. """ try: response = self.rekognition_client.list_faces( CollectionId=self.collection_id, MaxResults=max_results ) faces = [RekognitionFace(face) for face in response["Faces"]] logger.info( "Found %s faces in collection %s.", len(faces), self.collection_id ) except ClientError: logger.exception( "Couldn't list faces in collection %s.", self.collection_id ) raise else: return faces
-
有关 API 的详细信息,请参阅适用ListFaces于 Python 的AWS SDK (Boto3) API 参考。
-
以下代码示例演示如何使用 RecognizeCelebrities
。
有关更多信息,请参阅识别图像中的名人。
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 class RekognitionImage: """ Encapsulates an Amazon Rekognition image. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, image, image_name, rekognition_client): """ Initializes the image object. :param image: Data that defines the image, either the image bytes or an Amazon S3 bucket and object key. :param image_name: The name of the image. :param rekognition_client: A Boto3 Rekognition client. """ self.image = image self.image_name = image_name self.rekognition_client = rekognition_client def recognize_celebrities(self): """ Detects celebrities in the image. :return: A tuple. The first element is the list of celebrities found in the image. The second element is the list of faces that were detected but did not match any known celebrities. """ try: response = self.rekognition_client.recognize_celebrities(Image=self.image) celebrities = [ RekognitionCelebrity(celeb) for celeb in response["CelebrityFaces"] ] other_faces = [ RekognitionFace(face) for face in response["UnrecognizedFaces"] ] logger.info( "Found %s celebrities and %s other faces in %s.", len(celebrities), len(other_faces), self.image_name, ) except ClientError: logger.exception("Couldn't detect celebrities in %s.", self.image_name) raise else: return celebrities, other_faces
-
有关 API 的详细信息,请参阅适用RecognizeCelebrities于 Python 的AWS SDK (Boto3) API 参考。
-
以下代码示例演示如何使用 SearchFaces
。
有关更多信息,请参阅搜索人脸(人脸 ID)。
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 class RekognitionCollection: """ Encapsulates an Amazon Rekognition collection. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, collection, rekognition_client): """ Initializes a collection object. :param collection: Collection data in the format returned by a call to create_collection. :param rekognition_client: A Boto3 Rekognition client. """ self.collection_id = collection["CollectionId"] self.collection_arn, self.face_count, self.created = self._unpack_collection( collection ) self.rekognition_client = rekognition_client @staticmethod def _unpack_collection(collection): """ Unpacks optional parts of a collection that can be returned by describe_collection. :param collection: The collection data. :return: A tuple of the data in the collection. """ return ( collection.get("CollectionArn"), collection.get("FaceCount", 0), collection.get("CreationTimestamp"), ) def search_faces(self, face_id, threshold, max_faces): """ Searches for faces in the collection that match another face from the collection. :param face_id: The ID of the face in the collection to search for. :param threshold: The match confidence must be greater than this value for a face to be included in the results. :param max_faces: The maximum number of faces to return. :return: The list of matching faces found in the collection. This list does not contain the face specified by `face_id`. """ try: response = self.rekognition_client.search_faces( CollectionId=self.collection_id, FaceId=face_id, FaceMatchThreshold=threshold, MaxFaces=max_faces, ) faces = [RekognitionFace(face["Face"]) for face in response["FaceMatches"]] logger.info( "Found %s faces in %s that match %s.", len(faces), self.collection_id, face_id, ) except ClientError: logger.exception( "Couldn't search for faces in %s that match %s.", self.collection_id, face_id, ) raise else: return faces
-
有关 API 的详细信息,请参阅适用SearchFaces于 Python 的AWS SDK (Boto3) API 参考。
-
以下代码示例演示如何使用 SearchFacesByImage
。
有关更多信息,请参阅搜索人脸(图像)。
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 class RekognitionCollection: """ Encapsulates an Amazon Rekognition collection. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, collection, rekognition_client): """ Initializes a collection object. :param collection: Collection data in the format returned by a call to create_collection. :param rekognition_client: A Boto3 Rekognition client. """ self.collection_id = collection["CollectionId"] self.collection_arn, self.face_count, self.created = self._unpack_collection( collection ) self.rekognition_client = rekognition_client @staticmethod def _unpack_collection(collection): """ Unpacks optional parts of a collection that can be returned by describe_collection. :param collection: The collection data. :return: A tuple of the data in the collection. """ return ( collection.get("CollectionArn"), collection.get("FaceCount", 0), collection.get("CreationTimestamp"), ) def search_faces_by_image(self, image, threshold, max_faces): """ Searches for faces in the collection that match the largest face in the reference image. :param image: The image that contains the reference face to search for. :param threshold: The match confidence must be greater than this value for a face to be included in the results. :param max_faces: The maximum number of faces to return. :return: A tuple. The first element is the face found in the reference image. The second element is the list of matching faces found in the collection. """ try: response = self.rekognition_client.search_faces_by_image( CollectionId=self.collection_id, Image=image.image, FaceMatchThreshold=threshold, MaxFaces=max_faces, ) image_face = RekognitionFace( { "BoundingBox": response["SearchedFaceBoundingBox"], "Confidence": response["SearchedFaceConfidence"], } ) collection_faces = [ RekognitionFace(face["Face"]) for face in response["FaceMatches"] ] logger.info( "Found %s faces in the collection that match the largest " "face in %s.", len(collection_faces), image.image_name, ) except ClientError: logger.exception( "Couldn't search for faces in %s that match %s.", self.collection_id, image.image_name, ) raise else: return image_face, collection_faces
-
有关 API 的详细信息,请参阅适用SearchFacesByImage于 Python 的AWS SDK (Boto3) API 参考。
-
场景
以下代码示例展示了如何:
创建 Amazon Rekognition 集合。
将图像添加到集合中并检测其中的人脸。
在集合中搜索与参照图像相匹配的人脸。
删除集合。
有关更多信息,请参阅搜索集合中的人脸。
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 创建包装 Amazon Rekognition 函数的类。
import logging from pprint import pprint import boto3 from botocore.exceptions import ClientError from rekognition_objects import RekognitionFace from rekognition_image_detection import RekognitionImage logger = logging.getLogger(__name__) class RekognitionImage: """ Encapsulates an Amazon Rekognition image. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, image, image_name, rekognition_client): """ Initializes the image object. :param image: Data that defines the image, either the image bytes or an Amazon S3 bucket and object key. :param image_name: The name of the image. :param rekognition_client: A Boto3 Rekognition client. """ self.image = image self.image_name = image_name self.rekognition_client = rekognition_client @classmethod def from_file(cls, image_file_name, rekognition_client, image_name=None): """ Creates a RekognitionImage object from a local file. :param image_file_name: The file name of the image. The file is opened and its bytes are read. :param rekognition_client: A Boto3 Rekognition client. :param image_name: The name of the image. If this is not specified, the file name is used as the image name. :return: The RekognitionImage object, initialized with image bytes from the file. """ with open(image_file_name, "rb") as img_file: image = {"Bytes": img_file.read()} name = image_file_name if image_name is None else image_name return cls(image, name, rekognition_client) class RekognitionCollectionManager: """ Encapsulates Amazon Rekognition collection management functions. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, rekognition_client): """ Initializes the collection manager object. :param rekognition_client: A Boto3 Rekognition client. """ self.rekognition_client = rekognition_client def create_collection(self, collection_id): """ Creates an empty collection. :param collection_id: Text that identifies the collection. :return: The newly created collection. """ try: response = self.rekognition_client.create_collection( CollectionId=collection_id ) response["CollectionId"] = collection_id collection = RekognitionCollection(response, self.rekognition_client) logger.info("Created collection %s.", collection_id) except ClientError: logger.exception("Couldn't create collection %s.", collection_id) raise else: return collection def list_collections(self, max_results): """ Lists collections for the current account. :param max_results: The maximum number of collections to return. :return: The list of collections for the current account. """ try: response = self.rekognition_client.list_collections(MaxResults=max_results) collections = [ RekognitionCollection({"CollectionId": col_id}, self.rekognition_client) for col_id in response["CollectionIds"] ] except ClientError: logger.exception("Couldn't list collections.") raise else: return collections class RekognitionCollection: """ Encapsulates an Amazon Rekognition collection. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, collection, rekognition_client): """ Initializes a collection object. :param collection: Collection data in the format returned by a call to create_collection. :param rekognition_client: A Boto3 Rekognition client. """ self.collection_id = collection["CollectionId"] self.collection_arn, self.face_count, self.created = self._unpack_collection( collection ) self.rekognition_client = rekognition_client @staticmethod def _unpack_collection(collection): """ Unpacks optional parts of a collection that can be returned by describe_collection. :param collection: The collection data. :return: A tuple of the data in the collection. """ return ( collection.get("CollectionArn"), collection.get("FaceCount", 0), collection.get("CreationTimestamp"), ) def to_dict(self): """ Renders parts of the collection data to a dict. :return: The collection data as a dict. """ rendering = { "collection_id": self.collection_id, "collection_arn": self.collection_arn, "face_count": self.face_count, "created": self.created, } return rendering def describe_collection(self): """ Gets data about the collection from the Amazon Rekognition service. :return: The collection rendered as a dict. """ try: response = self.rekognition_client.describe_collection( CollectionId=self.collection_id ) # Work around capitalization of Arn vs. ARN response["CollectionArn"] = response.get("CollectionARN") ( self.collection_arn, self.face_count, self.created, ) = self._unpack_collection(response) logger.info("Got data for collection %s.", self.collection_id) except ClientError: logger.exception("Couldn't get data for collection %s.", self.collection_id) raise else: return self.to_dict() def delete_collection(self): """ Deletes the collection. """ try: self.rekognition_client.delete_collection(CollectionId=self.collection_id) logger.info("Deleted collection %s.", self.collection_id) self.collection_id = None except ClientError: logger.exception("Couldn't delete collection %s.", self.collection_id) raise def index_faces(self, image, max_faces): """ Finds faces in the specified image, indexes them, and stores them in the collection. :param image: The image to index. :param max_faces: The maximum number of faces to index. :return: A tuple. The first element is a list of indexed faces. The second element is a list of faces that couldn't be indexed. """ try: response = self.rekognition_client.index_faces( CollectionId=self.collection_id, Image=image.image, ExternalImageId=image.image_name, MaxFaces=max_faces, DetectionAttributes=["ALL"], ) indexed_faces = [ RekognitionFace({**face["Face"], **face["FaceDetail"]}) for face in response["FaceRecords"] ] unindexed_faces = [ RekognitionFace(face["FaceDetail"]) for face in response["UnindexedFaces"] ] logger.info( "Indexed %s faces in %s. Could not index %s faces.", len(indexed_faces), image.image_name, len(unindexed_faces), ) except ClientError: logger.exception("Couldn't index faces in image %s.", image.image_name) raise else: return indexed_faces, unindexed_faces def list_faces(self, max_results): """ Lists the faces currently indexed in the collection. :param max_results: The maximum number of faces to return. :return: The list of faces in the collection. """ try: response = self.rekognition_client.list_faces( CollectionId=self.collection_id, MaxResults=max_results ) faces = [RekognitionFace(face) for face in response["Faces"]] logger.info( "Found %s faces in collection %s.", len(faces), self.collection_id ) except ClientError: logger.exception( "Couldn't list faces in collection %s.", self.collection_id ) raise else: return faces def search_faces(self, face_id, threshold, max_faces): """ Searches for faces in the collection that match another face from the collection. :param face_id: The ID of the face in the collection to search for. :param threshold: The match confidence must be greater than this value for a face to be included in the results. :param max_faces: The maximum number of faces to return. :return: The list of matching faces found in the collection. This list does not contain the face specified by `face_id`. """ try: response = self.rekognition_client.search_faces( CollectionId=self.collection_id, FaceId=face_id, FaceMatchThreshold=threshold, MaxFaces=max_faces, ) faces = [RekognitionFace(face["Face"]) for face in response["FaceMatches"]] logger.info( "Found %s faces in %s that match %s.", len(faces), self.collection_id, face_id, ) except ClientError: logger.exception( "Couldn't search for faces in %s that match %s.", self.collection_id, face_id, ) raise else: return faces def search_faces_by_image(self, image, threshold, max_faces): """ Searches for faces in the collection that match the largest face in the reference image. :param image: The image that contains the reference face to search for. :param threshold: The match confidence must be greater than this value for a face to be included in the results. :param max_faces: The maximum number of faces to return. :return: A tuple. The first element is the face found in the reference image. The second element is the list of matching faces found in the collection. """ try: response = self.rekognition_client.search_faces_by_image( CollectionId=self.collection_id, Image=image.image, FaceMatchThreshold=threshold, MaxFaces=max_faces, ) image_face = RekognitionFace( { "BoundingBox": response["SearchedFaceBoundingBox"], "Confidence": response["SearchedFaceConfidence"], } ) collection_faces = [ RekognitionFace(face["Face"]) for face in response["FaceMatches"] ] logger.info( "Found %s faces in the collection that match the largest " "face in %s.", len(collection_faces), image.image_name, ) except ClientError: logger.exception( "Couldn't search for faces in %s that match %s.", self.collection_id, image.image_name, ) raise else: return image_face, collection_faces class RekognitionFace: """Encapsulates an Amazon Rekognition face.""" def __init__(self, face, timestamp=None): """ Initializes the face object. :param face: Face data, in the format returned by Amazon Rekognition functions. :param timestamp: The time when the face was detected, if the face was detected in a video. """ self.bounding_box = face.get("BoundingBox") self.confidence = face.get("Confidence") self.landmarks = face.get("Landmarks") self.pose = face.get("Pose") self.quality = face.get("Quality") age_range = face.get("AgeRange") if age_range is not None: self.age_range = (age_range.get("Low"), age_range.get("High")) else: self.age_range = None self.smile = face.get("Smile", {}).get("Value") self.eyeglasses = face.get("Eyeglasses", {}).get("Value") self.sunglasses = face.get("Sunglasses", {}).get("Value") self.gender = face.get("Gender", {}).get("Value", None) self.beard = face.get("Beard", {}).get("Value") self.mustache = face.get("Mustache", {}).get("Value") self.eyes_open = face.get("EyesOpen", {}).get("Value") self.mouth_open = face.get("MouthOpen", {}).get("Value") self.emotions = [ emo.get("Type") for emo in face.get("Emotions", []) if emo.get("Confidence", 0) > 50 ] self.face_id = face.get("FaceId") self.image_id = face.get("ImageId") self.timestamp = timestamp def to_dict(self): """ Renders some of the face data to a dict. :return: A dict that contains the face data. """ rendering = {} if self.bounding_box is not None: rendering["bounding_box"] = self.bounding_box if self.age_range is not None: rendering["age"] = f"{self.age_range[0]} - {self.age_range[1]}" if self.gender is not None: rendering["gender"] = self.gender if self.emotions: rendering["emotions"] = self.emotions if self.face_id is not None: rendering["face_id"] = self.face_id if self.image_id is not None: rendering["image_id"] = self.image_id if self.timestamp is not None: rendering["timestamp"] = self.timestamp has = [] if self.smile: has.append("smile") if self.eyeglasses: has.append("eyeglasses") if self.sunglasses: has.append("sunglasses") if self.beard: has.append("beard") if self.mustache: has.append("mustache") if self.eyes_open: has.append("open eyes") if self.mouth_open: has.append("open mouth") if has: rendering["has"] = has return rendering
使用包装类从一组图像中构建人脸集合,然后在集合中搜索人脸。
def usage_demo(): print("-" * 88) print("Welcome to the Amazon Rekognition face collection demo!") print("-" * 88) logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") rekognition_client = boto3.client("rekognition") images = [ RekognitionImage.from_file( ".media/pexels-agung-pandit-wiguna-1128316.jpg", rekognition_client, image_name="sitting", ), RekognitionImage.from_file( ".media/pexels-agung-pandit-wiguna-1128317.jpg", rekognition_client, image_name="hopping", ), RekognitionImage.from_file( ".media/pexels-agung-pandit-wiguna-1128318.jpg", rekognition_client, image_name="biking", ), ] collection_mgr = RekognitionCollectionManager(rekognition_client) collection = collection_mgr.create_collection("doc-example-collection-demo") print(f"Created collection {collection.collection_id}:") pprint(collection.describe_collection()) print("Indexing faces from three images:") for image in images: collection.index_faces(image, 10) print("Listing faces in collection:") faces = collection.list_faces(10) for face in faces: pprint(face.to_dict()) input("Press Enter to continue.") print( f"Searching for faces in the collection that match the first face in the " f"list (Face ID: {faces[0].face_id}." ) found_faces = collection.search_faces(faces[0].face_id, 80, 10) print(f"Found {len(found_faces)} matching faces.") for face in found_faces: pprint(face.to_dict()) input("Press Enter to continue.") print( f"Searching for faces in the collection that match the largest face in " f"{images[0].image_name}." ) image_face, match_faces = collection.search_faces_by_image(images[0], 80, 10) print(f"The largest face in {images[0].image_name} is:") pprint(image_face.to_dict()) print(f"Found {len(match_faces)} matching faces.") for face in match_faces: pprint(face.to_dict()) input("Press Enter to continue.") collection.delete_collection() print("Thanks for watching!") print("-" * 88)
以下代码示例展示了如何:
使用 Amazon Rekognition 检测图像中的元素。
显示图像并在检测到的元素周围绘制边界框。
有关更多信息,请参阅显示边界框。
- 适用于 Python 的 SDK(Boto3)
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 创建类来包装 Amazon Rekognition 函数。
import logging from pprint import pprint import boto3 from botocore.exceptions import ClientError import requests from rekognition_objects import ( RekognitionFace, RekognitionCelebrity, RekognitionLabel, RekognitionModerationLabel, RekognitionText, show_bounding_boxes, show_polygons, ) logger = logging.getLogger(__name__) class RekognitionImage: """ Encapsulates an Amazon Rekognition image. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, image, image_name, rekognition_client): """ Initializes the image object. :param image: Data that defines the image, either the image bytes or an Amazon S3 bucket and object key. :param image_name: The name of the image. :param rekognition_client: A Boto3 Rekognition client. """ self.image = image self.image_name = image_name self.rekognition_client = rekognition_client @classmethod def from_file(cls, image_file_name, rekognition_client, image_name=None): """ Creates a RekognitionImage object from a local file. :param image_file_name: The file name of the image. The file is opened and its bytes are read. :param rekognition_client: A Boto3 Rekognition client. :param image_name: The name of the image. If this is not specified, the file name is used as the image name. :return: The RekognitionImage object, initialized with image bytes from the file. """ with open(image_file_name, "rb") as img_file: image = {"Bytes": img_file.read()} name = image_file_name if image_name is None else image_name return cls(image, name, rekognition_client) @classmethod def from_bucket(cls, s3_object, rekognition_client): """ Creates a RekognitionImage object from an Amazon S3 object. :param s3_object: An Amazon S3 object that identifies the image. The image is not retrieved until needed for a later call. :param rekognition_client: A Boto3 Rekognition client. :return: The RekognitionImage object, initialized with Amazon S3 object data. """ image = {"S3Object": {"Bucket": s3_object.bucket_name, "Name": s3_object.key}} return cls(image, s3_object.key, rekognition_client) def detect_faces(self): """ Detects faces in the image. :return: The list of faces found in the image. """ try: response = self.rekognition_client.detect_faces( Image=self.image, Attributes=["ALL"] ) faces = [RekognitionFace(face) for face in response["FaceDetails"]] logger.info("Detected %s faces.", len(faces)) except ClientError: logger.exception("Couldn't detect faces in %s.", self.image_name) raise else: return faces def detect_labels(self, max_labels): """ Detects labels in the image. Labels are objects and people. :param max_labels: The maximum number of labels to return. :return: The list of labels detected in the image. """ try: response = self.rekognition_client.detect_labels( Image=self.image, MaxLabels=max_labels ) labels = [RekognitionLabel(label) for label in response["Labels"]] logger.info("Found %s labels in %s.", len(labels), self.image_name) except ClientError: logger.info("Couldn't detect labels in %s.", self.image_name) raise else: return labels def recognize_celebrities(self): """ Detects celebrities in the image. :return: A tuple. The first element is the list of celebrities found in the image. The second element is the list of faces that were detected but did not match any known celebrities. """ try: response = self.rekognition_client.recognize_celebrities(Image=self.image) celebrities = [ RekognitionCelebrity(celeb) for celeb in response["CelebrityFaces"] ] other_faces = [ RekognitionFace(face) for face in response["UnrecognizedFaces"] ] logger.info( "Found %s celebrities and %s other faces in %s.", len(celebrities), len(other_faces), self.image_name, ) except ClientError: logger.exception("Couldn't detect celebrities in %s.", self.image_name) raise else: return celebrities, other_faces def compare_faces(self, target_image, similarity): """ Compares faces in the image with the largest face in the target image. :param target_image: The target image to compare against. :param similarity: Faces in the image must have a similarity value greater than this value to be included in the results. :return: A tuple. The first element is the list of faces that match the reference image. The second element is the list of faces that have a similarity value below the specified threshold. """ try: response = self.rekognition_client.compare_faces( SourceImage=self.image, TargetImage=target_image.image, SimilarityThreshold=similarity, ) matches = [ RekognitionFace(match["Face"]) for match in response["FaceMatches"] ] unmatches = [RekognitionFace(face) for face in response["UnmatchedFaces"]] logger.info( "Found %s matched faces and %s unmatched faces.", len(matches), len(unmatches), ) except ClientError: logger.exception( "Couldn't match faces from %s to %s.", self.image_name, target_image.image_name, ) raise else: return matches, unmatches def detect_moderation_labels(self): """ Detects moderation labels in the image. Moderation labels identify content that may be inappropriate for some audiences. :return: The list of moderation labels found in the image. """ try: response = self.rekognition_client.detect_moderation_labels( Image=self.image ) labels = [ RekognitionModerationLabel(label) for label in response["ModerationLabels"] ] logger.info( "Found %s moderation labels in %s.", len(labels), self.image_name ) except ClientError: logger.exception( "Couldn't detect moderation labels in %s.", self.image_name ) raise else: return labels def detect_text(self): """ Detects text in the image. :return The list of text elements found in the image. """ try: response = self.rekognition_client.detect_text(Image=self.image) texts = [RekognitionText(text) for text in response["TextDetections"]] logger.info("Found %s texts in %s.", len(texts), self.image_name) except ClientError: logger.exception("Couldn't detect text in %s.", self.image_name) raise else: return texts
创建辅助函数来绘制边界框和多边形。
import io import logging from PIL import Image, ImageDraw logger = logging.getLogger(__name__) def show_bounding_boxes(image_bytes, box_sets, colors): """ Draws bounding boxes on an image and shows it with the default image viewer. :param image_bytes: The image to draw, as bytes. :param box_sets: A list of lists of bounding boxes to draw on the image. :param colors: A list of colors to use to draw the bounding boxes. """ image = Image.open(io.BytesIO(image_bytes)) draw = ImageDraw.Draw(image) for boxes, color in zip(box_sets, colors): for box in boxes: left = image.width * box["Left"] top = image.height * box["Top"] right = (image.width * box["Width"]) + left bottom = (image.height * box["Height"]) + top draw.rectangle([left, top, right, bottom], outline=color, width=3) image.show() def show_polygons(image_bytes, polygons, color): """ Draws polygons on an image and shows it with the default image viewer. :param image_bytes: The image to draw, as bytes. :param polygons: The list of polygons to draw on the image. :param color: The color to use to draw the polygons. """ image = Image.open(io.BytesIO(image_bytes)) draw = ImageDraw.Draw(image) for polygon in polygons: draw.polygon( [ (image.width * point["X"], image.height * point["Y"]) for point in polygon ], outline=color, ) image.show()
创建类来解析 Amazon Rekognition 返回的对象。
class RekognitionFace: """Encapsulates an Amazon Rekognition face.""" def __init__(self, face, timestamp=None): """ Initializes the face object. :param face: Face data, in the format returned by Amazon Rekognition functions. :param timestamp: The time when the face was detected, if the face was detected in a video. """ self.bounding_box = face.get("BoundingBox") self.confidence = face.get("Confidence") self.landmarks = face.get("Landmarks") self.pose = face.get("Pose") self.quality = face.get("Quality") age_range = face.get("AgeRange") if age_range is not None: self.age_range = (age_range.get("Low"), age_range.get("High")) else: self.age_range = None self.smile = face.get("Smile", {}).get("Value") self.eyeglasses = face.get("Eyeglasses", {}).get("Value") self.sunglasses = face.get("Sunglasses", {}).get("Value") self.gender = face.get("Gender", {}).get("Value", None) self.beard = face.get("Beard", {}).get("Value") self.mustache = face.get("Mustache", {}).get("Value") self.eyes_open = face.get("EyesOpen", {}).get("Value") self.mouth_open = face.get("MouthOpen", {}).get("Value") self.emotions = [ emo.get("Type") for emo in face.get("Emotions", []) if emo.get("Confidence", 0) > 50 ] self.face_id = face.get("FaceId") self.image_id = face.get("ImageId") self.timestamp = timestamp def to_dict(self): """ Renders some of the face data to a dict. :return: A dict that contains the face data. """ rendering = {} if self.bounding_box is not None: rendering["bounding_box"] = self.bounding_box if self.age_range is not None: rendering["age"] = f"{self.age_range[0]} - {self.age_range[1]}" if self.gender is not None: rendering["gender"] = self.gender if self.emotions: rendering["emotions"] = self.emotions if self.face_id is not None: rendering["face_id"] = self.face_id if self.image_id is not None: rendering["image_id"] = self.image_id if self.timestamp is not None: rendering["timestamp"] = self.timestamp has = [] if self.smile: has.append("smile") if self.eyeglasses: has.append("eyeglasses") if self.sunglasses: has.append("sunglasses") if self.beard: has.append("beard") if self.mustache: has.append("mustache") if self.eyes_open: has.append("open eyes") if self.mouth_open: has.append("open mouth") if has: rendering["has"] = has return rendering class RekognitionCelebrity: """Encapsulates an Amazon Rekognition celebrity.""" def __init__(self, celebrity, timestamp=None): """ Initializes the celebrity object. :param celebrity: Celebrity data, in the format returned by Amazon Rekognition functions. :param timestamp: The time when the celebrity was detected, if the celebrity was detected in a video. """ self.info_urls = celebrity.get("Urls") self.name = celebrity.get("Name") self.id = celebrity.get("Id") self.face = RekognitionFace(celebrity.get("Face")) self.confidence = celebrity.get("MatchConfidence") self.bounding_box = celebrity.get("BoundingBox") self.timestamp = timestamp def to_dict(self): """ Renders some of the celebrity data to a dict. :return: A dict that contains the celebrity data. """ rendering = self.face.to_dict() if self.name is not None: rendering["name"] = self.name if self.info_urls: rendering["info URLs"] = self.info_urls if self.timestamp is not None: rendering["timestamp"] = self.timestamp return rendering class RekognitionPerson: """Encapsulates an Amazon Rekognition person.""" def __init__(self, person, timestamp=None): """ Initializes the person object. :param person: Person data, in the format returned by Amazon Rekognition functions. :param timestamp: The time when the person was detected, if the person was detected in a video. """ self.index = person.get("Index") self.bounding_box = person.get("BoundingBox") face = person.get("Face") self.face = RekognitionFace(face) if face is not None else None self.timestamp = timestamp def to_dict(self): """ Renders some of the person data to a dict. :return: A dict that contains the person data. """ rendering = self.face.to_dict() if self.face is not None else {} if self.index is not None: rendering["index"] = self.index if self.bounding_box is not None: rendering["bounding_box"] = self.bounding_box if self.timestamp is not None: rendering["timestamp"] = self.timestamp return rendering class RekognitionLabel: """Encapsulates an Amazon Rekognition label.""" def __init__(self, label, timestamp=None): """ Initializes the label object. :param label: Label data, in the format returned by Amazon Rekognition functions. :param timestamp: The time when the label was detected, if the label was detected in a video. """ self.name = label.get("Name") self.confidence = label.get("Confidence") self.instances = label.get("Instances") self.parents = label.get("Parents") self.timestamp = timestamp def to_dict(self): """ Renders some of the label data to a dict. :return: A dict that contains the label data. """ rendering = {} if self.name is not None: rendering["name"] = self.name if self.timestamp is not None: rendering["timestamp"] = self.timestamp return rendering class RekognitionModerationLabel: """Encapsulates an Amazon Rekognition moderation label.""" def __init__(self, label, timestamp=None): """ Initializes the moderation label object. :param label: Label data, in the format returned by Amazon Rekognition functions. :param timestamp: The time when the moderation label was detected, if the label was detected in a video. """ self.name = label.get("Name") self.confidence = label.get("Confidence") self.parent_name = label.get("ParentName") self.timestamp = timestamp def to_dict(self): """ Renders some of the moderation label data to a dict. :return: A dict that contains the moderation label data. """ rendering = {} if self.name is not None: rendering["name"] = self.name if self.parent_name is not None: rendering["parent_name"] = self.parent_name if self.timestamp is not None: rendering["timestamp"] = self.timestamp return rendering class RekognitionText: """Encapsulates an Amazon Rekognition text element.""" def __init__(self, text_data): """ Initializes the text object. :param text_data: Text data, in the format returned by Amazon Rekognition functions. """ self.text = text_data.get("DetectedText") self.kind = text_data.get("Type") self.id = text_data.get("Id") self.parent_id = text_data.get("ParentId") self.confidence = text_data.get("Confidence") self.geometry = text_data.get("Geometry") def to_dict(self): """ Renders some of the text data to a dict. :return: A dict that contains the text data. """ rendering = {} if self.text is not None: rendering["text"] = self.text if self.kind is not None: rendering["kind"] = self.kind if self.geometry is not None: rendering["polygon"] = self.geometry.get("Polygon") return rendering
使用包装类来检测图像中的元素并显示其边界框。可以在上找到本示例中使用的图像 GitHub 以及说明和更多代码。
def usage_demo(): print("-" * 88) print("Welcome to the Amazon Rekognition image detection demo!") print("-" * 88) logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") rekognition_client = boto3.client("rekognition") street_scene_file_name = ".media/pexels-kaique-rocha-109919.jpg" celebrity_file_name = ".media/pexels-pixabay-53370.jpg" one_girl_url = "https://dhei5unw3vrsx.cloudfront.net/images/source3_resized.jpg" three_girls_url = "https://dhei5unw3vrsx.cloudfront.net/images/target3_resized.jpg" swimwear_object = boto3.resource("s3").Object( "console-sample-images-pdx", "yoga_swimwear.jpg" ) book_file_name = ".media/pexels-christina-morillo-1181671.jpg" street_scene_image = RekognitionImage.from_file( street_scene_file_name, rekognition_client ) print(f"Detecting faces in {street_scene_image.image_name}...") faces = street_scene_image.detect_faces() print(f"Found {len(faces)} faces, here are the first three.") for face in faces[:3]: pprint(face.to_dict()) show_bounding_boxes( street_scene_image.image["Bytes"], [[face.bounding_box for face in faces]], ["aqua"], ) input("Press Enter to continue.") print(f"Detecting labels in {street_scene_image.image_name}...") labels = street_scene_image.detect_labels(100) print(f"Found {len(labels)} labels.") for label in labels: pprint(label.to_dict()) names = [] box_sets = [] colors = ["aqua", "red", "white", "blue", "yellow", "green"] for label in labels: if label.instances: names.append(label.name) box_sets.append([inst["BoundingBox"] for inst in label.instances]) print(f"Showing bounding boxes for {names} in {colors[:len(names)]}.") show_bounding_boxes( street_scene_image.image["Bytes"], box_sets, colors[: len(names)] ) input("Press Enter to continue.") celebrity_image = RekognitionImage.from_file( celebrity_file_name, rekognition_client ) print(f"Detecting celebrities in {celebrity_image.image_name}...") celebs, others = celebrity_image.recognize_celebrities() print(f"Found {len(celebs)} celebrities.") for celeb in celebs: pprint(celeb.to_dict()) show_bounding_boxes( celebrity_image.image["Bytes"], [[celeb.face.bounding_box for celeb in celebs]], ["aqua"], ) input("Press Enter to continue.") girl_image_response = requests.get(one_girl_url) girl_image = RekognitionImage( {"Bytes": girl_image_response.content}, "one-girl", rekognition_client ) group_image_response = requests.get(three_girls_url) group_image = RekognitionImage( {"Bytes": group_image_response.content}, "three-girls", rekognition_client ) print("Comparing reference face to group of faces...") matches, unmatches = girl_image.compare_faces(group_image, 80) print(f"Found {len(matches)} face matching the reference face.") show_bounding_boxes( group_image.image["Bytes"], [[match.bounding_box for match in matches]], ["aqua"], ) input("Press Enter to continue.") swimwear_image = RekognitionImage.from_bucket(swimwear_object, rekognition_client) print(f"Detecting suggestive content in {swimwear_object.key}...") labels = swimwear_image.detect_moderation_labels() print(f"Found {len(labels)} moderation labels.") for label in labels: pprint(label.to_dict()) input("Press Enter to continue.") book_image = RekognitionImage.from_file(book_file_name, rekognition_client) print(f"Detecting text in {book_image.image_name}...") texts = book_image.detect_text() print(f"Found {len(texts)} text instances. Here are the first seven:") for text in texts[:7]: pprint(text.to_dict()) show_polygons( book_image.image["Bytes"], [text.geometry["Polygon"] for text in texts], "aqua" ) print("Thanks for watching!") print("-" * 88)
以下代码示例演示如何构建一个使用 Amazon Rekognition 按类别检测图像中对象的应用程序。
- 适用于 Python 的 SDK(Boto3)
-
向您展示如何使用创建 AWS SDK for Python (Boto3) 允许您执行以下操作的 Web 应用程序:
将照片上载到 Amazon Simple Storage Service (Amazon S3) 存储桶。
使用 Amazon Rekognition 来分析和标注照片。
使用 Amazon Simple Email Service (Amazon SES) 发送图像分析的电子邮件报告。
此示例包含两个主要组件:使用 React 构建的 JavaScript 网页和使用 Flask-RESTful 构建的用 Python 编写的 REST 服务。
可以使用 React 网页执行以下操作:
显示存储在 S3 存储桶中的图像列表。
将计算机中的图像上载到 S3 存储桶。
显示图像和用于识别图像中检测到的物品的标注。
获取 S3 存储桶中所有图像的报告并发送报告电子邮件。
该网页调用 REST 服务。该服务将请求发送到 AWS 以执行以下操作:
获取并筛选 S3 存储桶中的图像列表。
将照片上载到 S3 存储桶。
使用 Amazon Rekognition 分析各张照片并获取标注列表,这些标注用于识别在照片中检测到的物品。
分析 S3 存储桶中的所有照片,然后使用 Amazon SES 通过电子邮件发送报告。
有关如何设置和运行的完整源代码和说明,请参阅上的完整示例GitHub
。 本示例中使用的服务
Amazon Rekognition
Amazon S3
Amazon SES
以下代码示例展示了如何使用 Amazon Rekognition 检测视频中的人物和物体。
- 适用于 Python 的 SDK(Boto3)
-
通过启动异步检测任务,使用 Amazon Rekognition 来检测视频中的人脸、对象和人物。此示例还将 Amazon Rekognition 配置为在任务完成时通知 Amazon Simple Notification Service (Amazon SNS) 主题,并订阅该主题的 Amazon Simple Queue Service (Amazon SQS) 队列。当队列收到有关任务的消息时,将检索该任务并输出结果。
最好在上查看此示例 GitHub。有关如何设置和运行的完整源代码和说明,请参阅上的完整示例GitHub
。 本示例中使用的服务
Amazon Rekognition
Amazon SNS
Amazon SQS