-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialize_ground_truth_bbox.py
44 lines (36 loc) · 1.15 KB
/
serialize_ground_truth_bbox.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
import xml.etree.ElementTree as ET
import os
import json
"""
This script saves the ground truth bbox values in this json format for each image.
{
filename1.tif:[
xmin,
ymin,
xmax,
ymax
]
}
"""
ground_truth_dir = '/Users/veersingh/Desktop/docs_with_signs_dataset(tobacco800)/ground_truth_xml'
json_file_output_dir = 'assets'
ground_truth_values = dict()
for filename in os.listdir(ground_truth_dir):
current_xml_file = filename
current_image_file = filename.replace('.xml', '.tif')
# Get data from xml ground truth files
tree = ET.parse(ground_truth_dir + '/' + filename)
root = tree.getroot()
xmin = int(root[0][0][0].attrib['col'])
ymin = int(root[0][0][0].attrib['row'])
w = int(root[0][0][0].attrib['width'])
h = int(root[0][0][0].attrib['height'])
xmax = xmin + w
ymax = ymin + h
ground_truth_values[current_image_file] = [xmin, ymin, xmax, ymax]
# writing json output
json_output = json.dumps(ground_truth_values, indent=4)
output_json_file = json_file_output_dir + '/' + 'ground_truth_bbox.json'
jsonFile = open(output_json_file, "w")
jsonFile.write(json_output)
jsonFile.close()