개정판 4315c4e5
issue #663: craft testing
Change-Id: Ibb30cfd290868653d28335cfbd7f1ea388aa86df
DTI_PID/DTI_PID/TextDetector.py | ||
---|---|---|
114 | 114 |
#from imutils.object_detection import non_max_suppression |
115 | 115 |
from AppDocData import AppDocData |
116 | 116 |
|
117 |
list = [] |
|
117 |
res_list = []
|
|
118 | 118 |
ocr_image = None |
119 | 119 |
try: |
120 | 120 |
app_doc_data = AppDocData.instance() |
... | ... | |
131 | 131 |
not_containing_bbox, binary_image = self.getTextBox(ocr_image, imgGray, maxTextSize, minSize) |
132 | 132 |
|
133 | 133 |
# using craft |
134 |
#not_containing_bbox, binary_image = self.getTextBox_craft(ocr_image, maxTextSize, minSize)
|
|
134 |
#return self.getTextBox_craft(ocr_image, maxTextSize, minSize, offset_x, offset_y)
|
|
135 | 135 |
|
136 | 136 |
rects = [] |
137 | 137 |
|
... | ... | |
198 | 198 |
# if there is no boxes which contains |
199 | 199 |
if not matches: |
200 | 200 |
angle = rect[0] |
201 |
list.append(ti.TextInfo('', round(offset_x) + rect[1].x(), round(offset_y) + rect[1].y(), rect[1].width(), |
|
201 |
res_list.append(ti.TextInfo('', round(offset_x) + rect[1].x(), round(offset_y) + rect[1].y(), rect[1].width(),
|
|
202 | 202 |
rect[1].height(), angle)) |
203 | 203 |
except Exception as ex: |
204 | 204 |
message = 'error occurred({}) in {}:{}'.format(repr(ex), sys.exc_info()[-1].tb_frame.f_code.co_filename, |
205 | 205 |
sys.exc_info()[-1].tb_lineno) |
206 | 206 |
print(message) |
207 | 207 |
|
208 |
return list, ocr_image |
|
208 |
return res_list, ocr_image |
|
209 |
|
|
210 |
''' |
|
211 |
def getTextBox_craft(self, ocr_image, maxTextSize, minSize, offset_x, offset_y): |
|
212 |
""" get text box by using craft """ |
|
209 | 213 |
|
210 |
def getTextBox_craft(self, ocr_image, maxTextSize, minSize): |
|
211 |
pass |
|
212 |
''' |
|
213 | 214 |
from CRAFT_pytorch_master import text_craft |
214 | 215 |
from AppDocData import AppDocData |
215 | 216 |
|
... | ... | |
227 | 228 |
for box in boxes: |
228 | 229 |
rects.append(QRect(box[0], box[1], box[4] - box[0], box[5] - box[1])) |
229 | 230 |
|
230 |
return rects, binary_image |
|
231 |
''' |
|
231 |
#configs = app_doc_data.getConfigs('Text Recognition', 'Merge Size') |
|
232 |
#mergeSize = int(configs[0].value) if 1 == len(configs) else 10 |
|
233 |
#gap_size = mergeSize / 2 |
|
234 |
gap_size = 3 |
|
235 |
|
|
236 |
verticals = [] |
|
237 |
horizontals = [] |
|
238 |
for rect in rects: |
|
239 |
if rect.width() < minSize and rect.height() < maxTextSize: |
|
240 |
rect._vertical = False |
|
241 |
horizontals.append(rect) |
|
242 |
elif rect.height() < minSize and rect.width() < maxTextSize: |
|
243 |
rect._vertical = True |
|
244 |
verticals.append(rect) |
|
245 |
elif rect.width() < minSize or rect.height() < minSize: |
|
246 |
continue |
|
247 |
elif rect.height() > rect.width(): |
|
248 |
rect._vertical = True |
|
249 |
verticals.append(rect) |
|
250 |
else: |
|
251 |
rect._vertical = False |
|
252 |
horizontals.append(rect) |
|
253 |
|
|
254 |
v_merges = [] |
|
255 |
for vertical1 in verticals: |
|
256 |
for vertical2 in verticals: |
|
257 |
if vertical1 is vertical2: |
|
258 |
continue |
|
259 |
if abs(vertical1.center().x() - vertical2.center().x()) < gap_size: |
|
260 |
t1, t2 = vertical1.top() + gap_size, vertical2.top() + gap_size |
|
261 |
b1, b2 = vertical1.bottom() - gap_size, vertical2.bottom() - gap_size |
|
262 |
if not (b2 < t1 or t2 < b1): |
|
263 |
inserted = False |
|
264 |
for merge in v_merges: |
|
265 |
if vertical1 in merge and vertical2 in merge: |
|
266 |
inserted = True |
|
267 |
break |
|
268 |
elif vertical1 in merge and vertical2 not in merge: |
|
269 |
merge.append(vertical2) |
|
270 |
inserted = True |
|
271 |
break |
|
272 |
elif vertical2 in merge and vertical1 not in merge: |
|
273 |
merge.append(vertical1) |
|
274 |
inserted = True |
|
275 |
break |
|
276 |
if not inserted: |
|
277 |
v_merges.append([vertical1, vertical2]) |
|
278 |
|
|
279 |
h_merges = [] |
|
280 |
for horizontal1 in horizontals: |
|
281 |
for horizontal2 in horizontals: |
|
282 |
if horizontal1 is horizontal2: |
|
283 |
continue |
|
284 |
if abs(horizontal1.center().y() - horizontal2.center().y()) < gap_size: |
|
285 |
l1, l2 = horizontal1.left() - gap_size, horizontal2.left() - gap_size |
|
286 |
r1, r2 = horizontal1.right() + gap_size, horizontal2.right() + gap_size |
|
287 |
if not (l2 < r1 or r2 < l1): |
|
288 |
inserted = False |
|
289 |
for merge in h_merges: |
|
290 |
if horizontal1 in merge and horizontal2 in merge: |
|
291 |
inserted = True |
|
292 |
break |
|
293 |
elif horizontal1 in merge and horizontal2 not in merge: |
|
294 |
merge.append(horizontal2) |
|
295 |
inserted = True |
|
296 |
break |
|
297 |
elif horizontal2 in merge and horizontal1 not in merge: |
|
298 |
merge.append(horizontal1) |
|
299 |
inserted = True |
|
300 |
break |
|
301 |
if not inserted: |
|
302 |
h_merges.append([horizontal1, horizontal2]) |
|
303 |
|
|
304 |
for merge in v_merges + h_merges: |
|
305 |
for rect in merge: |
|
306 |
if rect in merge: |
|
307 |
rects.remove(rect) |
|
308 |
else: |
|
309 |
print(str(rect)) |
|
310 |
|
|
311 |
for merge in v_merges: |
|
312 |
max_x, max_y, min_x, min_y = 0, 0, sys.maxsize, sys.maxsize |
|
313 |
for rect in merge: |
|
314 |
if rect.left() < min_x: |
|
315 |
min_x = rect.left() |
|
316 |
if rect.right() > max_x: |
|
317 |
max_x = rect.right() |
|
318 |
if rect.top() > max_y: |
|
319 |
max_y = rect.top() |
|
320 |
if rect.bottom() < min_y: |
|
321 |
min_y = rect.bottom() |
|
322 |
|
|
323 |
rect = QRect(min_x, max_y, max_x - min_x, max_y - min_x) |
|
324 |
rect._vertical = True |
|
325 |
rects.append(rect) |
|
326 |
|
|
327 |
for merge in h_merges: |
|
328 |
max_x, max_y, min_x, min_y = 0, 0, sys.maxsize, sys.maxsize |
|
329 |
for rect in merge: |
|
330 |
if rect.left() < min_x: |
|
331 |
min_x = rect.left() |
|
332 |
if rect.right() > max_x: |
|
333 |
max_x = rect.right() |
|
334 |
if rect.top() > max_y: |
|
335 |
max_y = rect.top() |
|
336 |
if rect.bottom() < min_y: |
|
337 |
min_y = rect.bottom() |
|
338 |
|
|
339 |
rect = QRect(min_x, max_y, max_x - min_x, max_y - min_x) |
|
340 |
rect._vertical = False |
|
341 |
rects.append(rect) |
|
342 |
|
|
343 |
res_rects = [] |
|
344 |
for rect in rects: |
|
345 |
res_rects.append(ti.TextInfo('', round(offset_x) + rect.x(), round(offset_y) + rect.y(), rect.width(), |
|
346 |
rect.height(), 90 if rect._vertical else 0)) |
|
347 |
|
|
348 |
return res_rects, binary_image |
|
349 |
''' |
|
232 | 350 |
|
233 | 351 |
def getTextBox(self, ocr_image, imgGray, maxTextSize, minSize): |
234 | 352 |
""" get text box """ |
내보내기 Unified diff