Last active
March 29, 2018 23:57
Java Servlet using Google Vision API . This will only run live at App Engine since it doesn't use authorization. All apps running at App Engine use a service account and are automatic authenticated.
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
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I was trying using standalone programme .I was getting below error .Your suggestion would be great help.
16:37:11.095 [main] DEBUG io.netty.util.internal.CleanerJava6 - java.nio.ByteBuffer.cleaner(): available
16:37:11.097 [main] DEBUG io.netty.util.internal.NativeLibraryLoader - -Dio.netty.native.workdir: /var/folders/nn/l7lndn690yv6w8qs1hx00r9c0000gn/T (io.netty.tmpdir)
16:37:11.099 [main] DEBUG io.netty.util.internal.NativeLibraryLoader - Unable to load the library 'netty_tcnative_osx_x86_64', trying other loading mechanism.
java.lang.UnsatisfiedLinkError: no netty_tcnative_osx_x86_64 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at io.netty.util.internal.NativeLibraryUtil.loadLibrary(NativeLibraryUtil.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at io.netty.util.internal.NativeLibraryLoader$1.run(NativeLibraryLoader.java:229)
at java.security.AccessController.doPrivileged(Native Method)
at io.netty.util.internal.NativeLibraryLoader.loadLibraryByHelper(NativeLibraryLoader.java:221)
at io.netty.util.internal.NativeLibraryLoader.loadLibrary(NativeLibraryLoader.java:207)
at io.netty.util.internal.NativeLibraryLoader.load(NativeLibraryLoader.java:118)
at io.netty.util.internal.NativeLibraryLoader.loadFirstAvailable(NativeLibraryLoader.java:82)
at io.netty.handler.ssl.OpenSsl.loadTcNative(OpenSsl.java:419)
at io.netty.handler.ssl.OpenSsl.(OpenSsl.java:89)
at io.grpc.netty.GrpcSslContexts.defaultSslProvider(GrpcSslContexts.java:143)
at io.grpc.netty.GrpcSslContexts.configure(GrpcSslContexts.java:124)
at io.grpc.netty.GrpcSslContexts.forClient(GrpcSslContexts.java:94)
at io.grpc.netty.NettyChannelBuilder$NettyTransportFactory$DefaultNettyTransportCreationParamsFilterFactory.(NettyChannelBuilder.java:521)
at io.grpc.netty.NettyChannelBuilder$NettyTransportFactory$DefaultNettyTransportCreationParamsFilterFactory.(NettyChannelBuilder.java:514)
at io.grpc.netty.NettyChannelBuilder$NettyTransportFactory.(NettyChannelBuilder.java:453)
at io.grpc.netty.NettyChannelBuilder.buildTransportFactory(NettyChannelBuilder.java:312)
at io.grpc.internal.AbstractManagedChannelImplBuilder.build(AbstractManagedChannelImplBuilder.java:324)
at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createChannel(InstantiatingGrpcChannelProvider.java:165)
at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.getTransportChannel(InstantiatingGrpcChannelProvider.java:130)
at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:122)
at com.google.cloud.vision.v1.stub.GrpcImageAnnotatorStub.create(GrpcImageAnnotatorStub.java:62)
at com.google.cloud.vision.v1.ImageAnnotatorSettings.createStub(ImageAnnotatorSettings.java:101)
at com.google.cloud.vision.v1.ImageAnnotatorClient.(ImageAnnotatorClient.java:130)
at com.google.cloud.vision.v1.ImageAnnotatorClient.create(ImageAnnotatorClient.java:111)
at com.google.cloud.vision.v1.ImageAnnotatorClient.create(ImageAnnotatorClient.java:102)
Thanks
ramprasadgk6@gmail.com