-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
amykyttc
committed
Apr 11, 2016
1 parent
a846fc6
commit d90cfaa
Showing
7 changed files
with
208 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/com/fidel/recognizer/entity/VisionLabelInstance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.fidel.recognizer.entity; | ||
|
||
import com.google.api.services.vision.v1.Vision; | ||
import com.google.api.services.vision.v1.model.*; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
public class VisionLabelInstance { | ||
private final Vision vision; | ||
|
||
/** | ||
* Constructs a {@link VisionLabelInstance} which connects to the Vision API. | ||
*/ | ||
public VisionLabelInstance(Vision vision) { | ||
this.vision = vision; | ||
} | ||
|
||
/** | ||
* Gets up to {@code maxResults} labels for an image stored at {@code path}. | ||
*/ | ||
public List<EntityAnnotation> labelImage(Path path, int maxResults) throws IOException { | ||
// [START construct_request] | ||
byte[] data = Files.readAllBytes(path); | ||
|
||
AnnotateImageRequest request = | ||
new AnnotateImageRequest() | ||
.setImage(new Image().encodeContent(data)) | ||
.setFeatures(ImmutableList.of( | ||
new Feature() | ||
.setType("LABEL_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); | ||
// [END construct_request] | ||
|
||
// [START parse_response] | ||
BatchAnnotateImagesResponse batchResponse = annotate.execute(); | ||
assert batchResponse.getResponses().size() == 1; | ||
AnnotateImageResponse response = batchResponse.getResponses().get(0); | ||
if (response.getLabelAnnotations() == null) { | ||
throw new IOException( | ||
response.getError() != null | ||
? response.getError().getMessage() | ||
: "Unknown error getting image annotations"); | ||
} | ||
return response.getLabelAnnotations(); | ||
// [END parse_response] | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/java/com/fidel/recognizer/service/LabelRecognitionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.fidel.recognizer.service; | ||
|
||
import com.fidel.recognizer.entity.VisionLabelInstance; | ||
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; | ||
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; | ||
import com.google.api.client.json.JsonFactory; | ||
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.EntityAnnotation; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.security.GeneralSecurityException; | ||
import java.util.List; | ||
|
||
@Service | ||
public class LabelRecognitionService { | ||
/** | ||
* Be sure to specify the name of your application. If the application name is {@code null} or | ||
* blank, the application will log a warning. Suggested format is "MyCompany-ProductName/1.0". | ||
*/ | ||
private static final String APPLICATION_NAME = "VisionLabel/1.0"; | ||
|
||
public static final int MAX_LABELS = 3; | ||
|
||
/** | ||
* Annotates an image using the Vision API. | ||
*/ | ||
public List<EntityAnnotation> recognizeLabels(Path imagePath) throws IOException, GeneralSecurityException { | ||
|
||
|
||
VisionLabelInstance visionLabelInstance = new VisionLabelInstance(getVisionService()); | ||
//printLabels(System.out, imagePath, visionLabelInstance.labelImage(imagePath, MAX_LABELS)); | ||
return visionLabelInstance.labelImage(imagePath, MAX_LABELS); | ||
} | ||
|
||
|
||
// [START authenticate] | ||
/** | ||
* Connects to the Vision API using Application Default Credentials. | ||
*/ | ||
public Vision getVisionService() throws IOException, GeneralSecurityException { | ||
GoogleCredential credential = | ||
GoogleCredential.getApplicationDefault().createScoped(VisionScopes.all()); | ||
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); | ||
return new Vision.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, credential) | ||
.setApplicationName(APPLICATION_NAME) | ||
.build(); | ||
} | ||
// [END authenticate] | ||
|
||
/** | ||
* Prints the labels received from the Vision API. | ||
*/ | ||
public static void printLabels(PrintStream out, Path imagePath, List<EntityAnnotation> labels) { | ||
out.printf("Labels for image %s:\n", imagePath); | ||
for (EntityAnnotation label : labels) { | ||
out.printf( | ||
"\t%s (score: %.3f)\n", | ||
label.getDescription(), | ||
label.getScore()); | ||
} | ||
if (labels.isEmpty()) { | ||
out.println("\tNo labels found."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters