|
23 | 23 | import com.google.api.client.json.jackson2.JacksonFactory; |
24 | 24 | import com.google.api.services.vision.v1.Vision; |
25 | 25 | import com.google.api.services.vision.v1.VisionScopes; |
26 | | -import com.google.api.services.vision.v1.model.AnnotateImageRequest; |
27 | | -import com.google.api.services.vision.v1.model.AnnotateImageResponse; |
28 | | -import com.google.api.services.vision.v1.model.BatchAnnotateImagesRequest; |
29 | | -import com.google.api.services.vision.v1.model.BatchAnnotateImagesResponse; |
30 | | -import com.google.api.services.vision.v1.model.FaceAnnotation; |
31 | | -import com.google.api.services.vision.v1.model.Feature; |
32 | | -import com.google.api.services.vision.v1.model.Image; |
33 | | -import com.google.api.services.vision.v1.model.Vertex; |
| 26 | +import com.google.api.services.vision.v1.model.GoogleCloudVisionV1AnnotateImageRequest; |
| 27 | +import com.google.api.services.vision.v1.model.GoogleCloudVisionV1AnnotateImageResponse; |
| 28 | +import com.google.api.services.vision.v1.model.GoogleCloudVisionV1BatchAnnotateImagesRequest; |
| 29 | +import com.google.api.services.vision.v1.model.GoogleCloudVisionV1BatchAnnotateImagesResponse; |
| 30 | +import com.google.api.services.vision.v1.model.GoogleCloudVisionV1FaceAnnotation; |
| 31 | +import com.google.api.services.vision.v1.model.GoogleCloudVisionV1Feature; |
| 32 | +import com.google.api.services.vision.v1.model.GoogleCloudVisionV1Image; |
| 33 | +import com.google.api.services.vision.v1.model.GoogleCloudVisionV1Vertex; |
34 | 34 | import com.google.common.collect.ImmutableList; |
35 | 35 |
|
36 | 36 | import java.awt.BasicStroke; |
@@ -81,7 +81,7 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept |
81 | 81 | } |
82 | 82 |
|
83 | 83 | FaceDetectApp app = new FaceDetectApp(getVisionService()); |
84 | | - List<FaceAnnotation> faces = app.detectFaces(inputPath, MAX_RESULTS); |
| 84 | + List<GoogleCloudVisionV1FaceAnnotation> faces = app.detectFaces(inputPath, MAX_RESULTS); |
85 | 85 | System.out.printf("Found %d face%s\n", faces.size(), faces.size() == 1 ? "" : "s"); |
86 | 86 | System.out.printf("Writing to file %s\n", outputPath); |
87 | 87 | app.writeWithFaces(inputPath, outputPath, faces); |
@@ -115,25 +115,25 @@ public FaceDetectApp(Vision vision) { |
115 | 115 | /** |
116 | 116 | * Gets up to {@code maxResults} faces for an image stored at {@code path}. |
117 | 117 | */ |
118 | | - public List<FaceAnnotation> detectFaces(Path path, int maxResults) throws IOException { |
| 118 | + public List<GoogleCloudVisionV1FaceAnnotation> detectFaces(Path path, int maxResults) throws IOException { |
119 | 119 | byte[] data = Files.readAllBytes(path); |
120 | 120 |
|
121 | | - AnnotateImageRequest request = |
122 | | - new AnnotateImageRequest() |
123 | | - .setImage(new Image().encodeContent(data)) |
| 121 | + GoogleCloudVisionV1AnnotateImageRequest request = |
| 122 | + new GoogleCloudVisionV1AnnotateImageRequest() |
| 123 | + .setImage(new GoogleCloudVisionV1Image().encodeContent(data)) |
124 | 124 | .setFeatures(ImmutableList.of( |
125 | | - new Feature() |
| 125 | + new GoogleCloudVisionV1Feature() |
126 | 126 | .setType("FACE_DETECTION") |
127 | 127 | .setMaxResults(maxResults))); |
128 | 128 | Vision.Images.Annotate annotate = |
129 | 129 | vision.images() |
130 | | - .annotate(new BatchAnnotateImagesRequest().setRequests(ImmutableList.of(request))); |
| 130 | + .annotate(new GoogleCloudVisionV1BatchAnnotateImagesRequest().setRequests(ImmutableList.of(request))); |
131 | 131 | // Due to a bug: requests to Vision API containing large images fail when GZipped. |
132 | 132 | annotate.setDisableGZipContent(true); |
133 | 133 |
|
134 | | - BatchAnnotateImagesResponse batchResponse = annotate.execute(); |
| 134 | + GoogleCloudVisionV1BatchAnnotateImagesResponse batchResponse = annotate.execute(); |
135 | 135 | assert batchResponse.getResponses().size() == 1; |
136 | | - AnnotateImageResponse response = batchResponse.getResponses().get(0); |
| 136 | + GoogleCloudVisionV1AnnotateImageResponse response = batchResponse.getResponses().get(0); |
137 | 137 | if (response.getFaceAnnotations() == null) { |
138 | 138 | throw new IOException( |
139 | 139 | response.getError() != null |
@@ -148,7 +148,7 @@ public List<FaceAnnotation> detectFaces(Path path, int maxResults) throws IOExce |
148 | 148 | /** |
149 | 149 | * Reads image {@code inputPath} and writes {@code outputPath} with {@code faces} outlined. |
150 | 150 | */ |
151 | | - private static void writeWithFaces(Path inputPath, Path outputPath, List<FaceAnnotation> faces) |
| 151 | + private static void writeWithFaces(Path inputPath, Path outputPath, List<GoogleCloudVisionV1FaceAnnotation> faces) |
152 | 152 | throws IOException { |
153 | 153 | BufferedImage img = ImageIO.read(inputPath.toFile()); |
154 | 154 | annotateWithFaces(img, faces); |
@@ -158,19 +158,19 @@ private static void writeWithFaces(Path inputPath, Path outputPath, List<FaceAnn |
158 | 158 | /** |
159 | 159 | * Annotates an image {@code img} with a polygon around each face in {@code faces}. |
160 | 160 | */ |
161 | | - public static void annotateWithFaces(BufferedImage img, List<FaceAnnotation> faces) { |
162 | | - for (FaceAnnotation face : faces) { |
| 161 | + public static void annotateWithFaces(BufferedImage img, List<GoogleCloudVisionV1FaceAnnotation> faces) { |
| 162 | + for (GoogleCloudVisionV1FaceAnnotation face : faces) { |
163 | 163 | annotateWithFace(img, face); |
164 | 164 | } |
165 | 165 | } |
166 | 166 |
|
167 | 167 | /** |
168 | 168 | * Annotates an image {@code img} with a polygon defined by {@code face}. |
169 | 169 | */ |
170 | | - private static void annotateWithFace(BufferedImage img, FaceAnnotation face) { |
| 170 | + private static void annotateWithFace(BufferedImage img, GoogleCloudVisionV1FaceAnnotation face) { |
171 | 171 | Graphics2D gfx = img.createGraphics(); |
172 | 172 | Polygon poly = new Polygon(); |
173 | | - for (Vertex vertex : face.getFdBoundingPoly().getVertices()) { |
| 173 | + for (GoogleCloudVisionV1Vertex vertex : face.getFdBoundingPoly().getVertices()) { |
174 | 174 | poly.addPoint(vertex.getX(), vertex.getY()); |
175 | 175 | } |
176 | 176 | gfx.setStroke(new BasicStroke(5)); |
|