基础知识(十三)dlib python人脸检测 特征点定位

import  cv2
import  dlib
import numpy as np
#根据人脸框bbox,从一张完整图片裁剪出人脸
def getface():
    bgrImg = cv2.imread('1.jpg')
    print bgrImg.shape
    rgbImg = cv2.cvtColor(bgrImg, cv2.COLOR_BGR2RGB)

    detector=dlib.get_frontal_face_detector()
    #img = io.imread('1.jpg')
    faces = detector(rgbImg, 1)
    if len(faces) > 0:
    face=max(faces, key=lambda rect: rect.width() * rect.height())
    [x1,x2,y1,y2]=[face.left(),face.right(),face.top(),face.bottom()]


人脸特征点定位:需要先从网上下载预训练模型

http://sourceforge.net/projects/dclib/files/dlib/v18.10/shape_predictor_68_face_landmarks.dat.bz2

import  dlib
#根据人脸框bbox,从一张完整图片裁剪出人脸,并保存问文件名cropimgname
#如果未检测到人脸,那么返回false,否则返回true
face_detector=dlib.get_frontal_face_detector()
landmark_predictor=dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

def geteye_rect(imgpath):
	bgrImg = cv2.imread(imgpath)
	if bgrImg is None:
		return False
	rgbImg = cv2.cvtColor(bgrImg, cv2.COLOR_BGR2RGB)
	facesrect = face_detector(rgbImg, 1)
	if len(facesrect) <=0:
		return False

	for k, d in enumerate(facesrect):
		shape = landmark_predictor(rgbImg, d)
		for i in range(68):
			pt=shape.part(i)
			plt.plot(pt.x,pt.y,'ro')
		plt.imshow(rgbImg)
		plt.show()

二、C++版使用

1、在cmake中添加:dlib是dlib解压出来后,里面有个dlib源码文件。

include(dlib/cmake)

2、在可执行文件中,添加链接:

target_link_libraries( lightpredict dlib)
这样一来,就可以使用dlib了。

CDlibFace::CDlibFace() {
    m_facedetector= dlib::get_frontal_face_detector();

    dlib::deserialize("dlib/shape_predictor_68_face_landmarks.dat") >> m_facelandmarkdetector;
}
CDlibFace::~CDlibFace() {

}
cv::Rect CDlibFace::getfacerect(std::string imagefile)
{

    dlib::array2d<unsigned char> img;
    dlib::load_image(img,imagefile);
   // dlib::pyramid_down(img);

    clock_t begin = clock();
    std::vector<dlib::rectangle> dets = m_facedetector(img);

    double elapsed_secs = double(clock() - begin) / CLOCKS_PER_SEC;
    std::cout<<"face detection time:"<<elapsed_secs<<std::endl;
    if (dets.empty())
    {
        return  cv::Rect(0,0,0,0);
    }
    else{
        std::cout<<dets[0].left()<<"top"<<dets[0].top()<<std::endl;
        return cv::Rect(dets[0].left(),dets[0].top(),dets[0].width(),dets[0].height());
    }


}
std::vector<cv::Point2i>CDlibFace::getfacelandmark(std::string imagefile)
{
    dlib::array2d<unsigned char> img;
    dlib::load_image(img,imagefile);
    // dlib::pyramid_down(img);

    clock_t begin = clock();
    std::vector<dlib::rectangle> dets = m_facedetector(img);
    std::vector<cv::Point2i> pts;
    for (unsigned long j = 0; j < dets.size()&&j<1; ++j)
    {
        dlib::full_object_detection shape = m_facelandmarkdetector(img, dets[j]);
        for (int i = 0; i <shape.num_parts() ; ++i) {
            cv::Point2i pt(shape.part(i).x(),shape.part(i).y());
            pts.push_back(pt);

        }

    }


    return  pts;

}


  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值