首页 > 解决方案 > 谷歌视觉 API:java.lang.NoClassDefFoundError:com/google/cloud/vision/v1/ImageAnnotatorClient 错误

问题描述

我正在尝试运行 Google Api Vision 示例代码,但出现此错误:

java.lang.NoClassDefFoundError:com/google/cloud/vision/v1/ImageAnnotatorClient

这些是导入到我的项目中的依赖项。

<dependency>
  <groupId>com.google.cloud</groupId>
  <artifactId>google-cloud-vision</artifactId>
  <version>1.74.0</version>
</dependency>
<dependency>
  <groupId>com.google.api.grpc</groupId>
  <artifactId>proto-google-common-protos</artifactId>
  <version>1.7.0</version>
</dependency>
<dependency>
  <groupId>com.google.code.findbugs</groupId>
  <artifactId>jsr305</artifactId>
  <version>3.0.2</version>
</dependency>
<dependency>
  <groupId>javax.annotation</groupId>
  <artifactId>javax.annotation-api</artifactId>
  <version>1.3.2</version>
</dependency>

我正在使用的代码。谷歌视觉 API 来自:https ://cloud.google.com/vision/docs/libraries

package com.google.cloud.vision.api.utils;

//Imports the Google Cloud client library

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 QuickstartSample {

    public static void main(String... args) throws Exception {
      // Instantiates a client
      try (ImageAnnotatorClient vision = ImageAnnotatorClient.create()) {

        // The path to the image file to annotate
        String fileName = "/content/dam/USGBoral/Australia/Website/Images/products/steel_framing/SteelFraming-335x135_en.jpg";

        // Reads the image file into memory
        Path path = Paths.get(fileName);
        byte[] data = Files.readAllBytes(path);
        ByteString imgBytes = ByteString.copyFrom(data);

        // Builds the image annotation request
        List<AnnotateImageRequest> requests = new ArrayList<>();
        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);

        // Performs label detection on the image file
        BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();

        for (AnnotateImageResponse res : responses) {
          if (res.hasError()) {
            System.out.printf("Error: %s\n", res.getError().getMessage());
            return;
          }

          for (EntityAnnotation annotation :res.getLabelAnnotationsList()) {
            annotation.getAllFields()
                      .forEach((k, v) -> System.out.printf("%s : %s\n", k, v.toString()));
          }
        }
      }
   }
}

标签: google-cloud-visiongoogle-vision

解决方案


使用包Google.Cloud.Vision.V1或者您可能还想根据您的框架检查要使用的正确客户端库。


推荐阅读