• Python通过dlib模块实现人脸识别的案例


    环境准备

    1. 需要安装Visual Studio (C++平台),否则dlib模块无法安装成功。
    2. pip install dlib
    3. pip install numpy
    4. pip install opencv-python
    5. pip install scikit-image

    1. 实现给识别的人脸图片加上红色矩形框

    import dlib
    from skimage import io
    
    # 使用Dilb的正面人脸检测器frontal_face_detector
    detector = dlib.get_frontal_face_detector()
    # Dlib 的人脸检测模型
    predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
    # 图片所在路径
    # img = io.imread("x3.jpg")
    img = io.imread("img.png")
    # 生成Dlib的图像窗口
    win = dlib.image_window()
    win.set_image(img)
    # 使用detector检测器来检测图像中的人脸
    faces = detector(img, 1)
    print("人脸数:", len(faces))
    for i, d in enumerate(faces):
        print("第", i+1, "个人脸的矩形框坐标:", "left:", d.left(), "right:", d.right, "top:", d.top(), "bottom:", d.bottom)
    # 绘制人脸脸部矩形框
    win.add_overlay(faces)
    # 保持图像
    dlib.hit_enter_to_continue()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    img.png:
    在这里插入图片描述
    运行效果:
    在这里插入图片描述
    在这里插入图片描述
    测试如果一张图片中有多张人脸的效果:
    在这里插入图片描述
    在这里插入图片描述

    2. 画上人脸轮廓

    import dlib
    from skimage import io
    
    # 使用Dilb的正面人脸检测器frontal_face_detector
    detector = dlib.get_frontal_face_detector()
    # Dlib 的人脸检测模型
    predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
    # 图片所在路径
    # img = io.imread("zsm1.jpg")
    img = io.imread("img.png")
    # 生成Dlib的图像窗口
    win = dlib.image_window()
    win.set_image(img)
    # 使用detector检测器来检测图像中的人脸
    faces = detector(img, 1)
    print("人脸数:", len(faces))
    for i, d in enumerate(faces):
        print("第", i+1, "个人脸的矩形框坐标:", "left:", d.left(), "right:", d.right, "top:", d.top(), "bottom:", d.bottom)
        # 使用predictor来计算面部轮廓关键点位置
        shape = predictor(img, faces[i])
        # 绘制面部轮廓矩形框
        win.add_overlay(shape)
    # 绘制人脸脸部矩形框
    win.add_overlay(faces)
    # 保持图像
    dlib.hit_enter_to_continue()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    运行效果:
    在这里插入图片描述
    在这里插入图片描述
    多张人脸运行效果:
    在这里插入图片描述
    在这里插入图片描述

    3. 进行利用训练过的模型识别检测

    import dlib    # pip install dlib
    import glob
    import numpy    # pip install numpy
    import os
    import sys
    import cv2  # pip install opencv-python
    from skimage import io    # pip install scikit-image
    # 编写一个人脸识别程序
    
    if len(sys.argv) != 2:    # 命令行参数
        print("请检查参数是否正确")
        exit()
    
    current_path = os.getcwd()     # 获取当前路径
    # 1. 人脸关键点检测器
    # premod = "\\model\\shape_predictor_68_face_landmarks.dat"
    premod = "shape_predictor_68_face_landmarks.dat"
    # predictor_path = current_path + premod
    predictor_path = premod
    
    # 2. 人脸识别模型
    # recmod = "\\model\\dlib_face_recognition_resnet_model_v1.dat"
    recmod = "dlib_face_recognition_resnet_model_v1.dat"
    # face_rec_model_path = current_path + recmod
    face_rec_model_path = recmod
    
    # 3. 备选人脸文件夹
    faces_folder_path = "face_data1"
    
    # 4. 需识别的人脸
    img_path = sys.argv[1]
    # img_path = "face_data1/img2.png"
    
    # 5. 加载正脸检测器
    detector = dlib.get_frontal_face_detector()
    
    # 6. 加载人脸关键点检测器
    sp = dlib.shape_predictor(predictor_path)
    
    # 7. 加载人脸识别模型
    facerec = dlib.face_recognition_model_v1(face_rec_model_path)
    
    # 8. 加载显示人脸窗体
    win = dlib.image_window()
    # 候选人脸描述子list
    descriptors = []
    
    # 9. 对文件夹下的每一个人脸进行
    # (1)人脸检测
    # (2)关键点检测
    # (3)描述子提取
    # for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
    for f in glob.glob(os.path.join(faces_folder_path, "*.png")):
        print("Processing file: {}".format(f))
        img = io.imread(f)
        win.clear_overlay()
        win.set_image(img)
    
        # (1)人脸检测
        dets = detector(img, 1)
        print("Number of faces detected: {}".format(len(dets)))
    
        for k, d in enumerate(dets):
            # (2)关键点检测
            shape = sp(img, d)
            # 画出人脸区域和关键点
            win.clear_overlay()
            win.add_overlay(d)
            win.add_overlay(shape)
    
            # (3)描述子提取,128维向量
            face_descriptor = facerec.compute_face_descriptor(img, shape)
    
            # 转换为numpy array
            v = numpy.array(face_descriptor)
            descriptors.append(v)
        # 10. 对需识别人脸进行同样处理
        # 提取描述子,不再注释
    
        img = io.imread(img_path)
        dets = detector(img, 1)
    
        adist = []
        for k, d in enumerate(dets):
            shape = sp(img, d)
            face_descriptor = facerec.compute_face_descriptor(img, shape)
            d_test = numpy.array(face_descriptor)
    
            # 计算欧氏距离(什么是欧式距离:https://wiki.mbalib.com/wiki/%E6%AC%A7%E5%87%A0%E9%87%8C%E5%BE%97%E8%B7%9D%E7%A6%BB)
            # 即两点(正数)之间最短距离
            for i in descriptors:
                dist_ = numpy.linalg.norm(i - d_test)
                adist.append(dist_)
        # 11. 候选人名单
        candidate = ['jiejie', 'jobs', 'meimei', 'zsm1', 'zsm2', 'zsm3']
        c_d = []
    
        # 12. 候选人和距离组成一个dict
        c_d = dict(zip(candidate, adist))
        print(c_d)
        cd_sorted = sorted(c_d.items(), key=lambda d:d[1])
        print("\n 该照片上的人是:", cd_sorted[0][0])
    
    # note: 模型下载地址:http://dlib.net/files/dlib_face_recognition_resnet_model_v1.dat.bz2
    
    # 运行方法:python 11.6.py img.png
    # 运行方法:python 11.6.py img1.png
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
  • 相关阅读:
    小众却又极其实用的5款办公软件,建议收藏转发
    地平线旭日X3派小白上手
    基于antd实现动态修改节点的Tree组件
    ​创业15年,50岁回到农村过上退休的生活,上班和创业是两难的选择。
    23ai中的True Cache到底能做啥?
    【ELM】动态自适应可变加权极限学习机ELM预测(Matlab代码实现)
    Charles:移动端抓包 / windows客户端 iOS手机 / 手机访问PC本地项目做调试
    如何打造智慧公厕管理系统,提升公共厕所智能化服务质量?
    仿真数据检查器如何比较数据
    React之一些函数或者方法的扩展
  • 原文地址:https://blog.csdn.net/ungoing/article/details/124048670