-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_data.py
155 lines (118 loc) · 5.21 KB
/
create_data.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 2 15:27:26 2019
@authors: jaydeep thik , Vasudev Purandare
"""
import pandas as pd
import numpy as np
from PIL import Image
def generate():
data_folder = "./hackdataset"
df = pd.read_csv("./fer2013/fer2013.csv")
train_samples = df[df['Usage']=="Training"]
validation_samples = df[df["Usage"]=="PublicTest"]
test_samples = df[df["Usage"]=="PrivateTest"]
y_train = train_samples.emotion.astype(np.int32).values
y_valid = validation_samples.emotion.astype(np.int32).values
y_test = test_samples.emotion.astype(np.int32).values
i=0
for image, label in zip(train_samples.pixels, y_train):
#print(label)
img_array = np.fromstring(image, np.uint8, sep=" ").reshape((48,48))
if label==0:
im = Image.fromarray(img_array, 'L')
im.save(data_folder+'/train/Angry/A_'+str(i)+'.jpg')
i+=1
elif label==1:
im = Image.fromarray(img_array, 'L')
im.save(data_folder+'/train/Disgust/D_'+str(i)+'.jpg')
i+=1
elif label==2:
im = Image.fromarray(img_array, 'L')
im.save(data_folder+'/train/Fear/F_'+str(i)+'.jpg')
i+=1
elif label==3:
im = Image.fromarray(img_array, 'L')
im.save(data_folder+'/train/Happy/H_'+str(i)+'.jpg')
i+=1
elif label==4:
im = Image.fromarray(img_array, 'L')
im.save(data_folder+'/train/Sad/S_'+str(i)+'.jpg')
i+=1
elif label==5:
im = Image.fromarray(img_array, 'L')
im.save(data_folder+'/train/Surprise/S_'+str(i)+'.jpg')
i+=1
elif label==6:
im = Image.fromarray(img_array, 'L')
im.save(data_folder+'/train/Tran/T_'+str(i)+'.jpg')
i+=1
print(i)
for image, label in zip(test_samples.pixels, y_test):
#print(label)
img_array = np.fromstring(image, np.uint8, sep=" ").reshape((48,48))
if label==0:
im = Image.fromarray(img_array, 'L')
im.save(data_folder+'/test/Angry/A_'+str(i)+'.jpg')
i+=1
elif label==1:
im = Image.fromarray(img_array, 'L')
im.save(data_folder+'/test/Disgust/D_'+str(i)+'.jpg')
i+=1
elif label==2:
im = Image.fromarray(img_array, 'L')
im.save(data_folder+'/test/Fear/F_'+str(i)+'.jpg')
i+=1
elif label==3:
im = Image.fromarray(img_array, 'L')
im.save(data_folder+'/test/Happy/H_'+str(i)+'.jpg')
i+=1
elif label==4:
im = Image.fromarray(img_array, 'L')
im.save(data_folder+'/test/Sad/S_'+str(i)+'.jpg')
i+=1
elif label==5:
im = Image.fromarray(img_array, 'L')
im.save(data_folder+'/test/Surprise/S_'+str(i)+'.jpg')
i+=1
elif label==6:
im = Image.fromarray(img_array, 'L')
im.save(data_folder+'/test/Tran/T_'+str(i)+'.jpg')
i+=1
print(i)
for image, label in zip(validation_samples.pixels, y_valid):
# print(label)
img_array = np.fromstring(image, np.uint8, sep=" ").reshape((48,48))
if label==0:
im = Image.fromarray(img_array, 'L')
im.save(data_folder+'/valid/Angry/A_'+str(i)+'.jpg')
i+=1
elif label==1:
im = Image.fromarray(img_array, 'L')
im.save(data_folder+'/valid/Disgust/D_'+str(i)+'.jpg')
i+=1
elif label==2:
im = Image.fromarray(img_array, 'L')
im.save(data_folder+'/valid/Fear/F_'+str(i)+'.jpg')
i+=1
elif label==3:
im = Image.fromarray(img_array, 'L')
im.save(data_folder+'/valid/Happy/H_'+str(i)+'.jpg')
i+=1
elif label==4:
im = Image.fromarray(img_array, 'L')
im.save(data_folder+'/valid/Sad/S_'+str(i)+'.jpg')
i+=1
elif label==5:
im = Image.fromarray(img_array, 'L')
im.save(data_folder+'/valid/Surprise/S_'+str(i)+'.jpg')
i+=1
elif label==6:
im = Image.fromarray(img_array, 'L')
im.save(data_folder+'/valid/Tran/T_'+str(i)+'.jpg')
i+=1
#X_train =np.array([ np.fromstring(image, np.uint8, sep=" ").reshape((48,48)) for image in train_samples.pixels])
#X_valid =np.array([ np.fromstring(image, np.uint8, sep=" ").reshape((48,48)) for image in validation_samples.pixels])
#X_test =np.array([ np.fromstring(image, np.uint8, sep=" ").reshape((48,48)) for image in test_samples.pixels])
print(i)
#return X_train, y_train, X_valid, y_valid, X_test, y_test