文章目录

  • 一、安装dlib库
  • 二、人脸特征提取
    • 2.1 人脸照片采集
    • 2.2 数据集处理
  • 三、参考链接

一、安装dlib库

参考文献:python+opencv+dlib实现人脸识别

二、人脸特征提取

2.1 人脸照片采集

使用摄像头采集(视频流截图)

import cv2
import dlib
import os
import sys
import random
# 存储位置
output_dir = 'D:/Face/xjc/631907060325'
size = 64if not os.path.exists(output_dir):os.makedirs(output_dir)
# 改变图片的亮度与对比度def relight(img, light=1, bias=0):w = img.shape[1]h = img.shape[0]#image = []for i in range(0,w):for j in range(0,h):for c in range(3):tmp = int(img[j,i,c]*light + bias)if tmp > 255:tmp = 255elif tmp < 0:tmp = 0img[j,i,c] = tmpreturn img#使用dlib自带的frontal_face_detector作为我们的特征提取器
detector = dlib.get_frontal_face_detector()
# 打开摄像头 参数为输入流,可以为摄像头或视频文件
camera = cv2.VideoCapture(0)
#camera = cv2.VideoCapture('C:/Users/CUNGU/Videos/Captures/wang.mp4')index = 1
while True:if (index <= 20):#存储20张人脸特征图像print('Being processed picture %s' % index)# 从摄像头读取照片success, img = camera.read()# 转为灰度图片gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# 使用detector进行人脸检测dets = detector(gray_img, 1)for i, d in enumerate(dets):x1 = d.top() if d.top() > 0 else 0y1 = d.bottom() if d.bottom() > 0 else 0x2 = d.left() if d.left() > 0 else 0y2 = d.right() if d.right() > 0 else 0face = img[x1:y1,x2:y2]# 调整图片的对比度与亮度, 对比度与亮度值都取随机数,这样能增加样本的多样性face = relight(face, random.uniform(0.5, 1.5), random.randint(-50, 50))face = cv2.resize(face, (size,size))cv2.imshow('image', face)cv2.imwrite(output_dir+'/'+str(index)+'.jpg', face)index += 1key = cv2.waitKey(30) & 0xffif key == 27:breakelse:print('Finished!')# 释放摄像头 release cameracamera.release()# 删除建立的窗口 delete all the windowscv2.destroyAllWindows()break

在对应的输出目录下,会得到20张摄像头采集得到的图片

2.2 数据集处理

1.下载dlib的人脸识别模型

下载链接:https://pan.baidu.com/s/1nNcYZhVb7p2p_ph4eiDJmQ
提取码:2000

2.获取每张照片68个特征数据并分别保存到20个csv中,同时保存20张图片的特征均值到csv中

注意图片读取的路径,上面收集的图片是保存到D:/Face/xjc/631907060325下面,我们在这里读取图片时只需要读取到xjc目录就行,否则会出错

# 从人脸图像文件中提取人脸特征存入 CSV
# Features extraction from images and save into features_all.csv# return_128d_features()          获取某张图像的128D特征
# compute_the_mean()              计算128D特征均值from cv2 import cv2 as cv2
import os
import dlib
from skimage import io
import csv
import numpy as np# 要读取人脸图像文件的路径
path_images_from_camera = "D:/Face/xjc/"# Dlib 正向人脸检测器
detector = dlib.get_frontal_face_detector()# Dlib 人脸预测器
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')# Dlib 人脸识别模型
# Face recognition model, the object maps human faces into 128D vectors
face_rec = dlib.face_recognition_model_v1('dlib_face_recognition_resnet_model_v1.dat')# 返回单张图像的 128D 特征
def return_128d_features(path_img):img_rd = io.imread(path_img)img_gray = cv2.cvtColor(img_rd, cv2.COLOR_BGR2RGB)faces = detector(img_gray, 1)print("%-40s %-20s" % ("检测到人脸的图像 / image with faces detected:", path_img), '\n')# 因为有可能截下来的人脸再去检测,检测不出来人脸了# 所以要确保是 检测到人脸的人脸图像 拿去算特征if len(faces) != 0:shape = predictor(img_gray, faces[0])face_descriptor = face_rec.compute_face_descriptor(img_gray, shape)else:face_descriptor = 0print("no face")return face_descriptor# 将文件夹中照片特征提取出来, 写入 CSV
def return_features_mean_personX(path_faces_personX):features_list_personX = []photos_list = os.listdir(path_faces_personX)if photos_list:for i in range(len(photos_list)):# 调用return_128d_features()得到128d特征print("%-40s %-20s" % ("正在读的人脸图像 / image to read:", path_faces_personX + "/" + photos_list[i]))features_128d = return_128d_features(path_faces_personX + "/" + photos_list[i])#  print(features_128d)# 遇到没有检测出人脸的图片跳过if features_128d == 0:i += 1else:features_list_personX.append(features_128d)i1=str(i+1)add="D:/Face/get_features/"+i1+".csv"print(add)with open(add,"w",newline="") as csvfile:writer1 = csv.writer(csvfile)writer1.writerow(features_128d)else:print("文件夹内图像文件为空 / Warning: No images in " + path_faces_personX + '/', '\n')# 计算 128D 特征的均值# N x 128D -> 1 x 128Dif features_list_personX:features_mean_personX = np.array(features_list_personX).mean(axis=0)else:features_mean_personX = '0'return features_mean_personX# 读取某人所有的人脸图像的数据
people = os.listdir(path_images_from_camera)
people.sort()with open("D:/Face/get_features_all.csv", "w", newline="") as csvfile:writer = csv.writer(csvfile)for person in people:print("##### " + person + " #####")# Get the mean/average features of face/personX, it will be a list with a length of 128Dfeatures_mean_personX = return_features_mean_personX(path_images_from_camera + person)writer.writerow(features_mean_personX)print("特征均值 / The mean of features:", list(features_mean_personX))print('\n')print("所有录入人脸数据存入 / Save all the features of faces registered into: D:/Face/get_features_all.csv")

