Java Code Examples for com.google.cloud.vision.v1.AnnotateImageResponse#hasError()
The following examples show how to use
com.google.cloud.vision.v1.AnnotateImageResponse#hasError() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageContentAnalysis.java From TweetwallFX with MIT License | 6 votes |
public ImageContentAnalysis(final AnnotateImageResponse air) { if (air.hasError()) { setAnalysisError(new AnalysisError(air.getError())); } setSafeSearch(SafeSearch.of(air.getSafeSearchAnnotation())); setTexts(air.getTextAnnotationsList().stream() .map(TextEntry::new) .collect(Collectors.toList())); // air.getTextAnnotationsList(); // TODO: add it // air.getCropHintsAnnotation(); // TODO: add it // air.getFaceAnnotationsList(); // TODO: add it // air.getFullTextAnnotation(); // TODO: add it // air.getImagePropertiesAnnotation(); // TODO: add it // air.getLabelAnnotationsList(); // TODO: add it // air.getLandmarkAnnotationsList(); // TODO: add it // air.getLocalizedObjectAnnotationsList(); // TODO: add it // air.getLogoAnnotationsList(); // TODO: add it // air.getWebDetection(); // TODO: add it }
Example 2
Source File: DetectCropHintsGcs.java From java-docs-samples with Apache License 2.0 | 5 votes |
public static void detectCropHintsGcs(String gcsPath) throws 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(Feature.Type.CROP_HINTS).build(); AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // 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(); 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 CropHintsAnnotation annotation = res.getCropHintsAnnotation(); for (CropHint hint : annotation.getCropHintsList()) { System.out.println(hint.getBoundingPoly()); } } } }
Example 3
Source File: DetectFacesGcs.java From java-docs-samples with Apache License 2.0 | 5 votes |
public static void detectFacesGcs(String gcsPath) throws 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(Feature.Type.FACE_DETECTION).build(); AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // 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(); 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 for (FaceAnnotation annotation : res.getFaceAnnotationsList()) { System.out.format( "anger: %s%njoy: %s%nsurprise: %s%nposition: %s", annotation.getAngerLikelihood(), annotation.getJoyLikelihood(), annotation.getSurpriseLikelihood(), annotation.getBoundingPoly()); } } } }
Example 4
Source File: ImageMagick.java From java-docs-samples with Apache License 2.0 | 5 votes |
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())); } }
Example 5
Source File: DetectLabelsGcs.java From java-docs-samples with Apache License 2.0 | 5 votes |
public static void detectLabelsGcs(String gcsPath) throws 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(Feature.Type.LABEL_DETECTION).build(); AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // 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(); 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 for (EntityAnnotation annotation : res.getLabelAnnotationsList()) { annotation .getAllFields() .forEach((k, v) -> System.out.format("%s : %s%n", k, v.toString())); } } } }
Example 6
Source File: DetectSafeSearchGcs.java From java-docs-samples with Apache License 2.0 | 5 votes |
public static void detectSafeSearchGcs(String gcsPath) throws 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.SAFE_SEARCH_DETECTION).build(); AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // 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(); 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 SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); System.out.format( "adult: %s%nmedical: %s%nspoofed: %s%nviolence: %s%nracy: %s%n", annotation.getAdult(), annotation.getMedical(), annotation.getSpoof(), annotation.getViolence(), annotation.getRacy()); } } }
Example 7
Source File: AnnotateImage.java From google-cloud-java with Apache License 2.0 | 5 votes |
public static void main(String... args) throws Exception { // Instantiates a client ImageAnnotatorClient vision = ImageAnnotatorClient.create(); // The path to the image file to annotate String fileName = "your/image/path.jpg"; // for example "./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()) { for (Map.Entry<FieldDescriptor, Object> entry : annotation.getAllFields().entrySet()) { System.out.printf("%s : %s\n", entry.getKey(), entry.getValue()); } } } }
Example 8
Source File: DetectPropertiesGcs.java From java-docs-samples with Apache License 2.0 | 5 votes |
public static void detectPropertiesGcs(String gcsPath) throws 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(Feature.Type.IMAGE_PROPERTIES).build(); AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // 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(); 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 DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors(); for (ColorInfo color : colors.getColorsList()) { System.out.format( "fraction: %f%nr: %f, g: %f, b: %f%n", color.getPixelFraction(), color.getColor().getRed(), color.getColor().getGreen(), color.getColor().getBlue()); } } } }
Example 9
Source File: DetectSafeSearch.java From java-docs-samples with Apache License 2.0 | 5 votes |
public static void detectSafeSearch(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(Feature.Type.SAFE_SEARCH_DETECTION).build(); AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // 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(); 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 SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); System.out.format( "adult: %s%nmedical: %s%nspoofed: %s%nviolence: %s%nracy: %s%n", annotation.getAdult(), annotation.getMedical(), annotation.getSpoof(), annotation.getViolence(), annotation.getRacy()); } } }
Example 10
Source File: DetectLandmarksGcs.java From java-docs-samples with Apache License 2.0 | 5 votes |
public static void detectLandmarksGcs(String gcsPath) throws 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(Feature.Type.LANDMARK_DETECTION).build(); AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // 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(); 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 for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) { LocationInfo info = annotation.getLocationsList().listIterator().next(); System.out.format("Landmark: %s%n %s%n", annotation.getDescription(), info.getLatLng()); } } } }
Example 11
Source File: DetectLabels.java From java-docs-samples with Apache License 2.0 | 5 votes |
public static void detectLabels(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(Feature.Type.LABEL_DETECTION).build(); AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // 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(); 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 for (EntityAnnotation annotation : res.getLabelAnnotationsList()) { annotation .getAllFields() .forEach((k, v) -> System.out.format("%s : %s%n", k, v.toString())); } } } }
Example 12
Source File: DetectTextGcs.java From java-docs-samples with Apache License 2.0 | 5 votes |
public static void detectTextGcs(String gcsPath) throws 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(Feature.Type.TEXT_DETECTION).build(); AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // 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(); 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 for (EntityAnnotation annotation : res.getTextAnnotationsList()) { System.out.format("Text: %s%n", annotation.getDescription()); System.out.format("Position : %s%n", annotation.getBoundingPoly()); } } } }
Example 13
Source File: DetectFaces.java From java-docs-samples with Apache License 2.0 | 5 votes |
public static void detectFaces(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(Feature.Type.FACE_DETECTION).build(); AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // 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(); 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 for (FaceAnnotation annotation : res.getFaceAnnotationsList()) { System.out.format( "anger: %s%njoy: %s%nsurprise: %s%nposition: %s", annotation.getAngerLikelihood(), annotation.getJoyLikelihood(), annotation.getSurpriseLikelihood(), annotation.getBoundingPoly()); } } } }
Example 14
Source File: DetectLogos.java From java-docs-samples with Apache License 2.0 | 5 votes |
public static void detectLogos(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(Feature.Type.LOGO_DETECTION).build(); AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // 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(); 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 for (EntityAnnotation annotation : res.getLogoAnnotationsList()) { System.out.println(annotation.getDescription()); } } } }
Example 15
Source File: DetectText.java From java-docs-samples with Apache License 2.0 | 5 votes |
public static void detectText(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(Feature.Type.TEXT_DETECTION).build(); AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // 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(); 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 for (EntityAnnotation annotation : res.getTextAnnotationsList()) { System.out.format("Text: %s%n", annotation.getDescription()); System.out.format("Position : %s%n", annotation.getBoundingPoly()); } } } }
Example 16
Source File: DetectCropHints.java From java-docs-samples with Apache License 2.0 | 5 votes |
public static void detectCropHints(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(Feature.Type.CROP_HINTS).build(); AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // 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(); 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 CropHintsAnnotation annotation = res.getCropHintsAnnotation(); for (CropHint hint : annotation.getCropHintsList()) { System.out.println(hint.getBoundingPoly()); } } } }
Example 17
Source File: DetectLandmarks.java From java-docs-samples with Apache License 2.0 | 5 votes |
public static void detectLandmarks(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(Feature.Type.LANDMARK_DETECTION).build(); AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // 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(); 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 for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) { LocationInfo info = annotation.getLocationsList().listIterator().next(); System.out.format("Landmark: %s%n %s%n", annotation.getDescription(), info.getLatLng()); } } } }
Example 18
Source File: ImageMagick.java From java-docs-samples with Apache License 2.0 | 4 votes |
@Override // Blurs uploaded images that are flagged as Adult or Violence. public void accept(GcsEvent gcsEvent, Context context) { // Validate parameters if (gcsEvent.getBucket() == null || gcsEvent.getName() == null) { logger.severe("Error: Malformed GCS event."); return; } BlobInfo blobInfo = BlobInfo.newBuilder(gcsEvent.getBucket(), gcsEvent.getName()).build(); // Construct URI to GCS bucket and file. String gcsPath = String.format("gs://%s/%s", gcsEvent.getBucket(), gcsEvent.getName()); logger.info(String.format("Analyzing %s", gcsEvent.getName())); // Construct request. 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(); List<AnnotateImageRequest> requests = List.of(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()) { logger.info(String.format("Error: %s", res.getError().getMessage())); return; } // Get Safe Search Annotations 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); } }
Example 19
Source File: Detect.java From java-docs-samples with Apache License 2.0 | 4 votes |
/** * Performs document text detection on a remote image on Google Cloud Storage. * * @param gcsPath The path to the remote file on Google Cloud Storage to detect document text on. * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ // [START vision_fulltext_detection_gcs] public static void detectDocumentTextGcs(String gcsPath) throws 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.DOCUMENT_TEXT_DETECTION).build(); AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // 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()); } } }
Example 20
Source File: Detect.java From java-docs-samples with Apache License 2.0 | 4 votes |
/** * 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); // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // 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()); } } }