Search code examples
javajsongoogle-cloud-visiongoogle-visionjson-serialization

Google vision response labels not returning in raw JSON format


I'm trying to get the response to be in the JSON format and for some reason Google's sample code throws a bunch of {} in the response. Using the code below I can get the response but its not in JSON format.

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

        }
    }
}

Here is what the sample code will print that is close to JSON but formatted wrong. Is there anyway to get it formatted correctly?

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

Edit: I wanted to make a quick edit to my post because while all the answers here are great and helped I found that this was what made it work the way I wanted.
String theData = com.google.protobuf.util.JsonFormat.printer().print(response);


Solution

  • BatchAnnotateImagesResponse class extends com.google.api.client.json.GenericJson but it does not generate JSON by default. You need to set JsonFactory. You can choose provider: JacksonFactory, GsonFactory or other. After that, toString method should generate valid JSON. Example:

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

    See also: