-
Notifications
You must be signed in to change notification settings - Fork 5
/
googleinteraction.py
66 lines (61 loc) · 1.48 KB
/
googleinteraction.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import base64
import json
import time
APIKEY = open('apiKey.txt','r').read()
def convertBase(image):
return base64.b64encode(open(image, "rb").read())
def genCurl(basefile):
a = 'curl -v -s -H "Content-Type: application/json" https://vision.googleapis.com/v1/images:annotate?key={} --data-binary @{}'.format(APIKEY, basefile)
return a
class genOCR(object):
def __init__(self):
self.Files = []
self.OCR = {}
self.command = '''
{
"requests": [
'''
def addImage(self, image):
self.Files.append(image)
addition = '''
{
"image": {
"content": "BASE"
},
"features": [
{
"type": "TEXT_DETECTION",
"maxResults": 1
},
]
},
'''
addition = addition.replace("BASE", convertBase(image))
self.command = self.command + addition
def returnCommand(self, saveas="Request.tmp"):
responses= []
self.command = self.command + '''
]
}'''
out = open(saveas, 'w')
out.write(self.command)
out.close()
a = genCurl(saveas)
os.system("{} > tmp.json".format(a))
with open('tmp.json') as json_data:
d = json.load(json_data)
for i, lines in enumerate(d['responses']):
try:
self.OCR[self.Files[i]] = lines['textAnnotations'][0]['description']
except:
self.OCR[self.Files[i]] = ''
return self.OCR
'''start = time.time()
a = genOCR()
for i in range(1,16):
a.addImage("all_american_rejects_move_along/{}.jpg".format(i))
for key, value in a.returnCommand().iteritems():
print key, value
end = time.time()
print(end - start)'''