I am new to Google Cloud Vision API and I wanted to extract the colors from an image using their dominant color functionality. below is my code which is base on Terrence Ryan's Blog
$cvurl = "https://vision.googleapis.com/v1/images:annotate?key=API_KEY";
$data = file_get_contents($cache_job);
$base64 = base64_encode($data);
//Create this JSON
$r_json ='{
"requests": [
{
"image": {
"content":"' . $base64. '"
},
"features": [
{
"type": "IMAGE_PROPERTIES",
"maxResults": 200
}
]
}
]
}';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $cvurl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $r_json);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ( $status != 200 ) {
die("Error: $cvurl failed status $status" );
}
The code works but i got some issues with it. There are some colors that are obviously on the image but weren't included on the API response. And so I thought increasing the number of result will solve it but then i discovered that changing the "maxResults" (Google API docs: the number of results to be returned) parameter to any value does not affect anything on the response. The number of result are fixed to 10 colors even if I set the parameter to less than 10 and even if I change the image. Google's API documentation doesn't say anything about it so i was wondering if any of you guys here have experienced it.