Java Code Examples for com.google.cloud.vision.v1.ImageAnnotatorClient#create()

The following examples show how to use com.google.cloud.vision.v1.ImageAnnotatorClient#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DetectSafeSearch.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void detectSafeSearch(String filePath) throws IOException {
  List<AnnotateImageRequest> requests = new ArrayList<>();

  ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));

  Image img = Image.newBuilder().setContent(imgBytes).build();
  Feature feat = Feature.newBuilder().setType(Feature.Type.SAFE_SEARCH_DETECTION).build();
  AnnotateImageRequest request =
      AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
  requests.add(request);

  // 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 client = ImageAnnotatorClient.create()) {
    BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
    List<AnnotateImageResponse> responses = response.getResponsesList();

    for (AnnotateImageResponse res : responses) {
      if (res.hasError()) {
        System.out.format("Error: %s%n", res.getError().getMessage());
        return;
      }

      // For full list of available annotations, see http://g.co/cloud/vision/docs
      SafeSearchAnnotation annotation = res.getSafeSearchAnnotation();
      System.out.format(
          "adult: %s%nmedical: %s%nspoofed: %s%nviolence: %s%nracy: %s%n",
          annotation.getAdult(),
          annotation.getMedical(),
          annotation.getSpoof(),
          annotation.getViolence(),
          annotation.getRacy());
    }
  }
}
 
Example 2
Source File: DetectLogosGcs.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void detectLogosGcs(String gcsPath) throws IOException {
  List<AnnotateImageRequest> requests = new ArrayList<>();

  ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
  Image img = Image.newBuilder().setSource(imgSource).build();
  Feature feat = Feature.newBuilder().setType(Feature.Type.LOGO_DETECTION).build();
  AnnotateImageRequest request =
      AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
  requests.add(request);

  // 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 client = ImageAnnotatorClient.create()) {
    BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
    List<AnnotateImageResponse> responses = response.getResponsesList();

    for (AnnotateImageResponse res : responses) {
      if (res.hasError()) {
        System.out.format("Error: %s%n", res.getError().getMessage());
        return;
      }

      // For full list of available annotations, see http://g.co/cloud/vision/docs
      for (EntityAnnotation annotation : res.getLogoAnnotationsList()) {
        System.out.println(annotation.getDescription());
      }
    }
  }
}
 
Example 3
Source File: QuickstartSample.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void main(String... args) throws Exception {
  // 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 vision = ImageAnnotatorClient.create()) {

    // The path to the image file to annotate
    String fileName = "./resources/wakeupcat.jpg";

    // Reads the image file into memory
    Path path = Paths.get(fileName);
    byte[] data = Files.readAllBytes(path);
    ByteString imgBytes = ByteString.copyFrom(data);

    // Builds the image annotation request
    List<AnnotateImageRequest> requests = new ArrayList<>();
    Image img = Image.newBuilder().setContent(imgBytes).build();
    Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build();
    AnnotateImageRequest request =
        AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);

    // Performs label detection on the image file
    BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);
    List<AnnotateImageResponse> responses = response.getResponsesList();

    for (AnnotateImageResponse res : responses) {
      if (res.hasError()) {
        System.out.format("Error: %s%n", res.getError().getMessage());
        return;
      }

      for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
        annotation
            .getAllFields()
            .forEach((k, v) -> System.out.format("%s : %s%n", k, v.toString()));
      }
    }
  }
}
 
