Java Code Examples for com.google.cloud.vision.v1.AnnotateImageResponse

The following examples show how to use com.google.cloud.vision.v1.AnnotateImageResponse. These examples are extracted from open source projects. 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 Project: spring-cloud-gcp   Source File: CloudVisionTemplateTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testExtractTextError() {
	AnnotateImageResponse response = AnnotateImageResponse.newBuilder()
			.setError(
					Status.newBuilder()
							.setCode(Code.INTERNAL.value())
							.setMessage("Error Message from Vision API."))
			.build();

	BatchAnnotateImagesResponse responseBatch = BatchAnnotateImagesResponse
			.newBuilder()
			.addResponses(response)
			.build();

	when(this.imageAnnotatorClient.batchAnnotateImages(any(BatchAnnotateImagesRequest.class)))
			.thenReturn(responseBatch);

	this.expectedException.expect(CloudVisionException.class);
	this.expectedException.expectMessage("Error Message from Vision API.");

	this.cloudVisionTemplate.extractTextFromImage(FAKE_IMAGE);
}
 
Example 2
Source Project: beam   Source File: AnnotateImages.java    License: 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 3
Source Project: TweetwallFX   Source File: ImageContentAnalysis.java    License: MIT License 6 votes vote down vote up
public ImageContentAnalysis(final AnnotateImageResponse air) {
        if (air.hasError()) {
            setAnalysisError(new AnalysisError(air.getError()));
        }

        setSafeSearch(SafeSearch.of(air.getSafeSearchAnnotation()));
        setTexts(air.getTextAnnotationsList().stream()
                .map(TextEntry::new)
                .collect(Collectors.toList()));
//        air.getTextAnnotationsList(); // TODO: add it

//        air.getCropHintsAnnotation(); // TODO: add it
//        air.getFaceAnnotationsList(); // TODO: add it
//        air.getFullTextAnnotation(); // TODO: add it
//        air.getImagePropertiesAnnotation(); // TODO: add it
//        air.getLabelAnnotationsList(); // TODO: add it
//        air.getLandmarkAnnotationsList(); // TODO: add it
//        air.getLocalizedObjectAnnotationsList(); // TODO: add it
//        air.getLogoAnnotationsList(); // TODO: add it
//        air.getWebDetection(); // TODO: add it
    }
 
Example 4
Source Project: spring-cloud-gcp   Source File: VisionController.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * This method downloads an image from a URL and sends its contents to the Vision API for label detection.
 *
 * @param imageUrl the URL of the image
 * @param map the model map to use
 * @return a string with the list of labels and percentage of certainty
 * @throws org.springframework.cloud.gcp.vision.CloudVisionException if the Vision API call
 *    produces an error
 */
@GetMapping("/extractLabels")
public ModelAndView extractLabels(String imageUrl, ModelMap map) {
	AnnotateImageResponse response = this.cloudVisionTemplate.analyzeImage(
			this.resourceLoader.getResource(imageUrl), Type.LABEL_DETECTION);

	// This gets the annotations of the image from the response object.
	List<EntityAnnotation> annotations = response.getLabelAnnotationsList();

	map.addAttribute("annotations", annotations);
	map.addAttribute("imageUrl", imageUrl);

	return new ModelAndView("result", map);
}
 
Example 5
Source Project: spring-cloud-gcp   Source File: CloudVisionTemplate.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Extract the text out of an image and return the result as a String.
 * @param imageResource the image one wishes to analyze
 * @param imageContext the image context to customize the text extraction request
 * @return the text extracted from the image aggregated to a String
 * @throws CloudVisionException if the image could not be read or if text extraction failed
 */
public String extractTextFromImage(Resource imageResource, ImageContext imageContext) {
	AnnotateImageResponse response = analyzeImage(imageResource, imageContext, Type.TEXT_DETECTION);

	String result = response.getFullTextAnnotation().getText();
	if (result.isEmpty() && response.getError().getCode() != Code.OK.getNumber()) {
		throw new CloudVisionException(response.getError().getMessage());
	}

	return result;
}
 
Example 6
Source Project: spring-cloud-gcp   Source File: CloudVisionTemplate.java    License: 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 7
Source Project: spring-cloud-gcp   Source File: OcrPageRange.java    License: Apache License 2.0 5 votes vote down vote up
private static List<TextAnnotation> parseJsonBlob(Blob blob)
		throws InvalidProtocolBufferException {

	AnnotateFileResponse.Builder annotateFileResponseBuilder = AnnotateFileResponse.newBuilder();
	String jsonContent = new String(blob.getContent());
	JsonFormat.parser().ignoringUnknownFields().merge(jsonContent, annotateFileResponseBuilder);

	AnnotateFileResponse annotateFileResponse = annotateFileResponseBuilder.build();

	return annotateFileResponse.getResponsesList().stream()
			.map(AnnotateImageResponse::getFullTextAnnotation)
			.collect(Collectors.toList());
}
 
Example 8
Source Project: beam   Source File: AnnotateImages.java    License: 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 9
Source Project: beam   Source File: AnnotateImagesTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnAnnotationList() {
  when(response.getResponsesList())
      .thenReturn(Collections.singletonList(AnnotateImageResponse.newBuilder().build()));
  when(imageAnnotatorClient.batchAnnotateImages(anyList())).thenReturn(response);
  AnnotateImages.PerformImageAnnotation performImageAnnotation =
      new AnnotateImages.PerformImageAnnotation(imageAnnotatorClient);
  List<AnnotateImageResponse> responseList =
      performImageAnnotation.getResponse(Collections.emptyList());
  assertEquals(1, responseList.size());
}
 
Example 10
Source Project: beam   Source File: CloudVisionIT.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void annotateImageFromURINoContext() {
  PCollection<List<AnnotateImageResponse>> annotationResponses =
      testPipeline
          .apply(Create.of(TEST_IMAGE_URI))
          .apply(CloudVision.annotateImagesFromGcsUri(null, features, 1, NUMBER_OF_KEYS));

  PAssert.that(annotationResponses).satisfies(new VerifyImageAnnotationResult());

  testPipeline.run().waitUntilFinish();
}
 
Example 11
Source Project: beam   Source File: CloudVisionIT.java    License: Apache License 2.0 5 votes vote down vote up
private Consumer<? super List<AnnotateImageResponse>> findStringMatchesInAnnotationResponse(
    List<Boolean> labelMatches, String expectedLabel) {
  return annotateImageResponses -> {
    labelMatches.add(
        annotateImageResponses.stream()
            .anyMatch(result -> entityWithDescriptionFoundInResult(expectedLabel, result)));
  };
}
 
Example 12
Source Project: java-docs-samples   Source File: VisionController.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * This method downloads an image from a URL and sends its contents to the Vision API for label
 * detection.
 *
 * @param imageUrl the URL of the image
 * @param map the model map to use
 * @return a string with the list of labels and percentage of certainty
 */
@GetMapping("/extractLabels")
public ModelAndView extractLabels(String imageUrl, ModelMap map) {
  // [START spring_vision_image_labelling]
  AnnotateImageResponse response =
      this.cloudVisionTemplate.analyzeImage(
          this.resourceLoader.getResource(imageUrl), Type.LABEL_DETECTION);

  Map<String, Float> imageLabels =
      response
          .getLabelAnnotationsList()
          .stream()
          .collect(
              Collectors.toMap(
                  EntityAnnotation::getDescription,
                  EntityAnnotation::getScore,
                  (u, v) -> {
                    throw new IllegalStateException(String.format("Duplicate key %s", u));
                  },
                  LinkedHashMap::new));
  // [END spring_vision_image_labelling]

  map.addAttribute("annotations", imageLabels);
  map.addAttribute("imageUrl", imageUrl);

  return new ModelAndView("result", map);
}
 
Example 13
Source Project: java-docs-samples   Source File: QuickstartSample.java    License: 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 14
Source Project: java-docs-samples   Source File: Detect.java    License: 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 15
Source Project: java-docs-samples   Source File: Detect.java    License: 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 16
Source Project: java-docs-samples   Source File: DetectLandmarks.java    License: 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 17
Source Project: java-docs-samples   Source File: DetectCropHints.java    License: 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 18
Source Project: java-docs-samples   Source File: DetectText.java    License: 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 19
Source Project: java-docs-samples   Source File: DetectProperties.java    License: 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 20
Source Project: java-docs-samples   Source File: DetectLogos.java    License: 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 21
Source Project: java-docs-samples   Source File: DetectFaces.java    License: 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 22
Source Project: java-docs-samples   Source File: DetectTextGcs.java    License: 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 23
Source Project: java-docs-samples   Source File: DetectCropHintsGcs.java    License: 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 24
Source Project: java-docs-samples   Source File: DetectLabels.java    License: 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 25
Source Project: java-docs-samples   Source File: DetectLandmarksGcs.java    License: 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 26
Source Project: java-docs-samples   Source File: DetectSafeSearch.java    License: 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 27
Source Project: java-docs-samples   Source File: DetectPropertiesGcs.java    License: 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 28
Source Project: java-docs-samples   Source File: DetectLandmarksUrl.java    License: 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 29
Source Project: java-docs-samples   Source File: DetectSafeSearchGcs.java    License: 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 30
Source Project: java-docs-samples   Source File: DetectLabelsGcs.java    License: 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()));
      }
    }
  }
}