Last active
November 5, 2016 16:39
-
-
Save shoma/e64d0ecb9d3aa0673114 to your computer and use it in GitHub Desktop.
An example usage for Google Cloud Vision API with PHP on GCE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"google/apiclient": "2.x-dev", | |
"google/appengine-php-sdk": "^1.9" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require './vendor/autoload.php'; | |
use Google_Client; | |
use Google_Service_Vision; | |
use Google_Service_Vision_AnnotateImageRequest; | |
use Google_Service_Vision_BatchAnnotateImagesRequest; | |
use Google_Service_Vision_Feature; | |
use Google_Service_Vision_Image; | |
use Google_Service_Vision_ImageSource; | |
$client = new Google_Client(); | |
$client->useApplicationDefaultCredentials(); | |
$client->setScopes([Google_Service_Vision::CLOUD_PLATFORM]); | |
$service = new Google_Service_Vision($client); | |
$body = new Google_Service_Vision_BatchAnnotateImagesRequest(); | |
$feature = new Google_Service_Vision_Feature(); | |
$feature->setType('LABEL_DETECTION'); | |
$feature->setMaxResults(100); | |
$src = new Google_Service_Vision_ImageSource(); | |
$src->setGcsImageUri($gcsurl); | |
$image = new Google_Service_Vision_Image(); | |
$image->setSource($src); | |
$payload = new Google_Service_Vision_AnnotateImageRequest(); | |
$payload->setFeatures($features); | |
$payload->setImage($image); | |
$body->setRequests([$payload]); | |
/** @var $res \Google_Service_Vision_BatchAnnotateImagesResponse */ | |
$res = $service->images->annotate($body, $optParams); |
Thank you very much! this is very informative.
I think you have a typo in line 25 I think it should be $feature (in singular)
Also, I try to reproduce this script in my platform (Laravel + google/apiclient) and I keep gettign this error:
Error calling POST https://vision.googleapis.com/v1/images:annotate: (403) Request had insufficient authentication scopes.
Do you have any idea why?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
Thank you for the example. Could you please tell me what goes into $optParams? It gives me an error: Warning: array_merge(): Argument #2 is not an array in ../vendor/google/apiclient-services/src/Google/Service/Vision/Resource/Images.php on line 38