Example 4
Source File: DetectFacesGcs.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void detectFacesGcs(String gcsPath) throws IOException {
  List<AnnotateImageRequest> requests = new ArrayList<>();

  ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
  Image img = Image.newBuilder().setSource(imgSource).build();
  Feature feat = Feature.newBuilder().setType(Feature.Type.FACE_DETECTION).build();

  AnnotateImageRequest request =
      AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
  requests.add(request);

  // 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 client = ImageAnnotatorClient.create()) {
    BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
    List<AnnotateImageResponse> responses = response.getResponsesList();

    for (AnnotateImageResponse res : responses) {
      if (res.hasError()) {
        System.out.format("Error: %s%n", res.getError().getMessage());
        return;
      }

      // For full list of available annotations, see http://g.co/cloud/vision/docs
      for (FaceAnnotation annotation : res.getFaceAnnotationsList()) {
        System.out.format(
            "anger: %s%njoy: %s%nsurprise: %s%nposition: %s",
            annotation.getAngerLikelihood(),
            annotation.getJoyLikelihood(),
            annotation.getSurpriseLikelihood(),
            annotation.getBoundingPoly());
      }
    }
  }
}
 
Example 5
Source File: DetectLandmarksGcs.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void detectLandmarksGcs(String gcsPath) throws IOException {
  List<AnnotateImageRequest> requests = new ArrayList<>();

  ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
  Image img = Image.newBuilder().setSource(imgSource).build();
  Feature feat = Feature.newBuilder().setType(Feature.Type.LANDMARK_DETECTION).build();
  AnnotateImageRequest request =
      AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
  requests.add(request);

  // 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 client = ImageAnnotatorClient.create()) {
    BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
    List<AnnotateImageResponse> responses = response.getResponsesList();

    for (AnnotateImageResponse res : responses) {
      if (res.hasError()) {
        System.out.format("Error: %s%n", res.getError().getMessage());
        return;
      }

      // For full list of available annotations, see http://g.co/cloud/vision/docs
      for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) {
        LocationInfo info = annotation.getLocationsList().listIterator().next();
        System.out.format("Landmark: %s%n %s%n", annotation.getDescription(), info.getLatLng());
      }
    }
  }
}
 
Example 6
Source File: Detect.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Detects localized objects in the specified local image.
 *
 * @param filePath The path to the file to perform localized object detection on.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectLocalizedObjects(String filePath) throws IOException {
  List<AnnotateImageRequest> requests = new ArrayList<>();

  ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));

  Image img = Image.newBuilder().setContent(imgBytes).build();
  AnnotateImageRequest request =
      AnnotateImageRequest.newBuilder()
          .addFeatures(Feature.newBuilder().setType(Type.OBJECT_LOCALIZATION))
          .setImage(img)
          .build();
  requests.add(request);

  // 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 client = ImageAnnotatorClient.create()) {
    // Perform the request
    BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
    List<AnnotateImageResponse> responses = response.getResponsesList();

    // Display the results
    for (AnnotateImageResponse res : responses) {
      for (LocalizedObjectAnnotation entity : res.getLocalizedObjectAnnotationsList()) {
        System.out.format("Object name: %s%n", entity.getName());
        System.out.format("Confidence: %s%n", entity.getScore());
        System.out.format("Normalized Vertices:%n");
        entity
            .getBoundingPoly()
            .getNormalizedVerticesList()
            .forEach(vertex -> System.out.format("- (%s, %s)%n", vertex.getX(), vertex.getY()));
      }
    }
  }
}
 
Example 7
Source File: Detect.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Detects localized objects in a remote image on Google Cloud Storage.
 *
 * @param gcsPath The path to the remote file on Google Cloud Storage to detect localized objects
 *     on.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectLocalizedObjectsGcs(String gcsPath) throws IOException {
  List<AnnotateImageRequest> requests = new ArrayList<>();

  ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
  Image img = Image.newBuilder().setSource(imgSource).build();

  AnnotateImageRequest request =
      AnnotateImageRequest.newBuilder()
          .addFeatures(Feature.newBuilder().setType(Type.OBJECT_LOCALIZATION))
          .setImage(img)
          .build();
  requests.add(request);

  // 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 client = ImageAnnotatorClient.create()) {
    // Perform the request
    BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
    List<AnnotateImageResponse> responses = response.getResponsesList();
    client.close();
    // Display the results
    for (AnnotateImageResponse res : responses) {
      for (LocalizedObjectAnnotation entity : res.getLocalizedObjectAnnotationsList()) {
        System.out.format("Object name: %s%n", entity.getName());
        System.out.format("Confidence: %s%n", entity.getScore());
        System.out.format("Normalized Vertices:%n");
        entity
            .getBoundingPoly()
            .getNormalizedVerticesList()
            .forEach(vertex -> System.out.format("- (%s, %s)%n", vertex.getX(), vertex.getY()));
      }
    }
  }
}
 
