Skip to content

Commit 4126a7e

Browse files
authored
Image data creator from FER-2013 dataset (files merged seperately)
1 parent 946de99 commit 4126a7e

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed

create_data.py

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Sat Nov 2 15:27:26 2019
4+
5+
@authors: jaydeep thik , Vasudev Purandare
6+
"""
7+
8+
import pandas as pd
9+
import numpy as np
10+
from PIL import Image
11+
12+
def generate():
13+
data_folder = "./hackdataset"
14+
df = pd.read_csv("./fer2013/fer2013.csv")
15+
16+
train_samples = df[df['Usage']=="Training"]
17+
validation_samples = df[df["Usage"]=="PublicTest"]
18+
test_samples = df[df["Usage"]=="PrivateTest"]
19+
20+
y_train = train_samples.emotion.astype(np.int32).values
21+
y_valid = validation_samples.emotion.astype(np.int32).values
22+
y_test = test_samples.emotion.astype(np.int32).values
23+
i=0
24+
for image, label in zip(train_samples.pixels, y_train):
25+
#print(label)
26+
img_array = np.fromstring(image, np.uint8, sep=" ").reshape((48,48))
27+
if label==0:
28+
29+
im = Image.fromarray(img_array, 'L')
30+
im.save(data_folder+'/train/Angry/A_'+str(i)+'.jpg')
31+
i+=1
32+
33+
elif label==1:
34+
35+
im = Image.fromarray(img_array, 'L')
36+
im.save(data_folder+'/train/Disgust/D_'+str(i)+'.jpg')
37+
i+=1
38+
39+
elif label==2:
40+
41+
im = Image.fromarray(img_array, 'L')
42+
im.save(data_folder+'/train/Fear/F_'+str(i)+'.jpg')
43+
i+=1
44+
elif label==3:
45+
46+
im = Image.fromarray(img_array, 'L')
47+
im.save(data_folder+'/train/Happy/H_'+str(i)+'.jpg')
48+
i+=1
49+
50+
elif label==4:
51+
52+
im = Image.fromarray(img_array, 'L')
53+
im.save(data_folder+'/train/Sad/S_'+str(i)+'.jpg')
54+
i+=1
55+
elif label==5:
56+
57+
im = Image.fromarray(img_array, 'L')
58+
im.save(data_folder+'/train/Surprise/S_'+str(i)+'.jpg')
59+
i+=1
60+
elif label==6:
61+
62+
im = Image.fromarray(img_array, 'L')
63+
im.save(data_folder+'/train/Tran/T_'+str(i)+'.jpg')
64+
i+=1
65+
print(i)
66+
67+
for image, label in zip(test_samples.pixels, y_test):
68+
#print(label)
69+
img_array = np.fromstring(image, np.uint8, sep=" ").reshape((48,48))
70+
if label==0:
71+
72+
im = Image.fromarray(img_array, 'L')
73+
im.save(data_folder+'/test/Angry/A_'+str(i)+'.jpg')
74+
i+=1
75+
76+
elif label==1:
77+
78+
im = Image.fromarray(img_array, 'L')
79+
im.save(data_folder+'/test/Disgust/D_'+str(i)+'.jpg')
80+
i+=1
81+
82+
elif label==2:
83+
84+
im = Image.fromarray(img_array, 'L')
85+
im.save(data_folder+'/test/Fear/F_'+str(i)+'.jpg')
86+
i+=1
87+
elif label==3:
88+
89+
im = Image.fromarray(img_array, 'L')
90+
im.save(data_folder+'/test/Happy/H_'+str(i)+'.jpg')
91+
i+=1
92+
93+
elif label==4:
94+
95+
im = Image.fromarray(img_array, 'L')
96+
im.save(data_folder+'/test/Sad/S_'+str(i)+'.jpg')
97+
i+=1
98+
elif label==5:
99+
100+
im = Image.fromarray(img_array, 'L')
101+
im.save(data_folder+'/test/Surprise/S_'+str(i)+'.jpg')
102+
i+=1
103+
elif label==6:
104+
105+
im = Image.fromarray(img_array, 'L')
106+
im.save(data_folder+'/test/Tran/T_'+str(i)+'.jpg')
107+
i+=1
108+
print(i)
109+
110+
for image, label in zip(validation_samples.pixels, y_valid):
111+
# print(label)
112+
img_array = np.fromstring(image, np.uint8, sep=" ").reshape((48,48))
113+
if label==0:
114+
115+
im = Image.fromarray(img_array, 'L')
116+
im.save(data_folder+'/valid/Angry/A_'+str(i)+'.jpg')
117+
i+=1
118+
119+
elif label==1:
120+
121+
im = Image.fromarray(img_array, 'L')
122+
im.save(data_folder+'/valid/Disgust/D_'+str(i)+'.jpg')
123+
i+=1
124+
125+
elif label==2:
126+
127+
im = Image.fromarray(img_array, 'L')
128+
im.save(data_folder+'/valid/Fear/F_'+str(i)+'.jpg')
129+
i+=1
130+
elif label==3:
131+
132+
im = Image.fromarray(img_array, 'L')
133+
im.save(data_folder+'/valid/Happy/H_'+str(i)+'.jpg')
134+
i+=1
135+
136+
elif label==4:
137+
138+
im = Image.fromarray(img_array, 'L')
139+
im.save(data_folder+'/valid/Sad/S_'+str(i)+'.jpg')
140+
i+=1
141+
elif label==5:
142+
143+
im = Image.fromarray(img_array, 'L')
144+
im.save(data_folder+'/valid/Surprise/S_'+str(i)+'.jpg')
145+
i+=1
146+
elif label==6:
147+
148+
im = Image.fromarray(img_array, 'L')
149+
im.save(data_folder+'/valid/Tran/T_'+str(i)+'.jpg')
150+
i+=1
151+
#X_train =np.array([ np.fromstring(image, np.uint8, sep=" ").reshape((48,48)) for image in train_samples.pixels])
152+
#X_valid =np.array([ np.fromstring(image, np.uint8, sep=" ").reshape((48,48)) for image in validation_samples.pixels])
153+
#X_test =np.array([ np.fromstring(image, np.uint8, sep=" ").reshape((48,48)) for image in test_samples.pixels])
154+
155+
print(i)
156+
#return X_train, y_train, X_valid, y_valid, X_test, y_test

0 commit comments

Comments
 (0)