JAFFE数据集一共有213张图像.选取了10名日本女学生,每个人做出7种表情.7种表情包括: Angry,Disgust,Fear,Happy,Sad,Surprise,Neutral.(愤怒,厌恶,恐惧,高兴,悲伤,惊讶,中性)
将人脸区域裁剪出,去除多余干扰,然后将获得的人脸区域缩放为48*48大小的图像,存储为.csv格式数据.
#!/usr/bin/python
# coding:utf8
import cv2
import os
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import csv
def detect(img, cascade):
rects = cascade.detectMultiScale(img, scaleFactor=1.3, minNeighbors=4, minSize=(30, 30),flags=cv2.CASCADE_SCALE_IMAGE)
if len(rects) == 0:
return []
rects[:, 2:] += rects[:, :2]
return rects
cascade = cv2.CascadeClassifier("/home/.../data/haarcascades/haarcascade_frontalface_alt.xml")
f = "/home/w/mycode/jaffe/"
fs = os.listdir(f)
data = np.zeros([213, 48*48], dtype=np.uint8)
label = np.zeros([213], dtype=int)
i = 0
for f1 in fs:
tmp_path = os.path.join(f, f1)
if not os.path.isdir(tmp_path):
# print(tmp_path[len(f):])
img = cv2.imread(tmp_path, 1)
dst = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
rects = detect(dst, cascade)
for x1, y1, x2, y2 in rects:
cv2.rectangle(img,(x1+10,y1+20),(x2-10,y2),(0,255,255),2)
# 调整截取脸部区域大小
img_roi = np.uint8([y2-(y1+20), (x2-10)-(x1+10)])
roi = dst[y1+20:y2, x1+10:x2-10]
img_roi = roi
re_roi = cv2.resize(img_roi, (48,48))
# 获得表情label
img_label = tmp_path[len(f)+3:len(f)+5]
# print(img_label)
if img_label == 'AN':
label[i] = 0
elif img_label == 'DI':
label[i] = 1
elif img_label == 'FE':
label[i] = 2
elif img_label == 'HA':
label[i] = 3
elif img_label == 'SA':
label[i] = 4
elif img_label == 'SU':
label[i] = 5
elif img_label == 'NE':
label[i] = 6
else:
print("get label error.......\n")
data[i][0:48*48] = np.ndarray.flatten(re_roi)
i = i + 1
# cv2.imshow("src", dst)
# cv2.imshow("img", img)
# if cv2.waitKey() == 32:
# continue
with open(r"/home/.../face.csv","w") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['emotion', 'pixels'])
for i in range(len(label)):
data_list = list(data[i])
b = " ".join(str(x) for x in data_list)
l = np.hstack([label[i], b])
writer.writerow(l)
从保存的face.csv中查看保存的表情数据:
#!/usr/bin/python
# coding:utf8
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
emotion ={0:'Angry',1:'Disgust',2:'Fear',3:'Happy',4:'Sad',5:'Surprise',6:'Neutral'}
data = pd.read_csv(r'/home/.../face.csv', dtype='a')
label = np.array(data['emotion'])
img_data = np.array(data['pixels'])
N_sample = label.size
Face_data = np.zeros((N_sample, 48*48))
Face_label = np.zeros((N_sample, 7), dtype=int)
# # 显示人脸以及对应表情
for i in range(25):
x = img_data[i]
x = np.fromstring(x, dtype=float, sep=' ')
x = x/x.max()
img_x = np.reshape(x, (48, 48))
plt.subplot(5,5,i+1)
plt.axis('off')
plt.title(emotion[int(label[i])])
plt.imshow(img_x, plt.cm.gray)
plt.show()
输出:
从保存的表情数据中查看个部分表情的数量.
#!/usr/bin/python
# coding:utf8
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
emotion ={0:'Angry',1:'Disgust',2:'Fear',3:'Happy',4:'Sad',5:'Surprise',6:'Neutral'}
data = pd.read_csv(r'/home/w/下载/face.csv', dtype='a')
label = np.array(data['emotion'])
img_data = np.array(data['pixels'])
N_sample = label.size
emotions = np.zeros(7)
for i in label:
for j in range(7):
if int(i) == j:
emotions[j] = emotions[j] + 1
print(emotions)
plt.bar(range(7),emotions, 0.5, color= ['red','green','blue'])
plt.xlabel('emotions')
plt.xticks(range(7), ['Angry','Disgust','Fear','Happy','Sad','Surprise','Neutral'], rotation=0)
plt.ylabel('number')
plt.grid()
plt.show()
The Japanese Female Facial Expression (JAFFE) Database