PHP code example of clarifai / clarifai-php

1. Go to this page and download the library: Download clarifai/clarifai-php 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/ */

    

clarifai / clarifai-php example snippets


use Clarifai\API\ClarifaiClient;
use Clarifai\DTOs\Inputs\ClarifaiURLImage;
use Clarifai\DTOs\Outputs\ClarifaiOutput;
use Clarifai\DTOs\Predictions\Concept;
use Clarifai\DTOs\Searches\SearchBy;
use Clarifai\DTOs\Searches\SearchInputsResult;
use Clarifai\DTOs\Models\ModelType;

// Skip the argument to fetch the key from the CLARIFAI_API_KEY env. variable
$client = new ClarifaiClient('YOUR_API_KEY');

$model = $client->publicModels()->generalModel();
$response = $model->batchPredict([
    new ClarifaiURLImage('https://samples.clarifai.com/metro-north.jpg'),
    new ClarifaiURLImage('https://samples.clarifai.com/wedding.jpg'),
])->executeSync();

/** @var ClarifaiOutput[] $outputs */
$outputs = $response->get();

foreach ($outputs as $output) {
    /** @var ClarifaiURLImage $image */
    $image = $output->input();
    echo "Predicted concepts for image at url " . $image->url() . "\n";
    
    /** @var Concept $concept */
    foreach ($output->data() as $concept) {
        echo $concept->name() . ': ' . $concept->value() . "\n";
    }
    echo "\n";
}

if (!$response->isSuccessful()) {
    echo "Response is not successful. Reason: \n";
    echo $response->status()->description() . "\n";
    echo $response->status()->errorDetails() . "\n";
    echo "Status code: " . $response->status()->statusCode();
    exit(1);
}

$client->addConcepts([new Concept('boscoe')])
    ->executeSync();

$client->addInputs([
    (new ClarifaiURLImage('https://samples.clarifai.com/puppy.jpeg'))
        ->withPositiveConcepts([new Concept('boscoe')]),
    (new ClarifaiURLImage('https://samples.clarifai.com/wedding.jpg'))
        ->withNegativeConcepts([new Concept('boscoe')])
])
    ->executeSync();

$client->createModel('pets')
    ->withConcepts([new Concept('boscoe')])
    ->executeSync();

$response = $client->trainModel(ModelType::concept(), 'pets')
    ->executeSync();

if ($response->isSuccessful()) {
    echo "Response is successful.\n";
} else {
    echo "Response is not successful. Reason: \n";
    echo $response->status()->description() . "\n";
    echo $response->status()->errorDetails() . "\n";
    echo "Status code: " . $response->status()->statusCode();
}

$response = $client->searchInputs(
        SearchBy::urlImageVisually('https://samples.clarifai.com/metro-north.jpg'))
    ->executeSync();

if ($response->isSuccessful()) {
    echo "Response is successful.\n";

    /** @var SearchInputsResult $result */
    $result = $response->get();

    foreach ($result->searchHits() as $searchHit) {
        echo $searchHit->input()->id() . ' ' . $searchHit->score() . "\n";
    }
} else {
    echo "Response is not successful. Reason: \n";
    echo $response->status()->description() . "\n";
    echo $response->status()->errorDetails() . "\n";
    echo "Status code: " . $response->status()->statusCode();
}