【Dlib+Python+Ubuntu】人脸landmark

本文介绍如何使用dlib库进行人脸检测及面部地标定位,并通过实际案例演示整个过程。文中提供了必要的环境配置指导、代码实现细节及运行说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境配置参考:地址


(Windows+C++版本的见:地址


惯例先放效果图:




这里需要先下载landmark的模型

地址:

 http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2


新建fr.py(其实就是example里的例子,但把限定的jpg格式去掉了,所以需要手动输入后缀名)


#!/usr/bin/python
# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
#
#   This example program shows how to find frontal human faces in an image and
#   estimate their pose.  The pose takes the form of 68 landmarks.  These are
#   points on the face such as the corners of the mouth, along the eyebrows, on
#   the eyes, and so forth.
#
#   This face detector is made using the classic Histogram of Oriented
#   Gradients (HOG) feature combined with a linear classifier, an image pyramid,
#   and sliding window detection scheme.  The pose estimator was created by
#   using dlib's implementation of the paper:
#      One Millisecond Face Alignment with an Ensemble of Regression Trees by
#      Vahid Kazemi and Josephine Sullivan, CVPR 2014
#   and was trained on the iBUG 300-W face landmark dataset.
#
#   Also, note that you can train your own models using dlib's machine learning
#   tools. See train_shape_predictor.py to see an example.
#
#   You can get the shape_predictor_68_face_landmarks.dat file from:
#   http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
#
# COMPILING/INSTALLING THE DLIB PYTHON INTERFACE
#   You can install dlib using the command:
#       pip install dlib
#
#   Alternatively, if you want to compile dlib yourself then go into the dlib
#   root folder and run:
#       python setup.py install
#   or
#       python setup.py install --yes USE_AVX_INSTRUCTIONS
#   if you have a CPU that supports AVX instructions, since this makes some
#   things run faster.  
#
#   Compiling dlib should work on any operating system so long as you have
#   CMake and boost-python installed.  On Ubuntu, this can be done easily by
#   running the command:
#       sudo apt-get install libboost-python-dev cmake
#
#   Also note that this example requires scikit-image which can be installed
#   via the command:
#       pip install scikit-image
#   Or downloaded from http://scikit-image.org/download.html. 

import sys
import os
import dlib
import glob
from skimage import io

if len(sys.argv) != 3:
    print(
        "Give the path to the trained shape predictor model as the first "
        "argument and then the directory containing the facial images.\n"
        "For example, if you are in the python_examples folder then "
        "execute this program by running:\n"
        "    ./face_landmark_detection.py shape_predictor_68_face_landmarks.dat ../examples/faces\n"
        "You can download a trained facial shape predictor from:\n"
        "    http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2")
    exit()

predictor_path = sys.argv[1]
faces_folder_path = sys.argv[2]

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
win = dlib.image_window()

for f in glob.glob(os.path.join(faces_folder_path)):
    print("Processing file: {}".format(f))
    img = io.imread(f)

    win.clear_overlay()
    win.set_image(img)

    # Ask the detector to find the bounding boxes of each face. The 1 in the
    # second argument indicates that we should upsample the image 1 time. This
    # will make everything bigger and allow us to detect more faces.
    dets = detector(img, 1)
    print("Number of faces detected: {}".format(len(dets)))
    for k, d in enumerate(dets):
        print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
            k, d.left(), d.top(), d.right(), d.bottom()))
        # Get the landmarks/parts for the face in box d.
        shape = predictor(img, d)
        print("Part 0: {}, Part 1: {} ...".format(shape.part(0),
                                                  shape.part(1)))
        # Draw the face landmarks on the screen.
        win.add_overlay(shape)

    win.add_overlay(dets)
    dlib.hit_enter_to_continue()


运行程序

python fr.py + 68点dat模型+含后缀的图片路径

python fr.py shape_predictor_68_face_landmarks.dat 1.jpeg


然后就是结果图咯~~



祝好



人脸比对可以通过dlib和OpenCV库来实现。首先,你需要使用OpenCV库来加载图像并检测人脸。然后,你可以使用dlib库中的人脸识别模型来计算人脸的128位向量,这个向量可以表示人脸的独特特征。最后,你可以使用facenet模型将这些特征向量进行比对,以判断两个人脸是否相同。 以下是一个基本的示例代码: ```c++ #include <dlib/opencv.h> #include <dlib/image_processing.h> #include <dlib/image_processing/frontal_face_detector.h> #include <dlib/dnn.h> #include <opencv2/opencv.hpp> using namespace dlib; using namespace std; // 加载facenet模型 net_type net = dlib::deserialize("models/facenet.dat"); // 计算人脸特征向量 matrix<float,0,1> get_face_descriptor(cv::Mat img, frontal_face_detector detector, shape_predictor sp) { cv_image<bgr_pixel> cimg(img); std::vector<rectangle> faces = detector(cimg); std::vector<full_object_detection> shapes; for (unsigned long i = 0; i < faces.size(); ++i) { shapes.push_back(sp(cimg, faces[i])); } matrix<rgb_pixel> face_chip; extract_image_chip(cimg, get_face_chip_details(shapes[0]), face_chip); matrix<float,0,1> face_descriptor = net(face_chip); return face_descriptor; } // 计算两个人脸特征向量的距离 double face_distance(matrix<float,0,1> &face_descriptor1, matrix<float,0,1> &face_descriptor2) { return length(face_descriptor1 - face_descriptor2); } int main(int argc, char** argv) { // 加载人脸检测模型和特征点定位模型 frontal_face_detector detector = get_frontal_face_detector(); shape_predictor sp; deserialize("models/shape_predictor_68_face_landmarks.dat") >> sp; // 加载两张需要比对的人脸图片 cv::Mat img1 = cv::imread(argv[1]); cv::Mat img2 = cv::imread(argv[2]); // 计算两个人脸的特征向量并计算距离 matrix<float,0,1> face_descriptor1 = get_face_descriptor(img1, detector, sp); matrix<float,0,1> face_descriptor2 = get_face_descriptor(img2, detector, sp); double distance = face_distance(face_descriptor1, face_descriptor2); // 输出结果 cout << "Distance: " << distance << endl; return 0; } ``` 你需要将上面的代码保存为一个cpp文件,并且需要下载facenet模型和dlib人脸检测模型和特征点定位模型。你可以在dlib的官方网站上下载这些模型,然后将它们放在一个名为models的文件夹中。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朱铭德

五毛也是爱٩(●´৺`●)૭

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值