第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

Google Vision API:存儲桶不返回任何內(nèi)容,并且 ImageAnnotator

Google Vision API:存儲桶不返回任何內(nèi)容,并且 ImageAnnotator

慕尼黑8549860 2022-07-06 17:24:08
我有 Google Vision API 的代碼。我將 Google 憑據(jù)作為路徑和環(huán)境變量,但Page<Bucket> buckets = storage.list();不返回任何內(nèi)容,并且出現(xiàn)錯(cuò)誤 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()));                }            }        }    }
查看完整描述

1 回答

?
慕標(biāo)5832272

TA貢獻(xiàn)1966條經(jīng)驗(yàn) 獲得超4個(gè)贊

發(fā)生此錯(cuò)誤是因?yàn)镮mageAnnotatorClient client = ImageAnnotatorClient.create()需要在環(huán)境變量中設(shè)置默認(rèn)憑據(jù)或服務(wù)帳戶。


我使用以下代碼成功完成了請求:


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()));

    }

   }

  }

 }

}


查看完整回答
反對 回復(fù) 2022-07-06
  • 1 回答
  • 0 關(guān)注
  • 127 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號