Example 8
Source File: DetectWebEntitiesGcs.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void detectWebEntitiesGcs(String gcsPath) throws IOException {

    // 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 client = ImageAnnotatorClient.create()) {
      // Set the image source to the given gs uri
      ImageSource imageSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
      // Build the image
      Image image = Image.newBuilder().setSource(imageSource).build();

      // Create the request with the image and the specified feature: web detection
      AnnotateImageRequest request =
          AnnotateImageRequest.newBuilder()
              .addFeatures(Feature.newBuilder().setType(Feature.Type.WEB_DETECTION))
              .setImage(image)
              .build();

      // Perform the request
      BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request));

      // Display the results
      response.getResponsesList().stream()
          .forEach(
              r ->
                  r.getWebDetection().getWebEntitiesList().stream()
                      .forEach(
                          entity -> {
                            System.out.format("Description: %s%n", entity.getDescription());
                            System.out.format("Score: %f%n", entity.getScore());
                          }));
    }
  }
 
Example 9
Source File: DetectTextGcs.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void detectTextGcs(String gcsPath) throws IOException {
  List<AnnotateImageRequest> requests = new ArrayList<>();

  ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
  Image img = Image.newBuilder().setSource(imgSource).build();
  Feature feat = Feature.newBuilder().setType(Feature.Type.TEXT_DETECTION).build();
  AnnotateImageRequest request =
      AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
  requests.add(request);

  // 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 client = ImageAnnotatorClient.create()) {
    BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
    List<AnnotateImageResponse> responses = response.getResponsesList();

    for (AnnotateImageResponse res : responses) {
      if (res.hasError()) {
        System.out.format("Error: %s%n", res.getError().getMessage());
        return;
      }

      // For full list of available annotations, see http://g.co/cloud/vision/docs
      for (EntityAnnotation annotation : res.getTextAnnotationsList()) {
        System.out.format("Text: %s%n", annotation.getDescription());
        System.out.format("Position : %s%n", annotation.getBoundingPoly());
      }
    }
  }
}
 
Example 10
Source File: DetectLandmarks.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void detectLandmarks(String filePath) throws IOException {
  List<AnnotateImageRequest> requests = new ArrayList<>();
  ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));

  Image img = Image.newBuilder().setContent(imgBytes).build();
  Feature feat = Feature.newBuilder().setType(Feature.Type.LANDMARK_DETECTION).build();
  AnnotateImageRequest request =
      AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
  requests.add(request);

  // 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 client = ImageAnnotatorClient.create()) {
    BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
    List<AnnotateImageResponse> responses = response.getResponsesList();

    for (AnnotateImageResponse res : responses) {
      if (res.hasError()) {
        System.out.format("Error: %s%n", res.getError().getMessage());
        return;
      }

      // For full list of available annotations, see http://g.co/cloud/vision/docs
      for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) {
        LocationInfo info = annotation.getLocationsList().listIterator().next();
        System.out.format("Landmark: %s%n %s%n", annotation.getDescription(), info.getLatLng());
      }
    }
  }
}
 
