forked from datitran/raccoon_dataset
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathgenerate_yolo_txt.py
82 lines (66 loc) · 2.64 KB
/
generate_yolo_txt.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
import pandas as pd
import argparse
from collections import namedtuple
from tqdm import tqdm
import os
def __split(df, group):
data = namedtuple('data', ['filename', 'object'])
gb = df.groupby(group)
return [data(filename, gb.get_group(x)) for filename, x in zip(gb.groups.keys(), gb.groups)]
def yolo_txt_from_csv(input_csv, input_names, output_dir):
with open(input_names, "r") as file:
names = file.read().split('\n')
df = pd.read_csv(input_csv)
grouped = __split(df, 'filename')
for group in tqdm(grouped, desc='groups'):
filename = group.filename
xs = []
ys = []
widths = []
heights = []
classes = []
for _, row in group.object.iterrows():
if not set(['class', 'width', 'height', 'xmin', 'xmax', 'ymin', 'ymax']).issubset(
set(row.index)):
pass
img_width = row['width']
img_height = row['height']
xmin = row['xmin']
ymin = row['ymin']
xmax = row['xmax']
ymax = row['ymax']
xs.append(round(xmin / img_width, 5))
ys.append(round(ymin / img_height, 5))
widths.append(round((xmax - xmin) / img_width, 5))
heights.append(round((ymax - ymin) / img_height, 5))
classes.append(row['class'])
txt_filename = os.path.splitext(filename)[0] + '.txt'
with open(os.path.join(output_dir, txt_filename), 'w+') as f:
for i in range(len(classes)):
f.write('{} {} {} {} {}\n'.format(names.index(classes[i]),
xs[i],
ys[i],
widths[i],
heights[i]))
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=
'Reads the contents of a CSV file, containing object annotations and their corresponding images\'s dimensions, and generates TXT files for use with darknet and YOLOv3'
)
parser.add_argument('input_csv',
metavar='input_csv',
type=str,
help='Path to the input CSV file')
parser.add_argument(
'input_names',
metavar='input_names',
type=str,
help='Path to the input .names file used by darknet, containing names of object classes')
parser.add_argument(
'output_dir',
metavar='output_dir',
type=str,
help='Directory where the .txt output files will be created, one for each image contained in the CSV fle'
)
args = parser.parse_args()
yolo_txt_from_csv(args.input_csv, args.input_names, args.output_dir)