Google Vision API:java.lang.NoClassDefFoundError:com / google / cloud / vision / v1 / ImageAnnotatorClient错误

时间:2019-05-28 11:38:48

标签: google-cloud-vision google-vision

我正在尝试运行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>

我正在使用的代码。 google Vision 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()));
          }
        }
      }
   }
}

1 个答案:

答案 0 :(得分:0)

使用软件包Google.Cloud.Vision.V1,或者您可能还想检查correct Client Library以使用,具体取决于您的框架。