Example 11
Source File: DetectCropHints.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void detectCropHints(String filePath) throws IOException {
  List<AnnotateImageRequest> requests = new ArrayList<>();

  ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));

  Image img = Image.newBuilder().setContent(imgBytes).build();
  Feature feat = Feature.newBuilder().setType(Feature.Type.CROP_HINTS).build();
  AnnotateImageRequest request =
      AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
  requests.add(request);

  // 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 client = ImageAnnotatorClient.create()) {
    BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
    List<AnnotateImageResponse> responses = response.getResponsesList();

    for (AnnotateImageResponse res : responses) {
      if (res.hasError()) {
        System.out.format("Error: %s%n", res.getError().getMessage());
        return;
      }

      // For full list of available annotations, see http://g.co/cloud/vision/docs
      CropHintsAnnotation annotation = res.getCropHintsAnnotation();
      for (CropHint hint : annotation.getCropHintsList()) {
        System.out.println(hint.getBoundingPoly());
      }
    }
  }
}
 
Example 12
Source File: DetectText.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void detectText(String filePath) throws IOException {
  List<AnnotateImageRequest> requests = new ArrayList<>();

  ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));

  Image img = Image.newBuilder().setContent(imgBytes).build();
  Feature feat = Feature.newBuilder().setType(Feature.Type.TEXT_DETECTION).build();
  AnnotateImageRequest request =
      AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
  requests.add(request);

  // 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 client = ImageAnnotatorClient.create()) {
    BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
    List<AnnotateImageResponse> responses = response.getResponsesList();

    for (AnnotateImageResponse res : responses) {
      if (res.hasError()) {
        System.out.format("Error: %s%n", res.getError().getMessage());
        return;
      }

      // For full list of available annotations, see http://g.co/cloud/vision/docs
      for (EntityAnnotation annotation : res.getTextAnnotationsList()) {
        System.out.format("Text: %s%n", annotation.getDescription());
        System.out.format("Position : %s%n", annotation.getBoundingPoly());
      }
    }
  }
}
 
Example 13
Source File: DetectProperties.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void detectProperties(String filePath) throws IOException {
  List<AnnotateImageRequest> requests = new ArrayList<>();

  ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));

  Image img = Image.newBuilder().setContent(imgBytes).build();
  Feature feat = Feature.newBuilder().setType(Feature.Type.IMAGE_PROPERTIES).build();
  AnnotateImageRequest request =
      AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
  requests.add(request);

  // 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 client = ImageAnnotatorClient.create()) {
    BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
    List<AnnotateImageResponse> responses = response.getResponsesList();

    for (AnnotateImageResponse res : responses) {
      if (res.hasError()) {
        System.out.format("Error: %s%n", res.getError().getMessage());
        return;
      }

      // For full list of available annotations, see http://g.co/cloud/vision/docs
      DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors();
      for (ColorInfo color : colors.getColorsList()) {
        System.out.format(
            "fraction: %f%nr: %f, g: %f, b: %f%n",
            color.getPixelFraction(),
            color.getColor().getRed(),
            color.getColor().getGreen(),
            color.getColor().getBlue());
      }
    }
  }
}
 
Example 14
Source File: DetectLabelsGcs.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void detectLabelsGcs(String gcsPath) throws IOException {
  List<AnnotateImageRequest> requests = new ArrayList<>();

  ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
  Image img = Image.newBuilder().setSource(imgSource).build();
  Feature feat = Feature.newBuilder().setType(Feature.Type.LABEL_DETECTION).build();
  AnnotateImageRequest request =
      AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
  requests.add(request);

  // 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 client = ImageAnnotatorClient.create()) {
    BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
    List<AnnotateImageResponse> responses = response.getResponsesList();

    for (AnnotateImageResponse res : responses) {
      if (res.hasError()) {
        System.out.format("Error: %s%n", res.getError().getMessage());
        return;
      }

      // For full list of available annotations, see http://g.co/cloud/vision/docs
      for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
        annotation
            .getAllFields()
            .forEach((k, v) -> System.out.format("%s : %s%n", k, v.toString()));
      }
    }
  }
}
 
