com.google.cloud.vision.v1.AnnotateImageRequest Java Examples

The following examples show how to use com.google.cloud.vision.v1.AnnotateImageRequest. 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: CloudVisionTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddImageContext_analyzeImage() throws IOException {
	when(this.imageAnnotatorClient.batchAnnotateImages(any(BatchAnnotateImagesRequest.class)))
			.thenReturn(DEFAULT_API_RESPONSE);

	ImageContext imageContext = Mockito.mock(ImageContext.class);

	this.cloudVisionTemplate.analyzeImage(FAKE_IMAGE, imageContext, Type.FACE_DETECTION);

	BatchAnnotateImagesRequest expectedRequest =
			BatchAnnotateImagesRequest.newBuilder()
					.addRequests(
							AnnotateImageRequest.newBuilder()
									.addFeatures(Feature.newBuilder().setType(Type.FACE_DETECTION))
									.setImageContext(imageContext)
									.setImage(Image.newBuilder().setContent(
											ByteString.readFrom(FAKE_IMAGE.getInputStream())).build()))
					.build();

	verify(this.imageAnnotatorClient, times(1)).batchAnnotateImages(expectedRequest);
}
 
Example #2
Source File: CloudVisionTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddImageContext_extractText() throws IOException {
	when(this.imageAnnotatorClient.batchAnnotateImages(any(BatchAnnotateImagesRequest.class)))
			.thenReturn(DEFAULT_API_RESPONSE);

	ImageContext imageContext = Mockito.mock(ImageContext.class);

	this.cloudVisionTemplate.extractTextFromImage(FAKE_IMAGE, imageContext);

	BatchAnnotateImagesRequest expectedRequest =
			BatchAnnotateImagesRequest.newBuilder()
					.addRequests(
							AnnotateImageRequest.newBuilder()
									.addFeatures(Feature.newBuilder().setType(Type.TEXT_DETECTION))
									.setImageContext(imageContext)
									.setImage(Image.newBuilder().setContent(ByteString.readFrom(FAKE_IMAGE.getInputStream())).build()))
					.build();

	verify(this.imageAnnotatorClient, times(1)).batchAnnotateImages(expectedRequest);
}
 
Example #3
Source File: AnnotateImages.java    From beam with Apache License 2.0 6 votes vote down vote up
/**
 * Applies all necessary transforms to call the Vision API. In order to group requests into
 * batches, we assign keys to the requests, as {@link GroupIntoBatches} works only on {@link KV}s.
 */
@Override
public PCollection<List<AnnotateImageResponse>> expand(PCollection<T> input) {
  ParDo.SingleOutput<T, AnnotateImageRequest> inputToRequestMapper;
  if (contextSideInput != null) {
    inputToRequestMapper =
        ParDo.of(new MapInputToRequest(contextSideInput)).withSideInputs(contextSideInput);
  } else {
    inputToRequestMapper = ParDo.of(new MapInputToRequest(null));
  }
  return input
      .apply(inputToRequestMapper)
      .apply(
          WithKeys.of(
                  (SerializableFunction<AnnotateImageRequest, Integer>)
                      ignored -> new Random().nextInt(desiredRequestParallelism))
              .withKeyType(TypeDescriptors.integers()))
      .apply(GroupIntoBatches.ofSize(batchSize))
      .apply(ParDo.of(new PerformImageAnnotation()));
}
 
Example #4
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 #5
Source File: AnnotateImage.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void main(String... args) throws Exception {
  // Instantiates a client
  ImageAnnotatorClient vision = ImageAnnotatorClient.create();

  // The path to the image file to annotate
  String fileName = "your/image/path.jpg"; // for example "./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.printf("Error: %s\n", res.getError().getMessage());
      return;
    }

    for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
      for (Map.Entry<FieldDescriptor, Object> entry : annotation.getAllFields().entrySet()) {
        System.out.printf("%s : %s\n", entry.getKey(), entry.getValue());
      }
    }
  }
}
 
