The Vision API now supports online (synchronous) small batch annotation (PDF/TIFF/GIF) for all features. To do so, the relevant documentation is Small batch file annotation online.

Let’s see how can we do this with PHP.


Having PHP >= 7.4, the packages to require are:



How to upload the file in the storage


Text detection

Even with PDFs we are going to use ImageAnnotatorClient, the service that performs Google Cloud Vision API detection tasks over client images and returns detected entities from the images.

$path = "gs://";

/* If you have it, you can give an hint about the language in the doc */
$context = new ImageContext();

/* Here's the annotator described before */
$imageAnnotator = new ImageAnnotatorClient();

/* We create an AnnotateFileRequest instance to annotate one single file */
$file_request = new AnnotateFileRequest();

/* We express our input file in terms of a GcsSource
instance the represents the Google Cloud Storage location */
$gcs_source = (new GcsSource())

/* Let's specify the feature we need. You can find the options below */
$feature = (new Feature())

/* Let's specify the file info: a PDF in that location */
$input_config = (new InputConfig())

/* Some configurations, including the pages of the file to perform image annotation. */
$file_request = $file_request->setInputConfig($input_config)

/* Annotate the files and get the responses making the synchronous batch request. */
$result = $imageAnnotator->batchAnnotateFiles([$file_request]);

/* We take the first result, because that's 1 page only. */
$res = $result->getResponses();
$offset = $res->offsetGet(0);
$responses = $offset->getResponses();
$res = $responses[0];

/* Finally!!! The annotations! */
$annotations = $res->getFullTextAnnotation();

/* Clean up resources such as threads */


In your request you can set the type of annotation you want to perform on the file. You can check the reference or the features list documentation.

Some examples are:

  • Face detection
  • Landmark detection
  • Logo detection
  • Label detection
  • Text and document text detection
  • ..

Source link

Di Red


Questo sito usa Akismet per ridurre lo spam. Scopri come i tuoi dati vengono elaborati.

%d blogger hanno fatto clic su Mi Piace per questo: