-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlabels_voc.py
81 lines (63 loc) · 2.43 KB
/
labels_voc.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
#!/usr/bin/env python
#
# Copyright EAVISE
# Example: Transform annotations for VOCdevkit to the brambox pickle format
#
# modified by mileistone
import os
import sys
import xml.etree.ElementTree as ET
sys.path.insert(0, '.')
import brambox.boxes as bbb
DEBUG = True # Enable some debug prints with extra information
#ROOT = '/data2/yichaoxiong/data/VOCdevkit' # Root folder where the VOCdevkit is located
#ROOT = '/media/lishundong/DATA2/docker/data/VOCdevkit' # Root folder where the VOCdevkit is located
ROOT = "/home/ming.zhang04/data/VOCdevkit"
if not os.path.exists(os.path.join(ROOT, "onedet_cache")):
os.mkdir(os.path.join(ROOT, "onedet_cache"))
TRAINSET = [
('2012', 'train'),
('2012', 'val'),
('2007', 'train'),
('2007', 'val'),
]
TESTSET = [
('2007', 'test'),
]
def identify(xml_file):
root_dir = ROOT
root = ET.parse(xml_file).getroot()
folder = root.find('folder').text
filename = root.find('filename').text
return f'{root_dir}/{folder}/JPEGImages/{filename}'
if __name__ == '__main__':
print('Getting training annotation filenames')
train = []
for (year, img_set) in TRAINSET:
with open(f'{ROOT}/VOC{year}/ImageSets/Main/{img_set}.txt', 'r') as f:
ids = f.read().strip().split()
train += [f'{ROOT}/VOC{year}/Annotations/{xml_id}.xml' for xml_id in ids]
if DEBUG:
print(f'\t{len(train)} xml files')
print('Parsing training annotation files')
train_annos = bbb.parse('anno_pascalvoc', train, identify)
# Remove difficult for training
for k,annos in train_annos.items():
for i in range(len(annos)-1, -1, -1):
if annos[i].difficult:
del annos[i]
print('Generating training annotation file')
bbb.generate('anno_pickle', train_annos, f'{ROOT}/onedet_cache/train.pkl')
print()
print('Getting testing annotation filenames')
test = []
for (year, img_set) in TESTSET:
with open(f'{ROOT}/VOC{year}/ImageSets/Main/{img_set}.txt', 'r') as f:
ids = f.read().strip().split()
test += [f'{ROOT}/VOC{year}/Annotations/{xml_id}.xml' for xml_id in ids]
if DEBUG:
print(f'\t{len(test)} xml files')
print('Parsing testing annotation files')
test_annos = bbb.parse('anno_pascalvoc', test, identify)
print('Generating testing annotation file')
bbb.generate('anno_pickle', test_annos, f'{ROOT}/onedet_cache/test.pkl')