Example #6
Source File: DetectLogos.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void detectLogos(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.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 #7
Source File: DetectFaces.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void detectFaces(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.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 #8
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 #9
Source File: DetectCropHintsGcs.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void detectCropHintsGcs(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.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 #10
Source File: DetectLabels.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void detectLabels(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.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 #11
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 #12
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 #13
Source File: DetectWebEntities.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void detectWebEntities(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 client = ImageAnnotatorClient.create()) {
      // Read in the local image
      ByteString contents = ByteString.readFrom(new FileInputStream(filePath));

      // Build the image
      Image image = Image.newBuilder().setContent(contents).build();

      // Create the request with the image and the specified feature: web detection
      AnnotateImageRequest request =
          AnnotateImageRequest.newBuilder()
              .addFeatures(Feature.newBuilder().setType(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 #14
Source File: DetectPropertiesGcs.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void detectPropertiesGcs(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.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 #15
Source File: DetectLandmarksUrl.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void detectLandmarksUrl(String uri) throws IOException {
  List<AnnotateImageRequest> requests = new ArrayList<>();

  ImageSource imgSource = ImageSource.newBuilder().setImageUri(uri).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 #16
Source File: DetectSafeSearchGcs.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void detectSafeSearchGcs(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(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 #17
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 #18
Source File: SetEndpoint.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void setEndpoint() throws IOException {
  // [START vision_set_endpoint]
  ImageAnnotatorSettings settings =
      ImageAnnotatorSettings.newBuilder().setEndpoint("eu-vision.googleapis.com:443").build();

  // 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.
  ImageAnnotatorClient client = ImageAnnotatorClient.create(settings);
  // [END vision_set_endpoint]

  ImageSource imgSource =
      ImageSource.newBuilder()
          .setGcsImageUri("gs://cloud-samples-data/vision/text/screen.jpg")
          .build();
  Image image = Image.newBuilder().setSource(imgSource).build();
  Feature feature = Feature.newBuilder().setType(Feature.Type.TEXT_DETECTION).build();
  AnnotateImageRequest request =
      AnnotateImageRequest.newBuilder().addFeatures(feature).setImage(image).build();
  List<AnnotateImageRequest> requests = new ArrayList<>();
  requests.add(request);

  BatchAnnotateImagesResponse batchResponse = client.batchAnnotateImages(requests);

  for (AnnotateImageResponse response : batchResponse.getResponsesList()) {
    for (EntityAnnotation annotation : response.getTextAnnotationsList()) {
      System.out.format("Text: %s%n", annotation.getDescription());
      System.out.println("Position:");
      System.out.format("%s%n", annotation.getBoundingPoly());
    }
  }
  client.close();
}
 
Example #19
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 #20
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 #21
Source File: ImageMagick.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void blurOffensiveImages(JsonObject data) {
  String fileName = data.get("name").getAsString();
  String bucketName = data.get("bucket").getAsString();
  BlobInfo blobInfo = BlobInfo.newBuilder(bucketName, fileName).build();
  // Construct URI to GCS bucket and file.
  String gcsPath = String.format("gs://%s/%s", bucketName, fileName);
  System.out.println(String.format("Analyzing %s", fileName));

  // Construct request.
  List<AnnotateImageRequest> requests = new ArrayList<>();
  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();
  requests.add(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()) {
        System.out.println(String.format("Error: %s\n", res.getError().getMessage()));
        return;
      }
      // Get Safe Search Annotations
      SafeSearchAnnotation annotation = res.getSafeSearchAnnotation();
      if (annotation.getAdultValue() == 5 || annotation.getViolenceValue() == 5) {
        System.out.println(String.format("Detected %s as inappropriate.", fileName));
        blur(blobInfo);
      } else {
        System.out.println(String.format("Detected %s as OK.", fileName));
      }
    }
  } catch (Exception e) {
    System.out.println(String.format("Error with Vision API: %s", e.getMessage()));
  }
}
 
Example #22
Source File: GoogleVisionCache.java    From TweetwallFX with MIT License 5 votes vote down vote up
private AnnotateImageRequest createImageRequest(final String imageUri) {
    final AnnotateImageRequest.Builder builder = AnnotateImageRequest.newBuilder()
            .setImage(Image.newBuilder()
                    .setSource(ImageSource.newBuilder()
                            .setImageUri(imageUri)));

    GOOGLE_SETTINGS.getCloudVision().getFeatureTypes().stream()
            .map(GoogleVisionCache::convertFeatureType)
            .forEach(builder.addFeaturesBuilder()::setType);

    return builder.build();
}
 
Example #23
Source File: CloudVisionTemplate.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
/**
 * Analyze an image and extract the features of the image specified by
 * {@code featureTypes}.
 * <p>A feature describes the kind of Cloud Vision analysis one wishes to perform on an
 * image, such as text detection, image labelling, facial detection, etc. A full list of
 * feature types can be found in {@link Feature.Type}.
 * @param imageResource the image one wishes to analyze. The Cloud Vision APIs support
 *     image formats described here: https://cloud.google.com/vision/docs/supported-files
 * @param imageContext the image context used to customize the Vision API request
 * @param featureTypes the types of image analysis to perform on the image
 * @return the results of image analyses
 * @throws CloudVisionException if the image could not be read or if a malformed response
 *     is received from the Cloud Vision APIs
 */
public AnnotateImageResponse analyzeImage(
		Resource imageResource, ImageContext imageContext, Feature.Type... featureTypes) {
	ByteString imgBytes;
	try {
		imgBytes = ByteString.readFrom(imageResource.getInputStream());
	}
	catch (IOException ex) {
		throw new CloudVisionException("Failed to read image bytes from provided resource.", ex);
	}

	Image image = Image.newBuilder().setContent(imgBytes).build();

	List<Feature> featureList = Arrays.stream(featureTypes)
			.map((featureType) -> Feature.newBuilder().setType(featureType).build())
			.collect(Collectors.toList());

	BatchAnnotateImagesRequest request = BatchAnnotateImagesRequest.newBuilder()
			.addRequests(
					AnnotateImageRequest.newBuilder()
							.addAllFeatures(featureList)
							.setImageContext(imageContext)
							.setImage(image))
			.build();

	BatchAnnotateImagesResponse batchResponse = this.imageAnnotatorClient.batchAnnotateImages(request);
	List<AnnotateImageResponse> annotateImageResponses = batchResponse.getResponsesList();

	if (!annotateImageResponses.isEmpty()) {
		return annotateImageResponses.get(0);
	}
	else {
		throw new CloudVisionException(
				"Failed to receive valid response Vision APIs; empty response received.");
	}
}
 
Example #24
Source File: CloudVisionTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConvertByteStringToRequest() {
  CloudVision.AnnotateImagesFromBytes annotateImagesFromBytes =
      CloudVision.annotateImagesFromBytes(null, features, 1, 1);
  AnnotateImageRequest request = annotateImagesFromBytes.mapToRequest(TEST_BYTES, null);
  assertEquals(1, request.getFeaturesCount());
  assertEquals(TEST_BYTES, request.getImage().getContent());
}
 
Example #25
Source File: CloudVision.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Maps the {@link String} with encoded image data and the optional {@link ImageContext} into an
 * {@link AnnotateImageRequest}.
 *
 * @param uri Input element.
 * @param ctx optional image context.
 * @return a valid request.
 */
@Override
public AnnotateImageRequest mapToRequest(String uri, @Nullable ImageContext ctx) {
  AnnotateImageRequest.Builder builder = AnnotateImageRequest.newBuilder();
  if (ctx != null) {
    builder.setImageContext(ctx);
  }
  ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(uri).build();
  return builder
      .addAllFeatures(featureList)
      .setImage(Image.newBuilder().setSource(imgSource).build())
      .build();
}
 
Example #26
Source File: CloudVision.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Maps the {@link ByteString} with encoded image data and the optional {@link ImageContext}
 * into an {@link AnnotateImageRequest}.
 *
 * @param input Input element.
 * @param ctx optional image context.
 * @return a valid request.
 */
@Override
public AnnotateImageRequest mapToRequest(ByteString input, @Nullable ImageContext ctx) {
  AnnotateImageRequest.Builder builder = AnnotateImageRequest.newBuilder();
  if (ctx != null) {
    builder.setImageContext(ctx);
  }
  return builder
      .addAllFeatures(featureList)
      .setImage(Image.newBuilder().setContent(input).build())
      .build();
}
 
Example #27
Source File: CloudVision.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Maps {@link KV} of {@link String} (GCS URI to the image) and {@link ImageContext} to a valid
 * {@link AnnotateImageRequest}.
 *
 * @param input Input element.
 * @param ctx optional image context, ignored here since the input holds context.
 * @return a valid request.
 */
@Override
public AnnotateImageRequest mapToRequest(
    KV<String, ImageContext> input, @Nullable ImageContext ctx) {
  ImageSource imageSource = ImageSource.newBuilder().setGcsImageUri(input.getKey()).build();
  Image image = Image.newBuilder().setSource(imageSource).build();
  AnnotateImageRequest.Builder builder =
      AnnotateImageRequest.newBuilder().setImage(image).addAllFeatures(featureList);
  if (input.getValue() != null) {
    builder.setImageContext(input.getValue());
  }
  return builder.build();
}
 
Example #28
Source File: CloudVision.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Maps {@link KV} of {@link ByteString} (encoded image contents) and {@link ImageContext} to
 * {@link AnnotateImageRequest}.
 *
 * @param input Input element.
 * @param ctx optional image context.
 * @return valid request element.
 */
@Override
public AnnotateImageRequest mapToRequest(
    KV<ByteString, ImageContext> input, @Nullable ImageContext ctx) {
  Image image = Image.newBuilder().setContent(input.getKey()).build();
  AnnotateImageRequest.Builder builder =
      AnnotateImageRequest.newBuilder().setImage(image).addAllFeatures(featureList);
  if (input.getValue() != null) {
    builder.setImageContext(input.getValue());
  }
  return builder.build();
}
 
Example #29
Source File: AnnotateImages.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Performs the call to the Cloud Vision API using a client library. Default access for testing.
 *
 * @param requests request list.
 * @return response list.
 */
List<AnnotateImageResponse> getResponse(Iterable<AnnotateImageRequest> requests) {
  List<AnnotateImageRequest> requestList = new ArrayList<>();
  requests.forEach(requestList::add);
  BatchAnnotateImagesResponse batchAnnotateImagesResponse =
      imageAnnotatorClient.batchAnnotateImages(requestList);
  return batchAnnotateImagesResponse.getResponsesList();
}
 
Example #30
Source File: CloudVisionTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldConvertStringToRequest() {
  CloudVision.AnnotateImagesFromGcsUri annotateImagesFromGcsUri =
      CloudVision.annotateImagesFromGcsUri(null, features, 1, 1);
  AnnotateImageRequest request = annotateImagesFromGcsUri.mapToRequest(TEST_URI, null);
  assertEquals(1, request.getFeaturesCount());
  assertEquals(TEST_URI, request.getImage().getSource().getGcsImageUri());
}