I need to send a PDF file to Google Vision to extract and return text. From documentation I understood that DPF file must be located on Google Storage, so I am putting the file to my Google Storage bucket like this:
require '../vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
$storage = new StorageClient([
'keyFilePath' => '/my-keyfile.json',
'projectId' => PROJECT_ID
]);
$bucket = $storage->bucket(BUCKET_NAME);
$bucket->upload(
fopen($_SESSION['local_pdf_url'], 'r')
);
It works. After I redirect to another page that is suppose to get that file to Vision, and that's where it fails. I found an example function. Here's the code:
require '../vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
use Google\Cloud\Vision\V1\AnnotateFileResponse;
use Google\Cloud\Vision\V1\AsyncAnnotateFileRequest;
use Google\Cloud\Vision\V1\Feature;
use Google\Cloud\Vision\V1\Feature\Type;
use Google\Cloud\Vision\V1\GcsDestination;
use Google\Cloud\Vision\V1\GcsSource;
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\InputConfig;
use Google\Cloud\Vision\V1\OutputConfig;
$storage = new StorageClient([
'keyFilePath' => '/my-keyfile.json',
'projectId' => PROJECT_ID
]);
$path = 'gs://my-bucket/'.$_SESSION['pdf_file_name'];
When I run the second script I get the following errors:
Fatal error: Uncaught DomainException: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information in /home/domain/vendor/google/auth/src/ApplicationDefaultCredentials.php:168 Stack trace: #0 /home/domain/vendor/google/gax/src/CredentialsWrapper.php(197): Google\Auth\ApplicationDefaultCredentials::getCredentials(Array, Object(Google\Auth\HttpHandler\Guzzle6HttpHandler), NULL, NULL) #1 /home/domain/vendor/google/gax/src/CredentialsWrapper.php(114): Google\ApiCore\CredentialsWrapper::buildApplicationDefaultCredentials(Array, Object(Google\Auth\HttpHandler\Guzzle6HttpHandler)) #2 /home/domain/vendor/google/gax/src/GapicClientTrait.php(326): Google\ApiCore\CredentialsWrapper::build(Array) #3 /home/domain/vendor/google/gax/src/GapicClientTrait.php(308): Google\Cloud\Vision\V1\Gapic\ImageAnnotatorGapicClient->createCredentialsWrapper(NULL, Array) #4 /home/domain/vendor/google/cloud/Vision/src/V1/Gapic/ImageAnnotatorGapicClient.php(216): Google\Clou in /home/domain/vendor/google/gax/src/CredentialsWrapper.php on line 200
How do I authenticate for this service? What am I missing?
Pricing is tiered - the first 1000 units used each month are free, units 1001 to 5,000,000 are priced as marked, etc. If you pay in a currency other than USD, the prices listed in your currency on Cloud Platform SKUs apply.
Open a PDF file containing a scanned image in Acrobat for Mac or PC. Click on the “Edit PDF” tool in the right pane. Acrobat automatically applies optical character recognition (OCR) to your document and converts it to a fully editable copy of your PDF. Click the text element you wish to edit and start typing.
I realize how frustrating it could be when documentation is somewhat lacking organization, content or good examples. Here's what I ended-up doing myself, that finally allowed my script to work. Hope it'll help someone too:
require '../vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
use Google\Cloud\Vision\V1\AnnotateFileResponse;
use Google\Cloud\Vision\V1\AsyncAnnotateFileRequest;
use Google\Cloud\Vision\V1\Feature;
use Google\Cloud\Vision\V1\Feature\Type;
use Google\Cloud\Vision\V1\GcsDestination;
use Google\Cloud\Vision\V1\GcsSource;
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\InputConfig;
use Google\Cloud\Vision\V1\OutputConfig;
putenv('GOOGLE_APPLICATION_CREDENTIALS=/my-keyfile.json');
$path = 'gs://my-bucket/'.$_SESSION['pdf_file_name'];
The error indicates authentication issues. To resolve the issue, see and follow Using a service account for instructions on authenticating with a service account.
"The account used for authentication must have access to the Cloud Storage bucket that you specify for the output (roles/editor or roles/storage.objectCreator or above)." - more information here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With