Path: blob/master/Model-5/google_ocr.py
427 views
from __future__ import print_function1from base64 import b64encode2from os import makedirs, remove3from os.path import join, basename4from sys import argv5import json6import requests7import glob8from unidecode import unidecode910ENDPOINT_URL = 'https://vision.googleapis.com/v1/images:annotate'11RESULTS_DIR = 'jsons'1213def make_image_data_list(image_filenames):14"""15image_filenames is a list of filename strings16Returns a list of dicts formatted as the Vision API17needs them to be18"""19img_requests = []20with open(image_filenames, 'rb') as f:21ctxt = b64encode(f.read()).decode()22img_requests.append({23'image': {'content': ctxt},24'features': [{25'type': 'TEXT_DETECTION',26'maxResults': 127}]28})29return img_requests3031def make_image_data(image_filenames):32"""Returns the image data lists as bytes"""33imgdict = make_image_data_list(image_filenames)34return json.dumps({"requests": imgdict }).encode()353637def request_ocr(api_key, image_filenames):38response = requests.post(ENDPOINT_URL, data=make_image_data(image_filenames), params={'key': api_key}, headers={'Content-Type': 'application/json'})39return response4041def remove_non_ascii(text):42return unidecode(unicode(text, encoding = "utf-8"))4344def convert(filename):45api_key = "AIzaSyCSMpzBIKlZObk8Uzkx6Iavo967m7vFf0Q"46image_filename = filename47entities = []48bBoxes = []49if not api_key or not image_filename:50print("""Please supply a valid Google Cloud Vision api key. Follow this link for details""")51print("""https://cloud.google.com/vision/""")52else:53response = request_ocr(api_key, image_filename)54if response.status_code != 200 or response.json().get('error'):55print(response.text)56else:57for i in range(1,len(response.json()['responses'][0]['textAnnotations'])):58entities.append(remove_non_ascii(response.json()['responses'][0]['textAnnotations'][i]['description'].encode("utf-8")))59for i in range(1,len(response.json()['responses'][0]['textAnnotations'])):60bBoxes.append(response.json()['responses'][0]['textAnnotations'][i]['boundingPoly']['vertices'])61return entities,bBoxes6263if __name__ == "__main__":64convert(argv[1])656667