-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread.py
More file actions
46 lines (40 loc) · 1.39 KB
/
Copy pathread.py
File metadata and controls
46 lines (40 loc) · 1.39 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
38
39
40
41
42
43
44
45
46
import httplib, urllib, base64
import config
import json
from pprint import pprint
def read(filepath):
file = open(filepath, 'rb').read()
files = {'file': file}
headers = {
# Request headers. Replace the key below with your subscription key.
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': config.api_key
}
params = urllib.urlencode({
# Request parameters. The language setting "unk" means automatically detect the language.
'language': 'unk',
'detectOrientation ': 'true',
})
# Replace the three dots below with the URL of a JEPG image containing text.
body = file
try:
conn = httplib.HTTPSConnection('westus.api.cognitive.microsoft.com')
conn.request("POST", "/vision/v1.0/ocr?%s" % params, body, headers)
response = conn.getresponse()
data = response.read()
json_data = json.loads(data)
pprint(json_data)
lines = json_data['regions'][0]['lines']
text = []
sentence = ""
for line in lines:
words = line['words']
for word in words:
text.append(str(word['text']))
#print(word['text'])
sentence = " ".join(text)
print sentence
conn.close()
return sentence
except Exception as e:
print("[Errno {0}] {1}".format(e.message, e.message))