Skip to content

Instantly share code, notes, and snippets.

@rlskoeser
Last active March 29, 2021 13:57
Show Gist options
  • Save rlskoeser/4cc5f2bb6c2da67da3fd0036e74166b8 to your computer and use it in GitHub Desktop.
Save rlskoeser/4cc5f2bb6c2da67da3fd0036e74166b8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# pip install google-cloud-vision
# Follow quick start instructions to set up access to the API and authentication
# https://pypi.org/project/google-cloud-vision/
import glob
import io
import os
from google.cloud import vision
IMAGE_DIR = 'images'
def get_image_ocr():
# Instantiate google vision client
client = vision.ImageAnnotatorClient()
for imagefile in glob.iglob('%s/*/*.jpg' % IMAGE_DIR):
basepath = os.path.splitext(imagefile)[0]
textfile = '%s.txt' % basepath
# if text file already exists, skip
if os.path.isfile(textfile):
continue
# if text file does not exist, request ocr
# Load the image into memory
with io.open(imagefile, 'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
# Performs ocr and handwriting detection on the image file
response = client.document_text_detection(image=image)
# save plain text output to local file;
# even if text is empty, create text file so we don't request again
with open(textfile, 'w') as textfilehandle:
textfilehandle.write(response.full_text_annotation.text)
if response.error.message:
raise Exception(
'{}\nFor more info on error messages, check: '
'https://cloud.google.com/apis/design/errors'.format(
response.error.message))
if __name__ == '__main__':
get_image_ocr()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment