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