forked from MulongXie/UIED
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.py
213 lines (191 loc) · 7.7 KB
/
merge.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import json
import cv2
import numpy as np
from os.path import join as pjoin
import os
import time
from random import randint as rint
from UIED.utils.util_merge import *
from UIED.config.CONFIG import Config
from UIED.utils.Element import Element
C = Config()
# def merge_redundant_corner(org, compos):
# changed = False
# new_compos = []
# for i in range(len(compos)):
# # broad = draw_bounding_box(org, [compos[i]], show=True)
# merged = False
# for j in range(len(new_compos)):
# iou = compos[i].calc_iou(new_compos[j])
#
# # if iou > 0:
# # print('iou:%.3f' % iou)
# # draw_bounding_box(broad, [new_compos[j]], color=(255,0,0), line=2, show=True)
#
# if iou > 0.8:
# new_compos[j].element_merge(compos[i], new_element=False)
# merged = True
# changed = True
# break
# if not merged:
# new_compos.append(compos[i])
#
# if not changed:
# return compos
# else:
# return merge_redundant_corner(org, new_compos)
#
#
# def merge_text_line(compos, max_word_gap=10, max_word_height=30):
# changed = False
# new_compos = []
# for i in range(len(compos)):
# if compos[i].category != 'Text':
# new_compos.append(compos[i])
# continue
# merged = False
# height = compos[i].height
# if height > max_word_height:
# new_compos.append(compos[i])
# continue
# for j in range(len(new_compos)):
# # merge text line and paragraph
# if is_same_alignment(compos[i], new_compos[j], max_word_gap, flag='line'):
# new_compos[j].element_merge(compos[i])
# new_compos[j].category = 'Text'
# merged = True
# changed = True
# break
# if not merged:
# new_compos.append(compos[i])
#
# if not changed:
# return compos
# else:
# return merge_text_line(new_compos)
#
#
# def merge_paragraph(org, compos, max_para_gap=20, max_word_height=500):
# changed = False
# new_compos = []
# for i in range(len(compos)):
# if compos[i].category != 'Text':
# new_compos.append(compos[i])
# continue
# merged = False
# height = compos[i].height
# if height > max_word_height:
# new_compos.append(compos[i])
# continue
# for j in range(len(new_compos)):
# if is_same_alignment(compos[i], new_compos[j], max_para_gap, flag='paragraph'):
# if new_compos[j].category not in ['Text', 'Paragraph']:
# continue
# new_compos[j].element_merge(compos[i])
# new_compos[j].category = 'Text'
# merged = True
# changed = True
# break
# if not merged:
# new_compos.append(compos[i])
#
# if not changed:
# return compos
# else:
# return merge_paragraph(org, new_compos)
def reclassify_text_by_ocr(org, compos, texts):
compos_new = []
for i, compo in enumerate(compos):
# broad = draw_bounding_box(org, [compo], show=True)
new_compo = None
text_area = 0
for j, text in enumerate(texts):
# get the intersected area
inter = compo.calc_intersection_area(text)
if inter == 0:
continue
# calculate IoU
ioa = inter / compo.area
iob = inter / text.area
iou = inter / (compo.area + text.area - inter)
# print('ioa:%.3f, iob:%.3f, iou:%.3f' %(ioa, iob, iou))
# draw_bounding_box(broad, [text], color=(255,0,0), line=2, show=True)
# text area
if ioa >= 0.68 or iou > 0.55:
new_compo = compo.element_merge(text, new_element=True, new_category='Text')
texts[j] = new_compo
break
text_area += inter
# print("Text area ratio:%.3f" % (text_area / compo.area))
if new_compo is not None:
compos_new.append(new_compo)
elif text_area / compo.area > 0.4:
compo.category = 'Text'
compos_new.append(compo)
else:
compos_new.append(compo)
return compos_new
def merge_intersected_compos(org, compos, max_gap=(0, 0), merge_class=None):
changed = False
new_compos = []
for i in range(len(compos)):
if merge_class is not None and compos[i].category != merge_class:
new_compos.append(compos[i])
continue
merged = False
cur_compo = compos[i]
for j in range(len(new_compos)):
if merge_class is not None and new_compos[j].category != merge_class:
continue
relation = cur_compo.element_relation(new_compos[j], max_gap)
# print(relation)
# draw_bounding_box(org, [cur_compo, new_compos[j]], name='b-merge', show=True)
if relation != 0:
new_compos[j].element_merge(cur_compo)
cur_compo = new_compos[j]
# draw_bounding_box(org, [new_compos[j]], name='a-merge', show=True)
merged = True
changed = True
# break
if not merged:
new_compos.append(compos[i])
if not changed:
return compos
else:
return merge_intersected_compos(org, new_compos, max_gap, merge_class)
def incorporate(img_path, compo_path, text_path, output_root, resize_by_height=None, show=False):
org = cv2.imread(img_path)
compos = []
texts = []
background = None
for compo in json.load(open(compo_path, 'r'))['compos']:
if compo['class'] == 'Background':
background = compo
continue
element = Element((compo['column_min'], compo['row_min'], compo['column_max'], compo['row_max']), compo['class'])
compos.append(element)
for text in json.load(open(text_path, 'r'))['compos']:
element = Element((text['column_min'], text['row_min'], text['column_max'], text['row_max']), 'Text')
texts.append(element)
# bbox_text = refine_text(org, bbox_text, 20, 10)
# bbox_text = resize_label(bbox_text, resize_by_height, org.shape[0])
org_resize = resize_img_by_height(org, resize_by_height)
draw_bounding_box_class(org_resize, compos, show=show, name='ip')
draw_bounding_box(org_resize, texts, show=show, name='ocr')
compos_merged = reclassify_text_by_ocr(org_resize, compos, texts)
# compos_merged = merge_redundant_corner(org_resize, compos_merged)
draw_bounding_box_class(org_resize, compos_merged, name='text', show=show)
# compos_merged = merge_text_line(compos_merged)
compos_merged = merge_intersected_compos(org_resize, compos_merged, max_gap=(6, 0), merge_class='Text')
draw_bounding_box_class(org_resize, compos_merged, name='merged line', show=show)
# compos_merged = merge_paragraph(org_resize, compos_merged)
compos_merged = merge_intersected_compos(org_resize, compos_merged, max_gap=(0, 1), merge_class='Text')
board = draw_bounding_box_class(org_resize, compos_merged, name='merged paragraph', show=show)
draw_bounding_box_non_text(org_resize, compos_merged, org_shape=org.shape, show=show)
compos_json = save_corners_json(pjoin(output_root, 'compo.json'), background, compos_merged, org_resize.shape)
dissemble_clip_img_fill(pjoin(output_root, 'clips'), org_resize, compos_json)
cv2.imwrite(pjoin(output_root, 'result.jpg'), board)
print('Merge Complete and Save to', pjoin(output_root, 'result.jpg'))
print(time.ctime(), '\n')
# if show:
# cv2.destroyAllWindows()