-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathemotion_data_pre_ver0.1_160927.py
More file actions
132 lines (115 loc) · 3.97 KB
/
emotion_data_pre_ver0.1_160927.py
File metadata and controls
132 lines (115 loc) · 3.97 KB
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
#Ryan Shin: sungjin7127@gmail.com
#Date: 160927
#Obj: load all the data and do image preprocessing + convert into npz dataset
"""1. convert paths variable into your own image folder
2. convert imgsize into your desired size
"""
#Basic Generating Dataset
import cv2
import numpy as np
import os
from scipy.misc import imread, imresize
import matplotlib.pyplot as plt
import skimage.io
import skimage.transform
#%matplotlib inline
print("Package loaded")
cwd = os.getcwd()
print ("Current folder is %s" % (cwd) )
################################################################################
#Specify folder paths + reshape size + grayscale
#Training Set folder: change this
paths = {"dataset/anger/", "dataset/contempt/", "dataset/disgust/", "dataset/fear",
"dataset/happy/", "dataset/neutral/", "dataset/sadness/", "dataset/surprise/"}
#The reshape size
imgsize = [64, 64]
#Grayscale
use_gray = 1
#Save name
data_name = "data_gray"
######################################################################################3
print ("Your image should be at")
for i, path in enumerate(paths):
print (" [%d/%d] %s/%s" % (i, len(paths), cwd, path))
print ("Data will be save to %s"
% (cwd + '/data/' + data_name + '.npz'))
"""
#RGB2Gray Fucn.
def rgb2gray(rgb):
if len(rgb.shape) is 3:
return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])
else:
print("Current Image if Gray")
return rgb
"""
#RGB2Gray Opencv2 Func.
def rgb2gray(rgb):
if len(rgb.shape) is 3:
print("Image is not Gray")
return cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)
else:
print("Current Image if Gray")
return rgb
#Load Image
nclass = len(paths)
valid_exts = [".jpg",".gif",".png",".tga",".jpeg"]
imgcnt = 0
gray_cnt = 0
for i, relpath in zip(range(nclass), paths):
path = cwd + "/" + relpath
flist = os.listdir(path)
for f in flist:
if os.path.splitext(f)[1].lower() not in valid_exts:
continue
fullpath = os.path.join(path, f)
currimg = cv2.imread(fullpath)
#currimg = cv2.bilateralFilter(currimg, 9, 75, 75) #Image smoothing
#Convert to grayscale
if use_gray:
grayimg = rgb2gray(currimg)
#grayimg = cv2.cvtColor(currimg, cv2.COLOR_BGR2GRAY)
else:
print("Current Image if Gray")
grayimg = currimg
# Reshape
graysmall = cv2.resize(grayimg, (imgsize[0], imgsize[1]), interpolation = cv2.INTER_CUBIC) / 255. #Normalization
grayvec = np.reshape(graysmall, (1, -1))
# Save
curr_label = np.eye(nclass, nclass)[i:i+1, :]
if imgcnt is 0:
totalimg = grayvec
totallabel = curr_label
else:
totalimg = np.concatenate((totalimg, grayvec), axis = 0)
totallabel = np.concatenate((totallabel, curr_label), axis = 0)
imgcnt = imgcnt + 1
print ("Total %d images loaded." % (imgcnt))
#Divide total Data into Training and Test Set
def print_shape(string, x):
print ("Shape of '%s' is %s" % (string, x.shape,))
randidx = np.random.randint(imgcnt, size=imgcnt)
trainidx = randidx[0:int(3*imgcnt/5)]
testidx = randidx[int(3*imgcnt/5):imgcnt]
trainimg = totalimg[trainidx, :]
trainlabel = totallabel[trainidx, :]
testimg = totalimg[testidx, :]
testlabel = totallabel[testidx, :]
print_shape("trainimg", trainimg)
print_shape("trainlabel", trainlabel)
print_shape("testimg", testimg)
print_shape("testlabel", testlabel)
#Save to Npz
savepath = cwd + "/dataset/" + data_name + ".npz"
np.savez(savepath, trainimg=trainimg, trainlabel=trainlabel,
testimg = testimg, testlabel = testlabel, imgsize=imgsize)
print ("Saved to %s" % (savepath))
#See what's in here
l.files
#Parse data
trainimg_loaded = l['trainimg']
trainlabel_loaded = l['trainlabel']
testimg_loaded = l['testimg']
testlabel_loaded = l['testlabel']
print ("%d train image loaded" % (trainimg_loaded.shape[0]))
print ("%d test images loaded" % (testimg_loaded.shape[0]))
print ("Loaded from to %s" % (savepath))