|
package checkpic |
|
|
|
import java.io.IOException; |
|
import java.net.URL; |
|
import java.net.URLDecoder; |
|
import java.util.List; |
|
import java.util.logging.Logger; |
|
|
|
import javax.servlet.annotation.WebServlet; |
|
import javax.servlet.http.HttpServlet; |
|
import javax.servlet.http.HttpServletRequest; |
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import com.google.api.client.util.Lists; |
|
import com.google.cloud.vision.v1.AnnotateImageRequest; |
|
import com.google.cloud.vision.v1.AnnotateImageResponse; |
|
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; |
|
import com.google.cloud.vision.v1.Feature; |
|
import com.google.cloud.vision.v1.Feature.Type; |
|
import com.google.cloud.vision.v1.Image; |
|
import com.google.cloud.vision.v1.ImageAnnotatorClient; |
|
import com.google.cloud.vision.v1.Likelihood; |
|
import com.google.cloud.vision.v1.SafeSearchAnnotation; |
|
import com.google.common.io.ByteStreams; |
|
import com.google.protobuf.ByteString; |
|
|
|
/** |
|
* Servlet with POST containing URL to an image. The image is downloaded and sent to |
|
* Google Vision API and checks SAFE SEARCH (it's easy to select another check). |
|
* Note: This does not contain local authorization to Vision API. It only runs |
|
* live using the app's service account. |
|
*/ |
|
@SuppressWarnings("serial") |
|
@WebServlet(name = "Check Pic", value = "/check-pic/") |
|
public class CheckPicServlet extends HttpServlet { |
|
|
|
private static Logger log = Logger.getLogger(CheckPicServlet.class.getName()); |
|
|
|
@Override |
|
public void doPost(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException { |
|
String content = httpRequest.getReader().readLine(); |
|
String imageUrl = URLDecoder.decode(content, "UTF-8"); |
|
log.info("Checking:" + imageUrl); |
|
|
|
URL url = new URL(imageUrl); |
|
byte[] array = ByteStreams.toByteArray(url.openStream()); |
|
|
|
ImageAnnotatorClient vision = ImageAnnotatorClient.create(); |
|
List<AnnotateImageRequest> requests = Lists.newArrayList(); |
|
|
|
ByteString imgBytes = ByteString.copyFrom(array); |
|
Image img = Image.newBuilder().setContent(imgBytes).build(); |
|
|
|
// Use SAFE SEARCH |
|
Feature feat = Feature.newBuilder().setType(Type.SAFE_SEARCH_DETECTION).build(); |
|
AnnotateImageRequest visionRequest = AnnotateImageRequest.newBuilder() |
|
.addFeatures(feat) |
|
.setImage(img) |
|
.build(); |
|
requests.add(visionRequest); |
|
|
|
// Performs label detection on the image file |
|
BatchAnnotateImagesResponse visionResponse = vision.batchAnnotateImages(requests); |
|
List<AnnotateImageResponse> annotationResponses = visionResponse.getResponsesList(); |
|
|
|
for (AnnotateImageResponse annotationResponse : annotationResponses) { |
|
if (annotationResponse.hasError()) { |
|
httpResponse.setStatus(400); |
|
} |
|
|
|
// For full list of available annotations, see http://g.co/cloud/vision/docs |
|
SafeSearchAnnotation annotation = annotationResponse.getSafeSearchAnnotation(); |
|
|
|
} |
|
} |
|
} |