Detección de rostros en un vídeo almacenado - Amazon Rekognition

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.

Detección de rostros en un vídeo almacenado

Amazon Rekognition Video puede detectar rostros en vídeos almacenados en un bucket de Amazon S3 y proporcionar información como:

  • Las veces que los rostros se detectan en un vídeo.

  • La ubicación de los rostros en un fotograma de vídeo a la hora en la que se detectan.

  • Referencias faciales como, por ejemplo, la posición del ojo izquierdo.

  • Atributos adicionales, tal y como se explica en la página Directrices sobre los atributos faciales.

La detección de rostros de Amazon Rekognition Video en vídeos almacenados una operación asíncrona. Para iniciar la detección de rostros en los vídeos, llame. StartFaceDetection Amazon Rekognition Video publica el estado de finalización de una operación de análisis de video en un tema de Amazon Simple Notification Service (Amazon SNS). Si el análisis de vídeo es correcto, puede llamar a GetFaceDetection para obtener los resultados del análisis de vídeo. Para obtener más información sobre cómo iniciar el análisis de vídeo y obtener los resultados, consulte Cómo llamar a las operaciones de Amazon Rekognition Video.

Este procedimiento amplía el código de Análisis de un vídeo almacenado en un bucket de Amazon S3 con Java o Python (SDK), que utiliza una cola de Amazon Simple Queue Service (Amazon SQS) para obtener el estado de realización de una solicitud de análisis de vídeo.

Para detectar rostros en un vídeo almacenado en un bucket de Amazon S3 (SDK)
  1. Realice Análisis de un vídeo almacenado en un bucket de Amazon S3 con Java o Python (SDK).

  2. Añada el código siguiente a la clase VideoDetect que ha creado en el paso 1.

    AWS CLI
    • En el siguiente ejemplo de código, cambie bucket-name y video-name por el nombre del bucket de Amazon S3 y el nombre de archivo que especificó en el paso 2.

    • Cambie region-name por la región de AWS que está utilizando. Sustituya el valor de profile_name de por el nombre de su perfil de desarrollador.

    • Reemplace TopicARN por el ARN del tema de Amazon SNS que creó en el paso 3 de Configuración de Amazon Rekognition Video.

    • Cambie RoleARN por el ARN del rol de servicio de IAM que creó en el paso 7 de Configuración de Amazon Rekognition Video.

    aws rekognition start-face-detection --video "{"S3Object":{"Bucket":"Bucket-Name","Name":"Video-Name"}}" --notification-channel \ "{"SNSTopicArn":"Topic-ARN","RoleArn":"Role-ARN"}" --region region-name --profile profile-name

    Si accede a la CLI en un dispositivo Windows, utilice comillas dobles en lugar de comillas simples y evite las comillas dobles internas con una barra invertida (es decir, \) para corregir cualquier error del analizador que pueda encontrar. Por ver un ejemplo, consulte lo siguiente:

    aws rekognition start-face-detection --video "{\"S3Object\":{\"Bucket\":\"Bucket-Name\",\"Name\":\"Video-Name\"}}" --notification-channel \ "{\"SNSTopicArn\":\"Topic-ARN\",\"RoleArn\":\"Role-ARN\"}" --region region-name --profile profile-name

    Tras ejecutar la operación StartFaceDetection y obtener el número de ID del trabajo, ejecute la siguiente operación GetFaceDetection e indique el número de ID del trabajo:

    aws rekognition get-face-detection --job-id job-id-number --profile profile-name
    Java
    //Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. //PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-developer-guide/blob/master/LICENSE-SAMPLECODE.) private static void StartFaceDetection(String bucket, String video) throws Exception{ NotificationChannel channel= new NotificationChannel() .withSNSTopicArn(snsTopicArn) .withRoleArn(roleArn); StartFaceDetectionRequest req = new StartFaceDetectionRequest() .withVideo(new Video() .withS3Object(new S3Object() .withBucket(bucket) .withName(video))) .withNotificationChannel(channel); StartFaceDetectionResult startLabelDetectionResult = rek.startFaceDetection(req); startJobId=startLabelDetectionResult.getJobId(); } private static void GetFaceDetectionResults() throws Exception{ int maxResults=10; String paginationToken=null; GetFaceDetectionResult faceDetectionResult=null; do{ if (faceDetectionResult !=null){ paginationToken = faceDetectionResult.getNextToken(); } faceDetectionResult = rek.getFaceDetection(new GetFaceDetectionRequest() .withJobId(startJobId) .withNextToken(paginationToken) .withMaxResults(maxResults)); VideoMetadata videoMetaData=faceDetectionResult.getVideoMetadata(); System.out.println("Format: " + videoMetaData.getFormat()); System.out.println("Codec: " + videoMetaData.getCodec()); System.out.println("Duration: " + videoMetaData.getDurationMillis()); System.out.println("FrameRate: " + videoMetaData.getFrameRate()); //Show faces, confidence and detection times List<FaceDetection> faces= faceDetectionResult.getFaces(); for (FaceDetection face: faces) { long seconds=face.getTimestamp()/1000; System.out.print("Sec: " + Long.toString(seconds) + " "); System.out.println(face.getFace().toString()); System.out.println(); } } while (faceDetectionResult !=null && faceDetectionResult.getNextToken() != null); }

    En la función main, reemplace las líneas:

    StartLabelDetection(bucket, video); if (GetSQSMessageSuccess()==true) GetLabelDetectionResults();

    por:

    StartFaceDetection(bucket, video); if (GetSQSMessageSuccess()==true) GetFaceDetectionResults();
    Java V2

    Este código se ha tomado del GitHub repositorio de ejemplos del SDK de AWS documentación. Consulte el ejemplo completo aquí.

    //snippet-start:[rekognition.java2.recognize_video_faces.import] import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.rekognition.RekognitionClient; import software.amazon.awssdk.services.rekognition.model.*; import java.util.List; //snippet-end:[rekognition.java2.recognize_video_faces.import] /** * 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 VideoDetectFaces { private static String startJobId =""; public static void main(String[] args) { final String usage = "\n" + "Usage: " + " <bucket> <video> <topicArn> <roleArn>\n\n" + "Where:\n" + " bucket - The name of the bucket in which the video is located (for example, (for example, myBucket). \n\n"+ " video - The name of video (for example, people.mp4). \n\n" + " topicArn - The ARN of the Amazon Simple Notification Service (Amazon SNS) topic. \n\n" + " roleArn - The ARN of the AWS Identity and Access Management (IAM) role to use. \n\n" ; if (args.length != 4) { System.out.println(usage); System.exit(1); } String bucket = args[0]; String video = args[1]; String topicArn = args[2]; String roleArn = args[3]; Region region = Region.US_EAST_1; RekognitionClient rekClient = RekognitionClient.builder() .region(region) .credentialsProvider(ProfileCredentialsProvider.create("profile-name")) .build(); NotificationChannel channel = NotificationChannel.builder() .snsTopicArn(topicArn) .roleArn(roleArn) .build(); StartFaceDetection(rekClient, channel, bucket, video); GetFaceResults(rekClient); System.out.println("This example is done!"); rekClient.close(); } // snippet-start:[rekognition.java2.recognize_video_faces.main] public static void StartFaceDetection(RekognitionClient rekClient, NotificationChannel channel, String bucket, String video) { try { S3Object s3Obj = S3Object.builder() .bucket(bucket) .name(video) .build(); Video vidOb = Video.builder() .s3Object(s3Obj) .build(); StartFaceDetectionRequest faceDetectionRequest = StartFaceDetectionRequest.builder() .jobTag("Faces") .faceAttributes(FaceAttributes.ALL) .notificationChannel(channel) .video(vidOb) .build(); StartFaceDetectionResponse startLabelDetectionResult = rekClient.startFaceDetection(faceDetectionRequest); startJobId=startLabelDetectionResult.jobId(); } catch(RekognitionException e) { System.out.println(e.getMessage()); System.exit(1); } } public static void GetFaceResults(RekognitionClient rekClient) { try { String paginationToken=null; GetFaceDetectionResponse faceDetectionResponse=null; boolean finished = false; String status; int yy=0 ; do{ if (faceDetectionResponse !=null) paginationToken = faceDetectionResponse.nextToken(); GetFaceDetectionRequest recognitionRequest = GetFaceDetectionRequest.builder() .jobId(startJobId) .nextToken(paginationToken) .maxResults(10) .build(); // Wait until the job succeeds while (!finished) { faceDetectionResponse = rekClient.getFaceDetection(recognitionRequest); status = faceDetectionResponse.jobStatusAsString(); if (status.compareTo("SUCCEEDED") == 0) finished = true; else { System.out.println(yy + " status is: " + status); Thread.sleep(1000); } yy++; } finished = false; // Proceed when the job is done - otherwise VideoMetadata is null VideoMetadata videoMetaData=faceDetectionResponse.videoMetadata(); System.out.println("Format: " + videoMetaData.format()); System.out.println("Codec: " + videoMetaData.codec()); System.out.println("Duration: " + videoMetaData.durationMillis()); System.out.println("FrameRate: " + videoMetaData.frameRate()); System.out.println("Job"); // Show face information List<FaceDetection> faces= faceDetectionResponse.faces(); for (FaceDetection face: faces) { String age = face.face().ageRange().toString(); String smile = face.face().smile().toString(); System.out.println("The detected face is estimated to be" + age + " years old."); System.out.println("There is a smile : "+smile); } } while (faceDetectionResponse !=null && faceDetectionResponse.nextToken() != null); } catch(RekognitionException | InterruptedException e) { System.out.println(e.getMessage()); System.exit(1); } } // snippet-end:[rekognition.java2.recognize_video_faces.main] }
    Python
    #Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. #PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-developer-guide/blob/master/LICENSE-SAMPLECODE.) # ============== Faces=============== def StartFaceDetection(self): response=self.rek.start_face_detection(Video={'S3Object': {'Bucket': self.bucket, 'Name': self.video}}, NotificationChannel={'RoleArn': self.roleArn, 'SNSTopicArn': self.snsTopicArn}) self.startJobId=response['JobId'] print('Start Job Id: ' + self.startJobId) def GetFaceDetectionResults(self): maxResults = 10 paginationToken = '' finished = False while finished == False: response = self.rek.get_face_detection(JobId=self.startJobId, MaxResults=maxResults, NextToken=paginationToken) print('Codec: ' + response['VideoMetadata']['Codec']) print('Duration: ' + str(response['VideoMetadata']['DurationMillis'])) print('Format: ' + response['VideoMetadata']['Format']) print('Frame rate: ' + str(response['VideoMetadata']['FrameRate'])) print() for faceDetection in response['Faces']: print('Face: ' + str(faceDetection['Face'])) print('Confidence: ' + str(faceDetection['Face']['Confidence'])) print('Timestamp: ' + str(faceDetection['Timestamp'])) print() if 'NextToken' in response: paginationToken = response['NextToken'] else: finished = True

    En la función main, reemplace las líneas:

    analyzer.StartLabelDetection() if analyzer.GetSQSMessageSuccess()==True: analyzer.GetLabelDetectionResults()

    por:

    analyzer.StartFaceDetection() if analyzer.GetSQSMessageSuccess()==True: analyzer.GetFaceDetectionResults()
    nota

    Si ya ha ejecutado un ejemplo de vídeo distinto de Análisis de un vídeo almacenado en un bucket de Amazon S3 con Java o Python (SDK), el nombre de la función que se va a reemplazar es distinto.

  3. Ejecute el código. Se muestra información sobre los rostros detectados en el vídeo.

