-
Notifications
You must be signed in to change notification settings - Fork 1
/
createCSV.py
75 lines (63 loc) · 1.93 KB
/
createCSV.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 os
import glob
import numpy as np
import pandas as pd
## some code from https://www.kaggle.com/alexozerin/end-to-end-baseline-tf-estimator-lb-0-72
## create csv file that looks like
# imageName class
# bed_xxx.png bed
# better yet
# imageName class
# bed_xxx.png 1
def image2csv(what = None):
if not what or what not in ['train','test','valid']:
raise ValueError("Expected train/test/valid got None")
else:
fname = str(what)+'.csv'
POSSIBLE_LABELS = 'yes no up down left right on off stop go silence unknown'.split()
id2name = {i: name for i, name in enumerate(POSSIBLE_LABELS)}
name2id = {name: i for i, name in id2name.items()}
PATH = os.getcwd()+os.sep+'images'
TRAINPATH = PATH+os.sep+'train'
TESTPATH = PATH+os.sep+'test'
VALIDATIONPATH = PATH+os.sep+'valid'
if what == 'train':
USEPATH = TRAINPATH
elif what == 'test':
USEPATH = TESTPATH
elif what == 'valid':
USEPATH = VALIDATIONPATH
else:
raise ValueError("Expected train/test/valid got None")
files = os.listdir(USEPATH)
classes = []
images = []
for f in files:
if 'back' in f:
getClass = 'silence'
else:
split = f.split('_')
getClass = split[0]
if getClass not in POSSIBLE_LABELS:
getClass = 'unknown'
else:
getClass = getClass
classes.append(getClass)
images.append(f)
for i in range(len(classes)):
classes[i] = name2id[classes[i]]
#print(images[i],classes[i])
# df = pd.DataFrame(
# {
# 'image':images,
# 'label': classes[i]
# }
# )
df = pd.DataFrame(list(zip(images,classes)),
columns=['image','label'])
df.to_csv(fname)
# print(df.head(10))
if __name__ == '__main__':
# image2csv(what = 'test')
# image2csv(what = 'valid')
image2csv(what = 'train')