Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Google Vision API:Bucket不返回任何内容,错误出现在ImageAnnotatorClient上_Java_Google Cloud Vision - Fatal编程技术网

Java Google Vision API:Bucket不返回任何内容,错误出现在ImageAnnotatorClient上

Java Google Vision API:Bucket不返回任何内容,错误出现在ImageAnnotatorClient上,java,google-cloud-vision,Java,Google Cloud Vision,我有谷歌视觉API的代码。我使用Google凭据作为路径和环境变量,但Page bucket=storage.list()不返回任何内容,错误出现在try(ImageAnnotatorClient=ImageAnnotatorClient.create()) 以下是代码: public static void main(String... args) throws Exception { authExplicit("D:\\bp-mihalova\\2\\apikey.json

我有谷歌视觉API的代码。我使用Google凭据作为路径和环境变量,但
Page bucket=storage.list()不返回任何内容,错误出现在
try(ImageAnnotatorClient=ImageAnnotatorClient.create())

以下是代码:

 public static void main(String... args) throws Exception {
        authExplicit("D:\\bp-mihalova\\2\\apikey.json");
        detectLabels("D:\\bp-mihalova\\2\\Jellyfish.jpg", System.out);
    }

    public static void detectLabels(String filePath, PrintStream out) throws Exception, 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(Type.LABEL_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;
                }

                // For full list of available annotations, see http://g.co/cloud/vision/docs
                for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
                    annotation.getAllFields().forEach((k, v) -> out.printf("%s : %s\n", k, v.toString()));
                }
            }
        }
    }

    static void authExplicit(String jsonPath) throws IOException {
        // You can specify a credential file by providing a path to GoogleCredentials.
        // Otherwise credentials are read from the GOOGLE_APPLICATION_CREDENTIALS environment variable.
        GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath))
                .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
        Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();

        System.out.println("Buckets:");
        Page<Bucket> buckets = storage.list();
        for (Bucket bucket : buckets.iterateAll()) {
            System.out.println(bucket.toString());
        }
    }

发生此错误是因为
ImageAnnotatorClient=ImageAnnotatorClient.create()
需要在环境变量中设置默认凭据或服务帐户

我成功地使用以下代码执行了请求:

package com.example.vision;

import com.google.auth.oauth2.GoogleCredentials;
import java.io.FileInputStream;
import com.google.api.gax.paging.Page;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.io.PrintStream;
import com.google.cloud.vision.v1.ImageAnnotatorSettings;
import com.google.api.gax.core.FixedCredentialsProvider;

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.EntityAnnotation;
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.protobuf.ByteString;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class Sample {
 public static void main(String...args) throws Exception {
  String jsonPath = "../../key.json";
  authExplicit(jsonPath);
  detectLabels(jsonPath, "../wakeupcat.jpg", System.out);
 }
 static void authExplicit(String jsonPath) throws IOException {
  GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath))
   .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
  Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();

  System.out.println("Buckets:");
  Page < Bucket > buckets = storage.list();
  for (Bucket bucket: buckets.iterateAll()) {
   System.out.println(bucket.toString());
  }
 }
 public static void detectLabels(String jsonPath, String filePath, PrintStream out) throws Exception, IOException {
  GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath))
   .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
  List < AnnotateImageRequest > requests = new ArrayList < > ();

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

  Image img = Image.newBuilder().setContent(imgBytes).build();
  Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build();
  AnnotateImageRequest request =
   AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
  requests.add(request);
//Setting the credentials:
  ImageAnnotatorSettings imageAnnotatorSettings = ImageAnnotatorSettings.newBuilder()
   .setCredentialsProvider(FixedCredentialsProvider.create(credentials))
   .build();
  try (ImageAnnotatorClient client = ImageAnnotatorClient.create(imageAnnotatorSettings)) {
   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;
    }

    // For full list of available annotations, see http://g.co/cloud/vision/docs
    for (EntityAnnotation annotation: res.getLabelAnnotationsList()) {
     annotation.getAllFields().forEach((k, v) -> out.printf("%s : %s\n", k, v.toString()));
    }
   }
  }
 }
}
package com.example.vision;
导入com.google.auth.oauth2.GoogleCredentials;
导入java.io.FileInputStream;
导入com.google.api.gax.paging.Page;
导入com.google.cloud.storage.Bucket;
导入com.google.cloud.storage.storage;
导入com.google.cloud.storage.StorageOptions;
导入com.google.common.collect.list;
导入java.io.IOException;
导入java.io.PrintStream;
导入com.google.cloud.vision.v1.ImageAnnotatorSettings;
导入com.google.api.gax.core.FixedCredentialsProvider;
导入com.google.cloud.vision.v1.ImageRequest;
导入com.google.cloud.vision.v1.ImageResponse;
导入com.google.cloud.vision.v1.batch注释图像响应;
导入com.google.cloud.vision.v1.EntityAnnotation;
导入com.google.cloud.vision.v1.Feature;
导入com.google.cloud.vision.v1.Feature.Type;
导入com.google.cloud.vision.v1.Image;
导入com.google.cloud.vision.v1.ImageAnnotatorClient;
导入com.google.protobuf.ByteString;
导入java.nio.file.Files;
导入java.nio.file.Path;
导入java.nio.file.path;
导入java.util.ArrayList;
导入java.util.List;
公共类样本{
公共静态void main(字符串…参数)引发异常{
字符串jsonPath=“../../key.json”;
authoxplicit(jsonPath);
检测标签(jsonPath,“../wakeupcat.jpg”,System.out);
}
静态void authExplicit(字符串jsonPath)引发IOException{
GoogleCredentials=GoogleCredentials.fromStream(新文件输入流(jsonPath))
.createScope(列表.newArrayList(“https://www.googleapis.com/auth/cloud-platform"));
Storage Storage=StorageOptions.newBuilder().setCredentials(credentials.build().getService();
System.out.println(“bucket:”);
页面Bucket=storage.list();
for(Bucket:Bucket.iterateAll()){
System.out.println(bucket.toString());
}
}
公共静态void detectLabel(字符串jsonPath、字符串filePath、打印流输出)引发异常、IOException{
GoogleCredentials=GoogleCredentials.fromStream(新文件输入流(jsonPath))
.createScope(列表.newArrayList(“https://www.googleapis.com/auth/cloud-platform"));
Listrequests=new ArrayList<>();
ByteString imgBytes=ByteString.readFrom(新文件输入流(filePath));
Image img=Image.newBuilder().setContent(imgBytes.build();
Feature feat=Feature.newBuilder().setType(Type.LABEL_DETECTION).build();
注释图像请求请求=
AnnotateImageRequest.newBuilder().addFeatures(feat.setImage(img.build));
请求。添加(请求);
//设置凭据:
ImageAnnotatorSettings ImageAnnotatorSettings=ImageAnnotatorSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(凭据))
.build();
try(ImageAnnotatorClient=ImageAnnotatorClient.create(imageAnnotatorSettings)){
BatchAnnotateImagesResponse=client.batchAnnotateImages(请求);
Listresponses=response.getResponseList();
用于(注释ImageResponse res:responses){
if(res.hasError()){
out.printf(“错误:%s\n”,res.getError().getMessage());
返回;
}
//有关可用批注的完整列表,请参见http://g.co/cloud/vision/docs
对于(EntityAnnotation:res.getLabelAnnotationsList()){
annotation.getAllFields().forEach((k,v)->out.printf(“%s:%s\n”,k,v.toString());
}
}
}
}
}

“出现错误”。。你能说得更具体一点吗?酷,你能把它标记为正确答案吗?:)
package com.example.vision;

import com.google.auth.oauth2.GoogleCredentials;
import java.io.FileInputStream;
import com.google.api.gax.paging.Page;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.io.PrintStream;
import com.google.cloud.vision.v1.ImageAnnotatorSettings;
import com.google.api.gax.core.FixedCredentialsProvider;

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.EntityAnnotation;
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.protobuf.ByteString;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class Sample {
 public static void main(String...args) throws Exception {
  String jsonPath = "../../key.json";
  authExplicit(jsonPath);
  detectLabels(jsonPath, "../wakeupcat.jpg", System.out);
 }
 static void authExplicit(String jsonPath) throws IOException {
  GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath))
   .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
  Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();

  System.out.println("Buckets:");
  Page < Bucket > buckets = storage.list();
  for (Bucket bucket: buckets.iterateAll()) {
   System.out.println(bucket.toString());
  }
 }
 public static void detectLabels(String jsonPath, String filePath, PrintStream out) throws Exception, IOException {
  GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath))
   .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
  List < AnnotateImageRequest > requests = new ArrayList < > ();

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

  Image img = Image.newBuilder().setContent(imgBytes).build();
  Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build();
  AnnotateImageRequest request =
   AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
  requests.add(request);
//Setting the credentials:
  ImageAnnotatorSettings imageAnnotatorSettings = ImageAnnotatorSettings.newBuilder()
   .setCredentialsProvider(FixedCredentialsProvider.create(credentials))
   .build();
  try (ImageAnnotatorClient client = ImageAnnotatorClient.create(imageAnnotatorSettings)) {
   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;
    }

    // For full list of available annotations, see http://g.co/cloud/vision/docs
    for (EntityAnnotation annotation: res.getLabelAnnotationsList()) {
     annotation.getAllFields().forEach((k, v) -> out.printf("%s : %s\n", k, v.toString()));
    }
   }
  }
 }
}