Search code examples
curlimage-processingartificial-intelligenceimage-recognitionclarifai

Clarifai cURL image recognition intergration


I am trying to get tags from Clarifai image recognition using cURL and PHP.

On their site there is this user guide: https://developer.clarifai.com/guide/#authentication


QUESTION UPDATE

I managed to get valid responce, and it looks like this:

    {
  "status": {
    "code": 10000,
    "description": "Ok"
  },
  "outputs": [
    {
      "id": "ea68cac87c304b28a8046557062f34a0",
      "status": {
        "code": 10000,
        "description": "Ok"
      },
      "created_at": "2016-11-22T16:50:25Z",
      "model": {
        "name": "general-v1.3",
        "id": "aaa03c23b3724a16a56b629203edc62c",
        "created_at": "2017-08-09T17:11:39Z",
        "app_id": null,
        "output_info": {
          "message": "Show output_info with: GET /models/{model_id}/output_info",
          "type": "concept"
        },
        "model_version": {
          "id": "aa9ca48295b37401f8af92ad1af0d91d",
          "created_at": "2016-07-13T01:19:12Z",
          "status": {
            "code": 21100,
            "description": "Model trained successfully"
          }
        }
      },
      "input": {
        "id": "ea68cac87c304b28a8046557062f34a0",
        "data": {
          "image": {
            "url": "https://samples.clarifai.com/metro-north.jpg"
          }
        }
      },
      "data": {
        "concepts": [
          {
            "id": "ai_HLmqFqBf",
            "name": "train",
            "app_id": null,
            "value": 0.9989112
          },
          {
            "id": "ai_fvlBqXZR",
            "name": "railway",
            "app_id": null,
            "value": 0.9975532
          },
          {
            "id": "ai_VSVscs9k",
            "name": "terminal",
            "app_id": null,
            "value": 0.9230834
          }
        ]
      }
    }
  ]
}

Now, I tried to foreach tags:

     $response = curl_exec($curl);

     $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
     $body = substr($response, $header_size);
     $header = substr($response, 0, $header_size);
     $rows = explode("\n", $header);

     $err = curl_error($curl);

     curl_close($curl);
     $resp = json_decode( $body, true );

     if ($err) {echo $err; } else {
         // foreach thought tags, and if tag value is above 93, than show/echo it
     }

But I am having trouble to foreach tags, I can't echo any tag, and I would like to show only tag name with value above for example 93.


Solution

  • try:

    CURLOPT_CUSTOMREQUEST => "POST"
    Authorization: Key f26b16808e2a440abb5f28b1c00b6nnn (no brackets)

    And then

    CURLOPT_POSTFIELDS => "{ \"inputs\": [ { \"data\": { \"image\": { \"url\": \"https://samples.clarifai.com/metro-north.jpg\" } } } ] }

    It basically needs to match the format in the cURL column located here: https://developer.clarifai.com/guide/predict#predict