Update March 2023: The code below will not work after March 2023 as Clarifai has changed their authentication system. Please have a look at the index.php file in the CCTV project mentioned below (https://github.com/marcconrad/myCCTV) – search for ‘curl’
Update: link to Clarifai’s conference – Perceive 2020 – https://www.youtube.com/playlist?list=PLs05ayZZ-2yg02sEAQ2Ffah2ilDYmc4Ch
Update: I have now a project on github that is a cctv that uses Clarifai to identify concepts and make animated gifs: https://github.com/marcconrad/myCCTV
php code, to find out what is in an image:
$random_string=substr(str_shuffle(MD5(microtime())), 0, 5);
echo ‘<h1>’.$random_string.'</h1>’;
$imgurl=”http://perisic.com/four/train/img.php?s=”.$random_string.”&t=2.jpg”;
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_VERBOSE, ‘1’);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “POST”);
curl_setopt($ch, CURLOPT_URL, “https://api.clarifai.com/v2/models/aaa03c23b3724a16a56b629203edc62c/outputs”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // curl_exec returns the value
$headers = array(
‘Content-Type: application/json’,
“Authorization: xyzabcd”
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$fields = ‘{“inputs”:[{“data”:{“image”:{“url”:”‘.$imgurl.'”}}}]}’; // Your image here.
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
// grab URL and pass it to the browser
$result = curl_exec($ch);
// echo ‘<h2>’.$result.'</h2>’;
$mydata= json_decode($result, true);
$concepts = $mydata[“outputs”][0][“data”][“concepts”];
// var_dump($concepts);
$conceptlist=””;
for($i=0;$i < sizeof($concepts); $i++ ) {
if($i != 0 ) { $conceptlist .= ‘, ‘; }
$value = $concepts[$i];
$conceptlist .= $value[“name”];
}
echo “<h3>Concepts: “;
echo $conceptlist;
echo “</h3>”;
// close cURL resource, and free up system resources
curl_close($ch);
echo ‘<img src=”‘.$imgurl.'” alt=”‘.$conceptlist.'” title=”‘.$conceptlist.'”>’;
- About the model: https://clarifai.com/models/general-image-recognition-model-aaa03c23b3724a16a56b629203edc62c#documentation
- How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST? https://stackoverflow.com/questions/7172784/how-to-post-json-data-with-curl-from-terminal-commandline-to-test-spring-rest (for Windows: use “” to escape “)
- Download curl for Windows: https://curl.haxx.se/download.html
- Or directly here: https://winampplugins.co.uk/curl/
- “
If you’ve been struggling with trying to figure out how to get your fancy curl commandline to work in PHP, this makes it a breeze!” from: http://php.net/manual/en/book.curl.phpAfter a lot of frustration with the fact that nobody has documented which curl commandline options go with which library functions, I discovered that the curl commandline will tell you (in the form of a C program) if you add `--libcurl foo.c`
- About the –data switch and curl docu: https://curl.haxx.se/docs/httpscripting.html
Finally: C:\Users\vim\curl\curl.exe –verbose –libcurl tmp1.c -H “Authorization: Key xyzabcd” -H “Content-Type: application/json” –data “{“”inputs””:[{“”data””:{“”image””:{“”url””:””http://www.perisic.com/five/imgfive.php?s=helbo&w=10″”}}}]}” https://api.clarifai.com/v2/models/aaa03c23b3724a16a56b629203edc62c/outputs
(You must replace xyzabcd with your own API key)