之前已经介绍过人脸识别的基础概念,以及基于opencv的实现方式,今天,我们使用dlib来提取128维的人脸嵌入,并使用k临近值方法来实现人脸识别。

人脸识别系统的实现流程与之前是一样的,只是这里我们借助了dlib和face_recognition这两个库来实现。face_recognition是对dlib库的包装,使对dlib的使用更方便。所以首先要安装这2个库。

pip3 install dlib

pip3 install face_recognition

然后,还要安装imutils库

pip3 install imutils

我们看一下项目的目录结构:

.

├── dataset

│ ├── alan_grant [22 entries exceeds filelimit, not opening dir]

│ ├── claire_dearing [53 entries exceeds filelimit, not opening dir]

│ ├── ellie_sattler [31 entries exceeds filelimit, not opening dir]

│ ├── ian_malcolm [41 entries exceeds filelimit, not opening dir]

│ ├── john_hammond [36 entries exceeds filelimit, not opening dir]

│ └── owen_grady [35 entries exceeds filelimit, not opening dir]

├── examples

│ ├── example_01.png

│ ├── example_02.png

│ └── example_03.png

├── output

│ ├── lunch_scene_output.avi

│ └── webcam_face_recognition_output.avi

├── videos

│ └── lunch_scene.mp4

├── encode_faces.py

├── encodings.pickle

├── recognize_faces_image.py

├── recognize_faces_video_file.py

├── recognize_faces_video.py

└── search_bing_api.py

10 directories, 12 files

首先,提取128维的人脸嵌入:

命令如下:

python3 encode_faces.py --dataset dataset --encodings encodings.pickle -d hog

记住:如果你的电脑内存不够大,请使用hog模型进行人脸检测,如果内存够大,可以使用cnn神经网络进行人脸检测。

看代码:

# USAGE

# python encode_faces.py --dataset dataset --encodings encodings.pickle

# import the necessary packages

from imutils import paths

import face_recognition

import argparse

import pickle

import cv2

import os

# construct the argument parser and parse the arguments

ap = argparse.ArgumentParser()

ap.add_argument("-i", "--dataset", required=True,

help="path to input directory of faces + images")

ap.add_argument("-e", "--encodings", required=True,

help="path to serialized db of facial encodings")

ap.add_argument("-d", "--detection-method", type=str, default="hog",

help="face detection model to use: either `hog` or `cnn`")

args = vars(ap.parse_args())

# grab the paths to the input images in our dataset

print("[INFO] quantifying faces...")

imagePaths = list(paths.list_images(args["dataset"]))

# initialize the list of known encodings and known names

knownEncodings = []

knownNames = []

# loop over the image paths

for (i, imagePath) in enumerate(imagePaths):

# extract the person name from the image path

print("[INFO] processing image {}/{}".format(i + 1,

len(imagePaths)))

name = imagePath.split(os.path.sep)[-2]

# load the input image and convert it from RGB (OpenCV ordering)

# to dlib ordering (RGB)

image = cv2.imread(imagePath)

rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# detect the (x, y)-coordinates of the bounding boxes

# corresponding to each face in the input image

boxes = face_recognition.face_locations(rgb,

model=args["detection_method"])

# compute the facial embedding for the face

encodings = face_recognition.face_encodings(rgb, boxes)

# loop over the encodings

for encoding in encodings:

# add each encoding + name to our set of known names and

# encodings

knownEncodings.append(encoding)

knownNames.append(name)

# dump the facial encodings + names to disk

print("[INFO] serializing encodings...")

data = {"encodings": knownEncodings, "names": knownNames}

f = open(args["encodings"], "wb")

f.write(pickle.dumps(data))

f.close()

输出结果是每张图片输出一个人脸的128维的向量和对于的名字,并序列化到硬盘,供后续人脸识别使用。

识别图像中的人脸:

这里使用KNN方法实现最终的人脸识别,而不是使用SVM进行训练。

命令如下:

python3 recognize_faces_image.py --encodings encodings.pickle --image examples/example_01.png

看代码:

# USAGE

# python recognize_faces_image.py --encodings encodings.pickle --image examples/example_01.png

# import the necessary packages

import face_recognition

import argparse

import pickle

import cv2

# construct the argument parser and parse the arguments

ap = argparse.ArgumentParser()

ap.add_argument("-e", "--encodings", required=True,

help="path to serialized db of facial encodings")

ap.add_argument("-i", "--image", required=True,

help="path to input image")

ap.add_argument("-d", "--detection-method", type=str, default="cnn",

help="face detection model to use: either `hog` or `cnn`")

args = vars(ap.parse_args())

# load the known faces and embeddings

print("[INFO] loading encodings...")

data = pickle.loads(open(args["encodings"], "rb").read())

# load the input image and convert it from BGR to RGB

image = cv2.imread(args["image"])

rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# detect the (x, y)-coordinates of the bounding boxes corresponding

# to each face in the input image, then compute the facial embeddings

# for each face

print("[INFO] recognizing faces...")

boxes = face_recognition.face_locations(rgb,

model=args["detection_method"])

encodings = face_recognition.face_encodings(rgb, boxes)

# initialize the list of names for each face detected

names = []

# loop over the facial embeddings

for encoding in encodings:

# attempt to match each face in the input image to our known

# encodings

matches = face_recognition.compare_faces(data["encodings"],

encoding)

name = "Unknown"

# check to see if we have found a match

if True in matches:

# find the indexes of all matched faces then initialize a

# dictionary to count the total number of times each face

# was matched

matchedIdxs = [i for (i, b) in enumerate(matches) if b]

counts = {}

# loop over the matched indexes and maintain a count for

# each recognized face face

for i in matchedIdxs:

name = data["names"][i]

