This repository has been archived by the owner on Dec 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_xml.py
114 lines (84 loc) · 3.5 KB
/
get_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
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
from itertools import count
import os
import xml.etree.ElementTree as ET
from xml.dom import minidom
import glob
def prettify(elem):
rough_string = ET.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
counter = 1
valid = False
for filename in sorted(glob.glob('/Users/isabel/Downloads/DATASETS/OFA_Dataset/Ell/*.txt')):
with open(os.path.join("OFA_Dataset/Ell", filename), 'r') as f:
text = f.read()
text = text[:-1]
a = text.split("\t")
centerx = float(a[0])
centery = float(a[1])
minor = float(a[2])
major = float(a[3])
xmin = centerx - major
ymax = centery + minor
xmax = centerx + major
ymin = centery - minor
if(counter == 16235):
counter = 1
valid = True
counter_str = str(counter)
if(valid):
if(len(counter_str) == 1):
n = 'Validation_0000'+str(counter)
if(len(counter_str) == 2):
n = 'Validation_000'+str(counter)
if(len(counter_str) == 3):
n = 'Validation_00'+str(counter)
if(len(counter_str) == 4):
n = 'Validation_0'+str(counter)
if(len(counter_str) == 5):
n = 'Validation_'+str(counter)
n_path = '/Users/isabel/Downloads/DATASETS/OFA_Dataset/Ref/'+n+'.jpeg'
save_name = '/Users/isabel/Downloads/DATASETS/newxmls/'+n+'.xml'
else:
if(len(counter_str) == 1):
n = 'Training_0000'+str(counter)
if(len(counter_str) == 2):
n = 'Training_000'+str(counter)
if(len(counter_str) == 3):
n = 'Training_00'+str(counter)
if(len(counter_str) == 4):
n = 'Training_0'+str(counter)
if(len(counter_str) == 5):
n = 'Training_'+str(counter)
n_path = '/Users/isabel/Downloads/DATASETS/OFA_Dataset/Ref/'+n+'.jpeg'
save_name = '/Users/isabel/Downloads/DATASETS/newxmls/'+n+'.xml'
root = ET.Element("annotation")
folder = ET.SubElement(root, 'folder')
folder.text = "Ref"
filenam = ET.SubElement(root, 'filename')
s = n+'.jpeg'
filenam.text = s
path = ET.SubElement(root, 'path')
path.text = n_path
source = ET.SubElement(root, 'source')
ET.SubElement(source, 'database').text = "Unknown"
size = ET.SubElement(root, 'size')
ET.SubElement(size, 'width').text = '480'
ET.SubElement(size, 'height').text = '480'
ET.SubElement(size, 'depth').text = '1'
segmented = ET.SubElement(root, 'segmented')
segmented.text = "0"
object = ET.SubElement(root, 'object')
ET.SubElement(object, 'name').text = 'mouse'
ET.SubElement(object, 'pose').text = 'Unspecified'
ET.SubElement(object, 'truncated').text = '0'
ET.SubElement(object, 'difficult').text = '0'
bndbox = ET.SubElement(object, 'bndbox')
ET.SubElement(bndbox, 'xmin').text = str(round(xmin))
ET.SubElement(bndbox, 'ymin').text = str(round(ymin))
ET.SubElement(bndbox, 'xmax').text = str(round(xmax))
ET.SubElement(bndbox, 'ymax').text = str(round(ymax))
tree = ET.ElementTree(root)
tree.write(save_name)
print(counter)
counter += 1