Java 文本检测返回的JSON文件格式不正确

Java 文本检测返回的JSON文件格式不正确,java,json,google-cloud-platform,google-vision,Java,Json,Google Cloud Platform,Google Vision,我遵循Google的Vision API文档编写了一个程序,在图像上运行文本检测。与网站上的文档和GoogleVision测试仪返回正确格式的JSON不同,我得到的JSON文件在对象之间缺少逗号,并且缺少JSONArray的括号 我使用下面的行来获取JSON响应 BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests); 这是我的文本检测代码 public static final Type

我遵循Google的Vision API文档编写了一个程序,在图像上运行文本检测。与网站上的文档和GoogleVision测试仪返回正确格式的JSON不同,我得到的JSON文件在对象之间缺少逗号,并且缺少JSONArray的括号

我使用下面的行来获取JSON响应

BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);
这是我的文本检测代码

    public static final Type SEARCH_TYPE = Type.TEXT_DETECTION;

    public static void main(String... args) throws Exception {
        try (ImageAnnotatorClient vision = ImageAnnotatorClient.create()) {
            String fileName = "resources/testPic.jpg";

            Path path = Paths.get(fileName);
            byte[] data = Files.readAllBytes(path);
            ByteString imgBytes = ByteString.copyFrom(data);
            List<AnnotateImageRequest> requests = new ArrayList<>();
            Image img = Image.newBuilder().setContent(imgBytes).build();
            Feature feat = Feature.newBuilder().setType(SEARCH_TYPE).build();
            AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
            requests.add(request);

            BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);
            List<AnnotateImageResponse> responses = response.getResponsesList();

如何更改或修复它,使返回的文件是格式正确的JSON文件?

使用annotate时,没有JSON文件。响应是一个BatchAnnotateImagesResponse对象

如果要生成json文件,可以使用。asyncBatchAnnotate完成后将json文件写入gcs存储桶。

您可以添加

com.google.protobuf.util.JsonFormat.printer().print(响应)

之后

List responses=response.getResponsesList()


这将为响应对象提供JSON字符串。

谢谢,我现在使用BatchAnnotateImagesResponse,然后从bucket下载JSON文件。您知道是否可以同时对bucket中的多个映像运行BatchAnnotateImagesResponse?它们可能都在一个目录中?是的,您可以通过向列表中添加另一个请求,同时对多个映像运行AsyncBatchAnnotated。图像可以位于您可以访问的任何存储桶中。图像可以位于同一目录中。必须明确指定每个图像。
 text_annotations {
    description: "almora"
    bounding_poly {
      vertices {
        x: 2307
        y: 713
      }
      vertices {
        x: 2480
        y: 711
      }
      vertices {
        x: 2480
        y: 727
      }
      vertices {
        x: 2307
        y: 729
      }
    }
  }
  text_annotations {
    description: "ryan"
    bounding_poly {
      vertices {
        x: 2458
        y: 906
      }
      vertices {
        x: 2489
        y: 933
      }
      vertices {
        x: 2476
        y: 947
      }
      vertices {
        x: 2446
        y: 920
      }
    }
  }
  text_annotations {
    description: "flanco"
    bounding_poly {
      vertices {
        x: 2441
        y: 890
      }
      vertices {
        x: 2458
        y: 905
      }
      vertices {
        x: 2443
        y: 922
      }
      vertices {
        x: 2426
        y: 907
      }
    }
  }
  text_annotations {
    description: "garbanzo beans"
    bounding_poly {
      vertices {
        x: 3780
        y: 1051
      }
      vertices {
        x: 3824
        y: 1050
      }
      vertices {
        x: 3824
        y: 1063
      }
      vertices {
        x: 3780
        y: 1064
      }
    }
  }
  text_annotations {
    description: "roberto"
    bounding_poly {
      vertices {
        x: 2111
        y: 906
      }
      vertices {
        x: 2163
        y: 905
      }
      vertices {
        x: 2163
        y: 920
      }
      vertices {
        x: 2111
        y: 921
      }
    }
  }
  text_annotations {
    description: "A10"
    bounding_poly {
      vertices {
        x: 2398
        y: 935
      }
      vertices {
        x: 2442
        y: 972
      }
      vertices {
        x: 2424
        y: 994
      }
      vertices {
        x: 2380
        y: 956
      }
    }
  }