Skip to content

Commit

Permalink
Reverted Vision API classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtisvg committed Nov 13, 2017
1 parent 8d53243 commit bbc6669
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.vision.v1.Vision;
import com.google.api.services.vision.v1.VisionScopes;
import com.google.api.services.vision.v1.model.GoogleCloudVisionV1AnnotateImageRequest;
import com.google.api.services.vision.v1.model.GoogleCloudVisionV1AnnotateImageResponse;
import com.google.api.services.vision.v1.model.GoogleCloudVisionV1BatchAnnotateImagesRequest;
import com.google.api.services.vision.v1.model.GoogleCloudVisionV1BatchAnnotateImagesResponse;
import com.google.api.services.vision.v1.model.GoogleCloudVisionV1FaceAnnotation;
import com.google.api.services.vision.v1.model.GoogleCloudVisionV1Feature;
import com.google.api.services.vision.v1.model.GoogleCloudVisionV1Image;
import com.google.api.services.vision.v1.model.GoogleCloudVisionV1Vertex;
import com.google.api.services.vision.v1.model.AnnotateImageRequest;
import com.google.api.services.vision.v1.model.AnnotateImageResponse;
import com.google.api.services.vision.v1.model.BatchAnnotateImagesRequest;
import com.google.api.services.vision.v1.model.BatchAnnotateImagesResponse;
import com.google.api.services.vision.v1.model.FaceAnnotation;
import com.google.api.services.vision.v1.model.Feature;
import com.google.api.services.vision.v1.model.Image;
import com.google.api.services.vision.v1.model.Vertex;
import com.google.common.collect.ImmutableList;

import java.awt.BasicStroke;
Expand Down Expand Up @@ -81,7 +81,7 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
}

FaceDetectApp app = new FaceDetectApp(getVisionService());
List<GoogleCloudVisionV1FaceAnnotation> faces = app.detectFaces(inputPath, MAX_RESULTS);
List<FaceAnnotation> faces = app.detectFaces(inputPath, MAX_RESULTS);
System.out.printf("Found %d face%s\n", faces.size(), faces.size() == 1 ? "" : "s");
System.out.printf("Writing to file %s\n", outputPath);
app.writeWithFaces(inputPath, outputPath, faces);
Expand Down Expand Up @@ -115,25 +115,25 @@ public FaceDetectApp(Vision vision) {
/**
* Gets up to {@code maxResults} faces for an image stored at {@code path}.
*/
public List<GoogleCloudVisionV1FaceAnnotation> detectFaces(Path path, int maxResults) throws IOException {
public List<FaceAnnotation> detectFaces(Path path, int maxResults) throws IOException {
byte[] data = Files.readAllBytes(path);

GoogleCloudVisionV1AnnotateImageRequest request =
new GoogleCloudVisionV1AnnotateImageRequest()
.setImage(new GoogleCloudVisionV1Image().encodeContent(data))
AnnotateImageRequest request =
new AnnotateImageRequest()
.setImage(new Image().encodeContent(data))
.setFeatures(ImmutableList.of(
new GoogleCloudVisionV1Feature()
new Feature()
.setType("FACE_DETECTION")
.setMaxResults(maxResults)));
Vision.Images.Annotate annotate =
vision.images()
.annotate(new GoogleCloudVisionV1BatchAnnotateImagesRequest().setRequests(ImmutableList.of(request)));
.annotate(new BatchAnnotateImagesRequest().setRequests(ImmutableList.of(request)));
// Due to a bug: requests to Vision API containing large images fail when GZipped.
annotate.setDisableGZipContent(true);

GoogleCloudVisionV1BatchAnnotateImagesResponse batchResponse = annotate.execute();
BatchAnnotateImagesResponse batchResponse = annotate.execute();
assert batchResponse.getResponses().size() == 1;
GoogleCloudVisionV1AnnotateImageResponse response = batchResponse.getResponses().get(0);
AnnotateImageResponse response = batchResponse.getResponses().get(0);
if (response.getFaceAnnotations() == null) {
throw new IOException(
response.getError() != null
Expand All @@ -148,7 +148,7 @@ public List<GoogleCloudVisionV1FaceAnnotation> detectFaces(Path path, int maxRes
/**
* Reads image {@code inputPath} and writes {@code outputPath} with {@code faces} outlined.
*/
private static void writeWithFaces(Path inputPath, Path outputPath, List<GoogleCloudVisionV1FaceAnnotation> faces)
private static void writeWithFaces(Path inputPath, Path outputPath, List<FaceAnnotation> faces)
throws IOException {
BufferedImage img = ImageIO.read(inputPath.toFile());
annotateWithFaces(img, faces);
Expand All @@ -158,19 +158,19 @@ private static void writeWithFaces(Path inputPath, Path outputPath, List<GoogleC
/**
* Annotates an image {@code img} with a polygon around each face in {@code faces}.
*/
public static void annotateWithFaces(BufferedImage img, List<GoogleCloudVisionV1FaceAnnotation> faces) {
for (GoogleCloudVisionV1FaceAnnotation face : faces) {
public static void annotateWithFaces(BufferedImage img, List<FaceAnnotation> faces) {
for (FaceAnnotation face : faces) {
annotateWithFace(img, face);
}
}

/**
* Annotates an image {@code img} with a polygon defined by {@code face}.
*/
private static void annotateWithFace(BufferedImage img, GoogleCloudVisionV1FaceAnnotation face) {
private static void annotateWithFace(BufferedImage img, FaceAnnotation face) {
Graphics2D gfx = img.createGraphics();
Polygon poly = new Polygon();
for (GoogleCloudVisionV1Vertex vertex : face.getFdBoundingPoly().getVertices()) {
for (Vertex vertex : face.getFdBoundingPoly().getVertices()) {
poly.addPoint(vertex.getX(), vertex.getY());
}
gfx.setStroke(new BasicStroke(5));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import com.google.api.services.vision.v1.model.GoogleCloudVisionV1FaceAnnotation;
import com.google.api.services.vision.v1.model.FaceAnnotation;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -45,7 +45,7 @@ public class FaceDetectAppIT {
}

@Test public void detectFaces_withFace_returnsAtLeastOneFace() throws Exception {
List<GoogleCloudVisionV1FaceAnnotation> faces =
List<FaceAnnotation> faces =
appUnderTest.detectFaces(Paths.get("data/face.jpg"), MAX_RESULTS);

assertThat(faces).named("face.jpg faces").isNotEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

import static com.google.common.truth.Truth.assertThat;

import com.google.api.services.vision.v1.model.GoogleCloudVisionV1BoundingPoly;
import com.google.api.services.vision.v1.model.GoogleCloudVisionV1FaceAnnotation;
import com.google.api.services.vision.v1.model.GoogleCloudVisionV1Vertex;
import com.google.api.services.vision.v1.model.BoundingPoly;
import com.google.api.services.vision.v1.model.FaceAnnotation;
import com.google.api.services.vision.v1.model.Vertex;
import com.google.common.collect.ImmutableList;

import org.junit.Test;
Expand All @@ -36,21 +36,21 @@
public class FaceDetectAppTest {
@Test public void annotateWithFaces_manyFaces_outlinesFaces() throws Exception {
// Arrange
ImmutableList<GoogleCloudVisionV1FaceAnnotation> faces =
ImmutableList<FaceAnnotation> faces =
ImmutableList.of(
new GoogleCloudVisionV1FaceAnnotation()
new FaceAnnotation()
.setFdBoundingPoly(
new GoogleCloudVisionV1BoundingPoly().setVertices(ImmutableList.of(
new GoogleCloudVisionV1Vertex().setX(10).setY(5),
new GoogleCloudVisionV1Vertex().setX(20).setY(5),
new GoogleCloudVisionV1Vertex().setX(20).setY(25),
new GoogleCloudVisionV1Vertex().setX(10).setY(25)))),
new GoogleCloudVisionV1FaceAnnotation()
new BoundingPoly().setVertices(ImmutableList.of(
new Vertex().setX(10).setY(5),
new Vertex().setX(20).setY(5),
new Vertex().setX(20).setY(25),
new Vertex().setX(10).setY(25)))),
new FaceAnnotation()
.setFdBoundingPoly(
new GoogleCloudVisionV1BoundingPoly().setVertices(ImmutableList.of(
new GoogleCloudVisionV1Vertex().setX(60).setY(50),
new GoogleCloudVisionV1Vertex().setX(70).setY(60),
new GoogleCloudVisionV1Vertex().setX(50).setY(60)))));
new BoundingPoly().setVertices(ImmutableList.of(
new Vertex().setX(60).setY(50),
new Vertex().setX(70).setY(60),
new Vertex().setX(50).setY(60)))));
BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.vision.v1.Vision;
import com.google.api.services.vision.v1.VisionScopes;
import com.google.api.services.vision.v1.model.GoogleCloudVisionV1AnnotateImageRequest;
import com.google.api.services.vision.v1.model.GoogleCloudVisionV1AnnotateImageResponse;
import com.google.api.services.vision.v1.model.GoogleCloudVisionV1BatchAnnotateImagesRequest;
import com.google.api.services.vision.v1.model.GoogleCloudVisionV1BatchAnnotateImagesResponse;
import com.google.api.services.vision.v1.model.GoogleCloudVisionV1EntityAnnotation;
import com.google.api.services.vision.v1.model.GoogleCloudVisionV1Feature;
import com.google.api.services.vision.v1.model.GoogleCloudVisionV1Image;
import com.google.api.services.vision.v1.model.GoogleCloudVisionV1ImageSource;
import com.google.api.services.vision.v1.model.AnnotateImageRequest;
import com.google.api.services.vision.v1.model.AnnotateImageResponse;
import com.google.api.services.vision.v1.model.BatchAnnotateImagesRequest;
import com.google.api.services.vision.v1.model.BatchAnnotateImagesResponse;
import com.google.api.services.vision.v1.model.EntityAnnotation;
import com.google.api.services.vision.v1.model.Feature;
import com.google.api.services.vision.v1.model.Image;
import com.google.api.services.vision.v1.model.ImageSource;
import com.google.common.collect.ImmutableList;

import java.io.IOException;
Expand Down Expand Up @@ -68,9 +68,9 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
}

DetectLandmark app = new DetectLandmark(getVisionService());
List<GoogleCloudVisionV1EntityAnnotation> landmarks = app.identifyLandmark(args[0], MAX_RESULTS);
List<EntityAnnotation> landmarks = app.identifyLandmark(args[0], MAX_RESULTS);
System.out.printf("Found %d landmark%s\n", landmarks.size(), landmarks.size() == 1 ? "" : "s");
for (GoogleCloudVisionV1EntityAnnotation annotation : landmarks) {
for (EntityAnnotation annotation : landmarks) {
System.out.printf("\t%s\n", annotation.getDescription());
}
}
Expand Down Expand Up @@ -103,24 +103,24 @@ public DetectLandmark(Vision vision) {
/**
* Gets up to {@code maxResults} landmarks for an image stored at {@code uri}.
*/
public List<GoogleCloudVisionV1EntityAnnotation> identifyLandmark(String uri, int maxResults) throws IOException {
GoogleCloudVisionV1AnnotateImageRequest request =
new GoogleCloudVisionV1AnnotateImageRequest()
.setImage(new GoogleCloudVisionV1Image().setSource(
new GoogleCloudVisionV1ImageSource().setGcsImageUri(uri)))
public List<EntityAnnotation> identifyLandmark(String uri, int maxResults) throws IOException {
AnnotateImageRequest request =
new AnnotateImageRequest()
.setImage(new Image().setSource(
new ImageSource().setGcsImageUri(uri)))
.setFeatures(ImmutableList.of(
new GoogleCloudVisionV1Feature()
new Feature()
.setType("LANDMARK_DETECTION")
.setMaxResults(maxResults)));
Vision.Images.Annotate annotate =
vision.images()
.annotate(new GoogleCloudVisionV1BatchAnnotateImagesRequest().setRequests(ImmutableList.of(request)));
.annotate(new BatchAnnotateImagesRequest().setRequests(ImmutableList.of(request)));
// Due to a bug: requests to Vision API containing large images fail when GZipped.
annotate.setDisableGZipContent(true);

GoogleCloudVisionV1BatchAnnotateImagesResponse batchResponse = annotate.execute();
BatchAnnotateImagesResponse batchResponse = annotate.execute();
assert batchResponse.getResponses().size() == 1;
GoogleCloudVisionV1AnnotateImageResponse response = batchResponse.getResponses().get(0);
AnnotateImageResponse response = batchResponse.getResponses().get(0);
if (response.getLandmarkAnnotations() == null) {
throw new IOException(
response.getError() != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import com.google.api.services.vision.v1.model.GoogleCloudVisionV1EntityAnnotation;
import com.google.api.services.vision.v1.model.EntityAnnotation;

import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -49,7 +49,7 @@ public class DetectLandmarkIT {
}

@Test public void identifyLandmark_withLandmark_returnsKnownLandmark() throws Exception {
List<GoogleCloudVisionV1EntityAnnotation> landmarks = appUnderTest.identifyLandmark(LANDMARK_URI, MAX_RESULTS);
List<EntityAnnotation> landmarks = appUnderTest.identifyLandmark(LANDMARK_URI, MAX_RESULTS);

assertThat(landmarks).named("water.jpg landmarks").isNotEmpty();
assertThat(landmarks.get(0).getDescription())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.google.cloud.vision.samples.text;

import com.google.api.services.vision.v1.model.GoogleCloudVisionV1EntityAnnotation;
import com.google.api.services.vision.v1.model.GoogleRpcStatus;
import com.google.api.services.vision.v1.model.EntityAnnotation;
import com.google.api.services.vision.v1.model.Status;

import java.nio.file.Path;
import java.util.List;
Expand All @@ -29,8 +29,8 @@
*/
public class ImageText {
private Path pth;
private List<GoogleCloudVisionV1EntityAnnotation> ts;
private GoogleRpcStatus err;
private List<EntityAnnotation> ts;
private Status err;

public static Builder builder() {
return new Builder();
Expand All @@ -42,19 +42,19 @@ public Path path() {
return this.pth;
}

public List<GoogleCloudVisionV1EntityAnnotation> textAnnotations() {
public List<EntityAnnotation> textAnnotations() {
return this.ts;
}

@Nullable
public GoogleRpcStatus error() {
public Status error() {
return this.err;
}

public static class Builder {
private Path pth;
private List<GoogleCloudVisionV1EntityAnnotation> ts;
private GoogleRpcStatus err;
private List<EntityAnnotation> ts;
private Status err;

Builder() {}

Expand All @@ -63,12 +63,12 @@ public Builder path(Path path) {
return this;
}

public Builder textAnnotations(List<GoogleCloudVisionV1EntityAnnotation> ts) {
public Builder textAnnotations(List<EntityAnnotation> ts) {
this.ts = ts;
return this;
}

public Builder error(@Nullable GoogleRpcStatus err) {
public Builder error(@Nullable Status err) {
this.err = err;
return this;
}
Expand Down
Loading

0 comments on commit bbc6669

Please sign in to comment.