Google Cloud Vision PHP — Make Batch Request

524 Views Asked by At

I need to perform an OCR analysis on an image for an university project.

I am imposed to use PHP, unfortunately, on the Google Cloud Vision Documentation few are the code sample using PHP...

I succeed to perform OCR on one image at time but 80% of the time I have a lot of images (around 20) to treat at once.

So I tried to use BatchRequest, to minimize the API calls, as specified here but I can't found how to build the $requests array they put at the top.

Btw I tried other APIs like Tesseract but the recognition is not accurate enough to use.

1

There are 1 best solutions below

2
On BEST ANSWER

If you only want to perform a batch request you can just use batchAnnotateImages using ImageAnnotatorClient. Below you can find a sample using it as well as a way to create a request variable. Also, I include below a asyncBatchAnnotateImages sample but I recommend the one I mentioned earlier.

Using ImageAnnotatorClient with batchAnnotateImages

<?php 

require '../vendor/autoload.php';
  
use Google\Cloud\Storage\StorageClient; 
use Google\Cloud\Vision\V1\Feature;
use Google\Cloud\Vision\V1\Feature_Type; 
use Google\Cloud\Vision\V1\ImageAnnotatorClient; 
use Google\Cloud\Vision\V1\Image;
use Google\Cloud\Vision\V1\ImageSource;
use Google\Cloud\Vision\V1\AnnotateImageRequest; 
use Google\Cloud\Vision\V1\Likelihood;

$client = new ImageAnnotatorClient();

try {
    $feature = (new Feature())
        ->setType(Feature_Type::FACE_DETECTION);

    $image = (new Image())
        ->setContent(file_get_contents("../images/family.jpg","r"));

    $request = (new AnnotateImageRequest())
        ->setImage($image)
        ->setFeatures([$feature]);

    $requests = [$request];
    # note: you can add as many requests you want to perform. ie: [$request,$request2,..,..]

    $results = $client->batchAnnotateImages($requests);
    foreach($results->getResponses() as $result){
        foreach ($result->getFaceAnnotations() as $faceAnnotation) {
            $likelihood = Likelihood::name($faceAnnotation->getJoyLikelihood());
            echo "Likelihood of headwear: $likelihood" . PHP_EOL;
        }
    }
   
} finally {
    $client->close();
}

Using ImageAnnotatorClient with asyncBatchAnnotateImages

<?php 

require '../vendor/autoload.php';
  
use Google\Cloud\Storage\StorageClient; 
use Google\Cloud\Vision\V1\Feature;
use Google\Cloud\Vision\V1\Feature_Type; 
use Google\Cloud\Vision\V1\ImageAnnotatorClient; 
use Google\Cloud\Vision\V1\Image;
use Google\Cloud\Vision\V1\ImageSource;
use Google\Cloud\Vision\V1\AnnotateImageRequest; 
use Google\Cloud\Vision\V1\asyncBatchAnnotateImages;
use Google\Cloud\Vision\V1\OutputConfig;
use Google\Cloud\Vision\V1\GcsDestination;

$client = new ImageAnnotatorClient();

try {
    $feature = (new Feature())
        ->setType(Feature_Type::FACE_DETECTION);

    $gcsImageUri = 'gs://<YOUR BUCKET ID>/<YOUR IMAGE FILE>';
    $source = new ImageSource();
        $source->setImageUri($gcsImageUri);

    $image = (new Image())
        ->setSource($source);

    $request = (new AnnotateImageRequest())
        ->setImage($image)
        ->setFeatures([$feature]);

    $requests = [$request];

    $gcsDestination = (new GcsDestination())
        ->setUri("gs://<YOUR BUCKET>/<OUTPUT FOLDER>/");

    $outputConfig = (new OutputConfig())
            ->setGcsDestination($gcsDestination);

    $operationResponse = $client->asyncBatchAnnotateImages($requests, $outputConfig);
    $operationResponse->pollUntilComplete();
    if ($operationResponse->operationSucceeded()) {
        $result = $operationResponse->getResult(); 
        var_dump($result);
        #Your Folder output will have your file processing results.
    }
   
} finally {
    $client->close();
}

Note: To add on this, you can also check an official implementation of a similar case using vision client on this link but its a sample to detect text on a pdf file.

You can also find additional information on these links: