首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何定义GOOGLE_APPLICATION_CREDENTIALS?

如何定义GOOGLE_APPLICATION_CREDENTIALS?
EN

Stack Overflow用户
提问于 2020-04-26 17:05:11
回答 1查看 800关注 0票数 0

我的目标是使用google之一,为此我需要将GOOGLE_APPLICATION_CREDENTIALS设置为一个.json文件。

我收到这条错误消息和一个关于如何解决它的链接:

应用程序默认凭据不可用。如果在中运行,它们是可用的。否则,必须定义环境变量GOOGLE_APPLICATION_CREDENTIALS,指向定义凭据的文件。有关详细信息,请参阅https://developers.google.com/accounts/docs/application-default-credentials

在通过cmd (set GOOGLE_APPLICATION_CREDENTIALS=C:\...\...\File.json)将变量设置为我刚才创建的.json文件后,同样的错误将继续出现。

我做错了什么?

编辑:

我正在使用Eclipse。我想使用从本地图像中检测web实体。首先,使用html公式,我请求图像并将其发送到servlet类:(index.html)

代码语言:javascript
运行
复制
<!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>

这些是servlet及其映射:

代码语言:javascript
运行
复制
<?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>

这是用于云视觉的Servlet:

代码语言:javascript
运行
复制
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
        }

    }
}

以及servlet重定向的web检测类:(大多数导入没有被使用,但是由于类正在被修改,直到它正常工作,我不知道我需要哪些类)

代码语言:javascript
运行
复制
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());
          }
        }
      }
    }
}

我的主要怀疑是ImageSource.newBuilder().setImageUri(gcsPath),因为云视觉API似乎不适用于不属于Google云存储的http/https url。它似乎可以用证书解决,但我不能通过它。

EN

回答 1

Stack Overflow用户

发布于 2020-04-27 00:58:53

如果您在eclipse中使用Tomcat,下面的线程应该是有用的How to set GOOGLE_APPLICATION_CREDENTIALS for Google Compute Engine?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61444732

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档