הוספת הערות לתמונות רבות במצב אופליין

‫Vision API יכול להפעיל שירותי זיהוי (אסינכרוניים) וסימון של קבוצה גדולה של קובצי תמונה באמצעות כל סוג תכונה של Vision. לדוגמה, אפשר לציין תכונה אחת או יותר של Vision API (כמו TEXT_DETECTION, LABEL_DETECTION ו-LANDMARK_DETECTION) עבור קבוצה אחת של תמונות.

הפלט מבקשת Batch במצב אופליין נכתב לקובץ JSON שנוצר בקטגוריה של Cloud Storage שצוינה.

מגבלות

‫Vision API מקבל עד 2,000 קובצי תמונה. אם תנסו להעלות קבוצה גדולה יותר של קובצי תמונות, תופיע שגיאה.

סוגי התכונות שנתמכים כרגע

סוג התכונה
CROP_HINTS קביעת קודקודים מוצעים לאזור חיתוך בתמונה.
DOCUMENT_TEXT_DETECTION ביצוע OCR על תמונות עם טקסט צפוף, כמו מסמכים (PDF/TIFF) ותמונות עם כתב יד. אפשר להשתמש ב-TEXT_DETECTION לתמונות טקסט עם טקסט דליל. מקבל עדיפות אם מופיעים גם DOCUMENT_TEXT_DETECTION וגם TEXT_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

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי הוראות ההגדרה של Java במאמר התחלת העבודה עם Vision API באמצעות ספריות לקוח. מידע נוסף מופיע ב מאמרי העזרה של 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

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי Node.jsהוראות ההגדרה שבמדריך לתחילת העבודה עם Vision באמצעות ספריות לקוח. מידע נוסף מופיע במאמרי העזרה של Vision Node.js API.

כדי לבצע אימות ב-Vision, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

/**
 * 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