我有 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()));
}
}
}
}
慕标5832272
相关分类