Search in sources :

Example 21 with EntityAnnotation

use of com.google.cloud.vision.v1.EntityAnnotation in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectLandmarksUrl.

/**
 * Detects landmarks in the specified URI.
 *
 * @param uri The path to the file to perform landmark detection on.
 * @param out A {@link PrintStream} to write detected landmarks to.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectLandmarksUrl(String uri, PrintStream out) throws Exception, 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(Type.LANDMARK_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                out.printf("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();
                out.printf("Landmark: %s\n %s\n", annotation.getDescription(), info.getLatLng());
            }
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) ImageSource(com.google.cloud.vision.v1.ImageSource) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) Feature(com.google.cloud.vision.v1.Feature) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) LocationInfo(com.google.cloud.vision.v1.LocationInfo)

Example 22 with EntityAnnotation

use of com.google.cloud.vision.v1.EntityAnnotation in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectLandmarksGcs.

/**
 * Detects landmarks in the specified remote image on Google Cloud Storage.
 *
 * @param gcsPath The path to the remote file on Google Cloud Storage to perform landmark
 *                detection on.
 * @param out A {@link PrintStream} to write detected landmarks to.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectLandmarksGcs(String gcsPath, PrintStream out) throws Exception, 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.LANDMARK_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                out.printf("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();
                out.printf("Landmark: %s\n %s\n", annotation.getDescription(), info.getLatLng());
            }
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) ImageSource(com.google.cloud.vision.v1.ImageSource) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) Feature(com.google.cloud.vision.v1.Feature) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) LocationInfo(com.google.cloud.vision.v1.LocationInfo)

Example 23 with EntityAnnotation

use of com.google.cloud.vision.v1.EntityAnnotation in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectLogosGcs.

/**
 * Detects logos in the specified remote image on Google Cloud Storage.
 *
 * @param gcsPath The path to the remote file on Google Cloud Storage to perform logo detection
 *                on.
 * @param out A {@link PrintStream} to write detected logos to.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectLogosGcs(String gcsPath, PrintStream out) throws Exception, 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.LOGO_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                out.printf("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()) {
                out.println(annotation.getDescription());
            }
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) ImageSource(com.google.cloud.vision.v1.ImageSource) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) Feature(com.google.cloud.vision.v1.Feature) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 24 with EntityAnnotation

use of com.google.cloud.vision.v1.EntityAnnotation in project java-docs-samples by GoogleCloudPlatform.

the class QuickstartSample method main.

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()));
            }
        }
    }
}
Also used : Path(java.nio.file.Path) ByteString(com.google.protobuf.ByteString) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 25 with EntityAnnotation

use of com.google.cloud.vision.v1.EntityAnnotation in project spring-cloud-gcp by spring-cloud.

the class VisionController method extractLabels.

/**
 * 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);
}
Also used : AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ModelAndView(org.springframework.web.servlet.ModelAndView) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

EntityAnnotation (com.google.cloud.vision.v1.EntityAnnotation)36 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)34 ArrayList (java.util.ArrayList)30 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)25 AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)24 Feature (com.google.cloud.vision.v1.Feature)24 Image (com.google.cloud.vision.v1.Image)24 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)23 ByteString (com.google.protobuf.ByteString)19 ImageSource (com.google.cloud.vision.v1.ImageSource)11 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)9 FileInputStream (java.io.FileInputStream)8 Test (org.junit.Test)7 LocationInfo (com.google.cloud.vision.v1.LocationInfo)6 ModelAndView (org.springframework.web.servlet.ModelAndView)5 Path (java.nio.file.Path)4 GetMapping (org.springframework.web.bind.annotation.GetMapping)4 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3