離線批次圖片加註

Vision API 可使用任何 Vision 功能類型,離線 (非同步) 執行偵測服務,並為大量圖片檔案加上註解。舉例來說,您可以為單一圖片批次指定一或多項 Vision API 功能 (例如 TEXT_DETECTIONLABEL_DETECTIONLANDMARK_DETECTION)。

離線批次要求的輸出內容會寫入指定 Cloud Storage bucket 中建立的 JSON 檔案。

限制

Vision API 最多可接受 2,000 個圖片檔。如果圖片檔案數量過多,系統會傳回錯誤。

目前支援的特徵類型

特徵類型
CROP_HINTS 判斷圖片裁剪區域的建議端點。
DOCUMENT_TEXT_DETECTION 針對含有密集文字的圖片執行光學字元辨識,例如文件 (PDF/TIFF) 和含有手寫文字的圖片。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 = GcsDestination.newBuilder().setUri(outputUri).build();
      OutputConfig outputConfig =
          OutputConfig.newBuilder()
              .setGcsDestination(gcsDestination)
              .setBatchSize(2) // The max number of responses to output in each JSON file
              .build();

      // Add each `AnnotateImageRequest` object to the batch request and add the output config.
      AsyncBatchAnnotateImagesRequest request =
          AsyncBatchAnnotateImagesRequest.newBuilder()
              .addRequests(imageRequest)
              .setOutputConfig(outputConfig)
              .build();

      // Make the asynchronous batch request.
      AsyncBatchAnnotateImagesResponse response =
          imageAnnotatorClient.asyncBatchAnnotateImagesAsync(request).get();

      // The output is written to GCS with the provided output_uri as prefix
      String gcsOutputUri = response.getOutputConfig().getGcsDestination().getUri();
      System.out.format("Output written to GCS with prefix: %s%n", gcsOutputUri);
    }
  }
}

Node.js

在試用這個範例之前,請先按照「使用用戶端程式庫的 Vision 快速入門導覽課程」中的 Node.js 設定說明操作。詳情請參閱 Vision Node.js API 參考文件

如要向 Vision 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const inputImageUri = 'gs://cloud-samples-data/vision/label/wakeupcat.jpg';
// const outputUri = 'gs://YOUR_BUCKET_ID/path/to/save/results/';

// Imports the Google Cloud client libraries
const {ImageAnnotatorClient} = require('@google-cloud/vision').v1;

// Instantiates a client
const client = new ImageAnnotatorClient();

// 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 request object for each image that you want annotated.
async function asyncBatchAnnotateImages() {
  // 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
  const features = [{type: 'LABEL_DETECTION'}];