Reputation: 79
My goal is to use one of the google cloud API's, for which I need that GOOGLE_APPLICATION_CREDENTIALS is set to a .json file.
I receive this error message and a link on how to solve it:
The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.
After setting the variable through cmd (set GOOGLE_APPLICATION_CREDENTIALS=C:\...\...\File.json
) to the .json file that I have just created, the same exact error continues appearing.
What am I doing wrong?
EDIT:
I'm using Eclipse JEE. I want to detect web entities from a local image using google cloud vision API. First using a html formulary I request the image and send it to a servlet class: (index.html)
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Tittle</title>
</head>
<body>
<h1>Welcome!</h1>
<h4>Upload an image: </h4>
<div id="searchDiv">
<form id="searchForm" action="webdetection" method="get" enctype="multipart/form-data">
<div><input type="file" name="image" accept=".JPG, .JPEG, .PNG8, .PNG24, .GIF, .BMP, .WEBP, .RAW, .ICO, .PDF, .TIFF" required>
</div>
<div><input type="submit" name="searchBtn" value="Aceptar">
</div>
</form>
</div>
</body>
</html>
These are the servlets and their mappings:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<web:description></web:description>
<servlet-name>Web Detection</servlet-name>
<servlet-class>aiss.controller.google_vision.VisionServletController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Web Detection</servlet-name>
<url-pattern>/webdetection</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
This is the Servlet for cloud vision:
package aiss.controller.google_vision;
import java.io.IOException;
import java.io.PrintStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import aiss.model.CloudVision.DeteccionWeb;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletOutputStream;
public class VisionServletController extends HttpServlet {
private static final Logger log = Logger.getLogger(VisionServletController.class.getName());
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
log.log(Level.FINE, "Processing GET request");
PrintStream ps = new PrintStream(resp.getOutputStream());
try {
resp.setContentType("text/plain");
DeteccionWeb.detectWebDetectionsGcs(req.getRequestURL().toString(),ps);
}catch(Exception e) {
ps.println(e.toString());//this prints the previous error message I show
}
}
}
And the web detection class at which the servlet redirects: (most of the imports aren't being used, but due to the class is being modified until it works, I don't know which classes I need)
package aiss.model.CloudVision;
import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.vision.v1.AnnotateFileResponse;
import com.google.cloud.vision.v1.AnnotateFileResponse.Builder;
import com.google.cloud.vision.v1.AnnotateImageRequest;
import com.google.cloud.vision.v1.AnnotateImageResponse;
import com.google.cloud.vision.v1.AsyncAnnotateFileRequest;
import com.google.cloud.vision.v1.AsyncAnnotateFileResponse;
import com.google.cloud.vision.v1.AsyncBatchAnnotateFilesResponse;
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
import com.google.cloud.vision.v1.Block;
import com.google.cloud.vision.v1.ColorInfo;
import com.google.cloud.vision.v1.CropHint;
import com.google.cloud.vision.v1.CropHintsAnnotation;
import com.google.cloud.vision.v1.DominantColorsAnnotation;
import com.google.cloud.vision.v1.EntityAnnotation;
import com.google.cloud.vision.v1.FaceAnnotation;
import com.google.cloud.vision.v1.Feature;
import com.google.cloud.vision.v1.Feature.Type;
import com.google.cloud.vision.v1.GcsDestination;
import com.google.cloud.vision.v1.GcsSource;
import com.google.cloud.vision.v1.Image;
import com.google.cloud.vision.v1.ImageAnnotatorClient;
import com.google.cloud.vision.v1.ImageContext;
import com.google.cloud.vision.v1.ImageSource;
import com.google.cloud.vision.v1.InputConfig;
import com.google.cloud.vision.v1.LocalizedObjectAnnotation;
import com.google.cloud.vision.v1.LocationInfo;
import com.google.cloud.vision.v1.OperationMetadata;
import com.google.cloud.vision.v1.OutputConfig;
import com.google.cloud.vision.v1.Page;
import com.google.cloud.vision.v1.Paragraph;
import com.google.cloud.vision.v1.SafeSearchAnnotation;
import com.google.cloud.vision.v1.Symbol;
import com.google.cloud.vision.v1.TextAnnotation;
import com.google.cloud.vision.v1.WebDetection;
import com.google.cloud.vision.v1.WebDetection.WebEntity;
import com.google.cloud.vision.v1.WebDetection.WebImage;
import com.google.cloud.vision.v1.WebDetection.WebLabel;
import com.google.cloud.vision.v1.WebDetection.WebPage;
import com.google.cloud.vision.v1.WebDetectionParams;
import com.google.cloud.vision.v1.Word;
import com.google.protobuf.ByteString;
import com.google.protobuf.util.JsonFormat;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DeteccionWeb {
/**
* Detects whether the remote image on Google Cloud Storage has features you would want to
* moderate.
*
* @param gcsPath The path to the remote on Google Cloud Storage file to detect web annotations.
* @param out A {@link PrintStream} to write the results to.
* @throws Exception on errors while closing the client.
* @throws IOException on Input/Output errors.
*/
public static void detectWebDetectionsGcs(String gcsPath, PrintStream out) throws Exception,
IOException {
List<AnnotateImageRequest> requests = new ArrayList<>();
ImageSource imgSource = ImageSource.newBuilder().setImageUri(gcsPath).build();
Image img = Image.newBuilder().setSource(imgSource).build();
Feature feat = Feature.newBuilder().setType(Type.WEB_DETECTION).build();
AnnotateImageRequest request =
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();
for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
out.printf("Error: %s\n", res.getError().getMessage());
return;
}
// Search the web for usages of the image. You could use these signals later
// for user input moderation or linking external references.
// For a full list of available annotations, see http://g.co/cloud/vision/docs
WebDetection annotation = res.getWebDetection();
out.println("Entity:Id:Score");
out.println("===============");
for (WebEntity entity : annotation.getWebEntitiesList()) {
out.println(entity.getDescription() + " : " + entity.getEntityId() + " : "
+ entity.getScore());
}
for (WebLabel label : annotation.getBestGuessLabelsList()) {
out.format("\nBest guess label: %s", label.getLabel());
}
}
}
}
}
My main suspect is ImageSource.newBuilder().setImageUri(gcsPath)
, as it seems cloud vision API may not work on http/https url's that doesn't belong to Google cloud storage. And it seems it can be solved with the credentials, but I can't pass through it.
Upvotes: 0
Views: 1970
Reputation: 46
if you're using Tomcat in eclipse, the following thread should be helpful How to set GOOGLE_APPLICATION_CREDENTIALS for Google Compute Engine?.
Upvotes: 1