【问题标题】:Getting Empty Response from Google Vision API从 Google Vision API 获得空响应
【发布时间】:2016-07-19 00:56:14
【问题描述】:

我正在测试 Google Vision API 的一些功能,并且我从我的相机(5MP 相机)中点击的图像得到了 Empty 响应。但是,当我从网络下载任何图像时,例如,一个送货员的图像(具有纯色背景,例如白色),我会得到一个带有标签的有意义的响应。这两组图像都存在于我的本地磁盘上。以下是我参考谷歌文档编写的代码,

public class ImageAnalyzer {

final static String APPLICATION_NAME ="My_APP/1.0";
final static String IMAGE_PATH = "E:/Vision/SampleImages/IMAG0013.jpg";
final static int maxResults =3;

private Vision vision;

public ImageAnalyzer(Vision vision){
    this.vision=vision;
}
  /**
   * Connects to the Vision API using Application Default Credentials.
   */
  public static Vision getVisionService() throws IOException, GeneralSecurityException {
    GoogleCredential credential =
        GoogleCredential.getApplicationDefault().createScoped(VisionScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    return new Vision.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, credential)
            .setApplicationName(APPLICATION_NAME)
            .build();
  }

  public Map<String, List<String>> labelImage(String imagePath){

      final Map<String,List<String>> labels = new HashMap<String, List<String>>();

      final Path path = Paths.get(imagePath);  

      try {

        final byte[] raw = Files.readAllBytes(path);

        /*final AnnotateImageRequest request =new AnnotateImageRequest().setImage(new Image().encodeContent(raw))
        .setFeatures(ImmutableList.of(new Feature().setType("LABEL_DETECTION").setMaxResults(3)
                , new Feature().setType("LOGO_DETECTION").setMaxResults(3)));*/

        AnnotateImageRequest request =
                new AnnotateImageRequest()
                    .setImage(new Image().encodeContent(raw)).setFeatures(ImmutableList.of(
                new Feature()
                    .setType("LABEL_DETECTION")
                    .setMaxResults(maxResults),
                new Feature()
                    .setType("LOGO_DETECTION")
                    .setMaxResults(1),
                new Feature()
                    .setType("TEXT_DETECTION")
                    .setMaxResults(maxResults),
                new Feature()
                    .setType("LANDMARK_DETECTION")
                    .setMaxResults(1)));


        final Vision.Images.Annotate annotate = vision.images().annotate(
                new BatchAnnotateImagesRequest().
                setRequests(ImmutableList.of(request)));

        final BatchAnnotateImagesResponse batchResponse = annotate.execute();
        //assert batchResponse.getResponses().size() == 1;

        System.out.println("Size of searches"+batchResponse.getResponses().size());

        //final AnnotateImageResponse response = batchResponse.getResponses().get(0);
        if (batchResponse.getResponses().get(0).getLabelAnnotations() != null) {

            final List<String> label = new ArrayList<String>();
            for (EntityAnnotation ea: batchResponse.getResponses().get(0).getLabelAnnotations()) {
                label.add(ea.getDescription());
            }
            labels.put("LABEL_ANNOTATION", label);
        }

        if (batchResponse.getResponses().get(0).getLandmarkAnnotations() != null) {
            final List<String> landMark = new ArrayList<String>();
            for (EntityAnnotation ea : batchResponse.getResponses().get(0).getLandmarkAnnotations()) {
                landMark.add(ea.getDescription());
            }
            labels.put("LANDMARK_ANNOTATION", landMark);
        }

        if (batchResponse.getResponses().get(0).getLogoAnnotations() != null) {
            final List<String> logo = new ArrayList<String>();
            for (EntityAnnotation ea : batchResponse.getResponses().get(0).getLogoAnnotations()) {
                logo.add(ea.getDescription());
            }
            labels.put("LOGO_ANNOTATION", logo);
        }

        if (batchResponse.getResponses().get(0).getTextAnnotations() != null) {

            List<String> text = new ArrayList<String>();
            for (EntityAnnotation ea : batchResponse.getResponses().get(0).getTextAnnotations()) {
                text.add(ea.getDescription());
            }
            labels.put("TEXT_ANNOTATION", text);
        }


        return labels;

    } catch (IOException e) {
        e.printStackTrace();
    }
      return null;

  }

  public static void printAnnotations(final List<EntityAnnotation> entityAnnotations) {

      if(entityAnnotations!=null && !entityAnnotations.isEmpty()) {
          for (final EntityAnnotation entityAnnotation : entityAnnotations) {
              final String desc = entityAnnotation.getDescription();
              final Float score = entityAnnotation.getScore();
              System.out.println(desc+"     "+score);
          }

      }
  }

  public static void main(String[] args) throws IOException, GeneralSecurityException {
        // TODO Auto-generated method stub
        final ImageAnalyzer analyzer = new ImageAnalyzer(getVisionService());
        final Map<String, List<String>> labels = analyzer.labelImage(IMAGE_PATH);

        for (Entry<String, List<String>> entry : labels.entrySet()) {

             final String key = entry.getKey();
             final List<String> value = entry.getValue();
             if(value!=null && !value.isEmpty()) {

             System.out.println("Printing for key"+key);
             for (final String myLebel : value) {

                 System.out.println(" "+myLebel);
               }

             }
        }

       //System.out.println(System.getenv("GOOGLE_APPLICATION_CREDENTIALS"))            
    }

}

谁能帮帮我?

【问题讨论】:

  • 只是为了在我将图像的背景变为白色视觉 api 时添加更多内容,确实返回了一些图像标签而不是空响应。
  • 在调用 Cloud Vision post api 时出现连接超时错误。我还设置了代理。它在邮递员客户端工具上运行良好。关于问题的任何想法?

标签: java image-recognition google-cloud-vision


【解决方案1】:

根据使用限制,图像必须小于 4MB...

https://cloud.google.com/vision/docs/image-best-practices#usage_limits

【讨论】:

  • 是的,图像大小为 1.47MB,所以应该不是问题。去除背景后图像大小为 94.3KB。
  • 移除背景后它仍然有效,对吧?这可能是一些奇怪的东西,例如加载完图像后> 4mb?我会尝试用不同尺寸测试相同的图像,看看有什么效果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多