Google Vision API:存储桶不返回任何内容,并且 ImageAnnotator

我有 Google Vision API 的代码。我将 Google 凭据作为路径和环境变量,但Page<Bucket> buckets = storage.list();不返回任何内容,并且出现错误 try (ImageAnnotatorClient client = 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()));

                }

            }

        }

    }


慕尼黑8549860
浏览 109回答 1
1回答

慕标5832272

发生此错误是因为ImageAnnotatorClient client = 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 {&nbsp;public static void main(String...args) throws Exception {&nbsp; String jsonPath = "../../key.json";&nbsp; authExplicit(jsonPath);&nbsp; detectLabels(jsonPath, "../wakeupcat.jpg", System.out);&nbsp;}&nbsp;static void authExplicit(String jsonPath) throws IOException {&nbsp; GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath))&nbsp; &nbsp;.createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));&nbsp; Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();&nbsp; System.out.println("Buckets:");&nbsp; Page < Bucket > buckets = storage.list();&nbsp; for (Bucket bucket: buckets.iterateAll()) {&nbsp; &nbsp;System.out.println(bucket.toString());&nbsp; }&nbsp;}&nbsp;public static void detectLabels(String jsonPath, String filePath, PrintStream out) throws Exception, IOException {&nbsp; GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath))&nbsp; &nbsp;.createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));&nbsp; List < AnnotateImageRequest > requests = new ArrayList < > ();&nbsp; ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));&nbsp; Image img = Image.newBuilder().setContent(imgBytes).build();&nbsp; Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build();&nbsp; AnnotateImageRequest request =&nbsp; &nbsp;AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();&nbsp; requests.add(request);//Setting the credentials:&nbsp; ImageAnnotatorSettings imageAnnotatorSettings = ImageAnnotatorSettings.newBuilder()&nbsp; &nbsp;.setCredentialsProvider(FixedCredentialsProvider.create(credentials))&nbsp; &nbsp;.build();&nbsp; try (ImageAnnotatorClient client = ImageAnnotatorClient.create(imageAnnotatorSettings)) {&nbsp; &nbsp;BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);&nbsp; &nbsp;List < AnnotateImageResponse > responses = response.getResponsesList();&nbsp; &nbsp;for (AnnotateImageResponse res: responses) {&nbsp; &nbsp; if (res.hasError()) {&nbsp; &nbsp; &nbsp;out.printf("Error: %s\n", res.getError().getMessage());&nbsp; &nbsp; &nbsp;return;&nbsp; &nbsp; }&nbsp; &nbsp; // For full list of available annotations, see http://g.co/cloud/vision/docs&nbsp; &nbsp; for (EntityAnnotation annotation: res.getLabelAnnotationsList()) {&nbsp; &nbsp; &nbsp;annotation.getAllFields().forEach((k, v) -> out.printf("%s : %s\n", k, v.toString()));&nbsp; &nbsp; }&nbsp; &nbsp;}&nbsp; }&nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java