Google Cloud Vision

Typed interface to call the Google Cloud Vision gRPC API and analyze images (label, text, face and more).

Introduction

Google Cloud Vision analyzes images and returns insights such as labels, text, faces and landmarks. The gRPC API is exposed through the google.cloud.vision.v1.ImageAnnotator service, reached at vision.googleapis.com:443 over TLS, and the main method is BatchAnnotateImages, which annotates a batch of images in a single call.

Requests are built with TsgcGRPCVisionBatchAnnotateImagesRequest: call AddRequest to add an image (Image.Source.GcsImageUri for a gs:// image) and AddFeature to request a feature, setting FeatureType (for example LABEL_DETECTION) and MaxResults.

The example below authenticates with a service-account JWT, wires a TsgcGRPCClient over a TsgcHTTP2Client to the Vision host, sets the authorization Bearer metadata and calls BatchAnnotateImages to run label detection on a gs:// image:


    oHTTP2 := TsgcHTTP2Client.Create(nil);
    oHTTP2.Host := 'vision.googleapis.com';
    oHTTP2.Port := 443;
    oHTTP2.TLS := True;

    oGRPC := TsgcGRPCClient.Create(nil);
    oGRPC.Client := oHTTP2;

    // service-account JWT authentication
    oGRPC.GoogleCloudOptions.JWT.KeyFile := 'service-account.json';
    oGRPC.GoogleCloudOptions.JWT.API_Endpoint := 'https://vision.googleapis.com/';
    oGRPC.DefaultMetadata.AddValue('authorization', 'Bearer ' + oGRPC.GoogleCloudOptions.JWT.Token);

    // build the typed request and call the method
    oRequest := TsgcGRPCVisionBatchAnnotateImagesRequest.Create;
    try
      oImage := oRequest.AddRequest;
      oImage.Image.Source.GcsImageUri := 'gs://my-bucket/photo.jpg';
      oImage.AddFeature('LABEL_DETECTION', 10);
      oResponse := oGRPC.Call('google.cloud.vision.v1.ImageAnnotator', 'BatchAnnotateImages', oRequest.ToBytes);
      ShowMessage(oResponse.DataString);
    finally
      oRequest.Free;
    end;

Methods

NameDescription
BatchAnnotateImagesRuns image detection and annotation on a batch of images, returning the requested features (labels, text, faces, etc.).

Demo

A working sample is available in the demo folder Demos/21.GRPC/13.Vision, which shows how to authenticate and run label detection on an image with the BatchAnnotateImages method.

See Also