Methods - PHPfanatic/clarifai GitHub Wiki

Constructor

Instantiate your image client object.

Parameters

  • apikey - (string) api key generated at clarifai
$myclient = new ImageClient([apikey]);

ModelAdd

Add a custom model to your application.

Parameters

  • model - (string) name for your model
  • model_id - (string) id for your model
$response = $myclient->ModelAdd('model_name', 'modelid123');

ModelUpdate

Update a model with a concept you have added.

Parameters

  • id - (string) id of the model you want to update
  • concept - (array) name of the concept(s) to add/remove
  • action - (string) merge|remove merge or remove the concept, merge is the default
// Merge single concept
$response = $myclient->ModelUpdate('modelid123', array(array('id'=>'dog')))

// Merge multiple concepts
$response = $myclient->ModelUpdate('modelid123', array(array('id'=>'dog'),array('id'=>'cat')));

// Remove example
$response = $myclient->ModelUpdate('modelid123', array(array('id'=>'dog')), 'remove');

ModelGet

Retrieve a list of all models, specific model by id or specific version of a model id.

Parameters

  • id - (string) model id to return
  • version - (string) model version id to return
// Retrieve all models
$response = $myclient->ModelGet();

// Retrieve a specific model
$response = $myclient->ModelGet('modelid123');

// Retrieve a specific version of a model
$response = $myclient->ModelGet('modelid123', 'a81acc3956c05165a194120de44445c2');

ModelDelete

Delete all models, delete specific model or delete a specific version of a model. As of 3/20/2017 'delete_all' must be passed when an ID is not specified. This is not reflected in the ClarifAI documentation however it is needed.

Parameters

  • id - (string) model id to delete
  • version - (string) model version id to delete
// Delete all models
$response = $myclient->ModelDelete();

// Delete a specific model id
$response = $myclient->ModelDelete('pet1');

// Delete a specific model id version
$response = $myclient->ModelDelete('pet1', 'a81abb3956b04185a194120de44445c3');

ModelTrain

Train a model

Parameters

  • id - (string) model id to train
$response = $myclient->ModelTrain('modelid123');

Predict

Predict image content based on model passed in. As a shortcut we have added the ability to pass in the name of existing ClarifAI models which will be automatically converted to their model id's or you can pass in your own model id from a custom model that you have created. General is the default model.

You can retrieve a list of model id's by calling ModelGet().

Predict requires that you have added an image via AddImage() first or multiple images.

Parameters

  • model - (string) General|Adult|Weddings|Travel|Food|Color|Apparel|Celebrity|Face|[Custom_Model_ID]
// Default predict on single image url.
$myclient->AddImage('http://phpfanatic.com/projects/clarifai/cat.png');
$response = $myclient->Predict();

// Default predict on multiple images.
$myclient->AddImage('http://phpfanatic.com/projects/clarifai/cat.png');
$myclient->AddImage('http://phpfanatic.com/projects/clarifai/dog.jpg');
$response = $myclient->Predict();

// Color Predict on single image url.
$myclient->AddImage('http://phpfanatic.com/projects/clarifai/cat.png');
$response = $myclient->Predict('Color');

// Food Predict on single image data.
$image_data = file_get_content('/image/cat.png');
$myclient->AddImage($image_data);
$response = $myclient->Predict('Food');

// Predict multiple models
$myclient->AddImage('http://phpfanatic.com/projects/clarifai/cat.png');
$response1 = $myclient->Predict('Color');
$response2 = $myclient->Predict('Apparel');

InputsAdd

Add an image(s) to be indexed in your application.

Parameters

  • N/A
$myclient->AddImage('http://phpfanatic.com/projects/clarifai/cat.png', 'catid123');
$response = $myclient->InputsAdd();

InputsUpdate

Update an input by adding or deleting concepts for it. The default action is to merge concepts to the given input id(s). To delete a concept(s), pass 'remove' as the action variable.

This may seem confusing until you read AddConcept() documentation.

Parameters

  • action - (string) merge|remove
// Update input merge example
$myclient->AddConcept('catid123', array(array('id'=>'horse', 'value'=>false)));
$response = $myclient->InputsUpdate();

// Update input remove example
$myclient->AddConcept('catid123', array(array('id'=>'horse', 'value'=>false)));
$response = $myclient->InputsUpdate('remove');

InputsDelete

Delete an image by its ID. You can pass in either a single id as $id={ID1} or you can submit an array of id's $id=array({ID1},{ID2},{ID3}).

Parameters

  • id - (mixed) id of the image (input) to delete or an array of id's toe delete.
// Remove single input
$response = $myclient->InputsDelete('catid123');

// Remove multiple inputs
$input_array('imageid1','imageid2','imageid3');
$response = $myclient->InputsDelete($input_array);

InputsGet

Return inputs that you have indexed, you may pass an ID to return a specific input.

Parameters

  • id - (string) id of the image (input) to return.
// Return all images
$response = $myclient->InputsGet();

// Return a specific image
$response = $myclient->InputsGet('catid123');

InputsGetStatus

Return the status of your bulk inputs. If you add multiple images at the same time they are queued for processing. This method will tell you the status of that processing.

Parameters

  • N/A
// Return all images
$response = $myclient->InputsGetStatus();

AddImage

Prepare images for API call. This stores the image and optional data in $this->image. Calling AddImage() multiple times will build an array of images to be posted. A maximum of 128 images per post is allowed.