GetFaceDetection respuesta de operación

GetFaceDetection devuelve una matriz (Faces) que contiene información sobre los rostros detectados en el vídeo. Un elemento de matriz, FaceDetection, existe para cada vez que se detecta un rostro en el vídeo. Los elementos de la matriz se devuelven ordenados por tiempo, en milisegundos desde el inicio del vídeo.

El siguiente ejemplo es una respuesta JSON parcial desde GetFaceDetection. En la respuesta, tenga en cuenta lo siguiente:

  • Cuadro delimitador: las coordenadas del cuadro delimitador que rodea el rostro.

  • Confianza: grado de confianza de que el cuadro delimitador contiene un rostro.

  • Referencias faciales: una matriz de referencias faciales. Para cada referencia (como el ojo izquierdo, el ojo derecho y la boca), la respuesta proporciona las coordenadas x e y.

  • Atributos faciales: conjunto de atributos faciales, que incluye: barba AgeRange, emociones, anteojos, género, EyesOpen bigote MouthOpen, sonrisa y gafas de sol. El valor puede ser de diferentes tipos, como un tipo booleano (si una persona lleva gafas), una cadena (si la persona es un hombre o una mujer), etc. Además, para la mayoría de los atributos la respuesta proporciona también una confianza en el valor detectado para el atributo. Ten en cuenta que, si bien EyeDirection los atributos FaceOccluded y los atributos son compatibles cuando se utilizanDetectFaces, no se admiten cuando se analizan vídeos con y. StartFaceDetection GetFaceDetection

  • Marca de tiempo: hora a la que se detectó el rostro en el vídeo.

  • Información de paginación: el ejemplo muestra una página de información de detección de rostros. Puede especificar la cantidad de elementos de persona que se van a devolver en el parámetro de entrada MaxResults para GetFaceDetection. Si existen más resultados que MaxResults, GetFaceDetection devuelve un token (NextToken) que se utiliza para obtener la siguiente página de resultados. Para obtener más información, consulte Obtención de los resultados del análisis de Amazon Rekognition Video.

  • Información de vídeo: la respuesta incluye información acerca del formato de vídeo (VideoMetadata) de cada página de información devuelta por GetFaceDetection.

  • Calidad: describe el brillo y la nitidez del rostro.

  • Postura: describe la rotación del rostro.

