Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Aniket025
GitHub Repository: Aniket025/Medical-Prescription-OCR
Path: blob/master/Model-5/google_ocr.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
from unidecode import unidecode
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
def remove_non_ascii(text):
43
return unidecode(unicode(text, encoding = "utf-8"))
44
45
def convert(filename):
46
api_key = "AIzaSyCSMpzBIKlZObk8Uzkx6Iavo967m7vFf0Q"
47
image_filename = filename
48
entities = []
49
bBoxes = []
50
if not api_key or not image_filename:
51
print("""Please supply a valid Google Cloud Vision api key. Follow this link for details""")
52
print("""https://cloud.google.com/vision/""")
53
else:
54
response = request_ocr(api_key, image_filename)
55
if response.status_code != 200 or response.json().get('error'):
56
print(response.text)
57
else:
58
for i in range(1,len(response.json()['responses'][0]['textAnnotations'])):
59
entities.append(remove_non_ascii(response.json()['responses'][0]['textAnnotations'][i]['description'].encode("utf-8")))
60
for i in range(1,len(response.json()['responses'][0]['textAnnotations'])):
61
bBoxes.append(response.json()['responses'][0]['textAnnotations'][i]['boundingPoly']['vertices'])
62
return entities,bBoxes
63
64
if __name__ == "__main__":
65
convert(argv[1])
66
67