counts[name] = counts.get(name, 0) + 1

# determine the recognized face with the largest number of

# votes (note: in the event of an unlikely tie Python will

# select first entry in the dictionary)

name = max(counts, key=counts.get)

# update the list of names

names.append(name)

# loop over the recognized faces

for ((top, right, bottom, left), name) in zip(boxes, names):

# draw the predicted face name on the image

cv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2)

y = top - 15 if top - 15 > 15 else top + 15

cv2.putText(image, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,

0.75, (0, 255, 0), 2)

# show the output image

cv2.imshow("Image", image)

cv2.waitKey(0)

实际效果如下:

2020022614194059.jpg

到此这篇关于Python基于Dlib的人脸识别系统的实现的文章就介绍到这了,更多相关Python Dlib人脸识别内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

python人脸识别毕业设计-Python基于Dlib的人脸识别系统的实现相关推荐

  1. 基于dlib进行人脸检测

    1.dlib的基本概念 1. Dlib是一个深度学习开源工具,基于C++开发,也支持Python开发接口. 2. 由于Dlib对于人脸特征提取支持很好,有很多训练好的人脸特征提取模型供开发者使用,所以 ...

  2. 基于Dlib的疲劳检测系统

    需要源码的朋友可以私信我 基于Dlib的疲劳检测系统 1.设计背景及要求 2.系统分析 3.系统设计 3.1功能结构图 3.2基于EAR.MAR和HPE算法的疲劳检测 3.2.1基于EAR算法的眨眼检 ...

  3. [附源码]计算机毕业设计JAVA基于JSP社区生鲜配送系统

    [附源码]计算机毕业设计JAVA基于JSP社区生鲜配送系统 项目运行 环境配置: Jdk1.8 + Tomcat7.0 + Mysql + HBuilderX(Webstorm也行)+ Eclispe ...

  4. (附源码)计算机毕业设计SSM基于框架的点餐系统

    (附源码)计算机毕业设计SSM基于框架的点餐系统 项目运行 环境配置: Jdk1.8 + Tomcat7.0 + Mysql + HBuilderX(Webstorm也行)+ Eclispe(Inte ...

  5. (附源码)计算机毕业设计SSM基于web的网上订餐系统

    (附源码)计算机毕业设计SSM基于web的网上订餐系统 项目运行 环境配置: Jdk1.8 + Tomcat7.0 + Mysql + HBuilderX(Webstorm也行)+ Eclispe(I ...

  6. 计算机毕业设计ssm基于ssm流浪宠物领养系统8xg84系统+程序+源码+lw+远程部署

    计算机毕业设计ssm基于ssm流浪宠物领养系统8xg84系统+程序+源码+lw+远程部署 计算机毕业设计ssm基于ssm流浪宠物领养系统8xg84系统+程序+源码+lw+远程部署 本源码技术栈: 项目 ...

  7. 计算机毕业设计系列基于SSM的网上书城系统

    一.项目介绍 计算机毕业设计系列基于SSM的网上书城系统 该系统分为前台和后台两个功能模块,包含管理员和用户两个角色. (1)前台功能模块 首页.新闻资讯.图书列表.图书分类.图书搜索.图书详情.图书 ...

  8. java公交查询系统开题报告_毕业设计论文-基于JAVA的公交查询系统的设计与实现.doc...

    毕业设计论文-基于JAVA的公交查询系统的设计与实现.doc 还剩 42页未读, 继续阅读 下载文档到电脑,马上远离加班熬夜! 亲,很抱歉,此页已超出免费预览范围啦! 如果喜欢就下载吧,价低环保! 内 ...

  9. 计算机毕业设计ssm基于SSM的基金门户系统57d89系统+程序+源码+lw+远程部署

    计算机毕业设计ssm基于SSM的基金门户系统57d89系统+程序+源码+lw+远程部署 计算机毕业设计ssm基于SSM的基金门户系统57d89系统+程序+源码+lw+远程部署 本源码技术栈: 项目架构 ...

最新文章

  1. 35 利用构造函数和原型对象实现继承
  2. java SpringWeb如何实现打印日志
  3. 初入股市之 Hello Stock
  4. Linux里设置环境变量的方法(转)
  5. cmd窗口设置charset
  6. matlab如何使音频文件声音变大_如何制作视频课程
  7. with管理文件操作
  8. 萧山职称计算机考试培训,浙江萧山2017年职称计算机考试时间安排
  9. 实现Repeater控件的记录单选(二)
  10. LeetCode - Easy - 169. Majority Element
  11. mysql数据库如何配置服务_MySQL服务如何实现安装及配置
  12. 离线安装silver light 2
  13. arcgis分隔图层重复出文件_【干货】ArcGIS不可或缺的制图技巧,处理好细节才能让图更专业!...
  14. pycharm的python解释器选择_pycharm中配置python解释器
  15. 用Excel制作条形码
  16. unity 回合制_用Unity E3 Goodness制成
  17. 转行的35岁程序员们
  18. 访达前往文件夹_(苹果电脑excle没有查找全部)苹果笔记本有没有自带excel
  19. 2021 IEEE 编程语言排名
  20. Homework 1 : Knowledge items of C++ Answer (part 1)

热门文章

  1. 第2周个人作业:WordCount
  2. texturepacker使用心得
  3. ZOJ 3822 Known Notation(2014牡丹江Regional K题)
  4. 用Fiddler抓取手机APP数据包
  5. Selenium+Python ---- 免登录、等待、unittest单元测试框架、PO模型
  6. 关于 use-default-filters 属性的说明
  7. mysql连接数设置操作(Too many connections)及设置md5值的加密密码
  8. windows平台安装php_memcache模块
  9. 谈谈Javascript闭包
  10. 水平导航菜单(DIV+CSS)