use of com.google.api.services.vision.v1.Vision in project java-docs-samples by GoogleCloudPlatform.
the class FaceDetectApp method main.
// [START main]
/**
* Annotates an image using the Vision API.
*/
public static void main(String[] args) throws IOException, GeneralSecurityException {
if (args.length != 2) {
System.err.println("Usage:");
System.err.printf("\tjava %s inputImagePath outputImagePath\n", FaceDetectApp.class.getCanonicalName());
System.exit(1);
}
Path inputPath = Paths.get(args[0]);
Path outputPath = Paths.get(args[1]);
if (!outputPath.toString().toLowerCase().endsWith(".jpg")) {
System.err.println("outputImagePath must have the file extension 'jpg'.");
System.exit(1);
}
FaceDetectApp app = new FaceDetectApp(getVisionService());
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);
}
use of com.google.api.services.vision.v1.Vision in project java-docs-samples by GoogleCloudPlatform.
the class DetectLandmark method identifyLandmark.
/**
* Gets up to {@code maxResults} landmarks for an image stored at {@code 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 Feature().setType("LANDMARK_DETECTION").setMaxResults(maxResults)));
Vision.Images.Annotate annotate = vision.images().annotate(new BatchAnnotateImagesRequest().setRequests(ImmutableList.of(request)));
// Due to a bug: requests to Vision API containing large images fail when GZipped.
annotate.setDisableGZipContent(true);
BatchAnnotateImagesResponse batchResponse = annotate.execute();
assert batchResponse.getResponses().size() == 1;
AnnotateImageResponse response = batchResponse.getResponses().get(0);
if (response.getLandmarkAnnotations() == null) {
throw new IOException(response.getError() != null ? response.getError().getMessage() : "Unknown error getting image annotations");
}
return response.getLandmarkAnnotations();
}
use of com.google.api.services.vision.v1.Vision in project java-docs-samples by GoogleCloudPlatform.
the class TextApp method detectText.
/**
* Gets up to {@code maxResults} text annotations for images stored at {@code paths}.
*/
public ImmutableList<ImageText> detectText(List<Path> paths) {
ImmutableList.Builder<AnnotateImageRequest> requests = ImmutableList.builder();
try {
for (Path path : paths) {
byte[] data;
data = Files.readAllBytes(path);
requests.add(new AnnotateImageRequest().setImage(new Image().encodeContent(data)).setFeatures(ImmutableList.of(new Feature().setType("TEXT_DETECTION").setMaxResults(MAX_RESULTS))));
}
Vision.Images.Annotate annotate = vision.images().annotate(new BatchAnnotateImagesRequest().setRequests(requests.build()));
// Due to a bug: requests to Vision API containing large images fail when GZipped.
annotate.setDisableGZipContent(true);
BatchAnnotateImagesResponse batchResponse = annotate.execute();
assert batchResponse.getResponses().size() == paths.size();
ImmutableList.Builder<ImageText> output = ImmutableList.builder();
for (int i = 0; i < paths.size(); i++) {
Path path = paths.get(i);
AnnotateImageResponse response = batchResponse.getResponses().get(i);
output.add(ImageText.builder().path(path).textAnnotations(MoreObjects.firstNonNull(response.getTextAnnotations(), ImmutableList.<EntityAnnotation>of())).error(response.getError()).build());
}
return output.build();
} catch (IOException ex) {
// Got an exception, which means the whole batch had an error.
ImmutableList.Builder<ImageText> output = ImmutableList.builder();
for (Path path : paths) {
output.add(ImageText.builder().path(path).textAnnotations(ImmutableList.<EntityAnnotation>of()).error(new Status().setMessage(ex.getMessage())).build());
}
return output.build();
}
}
use of com.google.api.services.vision.v1.Vision in project java-docs-samples by GoogleCloudPlatform.
the class TextAppTest method setUp.
@Before
public void setUp() throws Exception {
// Mock out the vision service for unit tests.
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
HttpTransport transport = new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
response.setStatusCode(200);
response.setContentType(Json.MEDIA_TYPE);
response.setContent("{\"responses\": [{\"textAnnotations\": []}]}");
return response;
}
};
}
};
Vision vision = new Vision(transport, jsonFactory, null);
appUnderTest = new TextApp(vision, null);
}
use of com.google.api.services.vision.v1.Vision in project java-docs-samples by GoogleCloudPlatform.
the class FaceDetectApp method detectFaces.
// [START detect_face]
/**
* Gets up to {@code maxResults} faces for an image stored at {@code path}.
*/
public List<FaceAnnotation> detectFaces(Path path, int maxResults) throws IOException {
byte[] data = Files.readAllBytes(path);
AnnotateImageRequest request = new AnnotateImageRequest().setImage(new Image().encodeContent(data)).setFeatures(ImmutableList.of(new Feature().setType("FACE_DETECTION").setMaxResults(maxResults)));
Vision.Images.Annotate annotate = vision.images().annotate(new BatchAnnotateImagesRequest().setRequests(ImmutableList.of(request)));
// Due to a bug: requests to Vision API containing large images fail when GZipped.
annotate.setDisableGZipContent(true);
BatchAnnotateImagesResponse batchResponse = annotate.execute();
assert batchResponse.getResponses().size() == 1;
AnnotateImageResponse response = batchResponse.getResponses().get(0);
if (response.getFaceAnnotations() == null) {
throw new IOException(response.getError() != null ? response.getError().getMessage() : "Unknown error getting image annotations");
}
return response.getFaceAnnotations();
}
Aggregations