-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_azure_api.py
140 lines (119 loc) · 4.55 KB
/
my_azure_api.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import time
import requests
import cv2
import operator
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
_url = 'https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/RecognizeText'
_key = "b4f1b6fcb14e447e82dc2aad77d25f76"
#Here you have to paste your primary key
_maxNumRetries = 10
def processRequest( json, data, headers, params ):
retries = 0
result = None
while True:
response = requests.post(_url, json = json, data = data, headers = headers, params = params )
if response.status_code == 429:
print( "Message: %s" % ( response.json() ) )
if retries <= _maxNumRetries:
time.sleep(1)
retries += 1
continue
else:
print( 'Error: failed after retrying!' )
break
elif response.status_code == 202:
result = response.headers['Operation-Location']
else:
print( "Error code: %d" % ( response.status_code ) )
print( "Message: %s" % ( response.json() ) )
break
return result
def getOCRTextResult( operationLocation, headers ):
retries = 0
result = None
while True:
response = requests.get(operationLocation, json=None, data=None, headers=headers, params=None)
if response.status_code == 429:
print("Message: %s" % (response.json()))
if retries <= _maxNumRetries:
time.sleep(1)
retries += 1
continue
else:
print('Error: failed after retrying!')
break
elif response.status_code == 200:
result = response.json()
else:
print("Error code: %d" % (response.status_code))
print("Message: %s" % (response.json()))
break
return result
def showResultinFile(result):
lines = result['recognitionResult']['lines']
for i in range(len(lines)):
words = lines[i]['words']
s = ""
for word in words:
s += word['text'] + " "
print(s)
def showResultOnImage( result, img ):
img = img[:, :, (2, 1, 0)]
fig, ax = plt.subplots(figsize=(12, 12))
ax.imshow(img, aspect='equal')
lines = result['recognitionResult']['lines']
for i in range(len(lines)):
words = lines[i]['words']
for j in range(len(words)):
tl = (words[j]['boundingBox'][0], words[j]['boundingBox'][1])
tr = (words[j]['boundingBox'][2], words[j]['boundingBox'][3])
br = (words[j]['boundingBox'][4], words[j]['boundingBox'][5])
bl = (words[j]['boundingBox'][6], words[j]['boundingBox'][7])
text = words[j]['text']
x = [tl[0], tr[0], tr[0], br[0], br[0], bl[0], bl[0], tl[0]]
y = [tl[1], tr[1], tr[1], br[1], br[1], bl[1], bl[1], tl[1]]
line = Line2D(x, y, linewidth=3.5, color='red')
ax.add_line(line)
ax.text(tl[0], tl[1] - 2, '{:s}'.format(text),
bbox=dict(facecolor='blue', alpha=0.5),
fontsize=14, color='white')
plt.axis('off')
plt.tight_layout()
plt.draw()
plt.show()
def text_from_image(img):
# pathToFileInDisk = r'/home/thebhatman/Downloads/69107836_1116226275432174_5239825646892351488_n.jpg'
# with open(pathToFileInDisk, 'rb') as f:
# data = f.read()
data = img.read()
params = {'mode' : 'Handwritten'}
headers = dict()
headers['Ocp-Apim-Subscription-Key'] = _key
headers['Content-Type'] = 'application/octet-stream'
json = None
operationLocation = processRequest(json, data, headers, params)
result = None
if (operationLocation != None):
headers = {}
headers['Ocp-Apim-Subscription-Key'] = _key
while True:
time.sleep(1)
result = getOCRTextResult(operationLocation, headers)
if result['status'] == 'Succeeded' or result['status'] == 'Failed':
break
text_file = []
if result is not None and result['status'] == 'Succeeded':
data8uint = np.fromstring(data, np.uint8)
img = cv2.cvtColor(cv2.imdecode(data8uint, cv2.IMREAD_COLOR), cv2.COLOR_BGR2RGB)
showResultinFile(result)
# showResultOnImage(result, img)
all_text = result['recognitionResult']['lines']
for i in range(len(all_text)):
one_line = all_text[i]['words']
each_line_as_string = ""
for word in one_line:
each_line_as_string += word['text'] + " "
text_file.append(each_line_as_string)
return text_file