Search in sources :

Example 56 with BatchAnnotateImagesResponse

use of com.google.cloud.vision.v1p4beta1.BatchAnnotateImagesResponse in project spring-cloud-gcp by spring-cloud.

the class CloudVisionTemplate method analyzeImage.

/**
 * 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.");
    }
}
Also used : Arrays(java.util.Arrays) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) Type(com.google.cloud.vision.v1.Feature.Type) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Feature(com.google.cloud.vision.v1.Feature) ByteString(com.google.protobuf.ByteString) List(java.util.List) Image(com.google.cloud.vision.v1.Image) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ImageContext(com.google.cloud.vision.v1.ImageContext) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) BatchAnnotateImagesRequest(com.google.cloud.vision.v1.BatchAnnotateImagesRequest) Code(com.google.rpc.Code) Resource(org.springframework.core.io.Resource) Assert(org.springframework.util.Assert) BatchAnnotateImagesRequest(com.google.cloud.vision.v1.BatchAnnotateImagesRequest) ByteString(com.google.protobuf.ByteString) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) IOException(java.io.IOException) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 57 with BatchAnnotateImagesResponse

use of com.google.cloud.vision.v1p4beta1.BatchAnnotateImagesResponse in project java-docs-samples by GoogleCloudPlatform.

the class ImageMagick method blurOffensiveImages.

// [END run_imageproc_handler_setup]
// [END cloudrun_imageproc_handler_setup]
// [START cloudrun_imageproc_handler_analyze]
// [START run_imageproc_handler_analyze]
// Blurs uploaded images that are flagged as Adult or Violence.
public static void blurOffensiveImages(JsonObject data) {
    String fileName = data.get("name").getAsString();
    String bucketName = data.get("bucket").getAsString();
    BlobInfo blobInfo = BlobInfo.newBuilder(bucketName, fileName).build();
    // Construct URI to GCS bucket and file.
    String gcsPath = String.format("gs://%s/%s", bucketName, fileName);
    System.out.println(String.format("Analyzing %s", fileName));
    // Construct request.
    List<AnnotateImageRequest> requests = new ArrayList<>();
    ImageSource imgSource = ImageSource.newBuilder().setImageUri(gcsPath).build();
    Image img = Image.newBuilder().setSource(imgSource).build();
    Feature feature = Feature.newBuilder().setType(Type.SAFE_SEARCH_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feature).setImage(img).build();
    requests.add(request);
    // Send request to the Vision API.
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                System.out.println(String.format("Error: %s\n", res.getError().getMessage()));
                return;
            }
            // Get Safe Search Annotations
            SafeSearchAnnotation annotation = res.getSafeSearchAnnotation();
            if (annotation.getAdultValue() == 5 || annotation.getViolenceValue() == 5) {
                System.out.println(String.format("Detected %s as inappropriate.", fileName));
                blur(blobInfo);
            } else {
                System.out.println(String.format("Detected %s as OK.", fileName));
            }
        }
    } catch (Exception e) {
        System.out.println(String.format("Error with Vision API: %s", e.getMessage()));
    }
}
Also used : SafeSearchAnnotation(com.google.cloud.vision.v1.SafeSearchAnnotation) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ArrayList(java.util.ArrayList) BlobInfo(com.google.cloud.storage.BlobInfo) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) IOException(java.io.IOException) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ImageSource(com.google.cloud.vision.v1.ImageSource) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 58 with BatchAnnotateImagesResponse

use of com.google.cloud.vision.v1p4beta1.BatchAnnotateImagesResponse in project java-vision by googleapis.

the class ITSystemTest method detectWebEntitiesIncludeGeoResultsTest.

@Test
public void detectWebEntitiesIncludeGeoResultsTest() throws IOException {
    ByteString imgBytes = ByteString.readFrom(new FileInputStream(RESOURCES + "city.jpg"));
    Image img = Image.newBuilder().setContent(imgBytes).build();
    Feature feat = Feature.newBuilder().setType(Type.WEB_DETECTION).setMaxResults(MAX_RESULTS).build();
    WebDetectionParams webDetectionParams = WebDetectionParams.newBuilder().setIncludeGeoResults(true).build();
    ImageContext imageContext = ImageContext.newBuilder().setWebDetectionParams(webDetectionParams).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImageContext(imageContext).setImage(img).build();
    BatchAnnotateImagesResponse response = imageAnnotatorClient.batchAnnotateImages(ImmutableList.of(request));
    List<AnnotateImageResponse> responses = response.getResponsesList();
    List<String> actual = new ArrayList<>();
    for (AnnotateImageResponse imgResponse : responses) {
        for (WebDetection.WebEntity entity : imgResponse.getWebDetection().getWebEntitiesList()) {
            actual.add(entity.getDescription());
        }
    }
    List<String> expectedResults = new ArrayList<>();
    expectedResults.add("Skyscraper");
    expectedResults.add("Suburb");
    assertThat(actual).containsAnyIn(expectedResults);
}
Also used : WebDetectionParams(com.google.cloud.vision.v1.WebDetectionParams) WebDetection(com.google.cloud.vision.v1.WebDetection) ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) ReferenceImage(com.google.cloud.vision.v1.ReferenceImage) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) FileInputStream(java.io.FileInputStream) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ImageContext(com.google.cloud.vision.v1.ImageContext) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) Test(org.junit.Test)

Example 59 with BatchAnnotateImagesResponse

use of com.google.cloud.vision.v1p4beta1.BatchAnnotateImagesResponse in project java-vision by googleapis.

the class ITSystemTest method detectWebEntitiesIncludeGeoResultsGcsTest.

@Test
public void detectWebEntitiesIncludeGeoResultsGcsTest() {
    ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(SAMPLE_BUCKET + "landmark/pofa.jpg").build();
    Image img = Image.newBuilder().setSource(imgSource).build();
    Feature feat = Feature.newBuilder().setType(Type.WEB_DETECTION).setMaxResults(MAX_RESULTS).build();
    WebDetectionParams webDetectionParams = WebDetectionParams.newBuilder().setIncludeGeoResults(true).build();
    ImageContext imageContext = ImageContext.newBuilder().setWebDetectionParams(webDetectionParams).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImageContext(imageContext).setImage(img).build();
    BatchAnnotateImagesResponse response = imageAnnotatorClient.batchAnnotateImages(ImmutableList.of(request));
    List<AnnotateImageResponse> responses = response.getResponsesList();
    List<String> actual = new ArrayList<>();
    System.out.println("WebEntitiesGeo SIZE");
    System.out.println(actual.size());
    for (AnnotateImageResponse imgResponse : responses) {
        for (WebDetection.WebEntity entity : imgResponse.getWebDetection().getWebEntitiesList()) {
            actual.add(entity.getDescription());
        }
    }
    assertThat(actual).contains("The Palace Of Fine Arts");
}
Also used : WebDetectionParams(com.google.cloud.vision.v1.WebDetectionParams) WebDetection(com.google.cloud.vision.v1.WebDetection) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) ReferenceImage(com.google.cloud.vision.v1.ReferenceImage) 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) ImageSource(com.google.cloud.vision.v1.ImageSource) ImageContext(com.google.cloud.vision.v1.ImageContext) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) Test(org.junit.Test)

Example 60 with BatchAnnotateImagesResponse

use of com.google.cloud.vision.v1p4beta1.BatchAnnotateImagesResponse in project java-vision by googleapis.

the class Detect method detectDocumentText.

/**
 * Performs document text detection on a local image file.
 *
 * @param filePath The path to the local file to detect document text on.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
// [START vision_fulltext_detection]
public static void detectDocumentText(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(Type.DOCUMENT_TEXT_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    // 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();
        client.close();
        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
            TextAnnotation annotation = res.getFullTextAnnotation();
            for (Page page : annotation.getPagesList()) {
                String pageText = "";
                for (Block block : page.getBlocksList()) {
                    String blockText = "";
                    for (Paragraph para : block.getParagraphsList()) {
                        String paraText = "";
                        for (Word word : para.getWordsList()) {
                            String wordText = "";
                            for (Symbol symbol : word.getSymbolsList()) {
                                wordText = wordText + symbol.getText();
                                System.out.format("Symbol text: %s (confidence: %f)%n", symbol.getText(), symbol.getConfidence());
                            }
                            System.out.format("Word text: %s (confidence: %f)%n%n", wordText, word.getConfidence());
                            paraText = String.format("%s %s", paraText, wordText);
                        }
                        // Output Example using Paragraph:
                        System.out.println("%nParagraph: %n" + paraText);
                        System.out.format("Paragraph Confidence: %f%n", para.getConfidence());
                        blockText = blockText + paraText;
                    }
                    pageText = pageText + blockText;
                }
            }
            System.out.println("%nComplete annotation:");
            System.out.println(annotation.getText());
        }
    }
}
Also used : Word(com.google.cloud.vision.v1.Word) ByteString(com.google.protobuf.ByteString) Symbol(com.google.cloud.vision.v1.Symbol) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ArrayList(java.util.ArrayList) Page(com.google.cloud.vision.v1.Page) ByteString(com.google.protobuf.ByteString) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) FileInputStream(java.io.FileInputStream) Paragraph(com.google.cloud.vision.v1.Paragraph) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) Block(com.google.cloud.vision.v1.Block) TextAnnotation(com.google.cloud.vision.v1.TextAnnotation) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Aggregations

BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)72 AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)69 Image (com.google.cloud.vision.v1.Image)68 Feature (com.google.cloud.vision.v1.Feature)66 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)64 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)63 ArrayList (java.util.ArrayList)63 ByteString (com.google.protobuf.ByteString)44 ImageSource (com.google.cloud.vision.v1.ImageSource)37 FileInputStream (java.io.FileInputStream)32 EntityAnnotation (com.google.cloud.vision.v1.EntityAnnotation)28 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)26 IOException (java.io.IOException)13 ImageContext (com.google.cloud.vision.v1.ImageContext)12 WebDetection (com.google.cloud.vision.v1.WebDetection)11 LocationInfo (com.google.cloud.vision.v1.LocationInfo)10 SafeSearchAnnotation (com.google.cloud.vision.v1.SafeSearchAnnotation)10 WebPage (com.google.cloud.vision.v1.WebDetection.WebPage)9 Block (com.google.cloud.vision.v1.Block)8 ColorInfo (com.google.cloud.vision.v1.ColorInfo)8