Google Cloud Vision

用于调用 Google Cloud Vision gRPC API 并分析图像(标签、文本、人脸等)的类型化接口。

简介

Google Cloud Vision 分析图像并返回诸如标签、文本、人脸和地标等洞察结果。该 gRPC API 通过 google.cloud.vision.v1.ImageAnnotator 服务公开,在 vision.googleapis.com:443 上通过 TLS 访问,其主要方法是 BatchAnnotateImages,可在单次调用中标注一批图像。

请求通过 TsgcGRPCVisionBatchAnnotateImagesRequest 构建:调用 AddRequest 添加一张图像(对于 gs:// 图像使用 Image.Source.GcsImageUri),调用 AddFeature 请求某项功能,并设置 FeatureType(例如 LABEL_DETECTION)和 MaxResults。

下面的示例使用服务账户 JWT 进行身份验证,通过 TsgcHTTP2ClientTsgcGRPCClient 连接到 Vision 主机,设置授权 Bearer 元数据,并调用 BatchAnnotateImages 对某个 gs:// 图像运行标签检测:


    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;

方法

名称描述
BatchAnnotateImages对一批图像运行图像检测和标注,返回请求的功能结果(标签、文本、人脸等)。

演示

示例文件夹 Demos/21.GRPC/13.Vision 中提供了一个可用的示例,演示如何使用 BatchAnnotateImages 方法进行身份验证并对图像运行标签检测。

另请参阅