-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleCloudVision.py
More file actions
37 lines (29 loc) · 1 KB
/
GoogleCloudVision.py
File metadata and controls
37 lines (29 loc) · 1 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
import json
import requests
from base64 import b64encode
def makeImageData(imgpath):
img_req = None
with open(imgpath, 'rb') as f:
ctxt = b64encode(f.read()).decode()
img_req = {
'image': {
'content': ctxt
},
'features': [{
'type': 'DOCUMENT_TEXT_DETECTION',
'maxResults': 1
}]
}
return json.dumps({"requests": img_req}).encode()
def requestOCR(url, api_key, imgpath):
imgdata = makeImageData(imgpath)
response = requests.post(url,
data = imgdata,
params = {'key': api_key},
headers = {'Content-Type': 'application/json'})
return response
ENDPOINT_URL = 'https://vision.googleapis.com/v1/images:annotate'
api_key = "AIzaSyA_DOnmygw3L2z4qkkhJSrGxuYVo7kWk54"
img_loc = "0.png"
data = json.loads(requestOCR(ENDPOINT_URL,api_key,img_loc ).text)
print(data["responses"][0]['fullTextAnnotation']['text'])