Script to run Google VisionΒΆ

#!/usr/bin/env python

# Run Google OCR on the Ben Nevis sample image

import pickle

from google.cloud import vision
from google.cloud.vision import types
from google.protobuf import json_format

# Load the jpeg
with open("../../../samples/1901-01.jpg",'rb') as jf:
    ie=jf.read()

# Analyze the document
image = types.Image(content=ie)
client = vision.ImageAnnotatorClient()
response = client.document_text_detection(image=image)
document = response.full_text_annotation

# Save the resulting JSON
pickle.dump(document, open( "detection.pkl", "wb" ) )
with open('detection.txt', 'w') as file:
     file.write(json_format.MessageToJson(document))