应用程序凭据在Google Cloud Vision API中不可用。

huangapple go评论70阅读模式
英文:

Applications Credentials not avaliable in Google Cloud Vision API

问题

以下是翻译好的内容:

我正在尝试设置Google Cloud Vision API我已经通过使用命令行CMD定义了一个应用程序凭据变量命令是 set GOOGLE_APPLICATION_CREDENTIALS PathToJSON然而这仍然不能让我连接到Google Cloud Vision API进行OCR

我还尝试过通过Windows用户界面手动设置然而仍然没有成功我在Google Cloud页面上创建并定义了一个项目并生成了一个凭据密钥当它问我您是否打算在App Engine或Compute Engine中使用此API?”我选择了”。

我目前正在使用谷歌的样板代码

public class DetectText {

    public static void main(String args[]) {
        try {
            detectText();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void detectText() throws IOException {
        // TODO(developer): 在运行示例之前替换这些变量。
        String filePath = "C:\\Users\\Programming\\Desktop\\TextDetection\\Capture.PNG";
        detectText(filePath);
    }

    // 检测指定图像中的文本。
    public static void detectText(String filePath) throws IOException {
        List<AnnotateImageRequest> requests = new ArrayList<>();

        ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));

        Image img = Image.newBuilder().setContent(imgBytes).build();
        Feature feat = Feature.newBuilder().setType(Feature.Type.TEXT_DETECTION).build();
        AnnotateImageRequest request =
                AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
        requests.add(request);

        // 初始化将用于发送请求的客户端。此客户端只需要创建一次,可以用于多个请求。
        // 在完成所有请求后,调用客户端的 "close" 方法以安全地清理任何剩余的后台资源。
        try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
            BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
            List<AnnotateImageResponse> responses = response.getResponsesList();

            for (AnnotateImageResponse res : responses) {
                if (res.hasError()) {
                    System.out.format("错误: %s%n", res.getError().getMessage());
                    return;
                }

                // 有关可用注释的完整列表,请参阅 http://g.co/cloud/vision/docs
                for (EntityAnnotation annotation : res.getTextAnnotationsList()) {
                    System.out.format("文本: %s%n", annotation.getDescription());
                    System.out.format("位置: %s%n", annotation.getBoundingPoly());
                }
            }
        }
    }

    static void authExplicit(String jsonPath) throws IOException {
    }
}

我没有使用服务器或Google计算虚拟机

请问有人能解释一下问题是什么以及我应该如何修复它吗

**堆栈跟踪**
 
java.io.IOException: 应用程序默认凭据不可用如果在Google Compute Engine中运行则可用否则必须定义环境变量GOOGLE_APPLICATION_CREDENTIALS指向定义凭据的文件有关更多信息请参见https://developers.google.com/accounts/docs/application-default-credentials。
    at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:134)
    at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:119)
    at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:91)
    at com.google.api.gax.core.GoogleCredentialsProvider.getCredentials(GoogleCredentialsProvider.java:67)
    at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:142)
    at com.google.cloud.vision.v1.stub.GrpcImageAnnotatorStub.create(GrpcImageAnnotatorStub.java:117)
    at com.google.cloud.vision.v1.stub.ImageAnnotatorStubSettings.createStub(ImageAnnotatorStubSettings.java:156)
    at com.google.cloud.vision.v1.ImageAnnotatorClient.<init>(ImageAnnotatorClient.java:136)
    at com.google.cloud.vision.v1.ImageAnnotatorClient.create(ImageAnnotatorClient.java:117)
    at com.google.cloud.vision.v1.ImageAnnotatorClient.create(ImageAnnotatorClient.java:108)
    at DetectText.detectText(DetectText.java:54)
    at DetectText.detectText(DetectText.java:36)
    at DetectText.main(DetectText.java:25)
英文:

I am trying to setup Google Cloud Vision API, I have defined a Application Credential Variable through CMD by using set GOOGLE_APPLICATION_CREDENTIALS PathToJSON however this still does not allow me to connect to Google Cloud Vision API for OCR.

I have also tried to set it manually through the windows UI, however still no luck, I created and defined a project in the Google Cloud page, and generated a credential key, when it asked me "Are you planning to use this API with App Engine or Compute Engine?", I selected No.

