-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathVOCOperationLibrary.py
152 lines (138 loc) · 5.76 KB
/
VOCOperationLibrary.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
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
"""
File:VOCOperationLibrary.py
"""
import sys
import os
import xml.etree.ElementTree as ET
import numpy as np
import random
import shutil
def _parseannotation(annofile):
"""
return a array include class name, box([cls, xmin, ymin, xmax, ymax])
and a tuple include the size of object((weight, height))
"""
if os.path.exists(annofile) == False:
raise FileNotFoundError
tree = ET.parse(annofile)
annos = []
for annoobject in tree.iter():
if 'size' in annoobject.tag:
for element in list(annoobject):
if 'height' in element.tag:
height = int(element.text)
if 'width' in element.tag:
weight = int(element.text)
for annoobject in tree.iter():
if 'object' in annoobject.tag:
for element in list(annoobject):
if 'name' in element.tag:
name = element.text
if 'bndbox' in element.tag:
for size in list(element):
if 'xmin' in size.tag:
xmin = size.text
if 'ymin' in size.tag:
ymin = size.text
if 'xmax' in size.tag:
xmax = size.text
if 'ymax' in size.tag:
ymax = size.text
annos.append([name, int(xmin), int(ymin), int(xmax), int(ymax)])
return annos, (weight, height)
def _deletesinglefile(annofile, delclass):
if os.path.exists(annofile) == False:
raise FileNotFoundError
tree = ET.parse(annofile)
root = tree.getroot()
annos = [anno for anno in root.iter()]
for i, anno in enumerate(annos):
if 'object' in anno.tag:
for element in list(anno):
if 'name' in element.tag:
if element.text in delclass:
root.remove(annos[i])
print(os.path.basename(annofile)+' have something deleted')
break
tree = ET.ElementTree(root)
tree.write(annofile, encoding="utf-8", xml_declaration=True)
def _changeone(annofile, oldcls, newcls, newsize=None):
if os.path.exists(annofile) == False:
raise FileNotFoundError
tree = ET.parse(annofile)
root = tree.getroot()
annos = [anno for anno in root.iter()]
for i, anno in enumerate(annos):
if newsize != None:
if 'width' in anno.tag:
oldwidth = float(anno.text)
anno.text = str(newsize[0])
sizechangerate_x = newsize[0] / oldwidth
if 'height' in anno.tag:
oldheight = float(anno.text)
anno.text = str(newsize[1])
sizechangerate_y = newsize[1] / oldheight
if 'object' in anno.tag:
for element in list(anno):
if oldcls != newcls:
if 'name' in element.tag:
if element.text == oldcls:
element.text = newcls
print(os.path.basename(annofile)+' change the class name')
break
if newsize != None:
if 'bndbox' in element.tag:
for coordinate in list(element):
if 'xmin' in coordinate.tag:
coordinate.text = str(int(int(coordinate.text) * sizechangerate_x))
if 'xmax' in coordinate.tag:
coordinate.text = str(int(int(coordinate.text) * sizechangerate_x))
if 'ymin' in coordinate.tag:
coordinate.text = str(int(int(coordinate.text) * sizechangerate_y))
if 'ymax' in coordinate.tag:
coordinate.text = str(int(int(coordinate.text) * sizechangerate_y))
tree = ET.ElementTree(root)
tree.write(annofile, encoding="utf-8", xml_declaration=True)
def _mergeone(anno1, anno2):
tree = ET.parse(anno1)
root = tree.getroot()
annos, size = _parseannotation(anno2)
if annos == None:
return
for annotation in annos:
appendobj(root, annotation)
tree.write(anno1, encoding='utf-8', xml_declaration=True)
def appendobj(root, annotation):
obj = ET.Element('object')
name = ET.SubElement(obj, 'name')
name.text = annotation[0]
pose = ET.SubElement(obj, 'pose')
pose.text = 'Unspecified'
truncated = ET.SubElement(obj, 'truncated')
truncated.text = '0'
difficult = ET.SubElement(obj, 'difficult')
difficult.text = '0'
bndbox = ET.SubElement(obj, 'bndbox')
xmin = ET.SubElement(bndbox, 'xmin')
xmin.text = str(annotation[1])
ymin = ET.SubElement(bndbox, 'ymin')
ymin.text = str(annotation[2])
xmax = ET.SubElement(bndbox, 'xmax')
xmax.text = str(annotation[3])
ymax = ET.SubElement(bndbox, 'ymax')
ymax.text = str(annotation[4])
root.append(obj)
return root
def _find_one(annofile, cls):
if os.path.exists(annofile) == False:
raise FileNotFoundError
tree = ET.parse(annofile)
root = tree.getroot()
annos = [anno for anno in root.iter()]
for i, anno in enumerate(annos):
if 'object' in anno.tag:
for element in list(anno):
if 'name' in element.tag:
if element.text in cls:
return os.path.basename(annofile)
break