Created
March 27, 2020 18:18
-
-
Save alfredfrancis/d5f877f0db45155a67a0d8fa0d51936c to your computer and use it in GitHub Desktop.
Google Vision API OCR - Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
with open("yourfile.ext", "rb") as image_file: | |
encoded_string = base64.b64encode(image_file.read()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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