-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_annotation.py
77 lines (73 loc) · 2.64 KB
/
new_annotation.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu May 10 23:30:29 2018
@author: liushenghui
"""
import xml.etree.ElementTree as ET
from os import getcwd
import os
import pickle
import pdb
with open('class.pkl', 'rb') as f:
classes = pickle.load(f)
cc = []
def convert_annotation(file):
in_file = open(file)
tree=ET.parse(in_file)
root = tree.getroot()
for obj in root.iter('subcomponent'):
cls = obj.find('name').text
try:
category = obj.find('category').text
cls = cls+category
except:
pass
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
if 'shoes' in cls:
xmlbox = obj
try:
b = (int(xmlbox.find('xmin_l').text), int(xmlbox.find('ymin_l').text), int(xmlbox.find('xmax_l').text), int(xmlbox.find('ymax_l').text))
list_file.write(" " + ",".join([str(a) for a in b]) + ',' + str(cls_id))
except:
# pdb.set_trace()
pass
try:
b = (int(xmlbox.find('xmin_r').text), int(xmlbox.find('ymin_r').text), int(xmlbox.find('xmax_r').text), int(xmlbox.find('ymax_r').text))
list_file.write(" " + ",".join([str(a) for a in b]) + ',' + str(cls_id))
except:
pass
if cls_id not in cc:
cc.append(cls_id)
else:
if 'bag' in cls:
xmlbox = obj.find('id_1')
xmlbox = xmlbox.find('bndbox')
try:
b = (int(xmlbox.find('xmin').text), int(xmlbox.find('ymin').text), int(xmlbox.find('xmax').text), int(xmlbox.find('ymax').text))
list_file.write(" " + ",".join([str(a) for a in b]) + ',' + str(cls_id))
if cls_id not in cc:
cc.append(cls_id)
except:
pass
wd = getcwd()
anno_path = 'TRAIN/ANNOTATIONS_TRAIN'
anno_files = os.listdir(anno_path)
anno_files.remove('.DS_Store')
label = []
list_file = open('train.txt', 'w')
for file in anno_files:
path = anno_path+'/'+file
list_file.write('TRAIN/IMAGES_TRAIN'+'/'+file.split('.')[0]+'.jpg')
convert_annotation(path)
list_file.write('\n')
list_file.close()
#for year, image_set in sets:
# image_ids = open('VOCdevkit/VOC%s/ImageSets/Main/%s.txt'%(year, image_set)).read().strip().split()
# list_file = open('%s_%s.txt'%(year, image_set), 'w')
# for image_id in image_ids:
# list_file.write('%s/VOCdevkit/VOC%s/JPEGImages/%s.jpg'%(wd, year, image_id))
# convert_annotation(year, image_id, list_file)
# list_file.write('\n')
# list_file.close()