每张图片对应的数据集

20张图片的特征均值

三、参考链接

Dlib模型实现人脸识别

基于Dlib库构建人脸识别数据集相关推荐

  1. 基于Dlib库的人脸表情分析与识别——Python

    本项目主要由包含我在内的四名成员共同完成:孙明喆.吴震.张晨.张明 项目介绍,及可执行文件.模型文件.详细报告均在GitHub中可以查看: GitHub 关于项目的详细介绍,可能过些日子在复习时候会有 ...

  2. 【人脸识别】基于dlib库实现人脸特征值提取

    一.Dlib库介绍与安装 1. Dlib库简介 Dlib库是一个机器学习的开源库,包含了机器学习的很多算法,使用起来很方便,直接包含头文件即可,并且不依赖于其他库(自带图像编解码库源码).Dlib可以 ...

  3. 基于dlib库进行微笑识别和口罩识别

    环境配置 tensorflow和keras参考我之前的博客https://blog.csdn.net/A981012/article/details/106650686 下载dlib库不能从anaco ...

  4. 基于python的dlib库的人脸识别

    首先通过pip安装cmake,只有安装了cmake才能装上dlib库,建议装dlib的时候关闭后台的360杀毒软件. 源代码如下: import dlib import cv2 as cvimage_ ...

  5. 使用dlib库进行人脸识别

    一.安装opencv和dlib 我使用的anaconda,安装比较方便. 安装opencv,在指定环境下输入: conda install opencv 安装dlib: conda install - ...

  6. 基于Amazon Rekognition构建人脸识别系统

    人脸识别是目前机器视觉最成功的一个领域了,有许多的人脸检测与识别算法以及人脸识别的函数库.对于入门深度学习来说,从头开始一步一步训练出一个自己的人脸识别项目对你学习深度学习是非常有帮助的,但是在学习之 ...

  7. 基于DLIB的视频人脸识别对比

    环境:python3.6   win10 安装opencv,numpy,dlib 下载人脸关键点检测器 face_landmarks.dat 下载人脸识别.提取特征值 dlib_face_recogn ...

  8. python中dlib库_python 基于dlib库的人脸检测的实现

    本周暂时比较清闲,可以保持每日一更的速度. 国外身份证项目新增需求,检测出身份证正面的人脸.最开始考虑mobilenet-ssd,经同事提醒,有现成的人脸库dlib,那就用传统方法尝试一下. dlib ...

  9. python dlib opencv人脸识别准确度_基于dlib和opencv库的人脸识别

    基于dlib和opencv库的人脸识别 需下载68个特征点的人脸检测模型: http://dlib.net/files/ 文件名为shape_predictor_68_face_landmarks.d ...

  10. Python人脸识别教程 - 基于Python的开源人脸识别库:离线识别率高达99.38%

    Python人脸识别教程 - 基于Python的开源人脸识别库:离线识别率高达99.38% 仅用 Python 和命令行就可以实现人脸识别的库开源了.该库使用 dlib 顶尖的深度学习人脸识别技术构建 ...

最新文章

  1. 剑指offer青蛙跳台阶问题
  2. github如何make contribute to 其它开源项目
  3. 数学建模学习笔记——插值算法
  4. 别名、浅复制与深复制
  5. android http协议添加Authorization认证方式
  6. android webview 劫持,安卓包风险安全监测提示存在Activity劫持、WebView远程代码执行,请问怎么解决?...
  7. Google I/O 2018 之后, Android 工程师将何去何从?
  8. 安全验证框架使用笔记001---Shiro简介
  9. HadoopSourceAnalyse---ResourceMananger-initiate
  10. 低压差线性稳压器MPQ2013A-AEC1品牌MPS国产替代
  11. PS之PS 删除时出现无法完成请求,因为智能对象不能直接进行编辑。
  12. 死链检测工具Xenu的操作及使用方法
  13. 全国计算机等级考试 二级C语言考前复习资料(笔试)
  14. kindle上网看其他网址_原来kindle不止可以看书,快来看看体验版浏览器怎么玩!...
  15. burst什么意思_burst是什么意思_burst的用法
  16. Odoo16 主题推荐
  17. VMware vMotion简介
  18. netty-读半包处理--ByteToMessageDecoder
  19. 欧盟边检AI测谎仪上线了,第一天就差点让记者进了小黑屋
  20. 讯为iTOP4412开发板ARM-linux 使用OPENCV调用USB摄像头

热门文章

  1. 一篇文章详细解读Spring的AOP原理过程(Spring面向切面详解)
  2. Halcon教程四:一个小技巧
  3. Linux 2.6内核配置说明
  4. 小米笔记本air13-3安装黑苹果macOS
  5. 基于AD9833 的DDS信号发生器
  6. k近邻算法_k近邻算法
  7. 《阿里巴巴Java开发手册》版本演进历史
  8. PCL点云数据处理-滤波基础(C++)
  9. 前端特效-HTML+CSS - 图片悬浮效果
  10. 安装Navision Server5.0 注意事项