Search code examples
google-cloud-platformocrgoogle-cloud-vision

How to save the google vision API response to a handable file


I am trying to extract all the information that the google cloud vision OCR API offers to me from some tickets. I can save the text in a .txt but the rest of the response response = client.text_detection(image=image) I don't know how to save it.

Thanks


Solution

  • I followed this tutorial Quickstart: Using client libraries.

    Then, assuming that you got the response, you can save it to a file using this code:

    text_file = open("sample.txt", "w")
    text_file.write(str(response))
    text_file.close()
    

    Finally I tested on console:

    cat sample.txt
    
    label_annotations {
      mid: "/m/01yrx"
      description: "Cat"
      score: 0.9895679950714111
      topicality: 0.9895679950714111
    }
    label_annotations {
      mid: "/m/0307l"
      description: "Felidae"
      score: 0.9541760087013245
      topicality: 0.9541760087013245
    }
    label_annotations {
      mid: "/m/01l7qd"
      description: "Whiskers"
      score: 0.953809380531311
      topicality: 0.953809380531311
    }
    label_annotations {
      mid: "/m/01k74n"
      description: "Facial expression"
      score: 0.9447915554046631
      topicality: 0.9447915554046631
    }