-
Notifications
You must be signed in to change notification settings - Fork 66
/
google_vision.py
73 lines (64 loc) · 2.71 KB
/
google_vision.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# API Key for Google Vision (Cloud) = AIzaSyDInxyryEGaQGWGi1nEme5TxFgjEK8DOQs (unrestricted access)
from base64 import b64encode
from os import makedirs
from os.path import join, basename
from sys import argv
import json
import requests
ENDPOINT_URL = 'https://vision.googleapis.com/v1/images:annotate'
RESULTS_DIR = 'jsons'
makedirs(RESULTS_DIR, exist_ok=True)
def make_image_data_list(image_filenames):
"""
image_filenames is a list of filename strings
Returns a list of dicts formatted as the Vision API
needs them to be
"""
img_requests = []
for imgname in image_filenames:
with open(imgname, 'rb') as f:
ctxt = b64encode(f.read()).decode()
img_requests.append({
'image': {'content': ctxt},
'features': [{
'type': 'TEXT_DETECTION',
'maxResults': 1
}]
})
return img_requests
def make_image_data(image_filenames):
"""Returns the image data lists as bytes"""
imgdict = make_image_data_list(image_filenames)
return json.dumps({"requests": imgdict }).encode()
def request_ocr(api_key, image_filenames):
response = requests.post(ENDPOINT_URL,
data=make_image_data(image_filenames),
params={'key': api_key},
headers={'Content-Type': 'application/json'})
return response
if __name__ == '__main__':
api_key, *image_filenames = argv[1:]
if not api_key or not image_filenames:
print("""
Please supply an api key, then one or more image filenames
$ python google_vision.py api_key image1.jpg image2.png""")
else:
response = request_ocr(api_key, image_filenames)
if response.status_code != 200 or response.json().get('error'):
print(response.text)
else:
for idx, resp in enumerate(response.json()['responses']):
# save to JSON file
imgname = image_filenames[idx]
jpath = join(RESULTS_DIR, basename(imgname) + '.json')
with open(jpath, 'w') as f:
datatxt = json.dumps(resp, indent=2)
print("Wrote", len(datatxt), "bytes to", jpath)
f.write(datatxt)
# print the plaintext to screen for convenience
print("---------------------------------------------")
t = resp['textAnnotations'][0]
print(" Bounding Polygon:")
print(t['boundingPoly'])
print(" Text:")
print(t['description'])