Skip to content

Instantly share code, notes, and snippets.

@alfredfrancis
Created March 27, 2020 18:18
Show Gist options
  • Save alfredfrancis/d5f877f0db45155a67a0d8fa0d51936c to your computer and use it in GitHub Desktop.
Save alfredfrancis/d5f877f0db45155a67a0d8fa0d51936c to your computer and use it in GitHub Desktop.
Google Vision API OCR - Python
import base64
with open("yourfile.ext", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
import requests
base64_image = ""
base64_image = base64_image.replace("^data:image\/(png|jpg|jpeg);base64,", "")
url = "https://vision.googleapis.com/v1/images:annotate?key=<GOOGLE API ACCESS TOKEN goes here>"
payload = {
"requests": [{"image": {"content": base64_image },
"features": [{"type": "TEXT_DETECTION"}]}]
}
headers = {
'Content-Type': 'application/json; charset=utf-8',
'Content-Type': 'text/plain'
}
response = requests.request("POST", url, headers=headers, json = payload)
print(response.text.encode('utf8'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment