|
| 1 | +/* |
| 2 | + * Copyright 2017 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.vision.snippets; |
| 18 | + |
| 19 | +// [START vision_face_detection_gcs] |
| 20 | + |
| 21 | +import com.google.cloud.vision.v1.AnnotateImageRequest; |
| 22 | +import com.google.cloud.vision.v1.AnnotateImageResponse; |
| 23 | +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; |
| 24 | +import com.google.cloud.vision.v1.FaceAnnotation; |
| 25 | +import com.google.cloud.vision.v1.Feature; |
| 26 | +import com.google.cloud.vision.v1.Image; |
| 27 | +import com.google.cloud.vision.v1.ImageAnnotatorClient; |
| 28 | +import com.google.cloud.vision.v1.ImageSource; |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +public class DetectFacesGcs { |
| 34 | + |
| 35 | + public static void detectFacesGcs() throws IOException { |
| 36 | + // TODO(developer): Replace these variables before running the sample. |
| 37 | + String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg"; |
| 38 | + detectFacesGcs(filePath); |
| 39 | + } |
| 40 | + |
| 41 | + // Detects faces in the specified remote image on Google Cloud Storage. |
| 42 | + public static void detectFacesGcs(String gcsPath) throws IOException { |
| 43 | + List<AnnotateImageRequest> requests = new ArrayList<>(); |
| 44 | + |
| 45 | + ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); |
| 46 | + Image img = Image.newBuilder().setSource(imgSource).build(); |
| 47 | + Feature feat = Feature.newBuilder().setType(Feature.Type.FACE_DETECTION).build(); |
| 48 | + |
| 49 | + AnnotateImageRequest request = |
| 50 | + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); |
| 51 | + requests.add(request); |
| 52 | + |
| 53 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 54 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 55 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 56 | + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { |
| 57 | + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); |
| 58 | + List<AnnotateImageResponse> responses = response.getResponsesList(); |
| 59 | + |
| 60 | + for (AnnotateImageResponse res : responses) { |
| 61 | + if (res.hasError()) { |
| 62 | + System.out.format("Error: %s%n", res.getError().getMessage()); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + // For full list of available annotations, see http://g.co/cloud/vision/docs |
| 67 | + for (FaceAnnotation annotation : res.getFaceAnnotationsList()) { |
| 68 | + System.out.format( |
| 69 | + "anger: %s%njoy: %s%nsurprise: %s%nposition: %s", |
| 70 | + annotation.getAngerLikelihood(), |
| 71 | + annotation.getJoyLikelihood(), |
| 72 | + annotation.getSurpriseLikelihood(), |
| 73 | + annotation.getBoundingPoly()); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | +// [END vision_face_detection_gcs] |
0 commit comments