-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_label_from_csv.py
More file actions
49 lines (33 loc) · 1.19 KB
/
create_label_from_csv.py
File metadata and controls
49 lines (33 loc) · 1.19 KB
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
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 5 15:01:33 2019
@author: SarachErudite
"""
import pandas as pd
import csv
import os
# format csv file columns = [filename, class_id, xmin, xmax, ymin, ymax]
label_csv = "label_boxes.csv"
label_df = pd.read_csv(label_csv)
label_dir = 'labels'
current_path = os.getcwd()
# format for text file <class> <x_center> <y_center> <width> <height>
for r in range(len(label_df)):
row = label_df.iloc[r,:]
img_name = row.filename
file_path = os.path.join(current_path, label_dir, img_name).replace('.jpg', '.txt')
clss = row.class_id
xmin = row.xmin
xmax = row.xmax
ymin = row.ymin
ymax = row.ymax
x_cen = ((xmin + xmax)/2).values[0]
y_cen = ((ymin + ymax)/2).values[0]
width = (xmax-xmin).values[0]
height = (ymax-ymin).values[0]
with open(file_path, 'a', newline='') as csvFile:
text_line = [clss, x_cen, y_cen, width, height]
writer = csv.writer(csvFile, delimiter=' ', dialect= csv.unix_dialect, quoting=csv.QUOTE_NONE)
writer.writerow(text_line)
csvFile.close()
print('Generate labels done, check your directory at :', os.path.join(current_path, label_dir))