T.I.T Tanmoy
T.I.T Tanmoy

Reputation: 11

How to Integrate Google Cloud Vision API with PHP for Image Label Detection?

I am trying to integrate the Google Cloud Vision API into my PHP project to perform image label detection. I've followed the official documentation, but I'm running into some issues. Here are the details of what I've done so far:

What I've Done:

Set Up Google Cloud Project:

Set Up My PHP Environment:

composer require google/cloud-vision

Wrote the PHP Script:

Here is my PHP script:

<?php

require 'vendor/autoload.php';

use Google\Cloud\Vision\V1\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\Feature\Type;

putenv('GOOGLE_APPLICATION_CREDENTIALS=/../public/service-account-file.json');

function analyzeImage($imagePath) {
    $imageAnnotator = new ImageAnnotatorClient();
    
    try {
        // Read the image file
        $imageData = file_get_contents($imagePath);
        
        // Perform label detection
        $response = $imageAnnotator->annotateImage(
            $imageData,
            [Type::LABEL_DETECTION]
        );
        
        // Process the response
        $labels = $response->getLabelAnnotations();
        if ($labels) {
            echo "Labels found in the image:\n";
            foreach ($labels as $label) {
                echo $label->getDescription() . "\n";
            }
        } else {
            echo "No labels found.\n";
        }
    } finally {
        $imageAnnotator->close();
    }
}

// Replace with the path to your image file
analyzeImage('src/image/image1.ppg');

?>

What I've Tried:

  1. Checked the Path to the JSON File:

    • Verified that the path to the JSON file in the putenv function is correct.
  2. Verified the Service Account Permissions:

    • Made sure that the service account has the necessary permissions for the Vision API.

Upvotes: 0

Views: 453

Answers (0)

Related Questions