1

I want to use Google cloud Vision for detecting image properties. I have created an account with Google Cloud and found the exact solution on one of their code snippet here (https://cloud.google.com/vision/docs/detecting-properties#vision-image-property-detection-gcs-php).

I copied and adjust it to what I want to achieve. I installed their package using composer google/cloud-vision.

So here is my code:

<?php 

namespace Google\Cloud\Samples\Vision;

use Google\Cloud\Vision\VisionClient;

 $projectId = 'YOUR_PROJECT_ID';
 $path = 'event1.jpg'; 

function detect_image_property($projectId, $path)
{
    $vision = new VisionClient([
        'projectId' => $projectId,
    ]);
    $image = $vision->image(file_get_contents($path), [
        'IMAGE_PROPERTIES'
    ]);
    $result = $vision->annotate($image);
    print("Properties:\n");
    foreach ($result->imageProperties()->colors() as $color) {
        $rgb = $color['color'];
        printf("red:%s\n", $rgb['red']);
        printf("green:%s\n", $rgb['green']);
        printf("blue:%s\n\n", $rgb['blue']);
    }
}

detect_image_property($projectId, $path); 


?> 

So when I run my code it throws this error:

Fatal error: Uncaught Error: Class 'Google\Cloud\Vision\VisionClient' not found in C:\xampp\htdocs\vision\index.php:12 Stack trace: #0 C:\xampp\htdocs\vision\index.php(28): Google\Cloud\Samples\Vision\detect_image_property('YOUR_PROJECT_ID', 'event1.jpg') #1 {main} thrown in C:\xampp\htdocs\vision\index.php on line 12

Now am wondering what is the next step for me, also what will be my
$projectId = 'YOUR_PROJECT_ID'

*Please, if this question needs more explanation let me know in the comment instead of downvoting.

Thanks.

1 Answers1

0

@Abiodun Adetona

Project-Id : This is the private key, we've to generate using Google cloud vision for example - https://cloud.google.com/vision/docs/libraries#client-libraries-install-php

As per the error, we can say it's not able to find your file - Google\Cloud\Samples\Vision;

To avoid this we've to load require __DIR__ . '/vendor/autoload.php'; file before using them

namespace Google\Cloud\Samples\Vision;
  • which service account should I choose when creating the key? – Abiodun Adetona Apr 25 '18 at 09:55
  • This is the error when I added `require __DIR__ . '/vendor/autoload.php';` to the top. `Fatal error: Namespace declaration statement has to be the very first statement or after any declare call in the script in C:\xampp\htdocs\vision\index.php on line 6` – Abiodun Adetona Apr 25 '18 at 10:16
  • https://stackoverflow.com/questions/21433086/fatal-error-namespace-declaration-statement-has-to-be-the-very-first-statement – bharadwaja Gummadi Apr 25 '18 at 10:57
  • Thanks. This is a new error `Fatal error: Uncaught Google\Cloud\Core\Exception\ServiceException: { "error": { "code": 403, "message": "The request is missing a valid API key.", "status": "PERMISSION_DENIED" } }` – Abiodun Adetona Apr 25 '18 at 11:37
  • In the documentation https://cloud.google.com/vision/docs/libraries#client-libraries-install-php, after step 6 is done, check the examples for Linux/macOS or Windows to set the environment variable GOOGLE_APPLICATION_CREDENTIALS. – Jinjun May 16 '18 at 19:35