CS6825: Computer Vision word cloud

How to call from a Java Application--using the Java libraries for Google Cloud Vision API

***** note currently no direct Android support --- so will have to pass image to a backend Java program running on a server/cloud*****

 

Resources:


// Imports the Google Cloud client library

import com.google.cloud.vision.v1.AnnotateImageRequest;
import com.google.cloud.vision.v1.AnnotateImageResponse;
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
import com.google.cloud.vision.v1.EntityAnnotation;
import com.google.cloud.vision.v1.Feature;
import com.google.cloud.vision.v1.Feature.Type;
import com.google.cloud.vision.v1.Image;
import com.google.cloud.vision.v1.ImageAnnotatorClient;
import com.google.protobuf.ByteString;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class QuickstartSample {
 
public static void main(String... args) throws Exception {
   
// Instantiates a client
   
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.printf("Error: %s\n", res.getError().getMessage());
         
return;
       
}

       
for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
          annotation
.getAllFields().forEach((k, v)->
             
System.out.printf("%s : %s\n", k, v.toString()));
       
}
     
}
   
}
 
}
}

 

 

STEP 1: Download Client Library
STEP 2: create project and code
STEP 3: add library as dependency to your project
NOTE: this code assume image is in a file can load --you would have to modify this to be a servlet to receive the image data in the request and then the reset would be similar.

compile 'com.google.cloud:google-cloud-vision:0.25.0-beta'

 


EXAMPLE JAVA CODE (from github from google)-

  • import cloud libraries 

  • istantiate the vision client

  • load the image stored in a file

  • set up the request to run label detection

  • cycle through the results

// Imports the Google Cloud client library
import com.google.cloud.vision.v1.AnnotateImageRequest;
import com.google.cloud.vision.v1.AnnotateImageResponse;
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
import com.google.cloud.vision.v1.EntityAnnotation;
import com.google.cloud.vision.v1.Feature;
import com.google.cloud.vision.v1.Feature.Type;
import com.google.cloud.vision.v1.Image;
import com.google.cloud.vision.v1.ImageAnnotatorClient;
import com.google.protobuf.ByteString;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class QuickstartSample {
  public static void main(String... args) throws Exception {
        // Instantiates a client
        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
 	          BatchAnnotateImagesResponese 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()){
  	             annotation.getAllFields().forEach((k, v)->
  	              System.out.printf("%s : %s\n", k, v.toString()));
 	              }
	           }

}//end try }//end main }

 

© Lynne Grewe