Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Aniket025
GitHub Repository: Aniket025/Medical-Prescription-OCR
Path: blob/master/Model-4/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
file = open("output/words/normal/detection.txt","w")
46
list = glob.glob("/Users/Piyush_Jena/Documents/Opensoft/spell_correction/test_software/output/words/normal/*.jpg")
47
for i in range(0,len(list)):
48
image_filenames = list[i]
49
if not api_key or not image_filenames:
50
print("""Please supply an api key, then one or more image filenames $ python cloudvisreq.py image1.jpg image2.png""")
51
else:
52
response = request_ocr(api_key, image_filenames)
53
if response.status_code != 200 or response.json().get('error'):
54
print(response.text)
55
else:
56
for idx, resp in enumerate(response.json()['responses']):
57
# save to JSON file
58
imgname = image_filenames[idx]
59
jpath = join(RESULTS_DIR, basename(imgname) + '.json')
60
with open(jpath, 'w') as f:
61
datatxt = json.dumps(resp, indent=2)
62
print("Wrote", len(datatxt), "bytes to", jpath)
63
f.write(datatxt)
64
65
# print the plaintext to screen for convenience
66
print("---------------------------------------------")
67
t = resp['textAnnotations'][0]
68
print(" Bounding Polygon:")
69
print(t['boundingPoly'])
70
print(" Text:")
71
print(t['description'])
72
73
file.write(t['description'])
74
#print("---------------------------------------------")
75
76
#print(" Bounding Polygon:")
77
#print(t['boundingPoly'])
78
#print(" Text:")
79
#print(t['description'])
80
for file in list:
81
remove(file)
82
83