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

The following examples show how to use com.google.cloud.vision.v1.EntityAnnotation. 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: VisionController.java    From spring-cloud-gcp with 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 #2
Source File: VisionApiSampleApplicationTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testClassifyImageLabels() throws Exception {
	this.mockMvc.perform(get(LABEL_IMAGE_URL))
			.andDo((response) -> {
				ModelAndView result = response.getModelAndView();
				List<EntityAnnotation> annotations = (List<EntityAnnotation>) result.getModelMap().get("annotations");

				List<String> annotationNames = annotations.stream()
						.map(annotation -> annotation.getDescription().toLowerCase().trim())
						.collect(Collectors.toList());

				assertThat(annotationNames).contains("boston terrier");
			});
}
 
Example #3
Source File: VisionController.java    From java-docs-samples with 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 #4
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 #5
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 #6
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 #7
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 #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: 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 #10
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 #11
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 #12
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 #13
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 #14
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 #15
Source File: ImageContentAnalysis.java    From TweetwallFX with MIT License 5 votes vote down vote up
public TextEntry(final EntityAnnotation ea) {
    this.description = ea.getDescription();
    this.locale = ea.getLocale();
    this.locations = ea.getLocationsList().stream()
            .map(LocationEntry::new)
            .collect(Collectors.toList());
    this.score = ea.getScore();
}
 
Example #16
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());
      }
    }
  }
}