-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml_to_csv_for_obdc_data.py
70 lines (60 loc) · 1.91 KB
/
xml_to_csv_for_obdc_data.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
import os
import glob
import pandas as pd
import csv
import xml.etree.ElementTree as ET
import argparse
# it takes the folder name of xml files as argument
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", type=str, default="",
help="path to input image file")
args = vars(ap.parse_args())
images = args["image"]
tagss = []
to_be_removed = []
# /mnt/Train_YOLO/tensorflow-yolov3/data/dataset/train
for fl in os.listdir(images):
#print(fl)
if fl == ".DS_Store" or fl == "_DS_Store":
print(fl)
# print("stupid files")
else:
images2 = os.path.join(images,fl)
# print(images2)
# import xml.etree.ElementTree as ET
tree = ET.parse(images2)
# print(tree)
i = 0
vinnu_final = ""
for elem in tree.iter():
# print(elem.text)
# print(elem.tag)
tagss.append(elem.tag)
# filename,width,height,class,xmin,ymin,xmax,ymax
if elem.tag == "filename" or elem.tag == "width" or elem.tag == "height" or elem.tag == "name" or elem.tag == "xmin" or elem.tag == "ymin" or elem.tag == "xmax" or elem.tag == "ymax":
if elem.tag == "filename":
filename_1 = elem.text
elif elem.tag == "width":
width_1 = elem.text
elif elem.tag == "height":
height_1 = elem.text
elif elem.tag == "name":
class_1 = elem.text
print(class_1)
elif elem.tag == "xmin":
xmin_1 = elem.text
elif elem.tag == "ymin":
ymin_1 = elem.text
elif elem.tag == "xmax":
xmax_1 = elem.text
elif elem.tag == "ymax":
ymax_1 = elem.text
else:
print("done")
else:
continue
with open("csv_test.csv", 'a', newline='') as csvfile:
fieldnames = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']
# writer.writeheader()
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writerow({'filename': filename_1, 'width': width_1, 'height': height_1, 'class': class_1, 'xmin': xmin_1, 'ymin': ymin_1, 'xmax': xmax_1, 'ymax': ymax_1})