OpenCV实践之路——用OpenCV和Dlib进行人脸颜值预测(Python)
来源:互联网 发布:股票交易软件编程 编辑:程序博客网 时间:2024/05/29 07:23
参考:http://blog.csdn.net/xingchenbingbuyu/article/details/52804013
本项目部分基于这个博客Computer Vision for Predicting Facial Attractiveness。按照上面这个博客复现起来比较麻烦(当然是对我而言),而且我想要做的是对于任意一张人脸进行检测之后都可以进行颜值估计,但是他给出的代码还需要进行改动不少才行。现在就把自己的复现过程记录下来。
本文中只展示了部分关键代码。完整代码和测试用的数据可以在GitHub上获取。
不同点
- 在提取人脸关键点的时候,原文中提到用的是CLM framework,本文用的是之前配置好的Dlib。
- 实现了对自选图片中人脸的颜值估计
用到的Python库
- numpy
- opencv
- dlib
- sklearn
主要步骤
- 训练模型
- 人脸关键点提取
- 特征生成
- 颜值预测
1、训练模型
训练一个模型最重要的就是输入数据和模型的选取。这里训练模型用到的输入数据是给出的features_All.txt和rating.txt。这是华南理工大学人机智能交互实验室给出的数据集中提取出来的特征和标签。一共用了500幅人脸图像。
root = 'E:/Github/predict-facial-attractiveness/'features = np.loadtxt(root + 'data/features_ALL.txt', delimiter=',')#features = preprocessing.scale(features)features_train = features[0:-50]features_test = features[-50:]pca = decomposition.PCA(n_components=20)pca.fit(features_train)features_train = pca.transform(features_train)features_test = pca.transform(features_test)ratings = np.loadtxt(root + 'data/ratings.txt', delimiter=',')#ratings = preprocessing.scale(ratings)ratings_train = ratings[0:-50]ratings_test = ratings[-50:]
- 值得注意的是,这里用PCA算法对特征进行了压缩和降维。降维之后特征变成了20维,也就是说特征一共有500行,每行是一个人的特征向量,每个特征向量有20个元素。
根据Computer Vision for Predicting Facial Attractiveness一文中给出的结果,我们选取表现最好的模型之一——随机森林作为此次复现用的算法模型。
regr = RandomForestRegressor(n_estimators=50, max_depth=None, min_samples_split=1, random_state=0)regr = regr.fit(features_train, ratings_train)#一定要把conpress设为true或者其他的值,没有设置会输出很多的*.pkljoblib.dump(regr, root + 'model/my_face_rating.pkl',compress=1) print "Generate Model Successfully!"
- 用随机森林训练模型。因为想要预测自选图片的人脸颜值,所以要把模型保存下来以备后用。
2、人脸关键点提取
在之前的博客OpenCV实践之路——用dlib库进行人脸检测与人脸标记中已经提到过如何进行人脸关键点提取,这里的工作基于之前的内容,不同之处在于要把提取到的关键点按照模型可以用的方式进行保存。部分代码如下:
f = open(root + 'data/landmarks.txt','w')for i in range(len(rects)): 使用predictor进行人脸关键点识别 landmarks = numpy.matrix([[p.x,p.y] for p in predictor(im,rects[i]).parts()]) im = im.copy() #使用enumerate 函数遍历序列中的元素以及它们的下标 for idx,point in enumerate(landmarks): pos = (point[0,0],point[0,1]) f.write(str(point[0,0])) f.write(',') f.write(str(point[0,1])) f.write(',') #cv2.putText(im,str(idx),pos, #6.绘制特征点 cv2.circle(im,pos,3,color=(0,255,0)) f.write('\n') print "landmarks,get!"
3、特征生成
特征生成直接用了原始代码,只对路径进行了一些改动。这一部分我暂时也不太明朗,所以不敢多说。有兴趣的还是直接看代码吧。篇幅原因,这里只是部分代码,特征生成的完整代码在这里。
def generateFeatures(pointIndices1, pointIndices2, pointIndices3, pointIndices4, allLandmarkCoordinates): size = allLandmarkCoordinates.shape allFeatures = numpy.zeros((size[0], len(pointIndices1))) for x in range(0, size[0]): landmarkCoordinates = allLandmarkCoordinates[x, :] ratios = []; for i in range(0, len(pointIndices1)): x1 = landmarkCoordinates[2*(pointIndices1[i]-1)] y1 = landmarkCoordinates[2*pointIndices1[i] - 1] x2 = landmarkCoordinates[2*(pointIndices2[i]-1)] y2 = landmarkCoordinates[2*pointIndices2[i] - 1] x3 = landmarkCoordinates[2*(pointIndices3[i]-1)] y3 = landmarkCoordinates[2*pointIndices3[i] - 1] x4 = landmarkCoordinates[2*(pointIndices4[i]-1)] y4 = landmarkCoordinates[2*pointIndices4[i] - 1] points = [x1, y1, x2, y2, x3, y3, x4, y4] ratios.append(facialRatio(points)) allFeatures[x, :] = numpy.asarray(ratios) return allFeatures
4、颜值预测
到了这一步,我们就可以用第一步得到的模型和上一步得到的特征进行颜值的估计了。
#use your own pathroot = 'E:/Github/predict-facial-attractiveness/'clf = joblib.load(root+'model/my_face_rating.pkl')features = np.loadtxt(root + 'data/features_ALL.txt', delimiter=',')my_features = np.loadtxt(root + 'data/my_features.txt', delimiter=',')pca = decomposition.PCA(n_components=20)pca.fit(features)predictions = np.zeros([6,1]);for i in range(0, 6): features_test = features[i, :] features_test = pca.transform(features_test) #regr = linear_model.LinearRegression() #regr.fit(features_train, ratings_train) predictions[i] = clf.predict(features_test)#predictions = clf.predict(features)print predictions
- 这里有一点需要注意:一定要用原来的特征进行PCA的fit,因为自己的图片提取的只有六个特征,而PCA要压缩成20维,根本没法压缩,所以先用原本的特征进行PCA的fit,这样即使只有一个特征,也可以降维到20列。否则会出错。
- OpenCV实践之路——用OpenCV和Dlib进行人脸颜值预测(Python)
- OpenCV实践之路——用OpenCV和Dlib进行人脸颜值预测(Python)
- OpenCV实践之路——用dlib库进行人脸检测与人脸标记(Python)
- OpenCV实践之路——用dlib库进行人脸检测与人脸标记(Python)
- 用OpenCV和Dlib进行人脸颜值预测-解析
- 用anaconda安装dlib库(python—opencv)
- tensorflow+OpenCV+Dlib实现人脸颜值预测
- Python, dlib, OpenCV换脸进行时
- python dlib、opencv 配置
- 用dlib和opencv进行不同角度人脸检测及68个关键点提取(Python)
- OpenCV实践之路——Python的安装和使用
- 使用OpenCV和Dlib进行人头姿态估计
- OpenCV实践之路——Qt中用opencv提取和匹配SIFT特征点
- OpenCV实践之路——Qt中用opencv提取和匹配SIFT特征点
- 利用dlib+opencv进行人脸裁剪
- OpenCV实践之路——opencv玩数独之二九宫格小方格的提取和数字的提取
- deeplearning-python-dlib、opencv安装及使用
- 深度学习系列之(dlib+opencv)代码收藏
- CAD图纸转为dwf的方法
- SylixOS下Shell命令行执行模块内函数
- golang API json,struct结构中标签(Tag)的使用
- 华为 TD-LTE AT 指令
- ubuntu 14.04 安装freeswitch 教程
- OpenCV实践之路——用OpenCV和Dlib进行人脸颜值预测(Python)
- 遐想物联网概念企业战略演进之路
- JS时间处理工具类
- 在Linux系统中增加IP地址网段
- Lucene入门学习二
- 《C++内存泄漏》经验汇总
- Golang json用法详解(一)
- 极速扫码——zbar扫码库性能优化实践
- php的curl函数模拟post、get数据提交,速度非常慢的处理办法