Hay más AWS SDK ejemplos disponibles en el GitHub repositorio de AWS Doc SDK Examples
Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.
IndexFaces
Úselo con un AWS SDK o CLI
En los siguientes ejemplos de código, se muestra cómo utilizar IndexFaces
.
Para obtener información, consulte Adición de rostros a una colección.
- .NET
-
- AWS SDK for .NET
-
nota
Hay más en marcha GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.Rekognition; using Amazon.Rekognition.Model; /// <summary> /// Uses the Amazon Rekognition Service to detect faces in an image /// that has been uploaded to an Amazon Simple Storage Service (Amazon S3) /// bucket and then adds the information to a collection. /// </summary> public class AddFaces { public static async Task Main() { string collectionId = "MyCollection2"; string bucket = "amzn-s3-demo-bucket"; string photo = "input.jpg"; var rekognitionClient = new AmazonRekognitionClient(); var image = new Image { S3Object = new S3Object { Bucket = bucket, Name = photo, }, }; var indexFacesRequest = new IndexFacesRequest { Image = image, CollectionId = collectionId, ExternalImageId = photo, DetectionAttributes = new List<string>() { "ALL" }, }; IndexFacesResponse indexFacesResponse = await rekognitionClient.IndexFacesAsync(indexFacesRequest); Console.WriteLine($"{photo} added"); foreach (FaceRecord faceRecord in indexFacesResponse.FaceRecords) { Console.WriteLine($"Face detected: Faceid is {faceRecord.Face.FaceId}"); } } }
-
Para API obtener más información, consulte IndexFacesla AWS SDK for .NET APIReferencia.
-
- CLI
-
- AWS CLI
-
Adición de rostros a una colección
El siguiente comando
index-faces
añade los rostros que se encuentran en una imagen a la colección especificada.aws rekognition index-faces \ --image '
{"S3Object":{"Bucket":"MyVideoS3Bucket","Name":"MyPicture.jpg"}}
' \ --collection-idMyCollection
\ --max-faces1
\ --quality-filter"AUTO"
\ --detection-attributes"ALL"
\ --external-image-id"MyPicture.jpg"
Salida:
{ "FaceRecords": [ { "FaceDetail": { "Confidence": 99.993408203125, "Eyeglasses": { "Confidence": 99.11750030517578, "Value": false }, "Sunglasses": { "Confidence": 99.98249053955078, "Value": false }, "Gender": { "Confidence": 99.92769622802734, "Value": "Male" }, "Landmarks": [ { "Y": 0.26750367879867554, "X": 0.6202793717384338, "Type": "eyeLeft" }, { "Y": 0.26642778515815735, "X": 0.6787431836128235, "Type": "eyeRight" }, { "Y": 0.31361380219459534, "X": 0.6421601176261902, "Type": "nose" }, { "Y": 0.3495299220085144, "X": 0.6216195225715637, "Type": "mouthLeft" }, { "Y": 0.35194727778434753, "X": 0.669899046421051, "Type": "mouthRight" }, { "Y": 0.26844894886016846, "X": 0.6210268139839172, "Type": "leftPupil" }, { "Y": 0.26707562804222107, "X": 0.6817160844802856, "Type": "rightPupil" }, { "Y": 0.24834522604942322, "X": 0.6018546223640442, "Type": "leftEyeBrowLeft" }, { "Y": 0.24397172033786774, "X": 0.6172008514404297, "Type": "leftEyeBrowUp" }, { "Y": 0.24677404761314392, "X": 0.6339119076728821, "Type": "leftEyeBrowRight" }, { "Y": 0.24582654237747192, "X": 0.6619398593902588, "Type": "rightEyeBrowLeft" }, { "Y": 0.23973053693771362, "X": 0.6804757118225098, "Type": "rightEyeBrowUp" }, { "Y": 0.24441994726657867, "X": 0.6978968977928162, "Type": "rightEyeBrowRight" }, { "Y": 0.2695908546447754, "X": 0.6085202693939209, "Type": "leftEyeLeft" }, { "Y": 0.26716896891593933, "X": 0.6315826177597046, "Type": "leftEyeRight" }, { "Y": 0.26289820671081543, "X": 0.6202316880226135, "Type": "leftEyeUp" }, { "Y": 0.27123287320137024, "X": 0.6205548048019409, "Type": "leftEyeDown" }, { "Y": 0.2668408751487732, "X": 0.6663622260093689, "Type": "rightEyeLeft" }, { "Y": 0.26741549372673035, "X": 0.6910083889961243, "Type": "rightEyeRight" }, { "Y": 0.2614026665687561, "X": 0.6785826086997986, "Type": "rightEyeUp" }, { "Y": 0.27075251936912537, "X": 0.6789616942405701, "Type": "rightEyeDown" }, { "Y": 0.3211299479007721, "X": 0.6324167847633362, "Type": "noseLeft" }, { "Y": 0.32276326417922974, "X": 0.6558475494384766, "Type": "noseRight" }, { "Y": 0.34385165572166443, "X": 0.6444970965385437, "Type": "mouthUp" }, { "Y": 0.3671635091304779, "X": 0.6459195017814636, "Type": "mouthDown" } ], "Pose": { "Yaw": -9.54541015625, "Roll": -0.5709401965141296, "Pitch": 0.6045494675636292 }, "Emotions": [ { "Confidence": 39.90074157714844, "Type": "HAPPY" }, { "Confidence": 23.38753890991211, "Type": "CALM" }, { "Confidence": 5.840933322906494, "Type": "CONFUSED" } ], "AgeRange": { "High": 63, "Low": 45 }, "EyesOpen": { "Confidence": 99.80887603759766, "Value": true }, "BoundingBox": { "Width": 0.18562500178813934, "Top": 0.1618015021085739, "Left": 0.5575000047683716, "Height": 0.24770642817020416 }, "Smile": { "Confidence": 99.69740295410156, "Value": false }, "MouthOpen": { "Confidence": 99.97393798828125, "Value": false }, "Quality": { "Sharpness": 95.54405975341797, "Brightness": 63.867706298828125 }, "Mustache": { "Confidence": 97.05007934570312, "Value": false }, "Beard": { "Confidence": 87.34505462646484, "Value": false } }, "Face": { "BoundingBox": { "Width": 0.18562500178813934, "Top": 0.1618015021085739, "Left": 0.5575000047683716, "Height": 0.24770642817020416 }, "FaceId": "ce7ed422-2132-4a11-ab14-06c5c410f29f", "ExternalImageId": "example-image.jpg", "Confidence": 99.993408203125, "ImageId": "8d67061e-90d2-598f-9fbd-29c8497039c0" } } ], "UnindexedFaces": [], "FaceModelVersion": "3.0", "OrientationCorrection": "ROTATE_0" }
Para obtener más información, consulte Agregar rostros a una colección en la Guía para desarrolladores de Amazon Rekognition.
-
Para API obtener más información, consulte IndexFaces
la Referencia de AWS CLI comandos.
-
- Java
-
- SDKpara Java 2.x
-
nota
Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. import software.amazon.awssdk.core.SdkBytes; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.rekognition.RekognitionClient; import software.amazon.awssdk.services.rekognition.model.IndexFacesResponse; import software.amazon.awssdk.services.rekognition.model.IndexFacesRequest; import software.amazon.awssdk.services.rekognition.model.Image; import software.amazon.awssdk.services.rekognition.model.QualityFilter; import software.amazon.awssdk.services.rekognition.model.Attribute; import software.amazon.awssdk.services.rekognition.model.FaceRecord; import software.amazon.awssdk.services.rekognition.model.UnindexedFace; import software.amazon.awssdk.services.rekognition.model.RekognitionException; import software.amazon.awssdk.services.rekognition.model.Reason; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class AddFacesToCollection { public static void main(String[] args) { final String usage = """ Usage: <collectionId> <sourceImage> Where: collectionName - The name of the collection. sourceImage - The path to the image (for example, C:\\AWS\\pic1.png).\s """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String collectionId = args[0]; String sourceImage = args[1]; Region region = Region.US_EAST_1; RekognitionClient rekClient = RekognitionClient.builder() .region(region) .build(); addToCollection(rekClient, collectionId, sourceImage); rekClient.close(); } public static void addToCollection(RekognitionClient rekClient, String collectionId, String sourceImage) { try { InputStream sourceStream = new FileInputStream(sourceImage); SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream); Image souImage = Image.builder() .bytes(sourceBytes) .build(); IndexFacesRequest facesRequest = IndexFacesRequest.builder() .collectionId(collectionId) .image(souImage) .maxFaces(1) .qualityFilter(QualityFilter.AUTO) .detectionAttributes(Attribute.DEFAULT) .build(); IndexFacesResponse facesResponse = rekClient.indexFaces(facesRequest); System.out.println("Results for the image"); System.out.println("\n Faces indexed:"); List<FaceRecord> faceRecords = facesResponse.faceRecords(); for (FaceRecord faceRecord : faceRecords) { System.out.println(" Face ID: " + faceRecord.face().faceId()); System.out.println(" Location:" + faceRecord.faceDetail().boundingBox().toString()); } List<UnindexedFace> unindexedFaces = facesResponse.unindexedFaces(); System.out.println("Faces not indexed:"); for (UnindexedFace unindexedFace : unindexedFaces) { System.out.println(" Location:" + unindexedFace.faceDetail().boundingBox().toString()); System.out.println(" Reasons:"); for (Reason reason : unindexedFace.reasons()) { System.out.println("Reason: " + reason); } } } catch (RekognitionException | FileNotFoundException e) { System.out.println(e.getMessage()); System.exit(1); } } }
-
Para API obtener más información, consulte IndexFacesla AWS SDK for Java 2.x APIReferencia.
-
- Kotlin
-
- SDKpara Kotlin
-
nota
Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. suspend fun addToCollection( collectionIdVal: String?, sourceImage: String, ) { val souImage = Image { bytes = (File(sourceImage).readBytes()) } val request = IndexFacesRequest { collectionId = collectionIdVal image = souImage maxFaces = 1 qualityFilter = QualityFilter.Auto detectionAttributes = listOf(Attribute.Default) } RekognitionClient { region = "us-east-1" }.use { rekClient -> val facesResponse = rekClient.indexFaces(request) // Display the results. println("Results for the image") println("\n Faces indexed:") facesResponse.faceRecords?.forEach { faceRecord -> println("Face ID: ${faceRecord.face?.faceId}") println("Location: ${faceRecord.faceDetail?.boundingBox}") } println("Faces not indexed:") facesResponse.unindexedFaces?.forEach { unindexedFace -> println("Location: ${unindexedFace.faceDetail?.boundingBox}") println("Reasons:") unindexedFace.reasons?.forEach { reason -> println("Reason: $reason") } } } }
-
Para API obtener más información, consulta IndexFaces
la AWS SDKAPIreferencia sobre Kotlin.
-
- Python
-
- SDKpara Python (Boto3)
-
nota
Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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
-
Para API obtener más información, consulte IndexFacesla AWS SDKreferencia de Python (Boto3). API
-