Search in sources :

Example 1 with SafeSearchAnnotation

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

the class Detect method detectSafeSearchGcs.

// [END vision_detect_safe_search]
// [START vision_detect_safe_search_uri]
/**
 * Detects whether the specified image on Google Cloud Storage has features you would want
 * to moderate.
 *
 * @param gcsPath The path to the remote file on Google Cloud Storage to detect safe-search on.
 * @param out A {@link PrintStream} to write the results to.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectSafeSearchGcs(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.SAFE_SEARCH_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
            SafeSearchAnnotation annotation = res.getSafeSearchAnnotation();
            out.printf("adult: %s\nmedical: %s\nspoofed: %s\nviolence: %s\nracy: %s\n", annotation.getAdult(), annotation.getMedical(), annotation.getSpoof(), annotation.getViolence(), annotation.getRacy());
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) SafeSearchAnnotation(com.google.cloud.vision.v1.SafeSearchAnnotation) 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) Feature(com.google.cloud.vision.v1.Feature) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 2 with SafeSearchAnnotation

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

the class Detect method detectSafeSearch.

// [START vision_detect_safe_search]
/**
 * Detects whether the specified image has features you would want to moderate.
 *
 * @param filePath The path to the local file used for safe search detection.
 * @param out A {@link PrintStream} to write the results to.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectSafeSearch(String filePath, PrintStream out) throws Exception, 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.SAFE_SEARCH_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
            SafeSearchAnnotation annotation = res.getSafeSearchAnnotation();
            out.printf("adult: %s\nmedical: %s\nspoofed: %s\nviolence: %s\nracy: %s\n", annotation.getAdult(), annotation.getMedical(), annotation.getSpoof(), annotation.getViolence(), annotation.getRacy());
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) SafeSearchAnnotation(com.google.cloud.vision.v1.SafeSearchAnnotation) ByteString(com.google.protobuf.ByteString) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) FileInputStream(java.io.FileInputStream) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Aggregations

AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)2 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)2 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)2 Feature (com.google.cloud.vision.v1.Feature)2 Image (com.google.cloud.vision.v1.Image)2 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)2 SafeSearchAnnotation (com.google.cloud.vision.v1.SafeSearchAnnotation)2 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)2 ArrayList (java.util.ArrayList)2 ImageSource (com.google.cloud.vision.v1.ImageSource)1 ByteString (com.google.protobuf.ByteString)1 FileInputStream (java.io.FileInputStream)1