File size: 7,307 Bytes
3ac1768
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Copyright (C) 2023 Microsoft Corporation

Script to create a version of the dataset with a specified amount of padding around the table.
Does not add padding to the image, only crops the image to have the specified amount of padding
around the table.

Assumes the data is in PASCAL VOC data format and the folder structure is:
[data_directory]/
- images/
- train/
- test/
- val/
"""

import argparse
import os
from xml.dom import minidom
import xml.etree.ElementTree as ET
import json

from PIL import Image
from fitz import Rect

def read_pascal_voc(xml_filepath):

    tree = ET.parse(xml_filepath)
    root = tree.getroot()

    bboxes = []
    labels = []
    
    filename = root.find('filename').text
    size = root.find('size')
    source = root.find('source')
    database = source.find('database').text
    width = size.find('width').text
    height = size.find('height').text

    for object_ in root.iter('object'):

        filename = root.find('filename').text

        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, filename, width, height, database


def create_pascal_voc_page_element(image_filename, output_image_width, output_image_height, database):
    # Create XML of tables on PDF page in PASCAL VOC format
    annotation = ET.Element("annotation")

    folder = ET.SubElement(annotation, "folder").text = ""
    filename = ET.SubElement(annotation, "filename").text = image_filename
    path = ET.SubElement(annotation, "path").text = image_filename
    source = ET.SubElement(annotation, "source")
    database = ET.SubElement(source, "database").text = database
    size = ET.SubElement(annotation, "size")
    width = ET.SubElement(size, "width").text = str(output_image_width)
    height = ET.SubElement(size, "height").text = str(output_image_height)
    depth = ET.SubElement(size, "depth").text = "3"
    segmented = ET.SubElement(annotation, "segmented").text = "0"
    
    return annotation

def create_pascal_voc_object_element(class_label, bbox):
    object_ = ET.Element("object")
    name = ET.SubElement(object_, "name").text = class_label
    pose = ET.SubElement(object_, "pose").text = "Frontal"
    truncated = ET.SubElement(object_, "truncated").text = "0"
    difficult = ET.SubElement(object_, "difficult").text = "0"
    occluded = ET.SubElement(object_, "occluded").text = "0"
    bndbox = ET.SubElement(object_, "bndbox")
    
    ET.SubElement(bndbox, "xmin").text = str(bbox[0])
    ET.SubElement(bndbox, "ymin").text = str(bbox[1])
    ET.SubElement(bndbox, "xmax").text = str(bbox[2])
    ET.SubElement(bndbox, "ymax").text = str(bbox[3])
    
    return object_

def save_xml_pascal_voc(page_annotation, filepath):
    xmlstr = minidom.parseString(ET.tostring(page_annotation)).toprettyxml(indent="   ")
    with open(filepath, "w") as f:
        f.write(xmlstr)

def iob(bbox1, bbox2):
    """
    Compute the intersection area over box area, for bbox1.
    """
    intersection = Rect(bbox1).intersect(bbox2)
    return intersection.get_area() / Rect(bbox1).get_area()


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('--table_padding', type=int, default=2)
    return parser.parse_args()

def main():
    args = get_args()

    data_directory = args.pascal_data_dir
    if data_directory.endswith(os.sep):
        data_directory = data_directory[:-1]
    words_directory = args.words_data_dir
    if words_directory.endswith(os.sep):
        words_directory = words_directory[:-1]
    split = args.split
    padding = args.table_padding

    data_output_directory = data_directory + "_PADDING_" + str(padding)
    words_output_directory = words_directory + "_PADDING_" + str(padding)

    if not os.path.exists(data_output_directory):
        os.makedirs(data_output_directory)

    if not os.path.exists(words_output_directory):
        os.makedirs(words_output_directory)

    source_subdir = os.path.join(data_directory, split)
    dest_subdir = os.path.join(data_output_directory, split)
    source_image_directory = os.path.join(data_directory, "images")
    dest_image_directory = os.path.join(data_output_directory, "images")

    if not os.path.exists(dest_subdir):
        os.makedirs(dest_subdir)

    if not os.path.exists(dest_image_directory):
        os.makedirs(dest_image_directory)

    files = os.listdir(source_subdir)

    for file in files:
        filepath = os.path.join(source_subdir, file)
        words_filepath = os.path.join(words_directory, file.replace('.xml', '_words.json'))

        bboxes, labels, filename, width, height, database = read_pascal_voc(filepath)

        tables = [idx for idx, label in enumerate(labels) if label == 'table']

        image_filepath = os.path.join(source_image_directory, filename)

        img = Image.open(image_filepath)

        if not len(tables) == 1:
            print('Problem')

        table_bbox = bboxes[tables[0]]

        crop_bbox = [round(elem) for elem in table_bbox]
        crop_bbox[0] -= padding
        crop_bbox[1] -= padding
        crop_bbox[2] += padding
        crop_bbox[3] += padding

        img = img.crop(crop_bbox)

        annotation = create_pascal_voc_page_element(filename, img.width, img.height, database)

        for label, bbox in zip(labels, bboxes):
            bbox = [bbox[0]-crop_bbox[0],
                    bbox[1]-crop_bbox[1],
                    bbox[2]-crop_bbox[0],
                    bbox[3]-crop_bbox[1]]

            # Add to PASCAl VOC
            element = create_pascal_voc_object_element(label, bbox)
            annotation.append(element)  

        dest_img_path = os.path.join(dest_image_directory, filename)
        img.save(dest_img_path)
        dest_annot_path = os.path.join(dest_subdir, file)
        save_xml_pascal_voc(annotation, dest_annot_path)

        with open(words_filepath, 'r') as jf:
            data = json.load(jf)

        padded_words = []
        for word in data:
            bbox = word['bbox']

            if iob(bbox, crop_bbox) >= 0.5:

                bbox = [bbox[0]-crop_bbox[0],
                        bbox[1]-crop_bbox[1],
                        bbox[2]-crop_bbox[0],
                        bbox[3]-crop_bbox[1]]
                word['bbox'] = bbox
                padded_words.append(word)

        padded_words_filepath = os.path.join(words_output_directory,
                                             file.replace('.xml', '_words.json'))
        with open(padded_words_filepath, 'w') as jf:
            json.dump(padded_words, jf)

if __name__ == "__main__":
    main()