PHP code example of theranken / ruelo

1. Go to this page and download the library: Download theranken/ruelo library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

theranken / ruelo example snippets


use Ruelo\DeepFace;

$deepface = new DeepFace('http://localhost:4800'); // URL of FastAPI server

// Compare two image files
$result = $deepface->compare('path/to/image1.jpg', 'path/to/image2.jpg');
if (isset($result['result']['verified']) && $result['result']['verified']) {
    echo "Match found! Distance: " . $result['result']['distance'] . "\n";
    echo "Threshold: " . $result['result']['threshold'] . "\n";
    echo "Model: " . $result['result']['model'] . "\n";
}

// Using a custom threshold (0.0 to 1.0)
$result = $deepface->compare('image1.jpg', 'image2.jpg', 0.5);

use Ruelo\DeepFace;

$deepface = new DeepFace('http://localhost:4800');

// Convert an image file to base64
$base64 = $deepface->fileToBase64('path/to/image.jpg');

// Compare with mixed formats
$result = $deepface->compare($base64, 'path/to/image2.jpg');

// Compare two base64 images
$base64_1 = $deepface->fileToBase64('image1.jpg');
$base64_2 = $deepface->fileToBase64('image2.jpg');
$result = $deepface->compare($base64_1, $base64_2);

// Working with data URLs
$dataUrl = 'data:image/jpeg;base64,/9j/4AAQSkZJRg...';
$result = $deepface->compare($dataUrl, 'image2.jpg');

use Ruelo\DeepFace;

// Basic analysis (static method)
$result = DeepFace::analyze('path/to/image.jpg');

// The result contains age, gender, emotion, and race predictions
echo "Age: " . $result['age'] . "\n";
echo "Gender: " . $result['gender'] . "\n";
echo "Emotion: " . $result['dominant_emotion'] . "\n";

use Ruelo\DeepFace;

// Quick face comparison
$result = DeepFace::compareImages('image1.jpg', 'image2.jpg', 'http://localhost:8000');

[
    'result' => [
        'verified' => true|false,        // Whether the faces match (1 or 0)
        'distance' => 0.0,               // Distance between faces (lower is more similar)
        'threshold' => 0.3,              // Threshold used for verification
        'model' => 'Facenet512',         // Model used for comparison
        'detector_backend' => 'opencv',  // Face detection backend
        'similarity_metric' => 'cosine', // Similarity metric used
        'facial_areas' => [              // Detected facial areas
            'img1' => [
                'x' => 269,
                'y' => 163,
                'w' => 193,
                'h' => 193,
                'left_eye' => null,
                'right_eye' => null
            ],
            'img2' => [
                'x' => 269,
                'y' => 163,
                'w' => 193,
                'h' => 193,
                'left_eye' => null,
                'right_eye' => null
            ]
        ],
        'time' => 2.88                  // Processing time in seconds
    ],
    'total_time_seconds' => 2.9133     // Total time including overhead
]

[
    'age' => 25,
    'gender' => 'Man',
    'dominant_emotion' => 'happy',
    'emotion' => [
        'angry' => 0.01,
        'disgust' => 0.0,
        'fear' => 0.01,
        'happy' => 0.95,
        'sad' => 0.02,
        'surprise' => 0.01,
        'neutral' => 0.0
    ]
]

[
    'error' => 'Error message description'
]
bash
    php utilities/start_server.php
    
bash
    php vendor/theranken/ruelo/utilities/start_server.php
    
bash
    php utilities/stop_server.php
    
bash
    php vendor/theranken/ruelo/utilities/stop_server.php
    
bash
    php interact
    
bash
php interact