Skip to content

Commit

Permalink
label recognition v1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
amykyttc committed Apr 11, 2016
1 parent a846fc6 commit d90cfaa
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.fidel.recognizer.entity.UploadItem;
import com.fidel.recognizer.service.ImageRecognitionService;
import com.fidel.recognizer.service.LabelRecognitionService;
import com.google.api.services.vision.v1.model.EntityAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
Expand All @@ -17,15 +19,20 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/recognizeFaces")
//@RequestMapping(value = "/recognizeFaces")
public class RecognizeFileController {

@Autowired
ImageRecognitionService imageRecognitionService;

@RequestMapping(method = RequestMethod.GET)
@Autowired
LabelRecognitionService labelRecognitionService;

@RequestMapping(value = "/recognizeFaces", method = RequestMethod.GET)
public String recognizeFaces(UploadItem uploadItem, HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors,
HttpSession session) {
Expand All @@ -35,7 +42,34 @@ public String recognizeFaces(UploadItem uploadItem, HttpServletRequest request,
Path path = Paths.get(rootPath + File.separator + "webapps" + File.separator + "ROOT" + File.separator + imagePath);

try{
imageRecognitionService.recognize(path, path);
imageRecognitionService.recognizeFaces(path, path);
}catch(IOException | GeneralSecurityException e){
e.printStackTrace();
}

return "redirect:/uploadFileIndex";
}

@RequestMapping(value = "/recognizeLabels", method = RequestMethod.GET)
public String recognizeLabels(UploadItem uploadItem, HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors,
HttpSession session, Model model) {

String imagePath = (String) session.getAttribute("uploadFile");
String rootPath = System.getProperty("catalina.home");
Path path = Paths.get(rootPath + File.separator + "webapps" + File.separator + "ROOT" + File.separator + imagePath);

int i = 0;

try{
List<EntityAnnotation> labels = labelRecognitionService.recognizeLabels(path);
for (EntityAnnotation label : labels) {
if(label != null) {
session.setAttribute("label"+i, label.getDescription() + ", " + label.getScore());
i++;
}
}

}catch(IOException | GeneralSecurityException e){
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
import java.nio.file.Path;
import java.util.List;

public class VisionInstance {
public class VisionImageInstance {

private final Vision vision;

/**
* Constructs a {@link VisionInstance} which connects to the Vision API.
* Constructs a {@link VisionImageInstance} which connects to the Vision API.
*/
public VisionInstance (Vision vision) {
public VisionImageInstance(Vision vision) {
this.vision = vision;
}

Expand Down
57 changes: 57 additions & 0 deletions src/main/java/com/fidel/recognizer/entity/VisionLabelInstance.java
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]
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.fidel.recognizer.service;

import com.fidel.recognizer.entity.VisionInstance;
import com.fidel.recognizer.entity.VisionImageInstance;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.json.JsonFactory;
Expand All @@ -21,24 +21,24 @@ public class ImageRecognitionService {
* 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 = "visionproject-1277";
private static final String APPLICATION_NAME = "VisionImage/1.0";

//private static final int MAX_RESULTS = 4;
private static final int MAX_RESULTS = 20;


public void recognize(Path outputPath, Path inputPath)
public void recognizeFaces(Path outputPath, Path inputPath)
throws IOException, GeneralSecurityException {

VisionInstance app = new VisionInstance(getVisionService());
List<FaceAnnotation> faces = app.detectFaces(inputPath, MAX_RESULTS);
VisionImageInstance visionImageInstance = new VisionImageInstance(getVisionService());
List<FaceAnnotation> faces = visionImageInstance.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);
visionImageInstance.writeWithFaces(inputPath, outputPath, faces);

}

public static Vision getVisionService() throws IOException, GeneralSecurityException {
public Vision getVisionService() throws IOException, GeneralSecurityException {
GoogleCredential credential =
GoogleCredential.getApplicationDefault().createScoped(VisionScopes.all());
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
Expand Down
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.");
}
}
}
2 changes: 2 additions & 0 deletions src/main/webapp/WEB-INF/spring/root-context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

<bean id="imageRecognitionManager" class="com.fidel.recognizer.service.ImageRecognitionService">
</bean>
<bean id="labelRecognitionManager" class="com.fidel.recognizer.service.LabelRecognitionService">
</bean>

<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
Expand Down
31 changes: 31 additions & 0 deletions src/main/webapp/WEB-INF/views/uploadfileindex.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,36 @@
}
%>
<h2><a href="recognizeFaces">Recognize Faces</a></h2>
<h2><a href="recognizeLabels">Recognize Labels</a></h2>
<%
if (session.getAttribute("label0") != null) {
%>
<div>
<%=session.getAttribute("label0")%>
</div>
<%
session.removeAttribute("label0");
}
%>
<%
if (session.getAttribute("label1") != null) {
%>
<div>
<%=session.getAttribute("label1")%>
</div>
<%
session.removeAttribute("label1");
}
%>
<%
if (session.getAttribute("label2") != null) {
%>
<div>
<%=session.getAttribute("label2")%>
</div>
<%
session.removeAttribute("label2");
}
%>
</body>
</html>

0 comments on commit d90cfaa

Please sign in to comment.