微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Google Cloud Vision API中不提供应用程序凭据

如何解决Google Cloud Vision API中不提供应用程序凭据

我正在尝试设置Google Cloud Vision API,我已通过使用set GOOGLE_APPLICATION_CREDENTIALS PathToJSON通过CMD定义了应用程序凭据变量,但这仍然不允许我连接到OCR的Google Cloud Vision API。

我也尝试过通过Windows UI手动设置它,但是仍然没有运气,我在Google Cloud页面上创建并定义了一个项目,并生成一个凭据密钥,当它询问我“您打算使用此功能吗?是使用App Engine还是Compute Engine的API?”,我选择了否。

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

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 = "C:\\Users\\Programming\\Desktop\\TextDetection\\Capture.PNG";
    detectText(filePath);
}

// Detects text in the specified image.
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);

    // 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 "close" method on the client to safely clean up any remaining background resources.
    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("Error: %s%n",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("Text: %s%n",annotation.getDescription());
                System.out.format("Position : %s%n",annotation.getBoundingpoly());
            }
        }
    }
}

static void authExplicit(String jsonPath) throws IOException {

}

}

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

有人可以向我解释一下问题是什么,以及如何解决该问题?

堆栈跟踪

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.<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)

解决方法

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

一方面,在尝试运行text detection sample code之前,请按照documentation中概述的步骤进行操作,以放弃其中的任何一个已被跳过。

另一方面,如果您使用的是诸如IntelliJ或Eclipse之类的IDE,则必须通过Windows System Properties设置全局环境变量GOOGLE_APPLICATION_CREDENTIALS,以便IDE可以使用它。但是,在测试时,我必须关闭并重新打开IDE才能使更改生效,并且不会出现上述错误。

此外,还有一种方法可以指定代码中JSON文件的位置,如this example中所示。但是,最好不要这样说,最好使用环境变量。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。