I am currently using Googles boilerplate code

public class DetectText {
public static void main(String args[])
{
try{
detectText();
}catch (IOException e)
{
e.printStackTrace();
}
}
public static void detectText() throws IOException {
// TODO(developer): Replace these variables before running the sample.
String filePath = &quot;C:\\Users\\Programming\\Desktop\\TextDetection\\Capture.PNG&quot;;
detectText(filePath);
}
// Detects text in the specified image.
public static void detectText(String filePath) throws IOException {
List&lt;AnnotateImageRequest&gt; requests = new ArrayList&lt;&gt;();
ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
Image img = Image.newBuilder().setContent(imgBytes).build();
Feature feat = Feature.newBuilder().setType(Feature.Type.TEXT_DETECTION).build();
AnnotateImageRequest request =
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the &quot;close&quot; method on the client to safely clean up any remaining background resources.
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
List&lt;AnnotateImageResponse&gt; responses = response.getResponsesList();
for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
System.out.format(&quot;Error: %s%n&quot;, res.getError().getMessage());
return;
}
// For full list of available annotations, see http://g.co/cloud/vision/docs
for (EntityAnnotation annotation : res.getTextAnnotationsList()) {
System.out.format(&quot;Text: %s%n&quot;, annotation.getDescription());
System.out.format(&quot;Position : %s%n&quot;, annotation.getBoundingPoly());
}
}
}
}
static void authExplicit(String jsonPath) throws IOException {
}

}

I am not using a server or Google compute virtual machine.

Can someone please explain to me what the problem is, and how I would go about fixing it?

Stack Trace

java.io.IOException: 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.
at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:134)
at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:119)
at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:91)
at com.google.api.gax.core.GoogleCredentialsProvider.getCredentials(GoogleCredentialsProvider.java:67)
at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:142)
at com.google.cloud.vision.v1.stub.GrpcImageAnnotatorStub.create(GrpcImageAnnotatorStub.java:117)
at com.google.cloud.vision.v1.stub.ImageAnnotatorStubSettings.createStub(ImageAnnotatorStubSettings.java:156)
at com.google.cloud.vision.v1.ImageAnnotatorClient.&lt;init&gt;(ImageAnnotatorClient.java:136)
at com.google.cloud.vision.v1.ImageAnnotatorClient.create(ImageAnnotatorClient.java:117)
at com.google.cloud.vision.v1.ImageAnnotatorClient.create(ImageAnnotatorClient.java:108)
at DetectText.detectText(DetectText.java:54)
at DetectText.detectText(DetectText.java:36)
at DetectText.main(DetectText.java:25)

答案1

得分: 1

根据您的错误消息,似乎找不到 GOOGLE_APPLICATION_CREDENTIALS 环境变量。

一方面,在尝试运行文本检测示例代码之前,请按照文档中的步骤操作,以排除是否有任何步骤被跳过。

另一方面,如果您正在使用诸如IntelliJ或Eclipse之类的IDE,您需要通过Windows系统属性全局设置环境变量 GOOGLE_APPLICATION_CREDENTIALS,以便IDE可以使用它。然而,在测试时,我不得不关闭并重新打开IDE,以使更改生效,从而不会出现上述错误。

此外,还可以通过代码中的方法指定 JSON 文件的位置,如此示例所示。然而,不建议以这种方式放置它,最好使用环境变量。

英文:

Based on your error message, it seems that the GOOGLE_APPLICATION_CREDENTIALS environment variable is not being found.

On one hand, before trying to run the text detection sample code, follow the steps outlined in the documentation in order to discard that any of them has been skipped.

On the other hand, if you are using an IDE such as IntelliJ or Eclipse, you have to set the environment variable GOOGLE_APPLICATION_CREDENTIALS global through Windows System Properties, so it can be used by the IDE. Nevertheless, when testing I had to close and reopen the IDE for the changes to take effect and the aforementioned error would not appear.

Additionally, there is also a way to specify the location of the JSON file within the code, as shown in this example. However, it is not advisable to put it that way, it is best to use the environment variables.

huangapple
  • 本文由 发表于 2020年9月15日 21:21:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63902779.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定