Example 15
Source File: BatchAnnotateFilesGcs.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void batchAnnotateFilesGcs(String gcsUri) throws IOException {
  // 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 files to be annotated, this sample demonstrates how to do this with
    // one file. If you want to use multiple files, you have to create a `AnnotateImageRequest`
    // object for each file that you want annotated.
    // First specify where the vision api can find the image
    GcsSource gcsSource = GcsSource.newBuilder().setUri(gcsUri).build();

    // Specify the input config with the file's uri and its type.
    // Supported mime_type: application/pdf, image/tiff, image/gif
    // https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#inputconfig
    InputConfig inputConfig =
        InputConfig.newBuilder().setMimeType("application/pdf").setGcsSource(gcsSource).build();

    // Set the type of annotation you want to perform on the file
    // 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.DOCUMENT_TEXT_DETECTION).build();

    // Build the request object for that one file. Note: for additional file you have to create
    // additional `AnnotateFileRequest` objects and store them in a list to be used below.
    // Since we are sending a file of type `application/pdf`, we can use the `pages` field to
    // specify which pages to process. The service can process up to 5 pages per document file.
    // https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#google.cloud.vision.v1.AnnotateFileRequest
    AnnotateFileRequest fileRequest =
        AnnotateFileRequest.newBuilder()
            .setInputConfig(inputConfig)
            .addFeatures(feature)
            .addPages(1) // Process the first page
            .addPages(2) // Process the second page
            .addPages(-1) // Process the last page
            .build();

    // Add each `AnnotateFileRequest` object to the batch request.
    BatchAnnotateFilesRequest request =
        BatchAnnotateFilesRequest.newBuilder().addRequests(fileRequest).build();

    // Make the synchronous batch request.
    BatchAnnotateFilesResponse response = imageAnnotatorClient.batchAnnotateFiles(request);

    // Process the results, just get the first result, since only one file was sent in this
    // sample.
    for (AnnotateImageResponse imageResponse :
        response.getResponsesList().get(0).getResponsesList()) {
      System.out.format("Full text: %s%n", imageResponse.getFullTextAnnotation().getText());
      for (Page page : imageResponse.getFullTextAnnotation().getPagesList()) {
        for (Block block : page.getBlocksList()) {
          System.out.format("%nBlock confidence: %s%n", block.getConfidence());
          for (Paragraph par : block.getParagraphsList()) {
            System.out.format("\tParagraph confidence: %s%n", par.getConfidence());
            for (Word word : par.getWordsList()) {
              System.out.format("\t\tWord confidence: %s%n", word.getConfidence());
              for (Symbol symbol : word.getSymbolsList()) {
                System.out.format(
                    "\t\t\tSymbol: %s, (confidence: %s)%n",
                    symbol.getText(), symbol.getConfidence());
              }
            }
          }
        }
      }
    }
  }
}
 
Example 16
Source File: ImageMagick.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
@Override
// Blurs uploaded images that are flagged as Adult or Violence.
public void accept(GcsEvent gcsEvent, Context context) {
  // Validate parameters
  if (gcsEvent.getBucket() == null || gcsEvent.getName() == null) {
    logger.severe("Error: Malformed GCS event.");
    return;
  }

  BlobInfo blobInfo = BlobInfo.newBuilder(gcsEvent.getBucket(), gcsEvent.getName()).build();

  // Construct URI to GCS bucket and file.
  String gcsPath = String.format("gs://%s/%s", gcsEvent.getBucket(), gcsEvent.getName());
  logger.info(String.format("Analyzing %s", gcsEvent.getName()));

  // Construct request.
  ImageSource imgSource = ImageSource.newBuilder().setImageUri(gcsPath).build();
  Image img = Image.newBuilder().setSource(imgSource).build();
  Feature feature = Feature.newBuilder().setType(Type.SAFE_SEARCH_DETECTION).build();
  AnnotateImageRequest request =
      AnnotateImageRequest.newBuilder().addFeatures(feature).setImage(img).build();
  List<AnnotateImageRequest> requests = List.of(request);

  // Send request to the Vision API.
  try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
    BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
    List<AnnotateImageResponse> responses = response.getResponsesList();
    for (AnnotateImageResponse res : responses) {
      if (res.hasError()) {
        logger.info(String.format("Error: %s", res.getError().getMessage()));
        return;
      }
      // Get Safe Search Annotations
      SafeSearchAnnotation annotation = res.getSafeSearchAnnotation();
      if (annotation.getAdultValue() == 5 || annotation.getViolenceValue() == 5) {
        logger.info(String.format("Detected %s as inappropriate.", gcsEvent.getName()));
        blur(blobInfo);
      } else {
        logger.info(String.format("Detected %s as OK.", gcsEvent.getName()));
      }
    }
  } catch (IOException e) {
    logger.log(Level.SEVERE, "Error with Vision API: " + e.getMessage(), e);
  }
}
 
