2024年最全Python实现颜值检测!_sklearn 颜值评分模型训练过程(3)

本文档详细介绍了使用Python和sklearn库训练颜值评分模型的过程。通过修改getLandmarks.py中的第13行代码,然后依次运行train_model.py、getLandmarks.py、getFeatures.py和Predict.py,读者可以训练自己的模型或直接应用作者提供的模型进行颜值预测。
摘要由CSDN通过智能技术生成

若嫌麻烦或者有其他需求,请自行修改:

getLandmarks.py文件中第13行。

最后依次运行:

train_model.py(想直接用我模型的请忽略此步)

getLandmarks.py

getFeatures.py

Predict.py

train_model.py

# 模型训练脚本
import numpy as np
from sklearn import decomposition
from sklearn.ensemble import RandomForestRegressor
from sklearn.externals import joblib


# 特征和对应的分数路径
features_path = './data/features_ALL.txt'
ratings_path = './data/ratings.txt'

# 载入数据
# 共500组数据
# 其中前480组数据作为训练集,后20组数据作为测试集
features = np.loadtxt(features_path, delimiter=',')
features_train = features[0: -20]
features_test = features[-20: ]
ratings = np.loadtxt(ratings_path, delimiter=',')
ratings_train = ratings[0: -20]
ratings_test = ratings[-20: ]

# 训练模型
# 这里用PCA算法对特征进行了压缩和降维。
# 降维之后特征变成了20维,也就是说特征一共有500行,每行是一个人的特征向量,每个特征向量有20个元素。
# 用随机森林训练模型
pca = decomposition.PCA(n_components=20)
pca.fit(features_train)
features_train = pca.transform(features_train)
features_test = pca.transform(features_test)
regr = RandomForestRegressor(n_estimators=50, max_depth=None, min_samples_split=10, random_state=0)
regr = regr.fit(features_train, ratings_train)
joblib.dump(regr, './model/face_rating.pkl', compress=1)

# 训练完之后提示训练结束
print('Generate Model Successfully!')

getLandmarks.py

# 人脸关键点提取脚本
import cv2
import dlib
import numpy


# 模型路径
PREDICTOR_PATH = './model/shape_predictor_68_face_landmarks.dat'
# 使用dlib自带的frontal_face_detector作为人脸提取器
detector = dlib.get_frontal_face_detector()
# 使用官方提供的模型构建特征提取器
predictor = dlib.shape_predictor(PREDICTOR_PATH)
face_img = cv2.im
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值