-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre_process.py
34 lines (29 loc) · 985 Bytes
/
pre_process.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
import pandas as pd
import cv2
import numpy as np
dataset_path = 'data/train.csv' # 文件保存位置
image_size = (48, 48) # 图片大小
# 载入数据
# faces 为表情的灰度矩阵列表 emotions 为各表情所对应的心情序号
def load_fer2013():
data = pd.read_csv(dataset_path)
pixels = data['pixels'].tolist()
width, height = 48, 48
faces = []
for pixel_sequence in pixels:
face = [int(pixel) for pixel in pixel_sequence.split(' ')]
face = np.asarray(face).reshape(width, height)
face = cv2.resize(face.astype('uint8'), image_size)
faces.append(face.astype('float32'))
faces = np.asarray(faces)
faces = np.expand_dims(faces, -1)
emotions = pd.get_dummies(data['emotion'])
return faces, emotions
# 将数据归一化
def preprocess_input(x, v2=True):
x = x.astype('float32')
x = x / 255.0
if v2:
x = x - 0.5
x = x * 2.0
return x