-
Notifications
You must be signed in to change notification settings - Fork 0
/
naemura2voc.py
94 lines (80 loc) · 2.11 KB
/
naemura2voc.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
#import libraries
import re
import os
from collections import defaultdict
# Constants
FILE_NAME = 'BIRD_v210_1.txt'
WIDTH = 5616
HEIGHT = 3744
S_DIR = 'xml/'
IMG_FOLDER = 'imgs'
# XML templates for annotation
obj_xml = '''\
<object>
<name>{name}</name>
<difficult>0</difficult>
<bndbox>
<xmin>{xmin}</xmin>
<ymin>{ymin}</ymin>
<xmax>{xmax}</xmax>
<ymax>{ymax}</ymax>
</bndbox>
</object>'''
top = '''\
<annotation>
<folder>{folder}</folder>
<filename>{f_name}</filename>
<size>
<width>{width}</width>
<height>{height}</height>
<depth>3</depth>
</size>\n'''
bottom = '''\
\n</annotation>'''
# Default value function for defaultdict
def def_value():
return []
# Read the annotation file
with open(FILE_NAME) as f:
ano_list = f.readlines()
# Define regex patterns
pattern_img = 'IMG.*.jpg'
pattern_ano = '\d*[\d,]+[bun]'
f_img = re.compile(pattern_img)
f_ano = re.compile(pattern_ano)
# Initialize variables
filename = ''
ano = defaultdict(def_value)
# Function to add annotations to dictionary
def ano_dic(s):
ano[filename].append(s.replace('\n', ''))
# Parse the annotation list
for s in ano_list:
if f_img.match(s):
filename = s.replace('\n', '')
elif f_ano.match(s):
ano_dic(s)
# Create directory for XML files if it doesn't exist
new_path = S_DIR
if not os.path.exists(new_path):
os.mkdir(new_path)
# Function to convert annotation string to XML format
def ano2xml(s):
l = s.split(',')
if l[4] == 'b':
obj = 'birds'
elif l[4] == 'n':
obj = 'non-birds'
else:
obj = 'undefined-object'
return obj_xml.format(name=obj, xmin=l[0], ymin=l[1], xmax=int(l[0])+int(l[2]), ymax=int(l[1])+int(l[3]))
# Function to write XML file
def write2xml(l, s):
with open(S_DIR + s[:-4] + '.xml', mode='w') as f:
f.write(top.format(folder=IMG_FOLDER, f_name=s, width=WIDTH, height=HEIGHT) + '\n'.join(l) + bottom)
# Generate and write XML files
for s in ano:
xml = []
for t in ano[s]:
xml.append(ano2xml(t))
write2xml(xml, s)