Advertisement
Fhernd

rekognition_extract_face_vector.php

Jan 12th, 2025
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.58 KB | None | 0 0
  1. <?php
  2.  
  3. require 'vendor/autoload.php';
  4.  
  5. use Aws\Rekognition\RekognitionClient;
  6.  
  7. // Configuración de AWS Rekognition
  8. define('AWS_REGION', 'us-east-1'); // Cambia a tu región
  9. define('AWS_KEY', '<tu-access-key>');
  10. define('AWS_SECRET', '<tu-secret-key>');
  11.  
  12. // Crear el cliente de AWS Rekognition
  13. function getRekognitionClient() {
  14.     return new RekognitionClient([
  15.         'region' => AWS_REGION,
  16.         'version' => 'latest',
  17.         'credentials' => [
  18.             'key' => AWS_KEY,
  19.             'secret' => AWS_SECRET,
  20.         ],
  21.     ]);
  22. }
  23.  
  24. // Detectar el rostro y extraer el vector facial
  25. function extractFaceVector($imagePath) {
  26.     $rekognition = getRekognitionClient();
  27.  
  28.     try {
  29.         // Leer la imagen
  30.         $imageData = file_get_contents($imagePath);
  31.  
  32.         // Enviar la imagen a AWS Rekognition
  33.         $result = $rekognition->detectFaces([
  34.             'Image' => ['Bytes' => $imageData],
  35.             'Attributes' => ['ALL'] // Devuelve landmarks y otros atributos
  36.         ]);
  37.  
  38.         if (!empty($result['FaceDetails'])) {
  39.             return $result['FaceDetails'][0]; // Devuelve los detalles del primer rostro detectado
  40.         } else {
  41.             throw new Exception('No se detectó ningún rostro en la imagen.');
  42.         }
  43.     } catch (Exception $e) {
  44.         throw new Exception('Error al procesar la imagen: ' . $e->getMessage());
  45.     }
  46. }
  47.  
  48. // Ejemplo de uso
  49. try {
  50.     $faceVector = extractFaceVector('ruta/a/tu/imagen.jpg');
  51.     echo "Vector facial: " . json_encode($faceVector);
  52. } catch (Exception $e) {
  53.     echo "Error: " . $e->getMessage();
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement