-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
104 lines (93 loc) · 3.03 KB
/
dataset.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
95
96
97
98
99
100
101
102
103
104
import numpy as np
import pandas as pd
from PIL import Image
from tqdm import tqdm
import os
def atoi(s):
n = 0
for i in s:
n = n*10 + ord(i) - ord("0")
return n
# making folders
outer_names = ['test','train']
inner_names = ['angry', 'disgusted', 'fearful', 'happy', 'sad', 'surprised', 'neutral']
os.makedirs('data', exist_ok=True)
for outer_name in outer_names:
os.makedirs(os.path.join('data',outer_name), exist_ok=True)
for inner_name in inner_names:
os.makedirs(os.path.join('data',outer_name,inner_name), exist_ok=True)
# to keep count of each category
angry = 0
disgusted = 0
fearful = 0
happy = 0
sad = 0
surprised = 0
neutral = 0
angry_test = 0
disgusted_test = 0
fearful_test = 0
happy_test = 0
sad_test = 0
surprised_test = 0
neutral_test = 0
df = pd.read_csv('fer2013')
mat = np.zeros((48,48),dtype=np.uint8)
print("Saving images...")
# read the csv file line by line
for i in tqdm(range(len(df)-1)):
txt = df['pixels'][i]
words = txt.split()
# the image size is 48x48
for j in range(2304):
xind = j // 48
yind = j % 48
mat[xind][yind] = atoi(words[j])
img = Image.fromarray(mat)
# train
if i < 28709:
if df['emotion'][i] == 0:
img.save('train/angry/im'+str(angry)+'.png')
angry += 1
elif df['emotion'][i] == 1:
img.save('train/disgusted/im'+str(disgusted)+'.png')
disgusted += 1
elif df['emotion'][i] == 2:
img.save('train/fearful/im'+str(fearful)+'.png')
fearful += 1
elif df['emotion'][i] == 3:
img.save('train/happy/im'+str(happy)+'.png')
happy += 1
elif df['emotion'][i] == 4:
img.save('train/sad/im'+str(sad)+'.png')
sad += 1
elif df['emotion'][i] == 5:
img.save('train/surprised/im'+str(surprised)+'.png')
surprised += 1
elif df['emotion'][i] == 6:
img.save('train/neutral/im'+str(neutral)+'.png')
neutral += 1
# test
else:
if df['emotion'][i] == 0:
img.save('test/angry/im'+str(angry_test)+'.png')
angry_test += 1
elif df['emotion'][i] == 1:
img.save('test/disgusted/im'+str(disgusted_test)+'.png')
disgusted_test += 1
elif df['emotion'][i] == 2:
img.save('test/fearful/im'+str(fearful_test)+'.png')
fearful_test += 1
elif df['emotion'][i] == 3:
img.save('test/happy/im'+str(happy_test)+'.png')
happy_test += 1
elif df['emotion'][i] == 4:
img.save('test/sad/im'+str(sad_test)+'.png')
sad_test += 1
elif df['emotion'][i] == 5:
img.save('test/surprised/im'+str(surprised_test)+'.png')
surprised_test += 1
elif df['emotion'][i] == 6:
img.save('test/neutral/im'+str(neutral_test)+'.png')
neutral_test += 1
print("Done!")