Example 17
Source File: ProductSearch.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
/**
 * Search similar products to image in Google Cloud Storage.
 *
 * @param projectId - Id of the project.
 * @param computeRegion - Region name.
 * @param productSetId - Id of the product set.
 * @param productCategory - Category of the product.
 * @param gcsUri - GCS file path of the image to be searched
 * @param filter - Condition to be applied on the labels. Example for filter: (color = red OR
 *     color = blue) AND style = kids It will search on all products with the following labels:
 *     color:red AND style:kids color:blue AND style:kids
 * @throws Exception - on errors.
 */
public static void getSimilarProductsGcs(
    String projectId,
    String computeRegion,
    String productSetId,
    String productCategory,
    String gcsUri,
    String filter)
    throws Exception {
  try (ImageAnnotatorClient queryImageClient = ImageAnnotatorClient.create()) {

    // Get the full path of the product set.
    String productSetPath = ProductSetName.of(projectId, computeRegion, productSetId).toString();

    // Get the image from Google Cloud Storage
    ImageSource source = ImageSource.newBuilder().setGcsImageUri(gcsUri).build();

    // Create annotate image request along with product search feature.
    Feature featuresElement = Feature.newBuilder().setType(Type.PRODUCT_SEARCH).build();
    Image image = Image.newBuilder().setSource(source).build();
    ImageContext imageContext =
        ImageContext.newBuilder()
            .setProductSearchParams(
                ProductSearchParams.newBuilder()
                    .setProductSet(productSetPath)
                    .addProductCategories(productCategory)
                    .setFilter(filter))
            .build();

    AnnotateImageRequest annotateImageRequest =
        AnnotateImageRequest.newBuilder()
            .addFeatures(featuresElement)
            .setImage(image)
            .setImageContext(imageContext)
            .build();
    List<AnnotateImageRequest> requests = Arrays.asList(annotateImageRequest);

    // Search products similar to the image.
    BatchAnnotateImagesResponse response = queryImageClient.batchAnnotateImages(requests);

    List<Result> similarProducts =
        response.getResponses(0).getProductSearchResults().getResultsList();
    System.out.println("Similar Products: ");
    for (Result product : similarProducts) {
      System.out.println(String.format("\nProduct name: %s", product.getProduct().getName()));
      System.out.println(
          String.format("Product display name: %s", product.getProduct().getDisplayName()));
      System.out.println(
          String.format("Product description: %s", product.getProduct().getDescription()));
      System.out.println(String.format("Score(Confidence): %s", product.getScore()));
      System.out.println(String.format("Image name: %s", product.getImage()));
    }
  }
}
 
