java - 从 TEXT_DETECTION 返回的 JSON 文件格式不正确

标签 java json google-cloud-platform google-vision

我按照 Google Vision API 的文档编写了一个在图像上运行 TEXT_DETECTION 的程序。与文档和 Google Vision 网站上的测试人员返回格式正确的 JSON 不同,我返回的 JSON 文件在对象之间缺少逗号,并且缺少 JSONArray 的括号。

我使用以下行来获取 JSON 响应。

BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);

这是我的 TEXT_DETECTION 代码。

    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();

这是我从 BatchAnnotateImagesResponse 中得到的回复的片段。

 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
      }
    }
  }

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

最佳答案

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

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

关于java - 从 TEXT_DETECTION 返回的 JSON 文件格式不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57439013/

相关文章:

php - 如何将字符串响应转换为 JSON 对象?

sql - 在 json 数组列上加入 Postgresql 表

node.js - NPM 从 GCP App Engine 中的 Google Cloud Repository 安装

java - 如何设置ActiveMQ停止接受消息?

java - 将 2 维数组中的一行的 MIN 和 MAX 值存储在 1 维数组中?

java - 希望重新订购 map

javascript - 如何处理react.js中缺失的字段

tensorflow - 在 Datalab 中使用 Python3,我无法将表示 Google Cloud Storage 存储桶中文件的字符串列表作为带有 tensorflow 的 feed_dict

firebase - Firebase Cloud Functions 中的返回值重要吗

java - 转储部分 Java Flight Record