-
Notifications
You must be signed in to change notification settings - Fork 13
/
raccoon_annotation.py
53 lines (42 loc) · 1.6 KB
/
raccoon_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
import xml.etree.ElementTree as ET
from os import getcwd
import random
sets=[('raccoon_train'), ('raccoon_test')]
classes = ["raccoon"]
def convert_annotation(image_id, list_file):
in_file = open('Raccoon_dataset/annotations/raccoon-%s.xml'%(image_id))
tree=ET.parse(in_file)
root = tree.getroot()
for obj in root.iter('object'):
difficult = obj.find('difficult').text
cls = obj.find('name').text
if cls not in classes or int(difficult)==1:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
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))
wd = getcwd() # current working path
# 1 arrange train, val, test dataset:
ids = list(range(1,201))
random.shuffle(ids)
train = ids[:160]
test = ids[160:]
dataset = [train, test]
k = 0
for image_set in dataset:
subset_name = sets[k]
k += 1
image_ids = open('Raccoon_dataset/%s.txt'%(subset_name), 'w')
for i in range(len(image_set)):
image_ids.write('%d\n'%(image_set[i]))
image_ids.close()
# 2 produce train, val, test data:
for image_set in sets:
image_ids = open('Raccoon_dataset/%s.txt'%(image_set)).read().strip().split()
list_file = open('Raccoon_dataset/%s_data.txt'%(image_set), 'w')
for image_id in image_ids:
list_file.write('Raccoon_dataset/images/raccoon-%s.jpg'%(image_id))
convert_annotation(image_id, list_file)
list_file.write('\n')
list_file.close()