Source:
public void accept(GcsEvent gcsEvent, Context context) { if (gcsEvent.getBucket() == null || gcsEvent.getName() == null) { LOGGER.severe("Error: Malformed GCS event."); return; } BlobInfo blobInfo = BlobInfo.newBuilder(gcsEvent.getBucket(), gcsEvent.getName()).build(); String gcsPath = String.format("gs://%s/%s", gcsEvent.getBucket(), gcsEvent.getName()); LOGGER.info(String.format("Analyzing %s", gcsEvent.getName())); 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); try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); List<AnnotateImageResponse> responses = response.getResponsesList(); for (AnnotateImageResponse res : responses) { if (res.hasError()) { LOGGER.info(String.format("Error: %s%n", res.getError().getMessage())); return; } SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); if (annotation.getAdultValue() == 5 || annotation.getViolenceValue() == 5) { LOGGER.info(String.format("Detected %s as inappropriate.", gcsEvent.getName())); blur(blobInfo); } else { LOGGER.info(String.format("Detected %s as OK.", gcsEvent.getName())); } } } catch (IOException e) { LOGGER.info(String.format("Error with Vision API: %s", e.getMessage())); } }

Target:
public void accept(GcsEvent gcsEvent, Context context) { if (gcsEvent.getBucket() == null || gcsEvent.getName() == null) { LOGGER.severe("Error: Malformed GCS event."); return; } BlobInfo blobInfo = BlobInfo.newBuilder(gcsEvent.getBucket(), gcsEvent.getName()).build(); String gcsPath = String.format("gs://%s/%s", gcsEvent.getBucket(), gcsEvent.getName()); LOGGER.info(String.format("Analyzing %s", gcsEvent.getName())); 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); try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); List<AnnotateImageResponse> responses = response.getResponsesList(); for (AnnotateImageResponse res : responses) { if (res.hasError()) { LOGGER.info(String.format("Error: %s", res.getError().getMessage())); return; } SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); if (annotation.getAdultValue() == 5 || annotation.getViolenceValue() == 5) { LOGGER.info(String.format("Detected %s as inappropriate.", gcsEvent.getName())); blur(blobInfo); } else { LOGGER.info(String.format("Detected %s as OK.", gcsEvent.getName())); } } } catch (IOException e) { LOGGER.log(Level.SEVERE, "Error with Vision API: " + e.getMessage(), e); } }