Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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