-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasetGenerator.py
161 lines (139 loc) · 6 KB
/
datasetGenerator.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
153
154
155
156
157
158
159
160
161
import os
# import xml.etree.cElementTree as ET
from lxml import etree as ET
import inspect
import csv
import shutil
class Parser():
def __init__(self, path=None,percentage=1.0):
if not os.path.isdir('train'):
os.mkdir('train')
os.mkdir('train/images')
os.mkdir('train/annotations')
if not os.path.isdir('test'):
os.mkdir('test')
os.mkdir('test/images')
os.mkdir('test/annotations')
self.datasetPath = path
self.imgPath = './train/images'
self.annotationsPath = './train/annotations'
self.labels = set()
# print([x if os.path.isdir(x)==True for x in os.listdir('./dataset')])
self.LISAdatasetPath = './dataset'
self.datasetDirs = []
for x in os.listdir(self.LISAdatasetPath):
if os.path.isdir(os.path.join(self.LISAdatasetPath,x)) and ("vid" in x):
print('>>',x)
self.datasetDirs.append(os.path.join(self.LISAdatasetPath,x))
sorted(self.datasetDirs)
print(self.datasetDirs)
self.trainDataLen = int(percentage*len(self.datasetDirs))
self.trainDatasetPath = self.datasetDirs[:self.trainDataLen]
self.testDataLen = len(self.datasetDirs)-self.trainDataLen
self.testDatasetPath = self.datasetDirs[self.trainDataLen+1:]
print('train >>',self.trainDataLen)
print('train >>',self.trainDatasetPath)
print('test >>',self.testDataLen)
print('test >>',self.testDatasetPath)
print('\n\n')
def generateDataset(self):
# generate training dataset
self.FileSequence = []
self.imgPath = './train/images'
self.annotationsPath = './train/annotations'
for subdir in self.trainDatasetPath:
subdir = os.path.join(subdir,os.listdir(subdir)[0])
print('trainset >>',subdir)
self.datasetPath=subdir
self.generateDatasetFiles()
self.generateFileSequence('./train')
# generate testing dataset
self.FileSequence = []
self.imgPath = './test/images'
self.annotationsPath = './test/annotations'
for subdir in self.testDatasetPath:
subdir = os.path.join(subdir,os.listdir(subdir)[0])
print('testset >>',subdir)
self.datasetPath=subdir
self.generateDatasetFiles()
self.generateFileSequence('./test')
print(self.labels)
self.generateLabels()
def generateDatasetFiles(self):
with open(os.path.join(self.datasetPath,'frameAnnotations.csv')) as csvfile:
anotations_list = csvfile.readlines()
# print(anotations_list)
anotations_list.pop(0)
for sample in anotations_list:
sample = sample.split(';')
# print(sample)
self.labels.add(sample[1])
self.generateXML(file=sample[0],
label=sample[1],
_bndbox={
"xmin": sample[2],
"ymin": sample[3],
"xmax": sample[4],
"ymax": sample[5]})
shutil.copy(
os.path.join(self.datasetPath,sample[0]),
self.imgPath)
self.FileSequence.append(sample[0])
# break
def generateFileSequence(self,filePath):
with open(os.path.join(filePath,'FileSequence.txt'),'w') as fp:
for _file in self.FileSequence:
fp.write(_file+'\n')
def generateLabels(self):
with open(os.path.join('./labels.txt'),'w') as fp:
for label in self.labels:
fp.write(label+'\n')
def generateXML(self, folder='VOC2008',
file='00002.png',
_shape={
"width":704,
"height":480,
"depth":3,
},
label="person",
_bndbox={
"xmin": 135,
"ymin": 25,
"xmax": 236,
"ymax": 188}):
root = ET.Element("annotations")
ET.SubElement(root, "folder").text = folder
ET.SubElement(root, "filename").text = file
source = ET.SubElement(root, "source")
ET.SubElement(source, "database").text = "The VOC2007 Database"
ET.SubElement(source, "annotation").text = "PASCAL VOC2007"
ET.SubElement(source, "image").text = "flickr"
ET.SubElement(source, "flickrid").text = "341012865"
owner = ET.SubElement(root, "owner")
ET.SubElement(owner, "flickrid").text = "341012865"
ET.SubElement(owner, "name").text = "John Doe"
size = ET.SubElement(root, "size")
ET.SubElement(size, "width").text = str(_shape["width"])
ET.SubElement(size, "height").text = str(_shape["height"])
ET.SubElement(size, "depth").text = str(_shape["depth"])
segmented = ET.SubElement(root, "segmented").text = str(0)
_object = ET.SubElement(root, "object")
ET.SubElement(_object, "name").text = str(label)
ET.SubElement(_object, "pose").text = "left"
ET.SubElement(_object, "truncated").text = "0"
ET.SubElement(_object, "difficult").text = "0"
bndbox = ET.SubElement(_object, "bndbox")
ET.SubElement(bndbox, "xmin").text = str(_bndbox["xmin"])
ET.SubElement(bndbox, "xmax").text = str(_bndbox["xmax"])
ET.SubElement(bndbox, "ymin").text = str(_bndbox["ymin"])
ET.SubElement(bndbox, "ymax").text = str(_bndbox["ymax"])
tree = ET.ElementTree(root)
# print(inspect.getargspec(tree.write))
tree.write(
os.path.join(self.annotationsPath, "{}.xml".format(file)),
pretty_print=True)
if __name__ == '__main__':
gen = Parser(path='./dataset/vid0/frameAnnotations-vid_cmp2.avi_annotations/')
# gen = Parser()
gen.generateDataset()
# gen.generateXML()