Google Cloud Vision API. Golang . How to get API JSON











up vote
1
down vote

favorite












I use Google Cloud Vision API with the Go SDK.
In some cases I don't want to use Golang structures to read API results, I just want to get full JSON response of an API call. For example,



// detectDocumentText gets the full document text from the Vision API for an
// image at the given file path.
func detectDocumentTextURI(w io.Writer, file string) error {
ctx := context.Background()

client, err := vision.NewImageAnnotatorClient(ctx)
if err != nil {
return err
}

image := vision.NewImageFromURI(file)
annotation, err := client.DetectDocumentText(ctx, image, nil)
if err != nil {
return err
}

if annotation == nil {
fmt.Fprintln(w, "No text found.")
} else {
fmt.Fprintf(w, "API Response %s", ...JSON...)
}

return nil
}


How can I get that JSON from annotation structure? Is it possible?










share|improve this question




























    up vote
    1
    down vote

    favorite












    I use Google Cloud Vision API with the Go SDK.
    In some cases I don't want to use Golang structures to read API results, I just want to get full JSON response of an API call. For example,



    // detectDocumentText gets the full document text from the Vision API for an
    // image at the given file path.
    func detectDocumentTextURI(w io.Writer, file string) error {
    ctx := context.Background()

    client, err := vision.NewImageAnnotatorClient(ctx)
    if err != nil {
    return err
    }

    image := vision.NewImageFromURI(file)
    annotation, err := client.DetectDocumentText(ctx, image, nil)
    if err != nil {
    return err
    }

    if annotation == nil {
    fmt.Fprintln(w, "No text found.")
    } else {
    fmt.Fprintf(w, "API Response %s", ...JSON...)
    }

    return nil
    }


    How can I get that JSON from annotation structure? Is it possible?










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I use Google Cloud Vision API with the Go SDK.
      In some cases I don't want to use Golang structures to read API results, I just want to get full JSON response of an API call. For example,



      // detectDocumentText gets the full document text from the Vision API for an
      // image at the given file path.
      func detectDocumentTextURI(w io.Writer, file string) error {
      ctx := context.Background()

      client, err := vision.NewImageAnnotatorClient(ctx)
      if err != nil {
      return err
      }

      image := vision.NewImageFromURI(file)
      annotation, err := client.DetectDocumentText(ctx, image, nil)
      if err != nil {
      return err
      }

      if annotation == nil {
      fmt.Fprintln(w, "No text found.")
      } else {
      fmt.Fprintf(w, "API Response %s", ...JSON...)
      }

      return nil
      }


      How can I get that JSON from annotation structure? Is it possible?










      share|improve this question















      I use Google Cloud Vision API with the Go SDK.
      In some cases I don't want to use Golang structures to read API results, I just want to get full JSON response of an API call. For example,



      // detectDocumentText gets the full document text from the Vision API for an
      // image at the given file path.
      func detectDocumentTextURI(w io.Writer, file string) error {
      ctx := context.Background()

      client, err := vision.NewImageAnnotatorClient(ctx)
      if err != nil {
      return err
      }

      image := vision.NewImageFromURI(file)
      annotation, err := client.DetectDocumentText(ctx, image, nil)
      if err != nil {
      return err
      }

      if annotation == nil {
      fmt.Fprintln(w, "No text found.")
      } else {
      fmt.Fprintf(w, "API Response %s", ...JSON...)
      }

      return nil
      }


      How can I get that JSON from annotation structure? Is it possible?







      go google-cloud-vision






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 14:30









      Tyler Bui-Palsulich

      5318




      5318










      asked Nov 12 at 10:03









      Roman Gelembjuk

      6752732




      6752732
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Is there something in particular you're looking for in the JSON? You could pretty-print the response object as JSON, if you trying to explore what's returned:



          json, err := json.MarshalIndent(annotation, "", "  ")
          if err != nil {
          log.Fatal(err)
          }

          fmt.Println(string(json))


          It's a bit difficult to get the raw JSON response from this call because under the hood it's using gRPC, not JSON. If you follow the client code for a bit (it's open source), you'll eventually get to https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/vision/apiv1/image_annotator_client.go#L142:



          func (c *ImageAnnotatorClient) BatchAnnotateImages(ctx context.Context, req *visionpb.BatchAnnotateImagesRequest, opts ...gax.CallOption) (*visionpb.BatchAnnotateImagesResponse, error) 


          You can see that function build up the request, send it, and return the response (the same proto response you get from calling the original function, restricted to res.FullTextAnnotation). See https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/vision/apiv1/client.go#L109.






          share|improve this answer























          • Thanks. If there is gRPC used it is not possible to get JSON than . I have to make a JSON manually.
            – Roman Gelembjuk
            Nov 13 at 7:45










          • Yes. I updated my answer with a snippet to pretty print JSON for the annotation response.
            – Tyler Bui-Palsulich
            Nov 13 at 15:06











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53259815%2fgoogle-cloud-vision-api-golang-how-to-get-api-json%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          Is there something in particular you're looking for in the JSON? You could pretty-print the response object as JSON, if you trying to explore what's returned:



          json, err := json.MarshalIndent(annotation, "", "  ")
          if err != nil {
          log.Fatal(err)
          }

          fmt.Println(string(json))


          It's a bit difficult to get the raw JSON response from this call because under the hood it's using gRPC, not JSON. If you follow the client code for a bit (it's open source), you'll eventually get to https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/vision/apiv1/image_annotator_client.go#L142:



          func (c *ImageAnnotatorClient) BatchAnnotateImages(ctx context.Context, req *visionpb.BatchAnnotateImagesRequest, opts ...gax.CallOption) (*visionpb.BatchAnnotateImagesResponse, error) 


          You can see that function build up the request, send it, and return the response (the same proto response you get from calling the original function, restricted to res.FullTextAnnotation). See https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/vision/apiv1/client.go#L109.






          share|improve this answer























          • Thanks. If there is gRPC used it is not possible to get JSON than . I have to make a JSON manually.
            – Roman Gelembjuk
            Nov 13 at 7:45










          • Yes. I updated my answer with a snippet to pretty print JSON for the annotation response.
            – Tyler Bui-Palsulich
            Nov 13 at 15:06















          up vote
          0
          down vote













          Is there something in particular you're looking for in the JSON? You could pretty-print the response object as JSON, if you trying to explore what's returned:



          json, err := json.MarshalIndent(annotation, "", "  ")
          if err != nil {
          log.Fatal(err)
          }

          fmt.Println(string(json))


          It's a bit difficult to get the raw JSON response from this call because under the hood it's using gRPC, not JSON. If you follow the client code for a bit (it's open source), you'll eventually get to https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/vision/apiv1/image_annotator_client.go#L142:



          func (c *ImageAnnotatorClient) BatchAnnotateImages(ctx context.Context, req *visionpb.BatchAnnotateImagesRequest, opts ...gax.CallOption) (*visionpb.BatchAnnotateImagesResponse, error) 


          You can see that function build up the request, send it, and return the response (the same proto response you get from calling the original function, restricted to res.FullTextAnnotation). See https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/vision/apiv1/client.go#L109.






          share|improve this answer























          • Thanks. If there is gRPC used it is not possible to get JSON than . I have to make a JSON manually.
            – Roman Gelembjuk
            Nov 13 at 7:45










          • Yes. I updated my answer with a snippet to pretty print JSON for the annotation response.
            – Tyler Bui-Palsulich
            Nov 13 at 15:06













          up vote
          0
          down vote










          up vote
          0
          down vote









          Is there something in particular you're looking for in the JSON? You could pretty-print the response object as JSON, if you trying to explore what's returned:



          json, err := json.MarshalIndent(annotation, "", "  ")
          if err != nil {
          log.Fatal(err)
          }

          fmt.Println(string(json))


          It's a bit difficult to get the raw JSON response from this call because under the hood it's using gRPC, not JSON. If you follow the client code for a bit (it's open source), you'll eventually get to https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/vision/apiv1/image_annotator_client.go#L142:



          func (c *ImageAnnotatorClient) BatchAnnotateImages(ctx context.Context, req *visionpb.BatchAnnotateImagesRequest, opts ...gax.CallOption) (*visionpb.BatchAnnotateImagesResponse, error) 


          You can see that function build up the request, send it, and return the response (the same proto response you get from calling the original function, restricted to res.FullTextAnnotation). See https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/vision/apiv1/client.go#L109.






          share|improve this answer














          Is there something in particular you're looking for in the JSON? You could pretty-print the response object as JSON, if you trying to explore what's returned:



          json, err := json.MarshalIndent(annotation, "", "  ")
          if err != nil {
          log.Fatal(err)
          }

          fmt.Println(string(json))


          It's a bit difficult to get the raw JSON response from this call because under the hood it's using gRPC, not JSON. If you follow the client code for a bit (it's open source), you'll eventually get to https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/vision/apiv1/image_annotator_client.go#L142:



          func (c *ImageAnnotatorClient) BatchAnnotateImages(ctx context.Context, req *visionpb.BatchAnnotateImagesRequest, opts ...gax.CallOption) (*visionpb.BatchAnnotateImagesResponse, error) 


          You can see that function build up the request, send it, and return the response (the same proto response you get from calling the original function, restricted to res.FullTextAnnotation). See https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/vision/apiv1/client.go#L109.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 at 15:05

























          answered Nov 12 at 13:40









          Tyler Bui-Palsulich

          5318




          5318












          • Thanks. If there is gRPC used it is not possible to get JSON than . I have to make a JSON manually.
            – Roman Gelembjuk
            Nov 13 at 7:45










          • Yes. I updated my answer with a snippet to pretty print JSON for the annotation response.
            – Tyler Bui-Palsulich
            Nov 13 at 15:06


















          • Thanks. If there is gRPC used it is not possible to get JSON than . I have to make a JSON manually.
            – Roman Gelembjuk
            Nov 13 at 7:45










          • Yes. I updated my answer with a snippet to pretty print JSON for the annotation response.
            – Tyler Bui-Palsulich
            Nov 13 at 15:06
















          Thanks. If there is gRPC used it is not possible to get JSON than . I have to make a JSON manually.
          – Roman Gelembjuk
          Nov 13 at 7:45




          Thanks. If there is gRPC used it is not possible to get JSON than . I have to make a JSON manually.
          – Roman Gelembjuk
          Nov 13 at 7:45












          Yes. I updated my answer with a snippet to pretty print JSON for the annotation response.
          – Tyler Bui-Palsulich
          Nov 13 at 15:06




          Yes. I updated my answer with a snippet to pretty print JSON for the annotation response.
          – Tyler Bui-Palsulich
          Nov 13 at 15:06


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53259815%2fgoogle-cloud-vision-api-golang-how-to-get-api-json%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python