-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathview_annotations.py
240 lines (205 loc) · 10.5 KB
/
view_annotations.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
"""
Copyright (C) 2023 Microsoft Corporation
Assumes the data is in PASCAL VOC data format and the folder structure is:
[data_directory]/
- images/
- train/
- test/
- val/
"""
import argparse
import os
import json
from collections import defaultdict
import traceback
from PIL import Image
from matplotlib import pyplot as plt
import matplotlib.patches as patches
from matplotlib.patches import Patch
import xml.etree.ElementTree as ET
def read_pascal_voc(xml_file: str):
tree = ET.parse(xml_file)
root = tree.getroot()
bboxes = []
labels = []
for object_ in root.iter('object'):
ymin, xmin, ymax, xmax = None, None, None, None
label = object_.find("name").text
for box in object_.findall("bndbox"):
ymin = float(box.find("ymin").text)
xmin = float(box.find("xmin").text)
ymax = float(box.find("ymax").text)
xmax = float(box.find("xmax").text)
bbox = [xmin, ymin, xmax, ymax] # PASCAL VOC
bboxes.append(bbox)
labels.append(label)
return bboxes, labels
color_map = defaultdict(lambda: ('magenta', 0, 1))
color_map.update({'table': ('brown', 0.1, 3), 'table row': ('blue', 0.04, 1),
'table column': ('red', 0.04, 1), 'table projected row header': ('cyan', 0.2, 3),
'table column header': ('magenta', 0.2, 3), 'table spanning cell': ('green', 0.6, 3)})
def plot_bbox(ax, bbox, color='magenta', linewidth=1, alpha=0):
rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1], linewidth=linewidth,
edgecolor='none',facecolor=color, alpha=alpha)
ax.add_patch(rect)
rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1], linewidth=linewidth,
edgecolor=color,facecolor='none',linestyle="--")
ax.add_patch(rect)
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('--pascal_data_dir',
help="Root directory for source data to process")
parser.add_argument('--words_data_dir',
help="Root directory for source data to process")
parser.add_argument('--split', default='',
help="Split to process")
parser.add_argument('--output_dir',
help="Root directory for output data")
parser.add_argument('--num_samples', type=int)
return parser.parse_args()
def main():
args = get_args()
data_directory = args.pascal_data_dir
words_directory = args.words_data_dir
split = args.split
output_directory = args.output_dir
num_samples = args.num_samples
if not os.path.exists(output_directory):
os.makedirs(output_directory)
xml_filenames = [elem for elem in os.listdir(os.path.join(data_directory, split)) if elem.endswith(".xml")]
for idx, filename in enumerate(xml_filenames):
if not num_samples is None and idx == num_samples:
break
print(filename)
try:
xml_filepath = os.path.join(data_directory, split, filename)
img_filepath = xml_filepath.replace(split, "images").replace(".xml", ".jpg")
bboxes, labels = read_pascal_voc(xml_filepath)
img = Image.open(img_filepath)
# TODO: Add option to include words
#words_filepath = os.path.join(words_directory, filename.replace(".xml", "_words.json"))
#try:
# with open(words_filepath, 'r') as json_file:
# words = json.load(json_file)
#except:
# words = []
ax = plt.gca()
ax.imshow(img, interpolation="lanczos")
plt.gcf().set_size_inches((24, 24))
tables = [bbox for bbox, label in zip(bboxes, labels) if label == 'table']
columns = [bbox for bbox, label in zip(bboxes, labels) if label == 'table column']
rows = [bbox for bbox, label in zip(bboxes, labels) if label == 'table row']
column_headers = [bbox for bbox, label in zip(bboxes, labels) if label == 'table column header']
projected_row_headers = [bbox for bbox, label in zip(bboxes, labels) if label == 'table projected row header']
spanning_cells = [bbox for bbox, label in zip(bboxes, labels) if label == 'table spanning cell']
for column_num, bbox in enumerate(columns):
if column_num % 2 == 0:
linewidth = 2
alpha = 0.6
facecolor = 'none'
edgecolor = 'red'
hatch = '..'
else:
linewidth = 2
alpha = 0.15
facecolor = (1, 0, 0)
edgecolor = (0.8, 0, 0)
hatch = ''
rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1], linewidth=0,
edgecolor=edgecolor, facecolor=facecolor, linestyle="-",
hatch=hatch, alpha=alpha)
ax.add_patch(rect)
rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1], linewidth=linewidth,
edgecolor='red', facecolor='none', linestyle="-",
alpha=0.8)
ax.add_patch(rect)
for row_num, bbox in enumerate(rows):
if row_num % 2 == 1:
linewidth = 2
alpha = 0.6
edgecolor = 'blue'
facecolor = 'none'
hatch = '....'
else:
linewidth = 2
alpha = 0.1
facecolor = (0, 0, 1)
edgecolor = (0, 0, 0.8)
hatch = ''
rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1], linewidth=0,
edgecolor=edgecolor, facecolor=facecolor, linestyle="-",
hatch=hatch, alpha=alpha)
ax.add_patch(rect)
rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1], linewidth=linewidth,
edgecolor='blue', facecolor='none', linestyle="-",
alpha=0.8)
ax.add_patch(rect)
for bbox in column_headers:
linewidth = 3
alpha = 0.3
facecolor = (1, 0, 0.75) #(0.5, 0.45, 0.25)
edgecolor = (1, 0, 0.75) #(1, 0.9, 0.5)
rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1], linewidth=linewidth,
edgecolor='none',facecolor=facecolor, alpha=alpha)
ax.add_patch(rect)
rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1], linewidth=0,
edgecolor=edgecolor,facecolor='none',linestyle="-", hatch='///')
ax.add_patch(rect)
for bbox in projected_row_headers:
facecolor = (1, 0.9, 0.5) #(0, 0.75, 1) #(0, 0.4, 0.4)
edgecolor = (1, 0.9, 0.5) #(0, 0.7, 0.95)
alpha = 0.35
linewidth = 3
linestyle="--"
rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1], linewidth=linewidth,
edgecolor='none',facecolor=facecolor, alpha=alpha)
ax.add_patch(rect)
rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1], linewidth=1,
edgecolor=edgecolor,facecolor='none',linestyle=linestyle)
ax.add_patch(rect)
rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1], linewidth=0,
edgecolor=edgecolor,facecolor='none',linestyle=linestyle, hatch='\\\\')
ax.add_patch(rect)
for bbox in spanning_cells:
color = (0.2, 0.5, 0.2) #(0, 0.4, 0.4)
alpha = 0.4
linewidth = 4
linestyle="-"
rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1], linewidth=linewidth,
edgecolor='none',facecolor=color, alpha=alpha)
ax.add_patch(rect)
rect = patches.Rectangle(bbox[:2], bbox[2]-bbox[0], bbox[3]-bbox[1], linewidth=linewidth,
edgecolor=color,facecolor='none',linestyle=linestyle) # hatch='//'
ax.add_patch(rect)
table_bbox = tables[0]
plt.xlim([table_bbox[0]-5, table_bbox[2]+5])
plt.ylim([table_bbox[3]+5, table_bbox[1]-5])
plt.xticks([], [])
plt.yticks([], [])
legend_elements = [Patch(facecolor=(0.9, 0.9, 1), edgecolor=(0, 0, 0.8),
label='Row (odd)'),
Patch(facecolor=(1, 1, 1), edgecolor=(0, 0, 0.8),
label='Row (even)', hatch='...'),
Patch(facecolor=(1, 1, 1), edgecolor=(0.8, 0, 0),
label='Column (odd)', hatch='...'),
Patch(facecolor=(1, 0.85, 0.85), edgecolor=(0.8, 0, 0),
label='Column (even)'),
Patch(facecolor=(0.68, 0.8, 0.68), edgecolor=(0.2, 0.5, 0.2),
label='Spanning cell'),
Patch(facecolor=(1, 0.7, 0.925), edgecolor=(1, 0, 0.75),
label='Column header', hatch='///'),
Patch(facecolor=(1, 0.965, 0.825), edgecolor=(1, 0.9, 0.5),
label='Projected row header', hatch='\\\\')]
ax.legend(handles=legend_elements, bbox_to_anchor=(0, -0.02), loc='upper left', borderaxespad=0,
fontsize=16, ncol=4)
plt.gcf().set_size_inches(20, 20)
plt.axis('off')
save_filepath = os.path.join(output_directory, filename.replace(".xml", "_ANNOTATIONS.jpg"))
plt.savefig(save_filepath, bbox_inches='tight', dpi=150)
plt.show()
plt.close()
except:
traceback.print_exc()
continue
if __name__ == "__main__":
main()