您的位置:首页 > 其它

利用Dlib进行人脸特征局部定位

2016-11-10 13:17 316 查看
shape_predictor_68_face_landmarks.dat模型库下载地址:http://pan.baidu.com/s/1pLpREDd 密码:80v6
在运行该程序前,请配好Dlib和opencv。本人用的是vs2010+opencv2.4.11+Dlib18.18
void CDlib_MFCDlg::OnBnClickedFace()
{
// TODO: 在此添加控件通知处理程序代码
char img_file[]="G:\\Source\\111.jpg";
char mark_file[]="G:\\Source\\shape_predictor_68_face_landmarks.dat";//必须导入该模型
Mat img=imread(img_file);

//需要一个人脸检测器,获得一个边界框
frontal_face_detector detector=get_frontal_face_detector();

//需要一个形状预测器,这是一个工具用来预测给定的图片和脸边界狂的标记点的位置
//这里我们仅仅从shape_Predictor_68_face_landmarks.dat文件加载模型
shape_predictor sp;//定义个shape_predictor类的实例

deserialize(mark_file) >> sp;//模型解析
//  serialize("face_detector.svm")<<detector;//模型存储
/*
object_detector<image_scanner_type> detector2;
deserialize("face_detector.svm")>> detector2;
*/

/*
structural_object_detection_trainer<image_scanner_type> trainer(scanner);
// 设置训练参数
trainer.set_num_threads(4);
// 设置SVM的参数C,C越大表示更好地去拟合训练集,当然也有可能造成过拟合。通过尝试不同C在测试集上的效果得到最佳值
trainer.set_c(1);
trainer.be_verbose();
//设置训练结束条件,"risk gap"<0.01时训练结束,值越小表示SVM优化问题越精确,训练时间也会越久。
//通常取0.1-0.01.在verbose模式下每一轮的risk gap都会打印出来。
trainer.set_epsilon(0.01);
*/
array2d<rgb_pixel>arrImg;//注意变量类型rgb_pixel三通道彩色图像

load_image(arrImg,img_file);

std::vector<dlib::rectangle>dets= detector(arrImg);

for(unsigned long j=0;j<dets.size();j++)
{
full_object_detection shape=sp(arrImg,dets[j]);
for(unsigned long i=0;i<shape.num_parts();i++)//shape.num_parts()记录了68个点的坐标
{
point pt=shape.part(i);
int x=pt.x();
int y=pt.y();
line(img,Point(pt.x(),pt.y()),Point(pt.x(),pt.y()),Scalar(0,0,255),2);
}
}
imshow("img",img);
waitKey();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