forked from WuJie1010/Facial-Expression-Recognition.Pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess_CK+.py
87 lines (72 loc) · 2.45 KB
/
preprocess_CK+.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
# create data and label for CK+
# 0=anger 1=disgust, 2=fear, 3=happy, 4=sadness, 5=surprise, 6=contempt
# contain 135,177,75,207,84,249,54 images
import csv
import os
import numpy as np
import h5py
import skimage.io
ck_path = 'CK+48'
anger_path = os.path.join(ck_path, 'anger')
disgust_path = os.path.join(ck_path, 'disgust')
fear_path = os.path.join(ck_path, 'fear')
happy_path = os.path.join(ck_path, 'happy')
sadness_path = os.path.join(ck_path, 'sadness')
surprise_path = os.path.join(ck_path, 'surprise')
contempt_path = os.path.join(ck_path, 'contempt')
# # Creat the list to store the data and label information
data_x = []
data_y = []
datapath = os.path.join('data','CK_data.h5')
if not os.path.exists(os.path.dirname(datapath)):
os.makedirs(os.path.dirname(datapath))
# order the file, so the training set will not contain the test set (don't random)
files = os.listdir(anger_path)
files.sort()
for filename in files:
I = skimage.io.imread(os.path.join(anger_path,filename))
data_x.append(I.tolist())
data_y.append(0)
files = os.listdir(disgust_path)
files.sort()
for filename in files:
I = skimage.io.imread(os.path.join(disgust_path,filename))
data_x.append(I.tolist())
data_y.append(1)
files = os.listdir(fear_path)
files.sort()
for filename in files:
I = skimage.io.imread(os.path.join(fear_path,filename))
data_x.append(I.tolist())
data_y.append(2)
files = os.listdir(happy_path)
files.sort()
for filename in files:
I = skimage.io.imread(os.path.join(happy_path,filename))
data_x.append(I.tolist())
data_y.append(3)
files = os.listdir(sadness_path)
files.sort()
for filename in files:
I = skimage.io.imread(os.path.join(sadness_path,filename))
data_x.append(I.tolist())
data_y.append(4)
files = os.listdir(surprise_path)
files.sort()
for filename in files:
I = skimage.io.imread(os.path.join(surprise_path,filename))
data_x.append(I.tolist())
data_y.append(5)
files = os.listdir(contempt_path)
files.sort()
for filename in files:
I = skimage.io.imread(os.path.join(contempt_path,filename))
data_x.append(I.tolist())
data_y.append(6)
print(np.shape(data_x))
print(np.shape(data_y))
datafile = h5py.File(datapath, 'w')
datafile.create_dataset("data_pixel", dtype = 'uint8', data=data_x)
datafile.create_dataset("data_label", dtype = 'int64', data=data_y)
datafile.close()
print("Save data finish!!!")