Search in sources :

Example 1 with Builder

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

the class AsyncBatchAnnotateImagesGcs method asyncBatchAnnotateImagesGcs.

// Performs asynchronous batch annotation of images on Google Cloud Storage
public static void asyncBatchAnnotateImagesGcs(String gcsSourcePath, String gcsDestinationPath) throws Exception {
    // String gcsDestinationPath = "gs://YOUR_BUCKET_ID/path_to_store_annotation";
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        List<AnnotateImageRequest> requests = new ArrayList<>();
        ImageSource imgSource = ImageSource.newBuilder().setImageUri(gcsSourcePath).build();
        Image image = Image.newBuilder().setSource(imgSource).build();
        // Set the GCS destination path for where to save the results.
        GcsDestination gcsDestination = GcsDestination.newBuilder().setUri(gcsDestinationPath).build();
        // Create the configuration for the output with the batch size.
        // The batch size sets how many pages should be grouped into each json output file.
        OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).setBatchSize(2).build();
        // Select the Features required by the vision API
        Feature features = Feature.newBuilder().setType(Type.LABEL_DETECTION).setType(Type.TEXT_DETECTION).setType(Type.IMAGE_PROPERTIES).build();
        // Build the request
        AnnotateImageRequest annotateImageRequest = AnnotateImageRequest.newBuilder().setImage(image).addFeatures(features).build();
        requests.add(annotateImageRequest);
        AsyncBatchAnnotateImagesRequest request = AsyncBatchAnnotateImagesRequest.newBuilder().addAllRequests(requests).setOutputConfig(outputConfig).build();
        OperationFuture<AsyncBatchAnnotateImagesResponse, OperationMetadata> response = client.asyncBatchAnnotateImagesAsync(request);
        System.out.println("Waiting for the operation to finish.");
        // we're not processing the response, since we'll be reading the output from GCS.
        response.get(180, TimeUnit.SECONDS);
        // Once the request has completed and the output has been
        // written to GCS, we can list all the output files.
        Storage storage = StorageOptions.getDefaultInstance().getService();
        // Get the destination location from the gcsDestinationPath
        Pattern pattern = Pattern.compile("gs://([^/]+)/(.+)");
        Matcher matcher = pattern.matcher(gcsDestinationPath);
        if (matcher.find()) {
            String bucketName = matcher.group(1);
            String prefix = matcher.group(2);
            // Get the list of objects with the given prefix from the GCS bucket
            Bucket bucket = storage.get(bucketName);
            Page<Blob> pageList = bucket.list(BlobListOption.prefix(prefix));
            Blob firstOutputFile = null;
            // List objects with the given prefix.
            System.out.println("Output files:");
            for (Blob blob : pageList.iterateAll()) {
                System.out.println(blob.getName());
                // the first two image requests
                if (firstOutputFile == null) {
                    firstOutputFile = blob;
                }
            }
            // Get the contents of the file and convert the JSON contents to an
            // BatchAnnotateImagesResponse
            // object. If the Blob is small read all its content in one request
            // (Note: the file is a .json file)
            // Storage guide: https://cloud.google.com/storage/docs/downloading-objects
            String jsonContents = new String(firstOutputFile.getContent());
            Builder builder = BatchAnnotateImagesResponse.newBuilder();
            JsonFormat.parser().merge(jsonContents, builder);
            // Build the AnnotateFileResponse object
            BatchAnnotateImagesResponse batchAnnotateImagesResponse = builder.build();
            // Here we print the response for the first image
            // The response contains more information:
            // annotation/pages/blocks/paragraphs/words/symbols/colors
            // including confidence score and bounding boxes
            System.out.format("\nResponse: %s\n", batchAnnotateImagesResponse.getResponses(0));
        } else {
            System.out.println("No MATCH");
        }
    } catch (Exception e) {
        System.out.println("Error during asyncBatchAnnotateImagesGcs: \n" + e.toString());
    }
}
Also used : Pattern(java.util.regex.Pattern) Blob(com.google.cloud.storage.Blob) AsyncBatchAnnotateImagesRequest(com.google.cloud.vision.v1p4beta1.AsyncBatchAnnotateImagesRequest) Matcher(java.util.regex.Matcher) ImageAnnotatorClient(com.google.cloud.vision.v1p4beta1.ImageAnnotatorClient) Builder(com.google.cloud.vision.v1p4beta1.BatchAnnotateImagesResponse.Builder) ArrayList(java.util.ArrayList) Image(com.google.cloud.vision.v1p4beta1.Image) Feature(com.google.cloud.vision.v1p4beta1.Feature) AnnotateImageRequest(com.google.cloud.vision.v1p4beta1.AnnotateImageRequest) OutputConfig(com.google.cloud.vision.v1p4beta1.OutputConfig) Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket) AsyncBatchAnnotateImagesResponse(com.google.cloud.vision.v1p4beta1.AsyncBatchAnnotateImagesResponse) ImageSource(com.google.cloud.vision.v1p4beta1.ImageSource) GcsDestination(com.google.cloud.vision.v1p4beta1.GcsDestination) OperationMetadata(com.google.cloud.vision.v1p4beta1.OperationMetadata) AsyncBatchAnnotateImagesResponse(com.google.cloud.vision.v1p4beta1.AsyncBatchAnnotateImagesResponse) BatchAnnotateImagesResponse(com.google.cloud.vision.v1p4beta1.BatchAnnotateImagesResponse)

Aggregations

Blob (com.google.cloud.storage.Blob)1 Bucket (com.google.cloud.storage.Bucket)1 Storage (com.google.cloud.storage.Storage)1 AnnotateImageRequest (com.google.cloud.vision.v1p4beta1.AnnotateImageRequest)1 AsyncBatchAnnotateImagesRequest (com.google.cloud.vision.v1p4beta1.AsyncBatchAnnotateImagesRequest)1 AsyncBatchAnnotateImagesResponse (com.google.cloud.vision.v1p4beta1.AsyncBatchAnnotateImagesResponse)1 BatchAnnotateImagesResponse (com.google.cloud.vision.v1p4beta1.BatchAnnotateImagesResponse)1 Builder (com.google.cloud.vision.v1p4beta1.BatchAnnotateImagesResponse.Builder)1 Feature (com.google.cloud.vision.v1p4beta1.Feature)1 GcsDestination (com.google.cloud.vision.v1p4beta1.GcsDestination)1 Image (com.google.cloud.vision.v1p4beta1.Image)1 ImageAnnotatorClient (com.google.cloud.vision.v1p4beta1.ImageAnnotatorClient)1 ImageSource (com.google.cloud.vision.v1p4beta1.ImageSource)1 OperationMetadata (com.google.cloud.vision.v1p4beta1.OperationMetadata)1 OutputConfig (com.google.cloud.vision.v1p4beta1.OutputConfig)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1