Full Guide - PHPfanatic/clarifai GitHub Wiki

In trying to determine the best layout for a full guide I decided to go with common scenarios that outline some basic and some detailed abilities of the API and library. The full guide assumes that you have already added the PHPfanatic/clarifai library to your project.

Scenario 1

Adding multiple images to our Clarifai application with concepts and metadata in a single call.

use PhpFanatic\clarifAI\ImageClient;

$client = new ImageClient('[api_key]');

$myimage1 = 'http://phpfanatic.com/projects/clarifai/dog.jpg';
$myimage2 = 'http://phpfanatic.com/projects/clarifai/cat.png';

$client->AddImage($myimage1, 'animal1', array('id'=>'dog', 'value'=>true), array('name'=>'Sasha', 'breed'=>'German Shep'));
$client->AddImage($myimage2, 'animal2', array('id'=>'cat', 'value'=>true), array('name'=>'Fluffy', 'breed'=>'Tabby'));

$result = $client->InputsAdd();

Description By Line:

  1. Include the ImageClient library.

  2. Create our ImageClient object by instantiating the library class with your client_id and your client_secret.

  3. Set a variable with an image we want to use in our Clarifai application.

  4. Set a second image we want to use in our Clarifai application.

  5. AddImage() builds an array of image(s) and optional data that we want to send to the Clarifai API. In this scenario we are adding the first image, assigning it an ID of animal1, creating a concept of dog with a value of true, and adding two pieces of metadata name and breed.

  6. We call AddImage() again which will add a second image to our data array that we are going to send to the Clarifai API. This time adding the second image, assigning it an ID of animal2, creating a concept of cat with a value of true, and adding two pieces of metadata name and breed.

  7. InputsAdd() takes the information we have added via AddImage() and builds an API call to Clarifai adding the two images and their data to our application.

Scenario 2

Now that we have added two images to our application, we are going to update them with additional concepts.

use PhpFanatic\clarifAI\ImageClient;

$client = new ImageClient('[api_key]');

$client->AddConcept('animal1', array(array('id'=>'horse', 'value'=>false),array('id'=>'cat', 'value'=>false)));
$client->AddConcept('animal2', array(array('id'=>'horse', 'value'=>false),array('id'=>'dog', 'value'=>false)));

$result = $client->InputsUpdate();

Description By Line:

  1. Include the ImageClient library.

  2. Create our ImageClient object by instantiating the library class with your client_id and your client_secret.

  3. AddConcept() builds an array of concept(s) to update our inputs (images) with. In this line we specify that we want to update input (image) ID animal1 with two concepts horse with a value of false and cat with a value of false. The second argument of Addconcept() is an array of arrays.

  4. We can update multiple inputs (images) at the same time. This line adds a second element to the concept array. Here we are updating the input (image) ID animal2 with the concepts horse and dog as we did with the last line.

  5. We call InputsUpdate which requests that the Clarifai API process the two concepts that we just added.

Scenario 3

Predict on specific coordinates of a specified image.

use PhpFanatic\clarifAI\ImageClient;

$client = new ImageClient('[api_key]');

$client->AddImage('http://phpfanatic.com/projects/clarifai/dog.jpg', '', array(), array(), array(0.2,0.3,0.6,0.5));
$result = $client->Predict();

Description By Line:

  1. Include the ImageClient library.

  2. Create our ImageClient object by instantiating the library class with your client_id and your client_secret.

  3. AddImage() builds an array of image(s) and optional data that we want to send to the Clarifai API. In this scenario we are adding an image, assigning it an ID of blank, the concept array and the metadata array are not required for predict to work, so we add empty arrays for them. The final variable is an array of coordinates that we want our predict to run against.

  4. Predict() based on the variables we passed in through AddImage(), we make a request to the ClarifAI API to return a prediction about this image.

I can't type fast enough... more coming soon....