Example 18
Source File: BatchAnnotateFiles.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void batchAnnotateFiles(String filePath) throws IOException {
  // 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 files to be annotated, this sample demonstrates how to do this with
    // one file. If you want to use multiple files, you have to create a `AnnotateImageRequest`
    // object for each file that you want annotated.
    // First read the files contents
    Path path = Paths.get(filePath);
    byte[] data = Files.readAllBytes(path);
    ByteString content = ByteString.copyFrom(data);

    // Specify the input config with the file's contents and its type.
    // Supported mime_type: application/pdf, image/tiff, image/gif
    // https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#inputconfig
    InputConfig inputConfig =
        InputConfig.newBuilder().setMimeType("application/pdf").setContent(content).build();

    // Set the type of annotation you want to perform on the file
    // 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.DOCUMENT_TEXT_DETECTION).build();

    // Build the request object for that one file. Note: for additional file you have to create
    // additional `AnnotateFileRequest` objects and store them in a list to be used below.
    // Since we are sending a file of type `application/pdf`, we can use the `pages` field to
    // specify which pages to process. The service can process up to 5 pages per document file.
    // https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#google.cloud.vision.v1.AnnotateFileRequest
    AnnotateFileRequest fileRequest =
        AnnotateFileRequest.newBuilder()
            .setInputConfig(inputConfig)
            .addFeatures(feature)
            .addPages(1) // Process the first page
            .addPages(2) // Process the second page
            .addPages(-1) // Process the last page
            .build();

    // Add each `AnnotateFileRequest` object to the batch request.
    BatchAnnotateFilesRequest request =
        BatchAnnotateFilesRequest.newBuilder().addRequests(fileRequest).build();

    // Make the synchronous batch request.
    BatchAnnotateFilesResponse response = imageAnnotatorClient.batchAnnotateFiles(request);

    // Process the results, just get the first result, since only one file was sent in this
    // sample.
    for (AnnotateImageResponse imageResponse :
        response.getResponsesList().get(0).getResponsesList()) {
      System.out.format("Full text: %s%n", imageResponse.getFullTextAnnotation().getText());
      for (Page page : imageResponse.getFullTextAnnotation().getPagesList()) {
        for (Block block : page.getBlocksList()) {
          System.out.format("%nBlock confidence: %s%n", block.getConfidence());
          for (Paragraph par : block.getParagraphsList()) {
            System.out.format("\tParagraph confidence: %s%n", par.getConfidence());
            for (Word word : par.getWordsList()) {
              System.out.format("\t\tWord confidence: %s%n", word.getConfidence());
              for (Symbol symbol : word.getSymbolsList()) {
                System.out.format(
                    "\t\t\tSymbol: %s, (confidence: %s)%n",
                    symbol.getText(), symbol.getConfidence());
              }
            }
          }
        }
      }
    }
  }
}
 
Example 19
Source File: AsyncBatchAnnotateImages.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
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);
  }
}
 
Example 20
Source File: CloudVisionAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 3 votes vote down vote up
/**
 * Configure the Cloud Vision API client {@link ImageAnnotatorClient}. The
 * spring-cloud-gcp-starter autowires a {@link CredentialsProvider} object that provides
 * the GCP credentials, required to authenticate and authorize Vision API calls.
 * <p>Cloud Vision API client implements {@link AutoCloseable}, which is automatically
 * honored by Spring bean lifecycle.
 * <p>Most of the Google Cloud API clients are thread-safe heavy objects. I.e., it's better
 * to produce a singleton and re-using the client object for multiple requests.
 * @return a Cloud Vision API client
 * @throws IOException if an exception occurs creating the ImageAnnotatorClient
 */
@Bean
@ConditionalOnMissingBean
public ImageAnnotatorClient imageAnnotatorClient() throws IOException {
	ImageAnnotatorSettings clientSettings = ImageAnnotatorSettings.newBuilder()
			.setCredentialsProvider(this.credentialsProvider)
			.setHeaderProvider(new UserAgentHeaderProvider(CloudVisionAutoConfiguration.class))
			.build();

	return ImageAnnotatorClient.create(clientSettings);
}