-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
66 lines (46 loc) · 1.31 KB
/
util.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
import numpy as np
import csv
import matplotlib.pyplot as plt
import time
"""
Load raw data from Kaggle Dataset and convert to numpy array
@return - numpy array[emotions, image pixels as numpy array]
"""
def load_raw_data_from_file():
#Kaggle Dataset
f = open('../Datasets/fer2013/fer2013.csv','r')
file_reader = csv.reader(f)
i = -1
#A list of emotions and image pixel as numpy array
d = []
for row in file_reader:
i += 1
#Text- Emotion , Pixel in first(0th) row
if i==0:
continue
#row[0] - emotion between 0-6 as string
emotion = int(row[0])
#row[1] - pixels as string in row major order, size - 48*48
pixel_list = row[1].split()
#convert pixel to numpy array as 48*48 matrix
image = np.array(pixel_list,dtype='float64').reshape(48,48)
d.append([emotion,image])
#Changing to numpy array
dataset = np.array(d,dtype=object)
return dataset
#Save preprocessed numpy array "dataset" to file "file_name" in binary format
def save_to_file(file_name,dataset):
np.save(file_name,dataset)
#Load preprocessed Numpy array from file file_name
def load_from_file(file_name):
dataset = np.load(file_name)
return dataset
"""
Show image for 2 seconds
@image_array - Numpy array as image
"""
def show_image(image_array):
plt.imshow(image_array)
plt.show(block=False)
time.sleep(2)
plt.close()