SDKExemples d'utilisation d'Amazon Rekognition pour Python (Boto3) - Exemples de code de l'AWS SDK

D'autres AWS SDK exemples sont disponibles dans le GitHub dépôt AWS Doc SDK Examples.

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

SDKExemples d'utilisation d'Amazon Rekognition pour Python (Boto3)

Les exemples de code suivants vous montrent comment effectuer des actions et implémenter des scénarios courants à l' AWS SDK for Python (Boto3) aide d'Amazon Rekognition.

Les actions sont des extraits de code de programmes plus larges et doivent être exécutées dans leur contexte. Alors que les actions vous indiquent comment appeler des fonctions de service individuelles, vous pouvez les voir en contexte dans leurs scénarios associés.

Les Scénarios sont des exemples de code qui vous montrent comment accomplir des tâches spécifiques en appelant plusieurs fonctions au sein d’un même service ou combinés à d’autres Services AWS.

Chaque exemple inclut un lien vers le code source complet, où vous trouverez des instructions sur la façon de configurer et d'exécuter le code en contexte.

Actions

L'exemple de code suivant montre comment utiliserCompareFaces.

Pour de plus amples informations, veuillez consulter Comparaison de visages dans des images.

SDKpour Python (Boto3)
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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
  • Pour API plus de détails, reportez-vous CompareFacesà la section AWS SDKpour la référence Python (Boto3). API

L'exemple de code suivant montre comment utiliserCreateCollection.

Pour plus d'informations, consultez Création d'une collection.

SDKpour Python (Boto3)
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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
  • Pour API plus de détails, reportez-vous CreateCollectionà la section AWS SDKpour la référence Python (Boto3). API

L'exemple de code suivant montre comment utiliserDeleteCollection.

Pour plus d'informations, consultez Suppression d'une collection.

SDKpour Python (Boto3)
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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
  • Pour API plus de détails, reportez-vous DeleteCollectionà la section AWS SDKpour la référence Python (Boto3). API

L'exemple de code suivant montre comment utiliserDeleteFaces.

Pour plus d'informations, veuillez consulter Supprimer des visages d'une collection.

SDKpour Python (Boto3)
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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
  • Pour API plus de détails, reportez-vous DeleteFacesà la section AWS SDKpour la référence Python (Boto3). API

L'exemple de code suivant montre comment utiliserDescribeCollection.

Pour plus d'informations, veuillez consulter Description d'une collection.

SDKpour Python (Boto3)
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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()
  • Pour API plus de détails, reportez-vous DescribeCollectionà la section AWS SDKpour la référence Python (Boto3). API

L'exemple de code suivant montre comment utiliserDetectFaces.

Pour plus d'informations, veuillez consulter Détecter des visages dans une image.

SDKpour Python (Boto3)
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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
  • Pour API plus de détails, reportez-vous DetectFacesà la section AWS SDKpour la référence Python (Boto3). API

L'exemple de code suivant montre comment utiliserDetectLabels.

Pour plus d'informations, veuillez consulter Détection des étiquettes dans une image.

SDKpour Python (Boto3)
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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
  • Pour API plus de détails, reportez-vous DetectLabelsà la section AWS SDKpour la référence Python (Boto3). API

L'exemple de code suivant montre comment utiliserDetectModerationLabels.

Pour plus d'informations, veuillez consulter Détecter des images inappropriées.

SDKpour Python (Boto3)
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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
  • Pour API plus de détails, reportez-vous DetectModerationLabelsà la section AWS SDKpour la référence Python (Boto3). API

L'exemple de code suivant montre comment utiliserDetectText.

Pour plus d'informations, consultez Détection de texte dans une image.

SDKpour Python (Boto3)
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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
  • Pour API plus de détails, reportez-vous DetectTextà la section AWS SDKpour la référence Python (Boto3). API

L'exemple de code suivant montre comment utiliserDisassociateFaces.

