Google Vision Java客户端批处理请求-响应标识

Google Vision Java客户端批处理请求-响应标识,java,google-cloud-platform,bulk,google-cloud-vision,Java,Google Cloud Platform,Bulk,Google Cloud Vision,我正在尝试向Google Vision文本检测API发出批处理请求。到目前为止,我将图像的路径放入一个列表中,发出批处理请求并获得响应。但是,我无法确定哪个结果属于哪个图像。为此,我尝试在请求中输入一个ID,当我得到结果时,我会比较ID。但是,我不能将任何自定义字段放入请求中。我的方法有问题吗?我如何知道哪个响应属于哪个图像 以下是我用于这些请求的代码: private Vision vision; private static final String APPLICATION_NAME = "

我正在尝试向Google Vision文本检测API发出批处理请求。到目前为止,我将图像的路径放入一个列表中,发出批处理请求并获得响应。但是,我无法确定哪个结果属于哪个图像。为此,我尝试在请求中输入一个ID,当我得到结果时,我会比较ID。但是,我不能将任何自定义字段放入请求中。我的方法有问题吗?我如何知道哪个响应属于哪个图像

以下是我用于这些请求的代码:

private Vision vision;
private static final String APPLICATION_NAME = "ProjectName";

public static Vision getVisionService() throws IOException, GeneralSecurityException {

    GoogleCredential credential = GoogleCredential.fromStream
            (new FileInputStream("/project-key.json"))
            .createScoped(VisionScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    return new Vision.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, credential)
            .setApplicationName(APPLICATION_NAME)
            .build();
}
/**
 * Gets up to {@code maxResults} text annotations for images stored at {@code paths}.
 */
public List<String> detectText(List<Path> paths) {
    ImmutableList.Builder<AnnotateImageRequest> requests = ImmutableList.builder();

    try {
        for (Path path : paths) {
            byte[] data;
            data = Files.readAllBytes(path);
            requests.add(
                    new AnnotateImageRequest()
                    .setImage(new Image().encodeContent(data))
                    .setFeatures(ImmutableList.of(
                            new Feature()
                            .setType("TEXT_DETECTION")
                            .setMaxResults(10))));
        }

        Vision.Images.Annotate annotate =
                vision.images()
                .annotate(new BatchAnnotateImagesRequest().setRequests(requests.build()));
        // Due to a bug: requests to Vision API containing large images fail when GZipped.
        annotate.setDisableGZipContent(true);
        BatchAnnotateImagesResponse batchResponse = annotate.execute();
        assert batchResponse.getResponses().size() == paths.size();

        List<String> output = new ArrayList();

        for (int i = 0; i < paths.size(); i++) {
            AnnotateImageResponse response = batchResponse.getResponses().get(i);
            if(response != null && response.getTextAnnotations() != null){
                System.out.println(response.toString());
                String result = getDescriptionFromJson(response.getTextAnnotations().toString());
                System.out.println(response.get("customField"));
                output.add(result);
            }
        }
        return output;
    } catch (IOException ex) {
        System.out.println("Exception occured: " + ex);
        return null;
    }
}

public String getDescriptionFromJson(String json){
    JSONArray results = new JSONArray(json);
    JSONObject result = (JSONObject) results.get(0);
    return result.getString("description");
}

public static void main(String[] args) {
    GoogleVisionQueryHelper g = new GoogleVisionQueryHelper();

    try {
        g.vision = getVisionService();
        List<Path> paths = new ArrayList<>();

        String directory = "/images";

        File[] files = new File(directory).listFiles();
        for(File file : files){
          if(file.isFile() && !file.getName().contains("DS_Store") && !file.getName().startsWith(".")){
            System.out.println(file.getAbsolutePath());
            paths.add(Paths.get(file.getAbsolutePath()));
          }
        }           
        System.out.println("Starting...");

        for(String s: g.detectText(paths)){
            System.out.println(s);
        }
    } catch (IOException | GeneralSecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
私人视野;
私有静态最终字符串应用程序\u NAME=“ProjectName”;
公共静态Vision getVisionService()引发IOException、GeneralSecurityException{
GoogleCredential credential=GoogleCredential.fromStream
(新文件输入流(“/project key.json”))
.createScope(VisionScopes.all());
JsonFactory JsonFactory=JacksonFactory.getDefaultInstance();
返回新的Vision.Builder(GoogleNetHttpTransport.newTrustedTransport(),jsonFactory,凭证)
.setApplicationName(应用程序名称)
.build();
}
/**
*获取存储在{@code path}上的图像的最多{@code maxResults}文本批注。
*/
公共列表detectText(列表路径){
ImmutableList.Builder请求=ImmutableList.Builder();
试一试{
用于(路径:路径){
字节[]数据;
数据=文件。readAllBytes(路径);
请求。添加(
新注释的ImageRequest()
.setImage(新图像().encodeContent(数据))
.setFeatures(不可修改的列表)(
新功能()
.setType(“文本检测”)
.setMaxResults(10));
}
Vision.Images.Annotate注释=
vision.images()
.annotate(新批处理AnnotateImagesRequest().setRequests(requests.build());
//由于一个bug:gzip时,对包含大图像的Vision API的请求失败。
annotate.setDisablegzip内容(true);
BatchAnnotateImagesResponse batchResponse=annotate.execute();
断言batchResponse.getResponses().size()==path.size();
列表输出=新的ArrayList();
对于(int i=0;i
BatchAnnotateImageResponse以与BatchAnnotateImageRequest中的请求列表相同的顺序保存响应列表

你怎么知道的?嗨,兄弟,你能帮我个忙吗?我的程序将卡在
BatchAnnotateImagesResponse=vision.images().annotate(requestBatch).setDisablegzip内容(true).execute()中https://vision.googleapis.com
,而不是
https://vision.googleapis.com/v1/images:annotate?key=