【问题标题】:Google vision response labels not returning in raw JSON formatGoogle 视觉响应标签未以原始 JSON 格式返回
【发布时间】:2020-01-16 17:16:09
【问题描述】:

我试图让响应采用JSON 格式,但出于某种原因,Google 的示例代码在响应中抛出了一堆{}。使用下面的代码我可以获得响应,但它不是JSON 格式。

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 = "src/main/resources/city-park.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);

            System.out.println(response.toString());

        }
    }
}

下面是示例代码将打印的接近 JSON 但格式错误的内容。有没有办法让它正确格式化?

responses {
  label_annotations {
    mid: "/m/02l215"
    description: "Reflection"
    score: 0.9883928
    topicality: 0.9883928
  }
  label_annotations {
    mid: "/m/05h0n"
    description: "Nature"
    score: 0.98085856
    topicality: 0.98085856
  }
  label_annotations {
    mid: "/m/03d28y3"
    description: "Natural landscape"
    score: 0.9740803
    topicality: 0.9740803
  }
  label_annotations {
    mid: "/m/0838f"
    description: "Water"
    score: 0.9714835
    topicality: 0.9714835
  }
  label_annotations {
    mid: "/m/038hg"
    description: "Green"
    score: 0.9620494
    topicality: 0.9620494
  }
  label_annotations {
    mid: "/m/01bqvp"
    description: "Sky"
    score: 0.96003544
    topicality: 0.96003544
  }
  label_annotations {
    mid: "/m/015s2f"
    description: "Water resources"
    score: 0.9593428
    topicality: 0.9593428
  }
  label_annotations {
    mid: "/m/07j7r"
    description: "Tree"
    score: 0.9462387
    topicality: 0.9462387
  }
  label_annotations {
    mid: "/m/01fnns"
    description: "Vegetation"
    score: 0.9326158
    topicality: 0.9326158
  }
  label_annotations {
    mid: "/m/02yq2x"
    description: "Reflecting pool"
    score: 0.8776601
    topicality: 0.8776601
  }
}

编辑:我想快速编辑我的帖子,因为虽然这里的所有答案都很棒并且帮助我发现这是让它按我想要的方式工作的原因。
String theData = com.google.protobuf.util.JsonFormat.printer().print(response);

【问题讨论】:

    标签: java json google-cloud-vision google-vision json-serialization


    【解决方案1】:

    BatchAnnotateImagesResponse 类扩展了com.google.api.client.json.GenericJson,但默认情况下它不会生成JSON。您需要设置JsonFactory。您可以选择提供者:JacksonFactory、GsonFactory 或其他。之后,toString 方法应该生成有效的JSON。示例:

    response.setFactory(JacksonFactory.getDefaultInstance());
    response.toString();
    

    另见:

    【讨论】:

    • 花了很长时间试图弄清楚如何设置工厂。有没有地方我可以找到如何做到这一点的例子?对不起,我还在学习java。
    • @JamesMartin,看看这个codota - GenericJson.setFactory。例如,TestUtils
    猜你喜欢
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-11
    • 2021-10-19
    相关资源
    最近更新 更多