文章目录

  • 一、dlib和opencv建立
  • 二、人脸图像采集并储存
  • 三、采集并保存特征值建立数据集
  • 四、计算均值
  • 五、总结
  • 六、参考链接

一、dlib和opencv建立

请移步:基于dlib库实现人脸特征值提取

二、人脸图像采集并储存

采集20张人脸照片并储存在D:/renlian/p1中

import cv2
import dlib
import os
import sys
import random# coding:utf-8
# 存储位置
output_dir = 'D:/renlian/p1'
size = 1080if 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)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张图片的68个特征值

from cv2 import cv2 as cv2
import os
import dlib
from skimage import io
import csv
import numpy as np# 要读取人脸图像文件的路径
output_dir = 'D:/renlian/p1'# 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):personX = path_faces_personX.split('/')[-1]array_feature_path = './array_feature/' + personXif not os.path.exists(array_feature_path):  # 创建文件夹存储特征数组os.makedirs(array_feature_path)print('yes')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])# 保存每张图片的特征点face_feature_path = array_feature_path + '/face_features' + photos_list[i][:-4] + '.txt'with open(face_feature_path, 'w', encoding='utf-8')as fp:for j in range(len(features_128d)):fp.write(str(features_128d[j]))if j%2==0:fp.write(',')else:fp.write('\n')# 遇到没有检测出人脸的图片跳过if features_128d == 0:i += 1else:features_list_personX.append(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("./person_feature/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: ./feature/features_all2.csv")print('每张图片的特征点数组存入: ./array_feature')

运行结果:

四、计算均值

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:/test/aa/"# Dlib 正向人脸检测器
detector = dlib.get_frontal_face_detector()# Dlib 人脸预测器
predictor = dlib.shape_predictor("D:\\test\\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("D:\\test\\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)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:\\test\\PERSION.txt", "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:\\test\\PERSION.txt")

运行结果:

五、总结

通过dlib库建立人脸数据集,采集了每张图片的68个特征点,计算了20张图片的平均特征数据。

六、参考链接

Dlib人脸识别】1. Dlib人脸检测的基本原理

利用dlib和opencv建立人脸识别数据集并进行人脸识别相关推荐

  1. dlib 使用OpenCV,Python和深度学习进行人脸识别 源代码

    请直接访问原文章 dlib 使用OpenCV,Python和深度学习进行人脸识别 源代码 https://hotdog29.com/?p=595 在 2019年7月7日 上张贴 由 hotdog发表回 ...

  2. CV之FDFA:利用MTCNN的脚本实现对LFW数据集进行FD人脸检测和FA人脸校准

    CV之FD&FA:利用MTCNN的脚本实现对LFW数据集进行FD人脸检测和FA人脸校准 目录 运行结果 运行过程 运行(部分)代码 在裁剪好的LFW数据集进行验证 运行结果 运行过程 time ...

  3. python︱利用dlib和opencv实现简单换脸、人脸对齐、关键点定位与画图

    这是一个利用dlib进行关键点定位 + opencv处理的人脸对齐.换脸.关键点识别的小demo.原文来自于<Switching Eds: Face swapping with Python, ...

  4. 骨骼的动作识别数据集_[骨架动作识别]数据集

    NTU-RGBD CVPR2016 总共大约有56000个视频,60类动作,50类是单人动作,10类是双人交互动作.每个人捕捉了25个关节点.数据集有两种分割方式,cross subject 和cro ...

  5. 车牌识别数据集_行人再识别数据集

    目前行人再识别的数据集比较常用的有:Market-1501. DukeMTMC-reID.CUHK03,后面有时间会上传如何处理数据集的代码.目前常使用的方式:数据集下有以下几个文件夹: train: ...

  6. 利用dlib进行人脸检测

    一.dlib环境搭建 1)dlib安装,pip install dlib.参考:http://www.cnblogs.com/vipstone/p/8964656.html . 2)训练模型下载:训练 ...

  7. CNN表情识别系统制作(1)----fer2013人脸表情数据集简介

    fer2013人脸表情数据集简介 fer2013人脸表情数据集由35886张人脸表情图片组成,其中,测试图(Training)28708张,公共验证图(PublicTest)和私有验证图(Privat ...

  8. Github上10个开源好用的人脸识别数据集

    在本文中,我们列出了 10 个可用于启动人脸识别项目的人脸数据集. 1| Flickr-Faces-HQ 数据集 (FFHQ) Flickr-Faces-HQ 数据集(FFHQ)是一个由人脸组成的数据 ...

  9. 面部表情识别1:表情识别数据集(含下载链接)

    面部表情识别1:表情识别数据集(含下载链接) 目录 面部表情识别1:表情识别数据集(含下载链接) 1.前言 2.表情识别数据集介绍 1.JAFFE数据集 2.KDEF(Karolinska Direc ...

  10. 人脸表情数据集-fer2013

    ------韦访 20181102 1.概述 ---- 2.fer2013人脸表情数据集简介 Fer2013人脸表情数据集由35886张人脸表情图片组成,其中,测试图(Training)28708张, ...

最新文章

  1. 【JSConf EU 2018】WebAssembly 的手工艺术
  2. linux下指数函数,用GeoGebra画指数函数图像、查看函数变化轨迹
  3. mysql5.1.6安装_mysql 5.1.6的安装启动
  4. 为啥 .NET 自带的 JsonSerializer 无法序列化 Field ?
  5. 计算机ftp怎么登陆新用户,多用户登录ftp
  6. CodeSmith 5.0工具实例篇系列4——根据表生成修改的存储过程,针对MS Sqlserver
  7. 80-10-020-原理-Java NIO-HeapByteBuffer
  8. 如何为curl命令添加数据?
  9. 本人工作性质已改变,技术文摘随笔已经全部下线
  10. python程序设计基础期末考试题,python程序设计基础答案章节期末答案
  11. 玉米社:抖音玩法和运营机制,学会这些技巧,轻松上热门
  12. Ubuntu 18.04 究极美化教程
  13. 湖南计算机office三月份,2020年3月计算机二级MS Office考试怎么准备
  14. 美的华为鸿蒙,董明珠万没想到,格力终将被美的超越,华为鸿蒙“功不可没”...
  15. sicilyOJ 11珠海赛重现 C Unlosing Ranger V.S. Darkdeath Evilman(DP)
  16. caxa图文档服务器未启动,CAXA协同管理图文档
  17. pycharm安装netmiko、xlwt
  18. Linux中如何添加自己的路径到PATH
  19. python可以用什么软件编写,用python写的软件有哪些
  20. 撇开代码不说,谈谈我对架构的6个冷思考

热门文章

  1. 压力测试-Jmeter
  2. php我的世界网页地图,探险家地图 - Minecraft Wiki,最详细的官方我的世界百科
  3. Linux下ftp搭建
  4. 服务器内存有很多不显示,这里大神多,帮忙看看这个服务器内存是不是真的
  5. Java实现word转HTML
  6. python精通 epub_跟老齐学Python:从入门到精通[azw3+epub+mobi][8.59MB]
  7. 南方CASS9.0软件资源下载附安装教程
  8. 简单C语言程序的编写,c语言编写简单程序.doc
  9. es java api 获取总数_java Es Api --解决大量数据查询
  10. [Java] 自己写了个随机抽签器