MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

vision/async-batch-annotate-images.js:17–81  ·  view source on GitHub ↗
(
  inputImageUri = 'gs://cloud-samples-data/vision/label/wakeupcat.jpg',
  outputUri = 'gs://YOUR_BUCKET_ID/path/to/save/results/'
)

Source from the content-addressed store, hash-verified

15'use strict';
16
17function main(
18 inputImageUri = 'gs://cloud-samples-data/vision/label/wakeupcat.jpg',
19 outputUri = 'gs://YOUR_BUCKET_ID/path/to/save/results/'
20) {
21 // [START vision_async_batch_annotate_images]
22 /**
23 * TODO(developer): Uncomment these variables before running the sample.
24 */
25 // const inputImageUri = 'gs://cloud-samples-data/vision/label/wakeupcat.jpg';
26 // const outputUri = 'gs://YOUR_BUCKET_ID/path/to/save/results/';
27
28 // Imports the Google Cloud client libraries
29 const {ImageAnnotatorClient} = require('@google-cloud/vision').v1;
30
31 // Instantiates a client
32 const client = new ImageAnnotatorClient();
33
34 // You can send multiple images to be annotated, this sample demonstrates how to do this with
35 // one image. If you want to use multiple images, you have to create a request object for each image that you want annotated.
36 async function asyncBatchAnnotateImages() {
37 // Set the type of annotation you want to perform on the image
38 // https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#google.cloud.vision.v1.Feature.Type
39 const features = [{type: 'LABEL_DETECTION'}];
40
41 // Build the image request object for that one image. Note: for additional images you have to create
42 // additional image request objects and store them in a list to be used below.
43 const imageRequest = {
44 image: {
45 source: {
46 imageUri: inputImageUri,
47 },
48 },
49 features: features,
50 };
51
52 // Set where to store the results for the images that will be annotated.
53 const outputConfig = {
54 gcsDestination: {
55 uri: outputUri,
56 },
57 batchSize: 2, // The max number of responses to output in each JSON file
58 };
59
60 // Add each image request object to the batch request and add the output config.
61 const request = {
62 requests: [
63 imageRequest, // add additional request objects here
64 ],
65 outputConfig,
66 };
67
68 // Make the asynchronous batch request.
69 const [operation] = await client.asyncBatchAnnotateImages(request);
70
71 // Wait for the operation to complete
72 const [filesResponse] = await operation.promise();
73
74 // The output is written to GCS with the provided output_uri as prefix

Callers 1

Calls 1

asyncBatchAnnotateImagesFunction · 0.85

Tested by

no test coverage detected