Advertisement
Fhernd

face_api_extract_face_vector.php

Jan 12th, 2025
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.64 KB | None | 0 0
  1. <?php
  2.  
  3. require 'vendor/autoload.php';
  4.  
  5. use GuzzleHttp\Client;
  6.  
  7. // Configuración de Azure Face API
  8. define('AZURE_FACE_ENDPOINT', 'https://<tu-endpoint>.cognitiveservices.azure.com/face/v1.0');
  9. define('AZURE_FACE_KEY', '<tu-clave-de-api>');
  10.  
  11. // Función para obtener el vector facial
  12. function extractFaceVector($imagePath) {
  13.     $client = new Client();
  14.     $url = AZURE_FACE_ENDPOINT . '/detect';
  15.  
  16.     try {
  17.         // Leer la imagen
  18.         $imageData = file_get_contents($imagePath);
  19.  
  20.         // Enviar la imagen a Azure Face API
  21.         $response = $client->post($url, [
  22.             'headers' => [
  23.                 'Ocp-Apim-Subscription-Key' => AZURE_FACE_KEY,
  24.                 'Content-Type' => 'application/octet-stream'
  25.             ],
  26.             'body' => $imageData,
  27.             'query' => [
  28.                 'returnFaceId' => 'false',
  29.                 'returnFaceLandmarks' => 'true',
  30.                 'returnFaceAttributes' => 'age,gender,smile,facialHair'
  31.             ]
  32.         ]);
  33.  
  34.         // Decodificar la respuesta
  35.         $result = json_decode($response->getBody(), true);
  36.  
  37.         if (!empty($result)) {
  38.             return $result[0]; // Devuelve el vector del primer rostro detectado
  39.         } else {
  40.             throw new Exception('No se detectó ningún rostro en la imagen.');
  41.         }
  42.     } catch (Exception $e) {
  43.         throw new Exception('Error al procesar la imagen: ' . $e->getMessage());
  44.     }
  45. }
  46.  
  47. // Ejemplo de uso
  48. try {
  49.     $faceVector = extractFaceVector('ruta/a/tu/imagen.jpg');
  50.     echo "Vector facial: " . json_encode($faceVector);
  51. } catch (Exception $e) {
  52.     echo "Error: " . $e->getMessage();
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement