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

The following examples show how to use com.google.cloud.vision.v1.ImageAnnotatorSettings. 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: GoogleVisionCache.java    From TweetwallFX with MIT License 6 votes vote down vote up
private GoogleVisionCache() {
    if (null == GOOGLE_SETTINGS.getCredentialFilePath()) {
        LOG.warn("File path of Google Credentials not configured. Bypassing all analysis requests.");
        this.imageAnnotatorSettings = null;
    } else {
        try (final FileInputStream fis = new FileInputStream(GOOGLE_SETTINGS.getCredentialFilePath())) {
            final GoogleCredentials credentials = GoogleCredentials.fromStream(fis);
            this.imageAnnotatorSettings = ImageAnnotatorSettings.newBuilder().setCredentialsProvider(() -> credentials).build();
        } catch (final IOException ex) {
            LOG.error(ex, ex);
            throw new IllegalStateException("Failed loading Google Credentials from '" + GOOGLE_SETTINGS.getCredentialFilePath() + "'", ex);
        }
    }

    cache = CacheManagerProvider.getCache(
            "googleVision",
            String.class,
            ImageContentAnalysis.class);
}
 
Example #2
Source File: VisionTestConfiguration.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public ImageAnnotatorClient imageAnnotatorClient() throws IOException {
	ImageAnnotatorSettings clientSettings = ImageAnnotatorSettings.newBuilder()
			.setCredentialsProvider(this.credentialsProvider)
			.build();

	return ImageAnnotatorClient.create(clientSettings);
}
 
Example #3
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 #4
Source File: CloudVisionAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 3 votes vote down vote up
/**
 * Configure the Cloud Vision API client {@link ImageAnnotatorClient}. The
 * spring-cloud-gcp-starter autowires a {@link CredentialsProvider} object that provides
 * the GCP credentials, required to authenticate and authorize Vision API calls.
 * <p>Cloud Vision API client implements {@link AutoCloseable}, which is automatically
 * honored by Spring bean lifecycle.
 * <p>Most of the Google Cloud API clients are thread-safe heavy objects. I.e., it's better
 * to produce a singleton and re-using the client object for multiple requests.
 * @return a Cloud Vision API client
 * @throws IOException if an exception occurs creating the ImageAnnotatorClient
 */
@Bean
@ConditionalOnMissingBean
public ImageAnnotatorClient imageAnnotatorClient() throws IOException {
	ImageAnnotatorSettings clientSettings = ImageAnnotatorSettings.newBuilder()
			.setCredentialsProvider(this.credentialsProvider)
			.setHeaderProvider(new UserAgentHeaderProvider(CloudVisionAutoConfiguration.class))
			.build();

	return ImageAnnotatorClient.create(clientSettings);
}