SDKpour Python (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()
  • Pour API plus de détails, reportez-vous DisassociateFacesà la section AWS SDKpour la référence Python (Boto3). API

L'exemple de code suivant montre comment utiliserIndexFaces.

Pour plus d'informations, veuillez consulter Ajouter des visages à une collection.

SDKpour Python (Boto3)
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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
  • Pour API plus de détails, reportez-vous IndexFacesà la section AWS SDKpour la référence Python (Boto3). API

L'exemple de code suivant montre comment utiliserListCollections.

Pour en savoir plus, consultez Répertoriage de collections.

SDKpour Python (Boto3)
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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
  • Pour API plus de détails, reportez-vous ListCollectionsà la section AWS SDKpour la référence Python (Boto3). API

L'exemple de code suivant montre comment utiliserListFaces.

Pour plus d'informations, consultez Répertoriage de visages d'une collection.

SDKpour Python (Boto3)
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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
  • Pour API plus de détails, reportez-vous ListFacesà la section AWS SDKpour la référence Python (Boto3). API

L'exemple de code suivant montre comment utiliserRecognizeCelebrities.

Pour plus d'informations, consultez Reconnaissance de célébrités dans une image.

SDKpour Python (Boto3)
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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
  • Pour API plus de détails, reportez-vous RecognizeCelebritiesà la section AWS SDKpour la référence Python (Boto3). API

L'exemple de code suivant montre comment utiliserSearchFaces.

Pour plus d'informations, veuillez consulter Recherche d'un visage (identification faciale).

SDKpour Python (Boto3)
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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
  • Pour API plus de détails, reportez-vous SearchFacesà la section AWS SDKpour la référence Python (Boto3). API

L'exemple de code suivant montre comment utiliserSearchFacesByImage.

Pour plus d'informations, voir Recherche d'un visage (image).

SDKpour Python (Boto3)
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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
  • Pour API plus de détails, reportez-vous SearchFacesByImageà la section AWS SDKpour la référence Python (Boto3). API

Scénarios

L’exemple de code suivant illustre comment :

  • Créer une collection Amazon Rekognition.

  • Ajouter des images à la collection et détecter les visages qu'elle contient.

  • Rechercher dans la collection les visages qui correspondent à une image de référence.

  • Supprimer une collection.

Pour plus d'informations, veuillez consulter Recherche de visages dans une collection.

SDKpour Python (Boto3)
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

Créez des classes qui encapsulent les fonctions 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

Utilisez les classes wrapper pour créer une collection de visages à partir d'un ensemble d'images, puis recherchez des visages dans la collection.

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)

L’exemple de code suivant illustre comment :

  • Détecter des éléments dans des images à l'aide d'Amazon Rekognition.

  • Afficher des images et tracer des cadres autour des éléments détectés.

Pour plus d'informations, veuillez consulter Affichage des cadres de délimitation.

SDKpour Python (Boto3)
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

Créez des classes pour encapsuler les fonctions 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

Créez des fonctions d'assistance pour dessiner des cadres de délimitation et des polygones.

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()

Créer des classes pour analyser les objets renvoyés par 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

Utilisez les classes wrapper pour détecter des éléments dans les images et afficher leurs cadres de délimitation. Les images utilisées dans cet exemple se trouvent ici, GitHub ainsi que des instructions et du code supplémentaire.

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)

L'exemple de code suivant montre comment créer une application qui utilise Amazon Rekognition pour détecter des objets par catégorie dans des images.

SDKpour Python (Boto3)

Vous montre comment utiliser le AWS SDK for Python (Boto3) pour créer une application Web qui vous permet d'effectuer les opérations suivantes :

  • Chargez les photos dans un compartiment Amazon Simple Storage Service (Amazon S3).

  • Utilisez Amazon Rekognition pour analyser et étiqueter les photos.

  • Utilisez Amazon Simple Email Service (AmazonSES) pour envoyer par e-mail des rapports d'analyse d'images.

Cet exemple contient deux composants principaux : une page Web écrite en JavaScript React et un REST service écrit en Python construit avec Flask-RESTful.

Vous pouvez utiliser la page web React pour :

  • Affichez une liste d'images stockées dans votre compartiment S3.

  • Chargez des images depuis votre ordinateur dans votre compartiment S3.

  • Affichez des images et des étiquettes qui identifient les éléments détectés dans l'image.

  • Obtenez un rapport de toutes les images de votre compartiment S3 et envoyez un e-mail du rapport.

La page Web appelle le REST service. Le service envoie des demandes à AWS pour effectuer les opérations suivantes :

  • Obtenez et filtrez la liste des images de votre compartiment S3.

  • Chargez des photos dans votre compartiment S3.

  • Utilisez Amazon Rekognition pour analyser des photos individuelles et obtenir une liste d'étiquettes qui identifient les éléments détectés sur la photo.

  • Analysez toutes les photos de votre compartiment S3 et utilisez Amazon SES pour envoyer un rapport par e-mail.

Pour obtenir le code source complet et les instructions de configuration et d'exécution, consultez l'exemple complet sur GitHub.

Les services utilisés dans cet exemple
  • Amazon Rekognition

  • Amazon S3

  • Amazon SES

L'exemple de code suivant montre comment détecter des personnes et des objets dans une vidéo avec Amazon Rekognition.

SDKpour Python (Boto3)

Utilisez Amazon Rekognition pour détecter des visages, des objets et des personnes dans des vidéos en démarrant des tâches de détection asynchrone. Cet exemple configure également Amazon Rekognition pour avertir un sujet Amazon Simple Notification Service SNS (Amazon) lorsque les tâches sont terminées et abonne une file d'attente Amazon Simple Queue Service SQS (Amazon) au sujet. Lorsque la file d’attente reçoit un message concernant une tâche, elle est récupérée et les résultats sont affichés.

Il est préférable de visionner cet exemple sur GitHub. Pour obtenir le code source complet et les instructions de configuration et d'exécution, consultez l'exemple complet sur GitHub.

Les services utilisés dans cet exemple
  • Amazon Rekognition

  • Amazon SNS

  • Amazon SQS