-
Notifications
You must be signed in to change notification settings - Fork 0
/
yolo2voc.py
75 lines (70 loc) · 2.57 KB
/
yolo2voc.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
import argparse
from lxml import etree, objectify
import os
import cv2
from os.path import join
parse = argparse.ArgumentParser()
parse.add_argument('--class_list',
type=str,
default='classes.txt',
help='get class name from class_list file')
parse.add_argument('--label_dir',
type=str,
required=True,
help='convert yolo labels(txt file) in label_dir')
args = parse.parse_args()
label_path = os.path.abspath(args.label_dir)
if os.path.isfile(args.class_list):
list_file = args.class_list
else:
list_file = join(label_path, args.class_list)
class_list = []
with open(list_file) as f:
for line in f:
class_list.append(line.strip())
print(class_list)
if not os.path.isdir(label_path.replace('labels', 'annotations')):
os.mkdir(label_path.replace('labels', 'annotations'))
label_files = os.listdir(label_path)
for label_file in label_files:
if label_file == args.class_list:
continue
label_file = join(args.label_dir, label_file)
if label_file == args.class_list:
continue
image_file = label_file.replace('.txt', '.jpg').replace('labels', 'images')
voc_file = label_file.replace('.txt',
'.xml').replace('labels', 'annotations')
if not os.path.isfile(image_file):
print("Image {} doesn't exist!".format(image_file))
continue
img = cv2.imread(image_file)
shape = img.shape
with open(label_file) as f:
print('Processing {}'.format(label_file))
E = objectify.ElementMaker(annotate=False)
# xml head
anno_tree = E.annotation(
E.filename(image_file.split('/')[-1]),
E.size(E.width(shape[1]), E.height(shape[0]), E.depth(shape[2])),
E.segmented(0))
for line in f:
[c, x, y, w, h] = line.split(' ')
if int(c) >= len(class_list):
print('error label: ' + c)
continue
name = class_list[int(c)]
x = float(x) * shape[1]
y = float(y) * shape[0]
w = float(w) * shape[1]
h = float(h) * shape[0]
xmin = x - w / 2
ymin = y - h / 2
xmax = x + w / 2
ymax = y + h / 2
obj = E.object(
E.name(name),
E.bndbox(E.xmin(int(xmin)), E.ymin(int(ymin)),
E.xmax(int(xmax)), E.ymax(int(ymax))), E.difficult(0))
anno_tree.append(obj)
etree.ElementTree(anno_tree).write(voc_file, pretty_print=True)