Parameters

  • image - (string) url to image or image in bytes. (the library handles encoding the data for you)
  • id - (string) identifier to use with image when calling "inputs".
  • concept - (array) optional concept data
  • metadata - (array) optional metadata
  • crop - (array) optional image crop data
// Add image
$myclient->AddImage('http://phpfanatic.com/projects/clarifai/cat.png');

// Add image bytes
$image_data = file_get_content('/image/cat.png');
$myclient->AddImage($image_data);

// Add image and id
$myclient->AddImage('http://phpfanatic.com/projects/clarifai/cat.png', 'catid123');

// Add image, id and concept
$concept = array('id'=>'cat', 'value'=>true)
$myclient->AddImage('http://phpfanatic.com/projects/clarifai/cat.png', 'catid123', $concept);

// Add image, id, concept and metadata
$meta = array('name'=>'snuffles', 'breed'=>'tabby')
$concept = array('id'=>'cat', 'value'=>true)
$myclient->AddImage('http://phpfanatic.com/projects/clarifai/cat.png', 'catid123', $concept, $meta);

AddConcept

Prepare concepts for API call. This stores the concept in $this->concept. Calling AddConcept() multiple times will build and array of concepts to be posted. Currently only used when making an API call to InputsUpdate().

Calling this method does not update or make an API call. It simply builds the array and prepares for it for you to call InputsUpdate().

Parameters

  • id - (string) Image (input) id that this concept is for.
  • concepts - (array) this is an array of array's.
// Add a single concept to an input (image) with an id of catid123.
$myclient->AddConcept('catid123', array(array('id'=>'cat', 'value'=>true)));

// Add two concepts to an image with an id of catid123.
$myclient->AddConcept('catid123', array(array('id'=>'cat', 'value'=>true), array('id'=>'dog', 'value'=>false)));

// Add a concept to one image and a different concept to another image.
$myclient->AddConcept('catid123', array(array('id'=>'cat', 'value'=>true)));
$myclient->AddConcept('dogid456', array(array('id'=>'cat', 'value'=>false)));

Search

Search your indexed images, you may search by concept, user concept, metadata or url. The $term variable should only be an array when searching metadata. The 'by' variable is set to 'concept' by default and the 'exists' variable is set to true by default.

Parameters

  • term - (mixed) term to search by. Needs to be an array of key value pairs if you are searching by meta data.
  • by - (string) what to search on, values can be concept | user_concept | meta | url | image.
  • exists - (bool) when searching by concept / user_concept this sets if the term is true or false.
// Search by user concept for images that are cats
$myclient->Search('cat', 'user_concept');

// Search by user concept for images that are not cats
$myclient->Search('cat', 'user_concept', false);

// Search by meta data where the 'breed' = 'tabby'
$data = array('breed','tabby');
$myclient->Search($data, 'meta');

// Search for a specific image url
$myclient->('http://phpfanatic.com/projects/clarifai/cat.png', 'url');

// Search for images similar to this image
$myclient->('http://phpfanatic.com/projects/clarifai/cat.png', 'image');

SetLanguage

Allows you to change the language support of your Predict() and Search() requests. When using Predict() NOTE that you can only use language support for the 'General' model. This is a Clarifai limitation at this time. Allowed options are: ar,bn,da,de,en,es,fi,fr,hi,hu,it,ja,ko,nl,no,pa,pl,pt,ru,sv,tr,zh,zh-TW

Parameters

  • language - (string) language code to change to.
//Predict the image with the results in spanish.
$myclient->SetLanguage('es');
$myclient->AddImage('http://phpfanatic.com/projects/clarifai/dog.jpg');
$result = $myclient->Predict();

//Search for an image in Chinese (狗 = dog)
$myclient->SetLanguage('zh');
$result = $myclient->Search('狗');

**

ShowLanguage

Will return you the current language you last set for this object.

Parameters

  • N/A
$myclient->SetLanguage('zh');
$result = $myclient->ShowLanguage();

Paginate

Allows you to set what page and how many items to return when using ModelGet() or InputsGet(). This sets pagination for your imageclient object. Use ClearPaginate() to reset it if needed.

Parameters

  • page - (int) page number to display
  • count - (int) number of items to display per page
// Paginate your InputsGet call, show page 2 with 20 inputs.
$myclient->Pagination(2, 20);
$response = $myclient->InputsGet();

PageForward

Once you have set Pagination(), you can use the method to move forward "x" number of pages. Default is 1 page.

Parameters

  • page - (int) number of pages to move forward.
// Move forward one page
$myclient->PageForward();

// Move forward three pages
$myclient->PageForward(3);

PageBack

Once you have set Pagination(), you can use the method to move back "x" number of pages. Default is 1 page.

Parameters

  • page - (int) number of pages to move back.
// Move back one page
$myclient->PageBack();

// Move back three pages
$myclient->PageBack(3);

ClearImages

Clear the images you are actively working with that have been added via AddImage().

Parameters

  • N/A
$myclient->ClearImages();

ClearConcepts

Clear the concepts you are actively working with that have been added via AddConcepts().

Parameters

  • N/A
$myclient->ClearConcepts();

ClearPagination

Clear the pagination seetings you are actively working with that have been added via Paginate().

Parameters

  • N/A
$myclient->ClearPagination();