-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetting_ocr.py
More file actions
86 lines (73 loc) · 2.87 KB
/
getting_ocr.py
File metadata and controls
86 lines (73 loc) · 2.87 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
__author__ = "Abhijay, Neeraj and Nitish"
__credits__ = ["Abhijay, Neeraj and Nitish"]
__license__ = "GPL"
__version__ = "1.0.1"
__maintainer__ = "Nitish (Git: @nitish11)"
__status__ = "Development"
from base64 import b64encode
from os import makedirs
from os.path import join, basename
from sys import argv
import json
import requests
import os
ENDPOINT_URL = 'https://vision.googleapis.com/v1/images:annotate'
RESULTS_DIR = 'jsons'
if not os.path.exists(RESULTS_DIR):
makedirs(RESULTS_DIR)
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
def get_tag_information(image_filenames):
# api_key = 'AIzaSyC2Z6fymVj-StfrX7AeTJKp3l7Mo0ELc7I'
api_key = 'AIzaSyCzemskXK5JabNNLy6fnwMD-hQfws0jFE4'
if not api_key or not image_filenames:
print("""
Please supply an api key, then one or more image filenames
$ python cloudvisreq.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'])
return