-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxt转换xml.py
72 lines (62 loc) · 2.68 KB
/
txt转换xml.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, shutil
import glob
from PIL import Image
if os.path.exists("new_xml"):
shutil.rmtree("new_xml")
os.makedirs("new_xml")
# VEDAI 图像存储位置
src_img_dir = "./中"
# VEDAI 图像的 ground truth 的 txt 文件存放位置
src_txt_dir = "./new_txt"
src_xml_dir = "./new_xml"
img_cls = ".bmp"
img_Lists = glob.glob(src_img_dir + '/*'+img_cls)
img_basenames = [] # e.g. 100.jpg
for item in img_Lists:
img_basenames.append(os.path.basename(item))
img_names = [] # e.g. 100
for item in img_basenames:
temp1, temp2 = os.path.splitext(item)
print(temp1)
img_names.append(temp1)
for img in img_names:
im = Image.open((src_img_dir + '/' + img + img_cls))
width, height = im.size
# open the crospronding txt file
try:
gt = open(src_txt_dir + '/' + img + '.txt').read().splitlines()
# gt = open(src_txt_dir + '/gt_' + img + '.txt').read().splitlines()
except:
continue
# write in xml file
# os.mknod(src_xml_dir + '/' + img + '.xml')
xml_file = open((src_xml_dir + '/' + img + '.xml'), 'w')
xml_file.write('<annotation>\n')
xml_file.write(' <folder>VOC2007</folder>\n')
xml_file.write(' <filename>' + str(img) + img_cls + '</filename>\n')
xml_file.write(' <path>' + str(img) + img_cls + '</path>\n')
xml_file.write(' <source>\n')
xml_file.write(' <database>Unknown</database>\n')
xml_file.write(' </source>\n')
xml_file.write(' <size>\n')
xml_file.write(' <width>' + str(width) + '</width>\n')
xml_file.write(' <height>' + str(height) + '</height>\n')
xml_file.write(' <depth>3</depth>\n')
xml_file.write(' </size>\n')
xml_file.write(' <segmented>0</segmented>\n')
# write the region of image on xml file
for img_each_label in gt:
spt = img_each_label.split(',') # 这里如果txt里面是以逗号‘,’隔开的,那么就改为spt = img_each_label.split(',')。
xml_file.write(' <object>\n')
xml_file.write(' <name>' + str(spt[4].split(" ")[0]) + '</name>\n')
xml_file.write(' <pose>Unspecified</pose>\n')
xml_file.write(' <truncated>0</truncated>\n')
xml_file.write(' <difficult>0</difficult>\n')
xml_file.write(' <bndbox>\n')
xml_file.write(' <xmin>' + str(spt[0]) + '</xmin>\n')
xml_file.write(' <ymin>' + str(spt[1]) + '</ymin>\n')
xml_file.write(' <xmax>' + str(spt[2]) + '</xmax>\n')
xml_file.write(' <ymax>' + str(spt[3]) + '</ymax>\n')
xml_file.write(' </bndbox>\n')
xml_file.write(' </object>\n')
xml_file.write('</annotation>')