离线批量图片注释

Vision API 可以使用任何 Vision 特征类型运行离线(异步)检测服务和大批量图片文件注释。例如,您可以为一批图片指定一个或多个 Vision API 特征(例如 TEXT_DETECTIONLABEL_DETECTIONLANDMARK_DETECTION)。

离线批量请求的输出将写入在指定 Cloud Storage 存储桶中创建的 JSON 文件中。

限制

Vision API 最多可接受 2000 个图片文件。批量图片文件数量超过该限制会返回错误。

目前支持的特征类型

特征类型
CROP_HINTS 确定图片的建议剪裁区域顶点。
DOCUMENT_TEXT_DETECTION 对文档 (PDF/TIFF) 等包含密集文本的图片和包含手写内容的图片执行 OCR。TEXT_DETECTION 可用于包含稀疏文本的图片。 如果同时存在 DOCUMENT_TEXT_DETECTIONTEXT_DETECTION,则优先考虑。
FACE_DETECTION 检测图片中的人脸。
IMAGE_PROPERTIES 计算一组图片属性,例如图片的主色。
LABEL_DETECTION 根据图片内容添加标签。
LANDMARK_DETECTION 检测图片中的地标。
LOGO_DETECTION 检测图片中的公司徽标。
OBJECT_LOCALIZATION 检测并提取图片中的多个对象。
SAFE_SEARCH_DETECTION 运行安全搜索可检测可能不安全的内容或不良内容。
TEXT_DETECTION 对图片中的文本执行光学字符识别 (OCR)。 文本检测针对大型图片中的稀疏文本区域进行了优化。 如果图片为文档 (PDF/TIFF)、包含密集文本或包含手写内容,请改用 DOCUMENT_TEXT_DETECTION
WEB_DETECTION 检测图片中的新闻、事件或名人等主题实体,并借助强大的 Google 图片搜索在网络上查找相似的图片。

示例代码

使用以下代码示例对 Cloud Storage 中的一批图片文件运行离线注释服务。

Java

在试用此示例之前,请按照Vision API 快速入门:使用客户端库中的 Java 设置说明进行操作。如需了解详情,请参阅 Vision API Java 参考文档

import com.google.cloud.vision.v1.AnnotateImageRequest;
import com.google.cloud.vision.v1.AsyncBatchAnnotateImagesRequest;
import com.google.cloud.vision.v1.AsyncBatchAnnotateImagesResponse;
import com.google.cloud.vision.v1.Feature;
import com.google.cloud.vision.v1.GcsDestination;
import com.google.cloud.vision.v1.Image;
import com.google.cloud.vision.v1.ImageAnnotatorClient;
import com.google.cloud.vision.v1.ImageSource;
import com.google.cloud.vision.v1.OutputConfig;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class AsyncBatchAnnotateImages {

  public static void asyncBatchAnnotateImages()
      throws InterruptedException, ExecutionException, IOException {
    String inputImageUri = "gs://cloud-samples-data/vision/label/wakeupcat.jpg";
    String outputUri = "gs://YOUR_BUCKET_ID/path/to/save/results/";
    asyncBatchAnnotateImages(inputImageUri, outputUri);
  }

  public static void asyncBatchAnnotateImages(String inputImageUri, String outputUri)
      throws IOException, ExecutionException, InterruptedException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) {

      // You can send multiple images to be annotated, this sample demonstrates how to do this with
      // one image. If you want to use multiple images, you have to create a `AnnotateImageRequest`
      // object for each image that you want annotated.
      // First specify where the vision api can find the image
      ImageSource source = ImageSource.newBuilder().setImageUri(inputImageUri).build();
      Image image = Image.newBuilder().setSource(source).build();

      // Set the type of annotation you want to perform on the image
      // https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#google.cloud.vision.v1.Feature.Type
      Feature feature = Feature.newBuilder().setType(Feature.Type.LABEL_DETECTION).build();

      // Build the request object for that one image. Note: for additional images you have to create
      // additional `AnnotateImageRequest` objects and store them in a list to be used below.
      AnnotateImageRequest imageRequest =
          AnnotateImageRequest.newBuilder().setImage(image).addFeatures(feature).build();

      // Set where to store the results for the images that will be annotated.
      GcsDestination gcsDestination