Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Aniket025
GitHub Repository: Aniket025/Medical-Prescription-OCR
Path: blob/master/Model-3/Project/GCVocr.py
427 views
1
from __future__ import print_function
2
from base64 import b64encode
3
from os import makedirs
4
from os.path import join, basename
5
from sys import argv
6
import json
7
import requests
8
9
class GoogleCloudVisionOCR(object):
10
def __init__(self, filename):
11
self.ENDPOINT_URL = "https://vision.googleapis.com/v1/images:annotate"
12
self.RESULTS_DIR = 'jsons'
13
self.api_key = "AIzaSyCSMpzBIKlZObk8Uzkx6Iavo967m7vFf0Q"
14
self.filename = filename
15
16
def make_image_data_list():
17
"""
18
image_filename is the filename string
19
Returns a list of dicts formatted as the Vision API needs them to be
20
"""
21
img_requests = []
22
with open(self.filename, 'rb') as f:
23
ctxt = b64encode(f.read()).decode()
24
img_requests.append({'image': {'content': ctxt},'features': [{'type': 'TEXT_DETECTION','maxResults': 1}]})
25
return img_requests
26
27
def make_image_data():
28
"""Returns the image data lists as bytes"""
29
imgdict = make_image_data_list(self.filename)
30
return json.dumps({"requests": imgdict }).encode()
31
32
33
def request_ocr():
34
response = requests.post(ENDPOINT_URL, data=make_image_data(self.filename), params={'key': self.api_key}, headers={'Content-Type': 'application/json'})
35
return response
36
37