1 |
|
#import httplib
|
2 |
|
import http.client
|
3 |
|
import urllib
|
4 |
|
import base64
|
5 |
|
import json
|
6 |
|
import cv2
|
7 |
|
import numpy as np
|
8 |
|
import matplotlib.pyplot as plt
|
9 |
|
import matplotlib.image as mpimg
|
10 |
|
from PIL import Image
|
11 |
|
from io import BytesIO
|
12 |
|
|
13 |
|
|
14 |
|
def url_to_image(url):
|
15 |
|
# download the image, convert it to a NumPy array, and then read
|
16 |
|
# it into OpenCV format
|
17 |
|
resp = urllib.request.urlopen(url)
|
18 |
|
#resp = urllib.urlopen(url)
|
19 |
|
image = np.asarray(bytearray(resp.read()), dtype="uint8")
|
20 |
|
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
|
21 |
|
# return the image
|
22 |
|
return image
|
23 |
|
|
24 |
|
def getRollbackRotateCodeByOrientation(orientation):
|
25 |
|
rotatedAngle = None
|
26 |
|
if orientation == "Left":
|
27 |
|
rotatedAngle = cv2.ROTATE_90_COUNTERCLOCKWISE
|
28 |
|
elif orientation == "Right":
|
29 |
|
rotatedAngle = cv2.ROTATE_90_CLOCKWISE
|
30 |
|
else:
|
31 |
|
rotatedAngle = cv2.ROTATE_180
|
32 |
|
return rotatedAngle
|
33 |
|
|
34 |
|
def getRotateCodeByOrientation(orientation):
|
35 |
|
rotatedAngle = None
|
36 |
|
if orientation == "Left":
|
37 |
|
rotatedAngle = cv2.ROTATE_90_CLOCKWISE
|
38 |
|
elif orientation == "Right":
|
39 |
|
rotatedAngle = cv2.ROTATE_90_COUNTERCLOCKWISE
|
40 |
|
else:
|
41 |
|
rotatedAngle = cv2.ROTATE_180
|
42 |
|
return rotatedAngle
|
43 |
|
|
44 |
|
|
45 |
|
####################################
|
46 |
|
|
47 |
|
###############################################
|
48 |
|
#### Update or verify the following values. ###
|
49 |
|
###############################################
|
50 |
|
|
51 |
|
# Replace the subscription_key string value with your valid subscription key.
|
52 |
|
#subscription_key = '4774f949a51e4728b436d05a0d0415c2' # zombie612
|
53 |
|
#subscription_key = 'ef94d01a99854539a72b5696118b4ac9' # kyusu99
|
54 |
|
subscription_key = '828669ef78ae40959882ff4a897fb1a4' # master
|
55 |
|
|
56 |
|
# Replace or verify the region.
|
57 |
|
#
|
58 |
|
# You must use the same region in your REST API call as you used to obtain your
|
59 |
|
# subscription keys.
|
60 |
|
# For example, if you obtained your subscription keys from the westus region,
|
61 |
|
# replace
|
62 |
|
# "westcentralus" in the URI below with "westus".
|
63 |
|
#
|
64 |
|
# NOTE: Free trial subscription keys are generated in the westcentralus region,
|
65 |
|
# so if you are using
|
66 |
|
# a free trial subscription key, you should not need to change this region.
|
67 |
|
#uri_base = 'westcentralus.api.cognitive.microsoft.com' # USA
|
68 |
|
#uri_base = 'eastasia.api.cognitive.microsoft.com' # east asia for master
|
69 |
|
uri_base = 'southeastasia.api.cognitive.microsoft.com' # southeast asia for master
|
70 |
|
headers = {
|
71 |
|
# Request headers.
|
72 |
|
#'Content-Type': 'application/json',
|
73 |
|
'Content-Type': 'application/octet-stream',
|
74 |
|
'Ocp-Apim-Subscription-Key': subscription_key,
|
75 |
|
}
|
76 |
|
|
77 |
|
#params = urllib.urlencode({
|
78 |
|
params = urllib.parse.urlencode({
|
79 |
|
# Request parameters. The language setting "unk" means automatically
|
80 |
|
'handwriting': 'false'
|
81 |
|
})
|
82 |
|
|
83 |
|
# The URL of a JPEG image containing text.
|
84 |
|
#body = "{'url':'D:/Visual Studio Project/PyOpenCv/PyOpenCv/res/symbol1.png'}"
|
85 |
|
#body =
|
86 |
|
#"{'url':'https://upload.wikimedia.org/wikipedia/commons/thumb/a/af/Atomist_quote_from_Democritus.png/338px-Atomist_quote_from_Democritus.png'}"
|
87 |
|
#body = "{'url':'http://cfile9.uf.tistory.com/image/99AFE1505A322DBD114994'}"
|
88 |
|
|
89 |
|
def detectAndRemoveText(image):
|
90 |
|
tmp = image.copy()
|
91 |
|
img = np.array(tmp)
|
92 |
|
try:
|
93 |
|
tmp = image.copy()
|
94 |
|
bin_img = BytesIO()
|
95 |
|
tmp.save(bin_img, format='PNG')
|
96 |
|
tmp.close()
|
97 |
|
|
98 |
|
img_data = bin_img.getvalue()
|
99 |
|
bin_img.close()
|
100 |
|
|
101 |
|
#region REST API with raw file
|
102 |
|
# Execute the REST API call and get the response.
|
103 |
|
conn = http.client.HTTPSConnection(uri_base)
|
104 |
|
#conn = httplib.HTTPSConnection(uri_base)
|
105 |
|
conn.request("POST", "/vision/v1.0/recognizeText?%s" % params, img_data, headers)
|
106 |
|
response = conn.getresponse()
|
107 |
|
data = response.read()
|
108 |
|
#endregion
|
109 |
|
operationLocation = response.info().get('Operation-Location')
|
110 |
|
operationId = operationLocation.split("/")[-1]
|
111 |
|
|
112 |
|
print("oID : " + operationId)
|
113 |
|
#operationId = "319d55a2-fe52-482c-a961-f94e81840173"
|
114 |
|
isFinished = False
|
115 |
|
while isFinished == False:
|
116 |
|
conn2 = http.client.HTTPSConnection(uri_base)
|
117 |
|
conn2.request("GET", "/vision/v1.0/textOperations/"+operationId+"?%s" % params, "{body}", headers)
|
118 |
|
resp = conn2.getresponse()
|
119 |
|
data2 = resp.read()
|
120 |
|
tempRes = json.loads(data2)
|
121 |
|
if tempRes['status'] != 'Succeeded':
|
122 |
|
isFinished = True
|
123 |
|
else:
|
124 |
|
conn2.close()
|
125 |
|
|
126 |
|
#region REST API with url
|
127 |
|
## Execute the REST API call and get the response.
|
128 |
|
#conn = httplib.HTTPSConnection(uri_base)
|
129 |
|
#conn.request("POST", "/vision/v1.0/ocr?%s" % params, body, headers)
|
130 |
|
#response = conn.getresponse()
|
131 |
|
#data = response.read()
|
132 |
|
#endregion
|
133 |
|
|
134 |
|
# 'data' contains the JSON data. The following formats the JSON data
|
135 |
|
# for display.
|
136 |
|
parsed = json.loads(data2)
|
137 |
|
#print ("Response:")
|
138 |
|
#print (json.dumps(parsed, sort_keys=True, indent=2))
|
139 |
|
conn.close()
|
140 |
|
conn2.close()
|
141 |
|
#chan, w, h = img.shape[::-1]
|
142 |
|
w, h = img.shape[::-1]
|
143 |
|
|
144 |
|
orientation = parsed['orientation']
|
145 |
|
if orientation != "Up":
|
146 |
|
img = cv2.rotate(img, getRotateCodeByOrientation(orientation))
|
147 |
|
|
148 |
|
count = 0
|
149 |
|
if parsed['status'] != 'Succeeded':
|
150 |
|
print('Is Running')
|
151 |
|
return
|
152 |
|
for re in cs['lines']:
|
153 |
|
#print(re)
|
154 |
|
#print(re['words'])
|
155 |
|
#print(re['boundingBox'])
|
156 |
|
for words in re['words']:
|
157 |
|
print(words)
|
158 |
|
strArr = words['boundingBox'].split(',')
|
159 |
|
pt = (int(strArr[0]), int(strArr[1]))
|
160 |
|
ep = (int(strArr[4]), int(strArr[5]))
|
161 |
|
width = ep[0]-pt[0]
|
162 |
|
height = ep[1]-pt[1]
|
163 |
|
#cv2.rectangle(img, pt, (pt[0] + width, pt[1] + height), (255, 255, 255), -1)
|
164 |
|
roi = img[pt[1]:pt[1]+height, pt[0]:pt[0]+width]
|
165 |
|
temp = roi.copy()
|
166 |
|
#_ret, tempBin = cv2.threshold(temp, 250, 255, cv2.THRESH_BINARY_INV)
|
167 |
|
tempBin = cv2.bitwise_not(temp)
|
168 |
|
#temp = np.zeros((height, width, 3), np.uint8)
|
169 |
|
#temp[::] = (255, 255, 255)
|
170 |
|
roi = cv2.bitwise_xor(roi, tempBin, roi)
|
171 |
|
#dilate = cv2.dilate(roi, (5, 5))
|
172 |
|
#cv2.destroyAllWindows()
|
173 |
|
#cv2.imshow('temp', temp)
|
174 |
|
#cv2.imshow('tempbin', tempBin)
|
175 |
|
#cv2.imshow('ocr', roi)
|
176 |
|
##cv2.imshow('dilate', dilate)
|
177 |
|
#cv2.waitKey(0)
|
178 |
|
count = count + 1
|
179 |
|
#count = count + len(re['words'])
|
180 |
|
|
181 |
|
print("OCR Result count : " + str(count))
|
182 |
|
|
183 |
|
if orientation != "Up":
|
184 |
|
img = cv2.rotate(img, getRollbackRotateCodeByOrientation(orientation))
|
185 |
|
|
186 |
|
#resizedRet = cv2.resize(img, None, fx = 0.25, fy = 0.25)
|
187 |
|
#cv2.imshow('text removed', resizedRet)
|
188 |
|
#cv2.waitKey(0)
|
189 |
|
|
190 |
|
return img
|
191 |
|
|
192 |
|
except Exception as ex:
|
193 |
|
print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno))
|
194 |
|
|
195 |
|
return img
|
196 |
|
|
197 |
|
def removeText(imagePath):
|
198 |
|
image = Image.open(imagePath)
|
199 |
|
ret = np.array(image)
|
200 |
|
|
201 |
|
for i in range(4):
|
202 |
|
ret = detectAndRemoveText(Image.fromarray(ret))
|
203 |
|
ret = cv2.rotate(ret, cv2.ROTATE_90_CLOCKWISE)
|
204 |
|
#b, g, r, a = cv2.split(ret)
|
205 |
|
#pltSrc = cv2.merge([r, g, b])
|
206 |
|
#plt.imshow(pltSrc)
|
207 |
|
#plt.show()
|
208 |
|
#cv2.waitKey(0)
|
209 |
|
|
210 |
|
ret = detectAndRemoveText(Image.fromarray(ret))
|
211 |
|
|
212 |
|
resizedRet = cv2.resize(ret, None, fx = 0.25, fy = 0.25)
|
213 |
|
#cv2.imshow('text removed result', resizedRet)
|
214 |
|
#cv2.imwrite("res/p15_2_xor_word.png", ret)
|
215 |
|
#cv2.imwrite("res/p10_____text_white_box.png", ret)
|
216 |
|
#plt.imshow(ret)
|
217 |
|
#plt.show()
|
218 |
|
#cv2.waitKey(0)
|
219 |
|
|
220 |
|
image.close()
|
221 |
|
return ret
|
222 |
|
|
223 |
|
def removeTextFromNpArray(img):
|
224 |
|
ret = img.copy()
|
225 |
|
|
226 |
|
for i in range(4):
|
227 |
|
ret = detectAndRemoveText(Image.fromarray(ret))
|
228 |
|
ret = cv2.rotate(ret, cv2.ROTATE_90_CLOCKWISE)
|
229 |
|
#b, g, r, a = cv2.split(ret)
|
230 |
|
#pltSrc = cv2.merge([r, g, b])
|
231 |
|
#plt.imshow(pltSrc)
|
232 |
|
#plt.show()
|
233 |
|
#cv2.waitKey(0)
|
234 |
|
|
235 |
|
ret = detectAndRemoveText(Image.fromarray(ret))
|
236 |
|
|
237 |
|
#resizedRet = cv2.resize(ret, None, fx = 0.25, fy = 0.25)
|
238 |
|
#cv2.imshow('text removed result', resizedRet)
|
239 |
|
#cv2.imwrite("res/result.png", ret)
|
240 |
|
#plt.imshow(ret)
|
241 |
|
#plt.show()
|
242 |
|
#cv2.waitKey(0)
|
243 |
|
|
244 |
|
return ret
|
245 |
|
|
246 |
|
def getTextInfoFromNpArray(image):
|
247 |
|
try:
|
248 |
|
image = cv2.resize(image, None, fx = 2.0, fy = 2.0)
|
249 |
|
tmp = Image.fromarray(image)
|
250 |
|
bin_img = BytesIO()
|
251 |
|
tmp.save(bin_img, format='PNG')
|
252 |
|
tmp.close()
|
253 |
|
|
254 |
|
img_data = bin_img.getvalue()
|
255 |
|
bin_img.close()
|
256 |
|
|
257 |
|
#region REST API with raw file
|
258 |
|
# Execute the REST API call and get the response.
|
259 |
|
conn = http.client.HTTPSConnection(uri_base)
|
260 |
|
#conn = httplib.HTTPSConnection(uri_base)
|
261 |
|
conn.request("POST", "/vision/v1.0/recognizeText?%s" % params, img_data, headers)
|
262 |
|
response = conn.getresponse()
|
263 |
|
data = response.read()
|
264 |
|
#endregion
|
265 |
|
operationLocation = response.info().get('Operation-Location')
|
266 |
|
operationId = operationLocation.split("/")[-1]
|
267 |
|
|
268 |
|
print("oID : " + operationId)
|
269 |
|
#operationId = "319d55a2-fe52-482c-a961-f94e81840173"
|
270 |
|
isFinished = False
|
271 |
|
while isFinished == False:
|
272 |
|
conn2 = http.client.HTTPSConnection(uri_base)
|
273 |
|
conn2.request("GET", "/vision/v1.0/textOperations/"+operationId+"?%s" % params, "{body}", headers)
|
274 |
|
resp = conn2.getresponse()
|
275 |
|
data2 = resp.read()
|
276 |
|
tempRes = json.loads(data2)
|
277 |
|
if tempRes['status'] == 'Succeeded':
|
278 |
|
isFinished = True
|
279 |
|
else:
|
280 |
|
isFinished = False
|
281 |
|
conn2.close()
|
282 |
|
|
283 |
|
#region REST API with url
|
284 |
|
## Execute the REST API call and get the response.
|
285 |
|
#conn = httplib.HTTPSConnection(uri_base)
|
286 |
|
#conn.request("POST", "/vision/v1.0/ocr?%s" % params, body, headers)
|
287 |
|
#response = conn.getresponse()
|
288 |
|
#data = response.read()
|
289 |
|
#endregion
|
290 |
|
|
291 |
|
# 'data' contains the JSON data. The following formats the JSON data
|
292 |
|
# for display.
|
293 |
|
parsed = json.loads(data2)
|
294 |
|
#print ("Response:")
|
295 |
|
#print (json.dumps(parsed, sort_keys=True, indent=2))
|
296 |
|
conn.close()
|
297 |
|
conn2.close()
|
298 |
|
#chan, w, h = img.shape[::-1]
|
299 |
|
chan, w, h = image.shape[::-1]
|
300 |
|
|
301 |
|
#orientation = parsed['orientation']
|
302 |
|
#if orientation != "Up":
|
303 |
|
# image = cv2.rotate(image, getRotateCodeByOrientation(orientation))
|
304 |
|
|
305 |
|
count = 0
|
306 |
|
if parsed['status'] != 'Succeeded':
|
307 |
|
print('Is Running')
|
308 |
|
return
|
309 |
|
textInfoList = []
|
310 |
|
cs = parsed['recognitionResult']
|
311 |
|
for re in cs['lines']:
|
312 |
|
#print(re)
|
313 |
|
#print(re['words'])
|
314 |
|
#print(re['boundingBox'])
|
315 |
|
#for words in re['words']:
|
316 |
|
# print(words)
|
317 |
|
# text = words['text']
|
318 |
|
# strArr = words['boundingBox']
|
319 |
|
# pt = (int(strArr[0])//2, int(strArr[1])//2)
|
320 |
|
# ep = (int(strArr[4])//2, int(strArr[5])//2)
|
321 |
|
# width = ep[0]-pt[0]
|
322 |
|
# height = ep[1]-pt[1]
|
323 |
|
|
324 |
|
# textInfoList.append((text, pt, width, height))
|
325 |
|
print(re)
|
326 |
|
text = re['text']
|
327 |
|
strArr = re['boundingBox']
|
328 |
|
pt = (int(strArr[0])//2, int(strArr[1])//2)
|
329 |
|
ep = (int(strArr[4])//2, int(strArr[5])//2)
|
330 |
|
width = ep[0]-pt[0]
|
331 |
|
height = ep[1]-pt[1]
|
332 |
|
textInfoList.append((text, pt, width, height))
|
333 |
|
|
334 |
|
#if orientation != "Up":
|
335 |
|
# image = cv2.rotate(image, getRollbackRotateCodeByOrientation(orientation))
|
336 |
|
|
337 |
|
return textInfoList
|
338 |
|
|
339 |
|
except Exception as ex:
|
340 |
|
print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno))
|
341 |
|
|
342 |
|
return None
|
343 |
|
|
344 |
|
|
345 |
|
|
346 |
|
|
347 |
|
|
348 |
|
|
349 |
|
|
350 |
|
|
351 |
|
# image = cv2.resize(image, None, fx=40.0, fy=40.0)
|
352 |
|
|
353 |
|
# tmp = Image.fromarray(image)
|
354 |
|
# bin_img = BytesIO()
|
355 |
|
# tmp.save(bin_img, format='PNG')
|
356 |
|
# tmp.close()
|
357 |
|
|
358 |
|
# #cv2.imshow('image', image)
|
359 |
|
# #cv2.waitKey(0)
|
360 |
|
|
361 |
|
# img_data = bin_img.getvalue()
|
362 |
|
# bin_img.close()
|
363 |
|
|
364 |
|
# #region REST API with raw file
|
365 |
|
# # Execute the REST API call and get the response.
|
366 |
|
# conn = http.client.HTTPSConnection(uri_base)
|
367 |
|
# #conn = httplib.HTTPSConnection(uri_base)
|
368 |
|
# conn.request("POST", "/vision/v1.0/recognizeText?%s" % params, img_data, headers)
|
369 |
|
# response = conn.getresponse()
|
370 |
|
# data = response.read()
|
371 |
|
# #endregion
|
372 |
|
|
373 |
|
# #region REST API with url
|
374 |
|
# ## Execute the REST API call and get the response.
|
375 |
|
# #conn = httplib.HTTPSConnection(uri_base)
|
376 |
|
# #conn.request("POST", "/vision/v1.0/ocr?%s" % params, body, headers)
|
377 |
|
# #response = conn.getresponse()
|
378 |
|
# #data = response.read()
|
379 |
|
# #endregion
|
380 |
|
|
381 |
|
# # 'data' contains the JSON data. The following formats the JSON data
|
382 |
|
# # for display.
|
383 |
|
# parsed = json.loads(data)
|
384 |
|
# #print ("Response:")
|
385 |
|
# #print (json.dumps(parsed, sort_keys=True, indent=2))
|
386 |
|
# conn.close()
|
387 |
|
# #chan, w, h = img.shape[::-1]
|
388 |
|
# chan, w, h = image.shape[::-1]
|
389 |
|
|
390 |
|
# orientation = parsed['orientation']
|
391 |
|
# if orientation != "Up":
|
392 |
|
# image = cv2.rotate(image, getRotateCodeByOrientation(orientation))
|
393 |
|
|
394 |
|
# count = 0
|
395 |
|
|
396 |
|
# textInfoList = []
|
397 |
|
# for cs in parsed['regions']:
|
398 |
|
# for re in cs['lines']:
|
399 |
|
# #print(re)
|
400 |
|
# #print(re['words'])
|
401 |
|
# #print(re['boundingBox'])
|
402 |
|
# for words in re['words']:
|
403 |
|
# print(words)
|
404 |
|
# text = words['text']
|
405 |
|
# strArr = words['boundingBox'].split(',')
|
406 |
|
# pt = (int(strArr[0])//40, int(strArr[1])//40)
|
407 |
|
# width = int(strArr[2])//40
|
408 |
|
# height = int(strArr[3])//40
|
409 |
|
|
410 |
|
# textInfoList.append((text, pt, width, height))
|
411 |
|
|
412 |
|
# if orientation != "Up":
|
413 |
|
# image = cv2.rotate(image, getRollbackRotateCodeByOrientation(orientation))
|
414 |
|
|
415 |
|
# return textInfoList
|
416 |
|
|
417 |
|
#except Exception as e:
|
418 |
|
# print('Error:')
|
419 |
|
# print(e)
|
420 |
|
|
421 |
|
# return None
|