PHP code example of google / cloud-vision

1. Go to this page and download the library: Download google/cloud-vision library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

google / cloud-vision example snippets




use Google\Cloud\Vision\V1\AnnotateImageRequest;
use Google\Cloud\Vision\V1\BatchAnnotateImagesRequest;
use Google\Cloud\Vision\V1\Client\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\Feature;
use Google\Cloud\Vision\V1\Image;
use Google\Cloud\Vision\V1\Likelihood;

$client = new ImageAnnotatorClient();

// Prepare the request
$content = file_get_contents('/data/photos/family-photo.jpg', 'r');
$image = (new Image())
    ->setContent($content);
$feature = (new Feature())
    ->setType(Feature\Type::FACE_DETECTION);
$request = (new AnnotateImageRequest())
    ->setImage($image)
    ->setFeatures([$feature]);
$batchRequest = (new BatchAnnotateImagesRequest())
    ->setRequests([$request]);

// Annotate an image, detecting faces.
$batchResponse = $client->batchAnnotateImages($batchRequest);

// Determine if the detected faces have headwear.
foreach ($batchResponse->getResponses() as $response) {
    foreach ($response->getFaceAnnotations() as $faceAnnotation) {
        $likelihood = Likelihood::name($faceAnnotation->getHeadwearLikelihood());
        echo "Likelihood of headwear: $likelihood" . PHP_EOL;
    }
}