zoukankan      html  css  js  c++  java
  • Python-Dlib人脸识别

    Python-Dlib人脸识别

      前言:基于dlib实现人脸识别功能。Anaconda是基于Windows和Linux -X86系统,Miniforge是基于ARM

    一、环境

      windows/linux+python(Anaconda/Miniforge)+cmake+dlib

    二、安装过程

      1、windows

      1)基于Anaconda安装Python

      2)python导入dlib模块

      2、Linux

      1)基于Miniforge安装Pytho  

      2)python导入dlib/numpy模块

    三、代码 

      1 # -*- coding: utf-8 -*-
      2 # @Time : 2020/9/10 10:16
      3 # @Author : ZhaoYang
      4 # @FileName: face_demo.py
      5 # @Email : zhaoyang@ifreegroup.com
      6 # @Software: PyCharm
      7 
      8 """
      9 从图片中获取人脸并进行人脸识别检测
     10 """
     11 
     12 import glob                # 文件操作模块
     13 import os                # 操作系统功能接口函数
     14 import dlib                # 人脸识别库
     15 import numpy as np        # 数据处理库
     16 
     17 
     18 # 声明descriptors,用于存放已知图片对应的人脸特征向量
     19 descriptors = []
     20 
     21 # 声明names,用于存放于人脸特征向量对应的名字。
     22 names = ["HuGe", "SongJia", "StefanieSun", "ZhaoYang"]
     23 
     24 # 正向人脸检测器将图像
     25 detector = dlib.get_frontal_face_detector()
     26 
     27 # Dlib 人脸特征点识别模型
     28 predictor = dlib.shape_predictor(r"G:face_testshape_predictor_68_face_landmarks.dat")
     29 
     30 # Dlib 人脸识别模型
     31 face_rec = dlib.face_recognition_model_v1(r"G:face_testdlib_face_recognition_resnet_model_v1.dat")
     32 
     33 
     34 # 遍历image_file_path目录下所有.jpg文件
     35 def logging_data(image_file_data_path: str):
     36     for image in glob.glob(os.path.join(image_file_data_path, "*.jpg")):
     37         img = dlib.load_rgb_image(image)
     38         # 1.人脸检测
     39         dets = detector(img, 1)
     40 
     41         for k, d in enumerate(dets):
     42             # 2.关键点检测
     43             shape = predictor(img, d)
     44             # 3.计算特征值向量(人脸描述符)
     45             descriptor = face_rec.compute_face_descriptor(img, shape)
     46             # 转换numpy数组
     47             v = np.array(descriptor)
     48             descriptors.append(v)
     49 
     50 
     51 # 单张人脸识别检测(图片路径)
     52 def face_alone(file_path: str):
     53     print("单张人脸识别检测================================================================================================")
     54     face_name = face_common(file_path)
     55     print(face_name)
     56     return face_name
     57 
     58 
     59 # 图片集人脸识别检测(图片集路径)
     60 def face_all(image_file_path):
     61     print("图片集人脸识别检测==============================================================================================")
     62     face_list = []
     63     for f in glob.glob(os.path.join(image_file_path, "*.jpg")):
     64         face_name = face_common(f)
     65         face_list.append(face_name)
     66     print(face_list)
     67     return face_list
     68 
     69 
     70 # 公共人脸识别检测接口
     71 def face_common(file_path: str):
     72     img = dlib.load_rgb_image(file_path)
     73     # 使用 detector 检测器来检测图像中的人脸
     74     dets = detector(img, 1)
     75     for k, d in enumerate(dets):
     76         # 获取人脸特征点
     77         shape = predictor(img, d)
     78         # 计算特征值向量
     79         face_descriptor = face_rec.compute_face_descriptor(img, shape)
     80         # 将当前待判断的图片特征向量转化为 current
     81         current = np.array(face_descriptor)
     82         # 计算欧式距离,识别人脸
     83         # 设置阈值
     84         tolerance = 0.4
     85         # 设置该图片名字初值为:Unknow
     86         current_name = "Unknow name"
     87         # 遍历已存放的图片特征值descriptor[]
     88         for i in range(len(descriptors)):
     89             # 计算欧式距离
     90             distance = np.linalg.norm(descriptors[i] - current)
     91             # 对欧式距离进行判断
     92             if distance < tolerance:
     93                 current_name = names[i]
     94                 return current_name
     95                 break
     96 
     97 
     98 # 测试
     99 logging_data(r"G:face_test4-test")
    100 face_alone(r"G:face_test5-test3.jpg")
    101 face_all(r"G:face_test5-test")
    View Code

     四、下载地址

      注意:模块版本与Python版本的差异

      1、dlib 

      链接:https://pan.baidu.com/s/16yE56YgakaA2QuNeVPg1TA
      提取码:2cuy

      官网:http://dlib.net/files/

      2、numpy  

      链接:https://pan.baidu.com/s/1kYoCP_vP-5DeNB3T_ynjpw
      提取码:zekd

      官网:https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy

      3、cmake

      链接:https://pan.baidu.com/s/1P9Ic8byYhHOs1hkYtvNocQ
      提取码:l26v

      官网:

      4、人脸特征点识别模型 

      链接:https://pan.baidu.com/s/1G5CZBuumTfkftCmuKyj-GQ
      提取码:kaiu

      5、人脸识别模型

      链接:https://pan.baidu.com/s/1ol6qqDL1FIUXa1z_ruTwZg
      提取码:9xbe

    五、参考连接

    往外张望的人在做梦,向内审视的人才是清醒的
  • 相关阅读:
    Asp.net 在VS下发布失败
    Delphi XE安装提示 Item Not Found
    使用VS时,有个提醒如下:"驗證 (XHTML 1.0 Transitional): 系統將 'font' 視為過期舊式的項目。建議使用較新的建構."
    安装DELPHI7后出现的问题 1,一运行DELPHI7就出现一对话框 can't load package ...borland\delphi7\bin\dclite.bpl.Access
    Unidac手工安装
    一个非常好的框架
    英文帮助翻译
    arcgis javascript api学习5
    Dojo学习要点(转载)
    vba 生成点的矩形buffer
  • 原文地址:https://www.cnblogs.com/StefanieYang/p/13644479.html
Copyright © 2011-2022 走看看