{ "Faces": [ { "Face": { "BoundingBox": { "Height": 0.23000000417232513, "Left": 0.42500001192092896, "Top": 0.16333332657814026, "Width": 0.12937499582767487 }, "Confidence": 99.97504425048828, "Landmarks": [ { "Type": "eyeLeft", "X": 0.46415066719055176, "Y": 0.2572723925113678 }, { "Type": "eyeRight", "X": 0.5068183541297913, "Y": 0.23705792427062988 }, { "Type": "nose", "X": 0.49765899777412415, "Y": 0.28383663296699524 }, { "Type": "mouthLeft", "X": 0.487221896648407, "Y": 0.3452930748462677 }, { "Type": "mouthRight", "X": 0.5142884850502014, "Y": 0.33167609572410583 } ], "Pose": { "Pitch": 15.966927528381348, "Roll": -15.547388076782227, "Yaw": 11.34195613861084 }, "Quality": { "Brightness": 44.80223083496094, "Sharpness": 99.95819854736328 } }, "Timestamp": 0 }, { "Face": { "BoundingBox": { "Height": 0.20000000298023224, "Left": 0.029999999329447746, "Top": 0.2199999988079071, "Width": 0.11249999701976776 }, "Confidence": 99.85971069335938, "Landmarks": [ { "Type": "eyeLeft", "X": 0.06842322647571564, "Y": 0.3010137975215912 }, { "Type": "eyeRight", "X": 0.10543643683195114, "Y": 0.29697132110595703 }, { "Type": "nose", "X": 0.09569807350635529, "Y": 0.33701086044311523 }, { "Type": "mouthLeft", "X": 0.0732642263174057, "Y": 0.3757539987564087 }, { "Type": "mouthRight", "X": 0.10589495301246643, "Y": 0.3722417950630188 } ], "Pose": { "Pitch": -0.5589138865470886, "Roll": -5.1093974113464355, "Yaw": 18.69594955444336 }, "Quality": { "Brightness": 43.052337646484375, "Sharpness": 99.68138885498047 } }, "Timestamp": 0 }, { "Face": { "BoundingBox": { "Height": 0.2177777737379074, "Left": 0.7593749761581421, "Top": 0.13333334028720856, "Width": 0.12250000238418579 }, "Confidence": 99.63436889648438, "Landmarks": [ { "Type": "eyeLeft", "X": 0.8005779385566711, "Y": 0.20915353298187256 }, { "Type": "eyeRight", "X": 0.8391435146331787, "Y": 0.21049551665782928 }, { "Type": "nose", "X": 0.8191410899162292, "Y": 0.2523227035999298 }, { "Type": "mouthLeft", "X": 0.8093273043632507, "Y": 0.29053622484207153 }, { "Type": "mouthRight", "X": 0.8366993069648743, "Y": 0.29101791977882385 } ], "Pose": { "Pitch": 3.165884017944336, "Roll": 1.4182015657424927, "Yaw": -11.151537895202637 }, "Quality": { "Brightness": 28.910892486572266, "Sharpness": 97.61507415771484 } }, "Timestamp": 0 }....... ], "JobStatus": "SUCCEEDED", "NextToken": "i7fj5XPV/fwviXqz0eag9Ow332Jd5G8ZGWf7hooirD/6V1qFmjKFOQZ6QPWUiqv29HbyuhMNqQ==", "VideoMetadata": { "Codec": "h264", "DurationMillis": 67301, "FileExtension": "mp4", "Format": "QuickTime / MOV", "FrameHeight": 1080, "FrameRate": 29.970029830932617, "FrameWidth": 1920 } }