Reputation: 11
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:
Set Up Google Cloud Project:
Created a project in the Google Cloud Console.
Enabled the Cloud Vision API for my project.
Created a service account and downloaded the JSON credentials file.
Set Up My PHP Environment:
Installed Composer.
Created a new PHP project.
Installed the Google Cloud Vision Client Library using
composer require google/cloud-vision
Wrote the PHP Script:
Loaded the Composer autoloader.
Set up authentication using the service account key.
Initialized the Vision Client.
Sent an image for analysis and processed the response.
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');
?>
Checked the Path to the JSON File:
putenv
function is correct.Verified the Service Account Permissions:
Upvotes: 0
Views: 453