本文主要是介绍【人工智能项目】LBP+SVM人脸表情识别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
【人工智能项目】人脸表情识别
本次将采用传统的机器方法对人脸表情进行识别,主要步骤为先提取人脸表情的特征,拼接特征,送入机器学习模型当中训练测试,得到最终的检测结果。那开搞!!!
生成本次所需的图片
本次所用的数据是fer2013.csv数据,图片以像素值的形式保存在了csv文件中,所以我们需要先生成图片。
import pandas as pddf = pd.read_csv("./fer2013.csv")
df.head()
#encoding:utf-8
import pandas as pd
import numpy as np
import os
import cv2emotions = {"0":"anger","1":"disgust","2":"fear","3":"happy","4":"sad","5":"surprised","6":"normal"
}def createDir(dir):if os.path.exists(dir) is False:os.makedirs(dir)def saveImageFromFer2013(file):# 读取csv文件faces_data = pd.read_csv(file)imageCount = 0# 遍历csv文件内容,并将图片数据按分类保存for index in range(len(faces_data)):# 解析每一行csv文件内容emotion_data = faces_data.loc[index][0]image_data = faces_data.loc[index][1]usage_data = faces_data.loc[index][2]# 将图片数据转换为48*48data_array = list(map(float,image_data.split()))data_array = np.asarray(data_array)image = data_array.reshape(48,48)# 选择分类,并创建文件名dirName = usage_dataemotionName = emotions[str(emotion_data)]# 图片要保存的文件夹imagePath = os.path.join(dirName,emotionName)# 创建分类文件夹以及表情文件夹createDir(dirName)createDir(imagePath)# 图片文件名imageName = os.path.join(imagePath,str(index)+".jpg")# 保存图片cv2.imwrite(imageName,image)imageCount = indexprint("总共有"+str(imageCount)+"张图片")if __name__ == "__main__":saveImageFromFer2013("fer2013.csv")
总共有35886张图片
对生成的图片进行可视化展示分析。
# 可视化图像 anger disgust fear happy normal sad surprised
from tensorflow.keras.preprocessing.image import load_img,img_to_array
import matplotlib.pyplot as plt
import os
import warnings%matplotlib inline# 图像像素大小为48*48
pic_size = 48
plt.figure(0,figsize=(12,20))
cpt = 0
for expression in os.listdir("./Training/"):for i in range(1,6):cpt = cpt +1plt.subplot(7,5,cpt)img = load_img("./Training/"+expression+"/"+os.listdir("./Training/"+expression)[i],target_size=(pic_size,pic_size))plt.imshow(img,cmap="gray")
plt.tight_layout()
plt.show()
之后对训练图像中类别数量进行统计
# 统计训练图像中每个类别的数量
for expression in os.listdir("./Training/"):print(str(len(os.listdir("./Training/"+expression)))+" " + expression +" images")
95 anger images
436 disgust images
4097 fear images
7215 happy images
4965 normal images
4830 sad images
3171 surprised images
特征提取工作
import os
import numpy as np
from skimage import feature as skif
from skimage import io, transform
import random
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import SVR
<img src="./2.png">
def get_lbp_data(images_data, hist_size=256, lbp_radius=1, lbp_point=8):n_images = images_data.shape[0]hist = np.zeros((n_images, hist_size))for i in np.arange(n_images):# 使用LBP方法提取图像的纹理特征.lbp = skif.local_binary_pattern(images_data[i], lbp_point, lbp_radius, 'default')# 统计图像的直方图max_bins = int(lbp.max() + 1)# hist size:256hist[i], _ = np.histogram(lbp, normed=True, bins=max_bins, range=(0, max_bins))return hist
import os
import cv2anger_imgs_path = "./Training/anger/"
anger_hists = []
for img_path in os.listdir(anger_imgs_path):img = cv2.imread(os.path.join(anger_imgs_path,img_path))hist = get_lbp_data(img, hist_size=256, lbp_radius=1, lbp_point=8)anger_hists.append(hist)
anger_hists = np.array(anger_hists)
print(anger_hists.shape)
import numpy as np
anger_arr = np.zeros(3995)
anger_arr.shape
同理,对其余文件夹中的图片进行相同操作,生成各个表情的特征之后,对其拼接。
x_train = np.vstack((anger_hists,disgust_hists,fear_hists,happy_hists,normal_hists,sad_hists,surprised_hists))y_train = []anger_list = list(anger_arr)
disgust_list = list(disgust_arr)
fear_list = list(fear_arr)
happy_list = list(happy_arr)
normal_list = list(normal_arr)
sad_list = list(sad_arr)
surprised_list = list(surprised_arr)anger_list.extend(disgust_list)
anger_list.extend(fear_list)
anger_list.extend(happy_list)
anger_list.extend(normal_list)
anger_list.extend(sad_list)
anger_list.extend(surprised_list)y_train = np.array(anger_list)
x_train.shape
y_train.shape
重新划分
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.1,random_state=2019)
SVM模型
from sklearn.svm import SVCsvm = SVC(kernel="linear")
svm.fit(x_train,y_train)
SVC(C=1.0, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape=‘ovr’, degree=3, gamma=‘scale’, kernel=‘linear’,
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
from sklearn.externals import joblib
svm = joblib.load("svm_train_model.m")
print ("Done\n")
from sklearn.metrics import accuracy_score,f1_score,confusion_matrix,classification_reportfrom sklearn.externals import joblib
joblib.dump(svm, "svm_train_model.m")
print ("Done\n")
prediction =svm.predict(x_test)
score=accuracy_score(y_test,prediction)
import seaborn as snsgarbage_types = ['anger','disgust','fear','happy','normal','sad','surprised']
labels = {0:'anger',1:'disgust',2:'fear',3:'fear',4:'normal',5:'sad',6:'surprised'}acc = accuracy_score(y_test,prediction)
print(acc)
con_matrix = confusion_matrix(y_test, prediction,labels=[0, 1, 2, 3, 4, 5, 6])
plt.figure(figsize=(10, 10))
plt.title('Prediction of garbage types')
plt.ylabel('True label')
plt.xlabel('Predicted label')
# plt.show(sns.heatmap(con_matrix, annot=True, fmt="d",annot_kws={"size": 7},cmap='Blues',square=True))
ax = sns.heatmap(con_matrix, annot=True, fmt="d", annot_kws={"size": 7}, cmap='Blues', square=True)
bottom, top = ax.get_ylim()
ax.set_ylim(bottom + 0.5, top - 0.5)
plt.show()
小节
LBP+SVM人脸识别的准确率相比较深度学习比较一般,不过主要是让大家了解如何提取特征,送入机器学习模型当中。那么本次就到这里了,下次见!
这篇关于【人工智能项目】LBP+SVM人脸表情识别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!