<div id="article_content" class="article_content clearfix">
<link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-1a85854398.css">
<div id="content_views" class="markdown_views prism-atom-one-light">
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
<path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
</svg>
<h1><a name="t0"></a><a id="_0"></a>一.项目简介</h1>
<h2><a name="t1"></a><a id="1_1"></a>1.数据</h2>
<p><strong>数据下载链接</strong><a href="https://pan.baidu.com/s/1vXrv0E4rmvVKn0GpuGoeOw">https://pan.baidu.com/s/1vXrv0E4rmvVKn0GpuGoeOw</a></p>
<blockquote>
<p>数据集:总共数据集由两部分组成:他人脸图片集及我自己的部分图片<br> 自己图片目录:face_recog/my_faces<br> 他人图片目录:face_recog/other_faces<br> 我的测试图片目录:face_recog/test_faces</p>
</blockquote>
<h2><a name="t2"></a><a id="2_7"></a>2.人脸识别</h2>
<blockquote>
<p>获取数据后,第一件事就对对图片进行处理,即人脸识别,把人脸的范围确定下来,人脸识别有很多方法,这里使用的是 dlib 来识别人脸部分,当然也可以使用 opencv 来识别人脸,<code>在实际使用过程中,dlib 的识别效果比 opencv 的好一些</code>。<br> 识别处理后的图片存放路径为:my_faces(存放预处理我的图片,里面还复制一些图片),other_faces(存放预处理别人图片)</p>
</blockquote>
<h2><a name="t3"></a><a id="3_10"></a>3.建立模型,训练数据</h2>
<blockquote>
<p>这里使用卷积神经网络来建立模型,用了 3 个卷积层(采用了池化、dropout 等技术),一个全连接层,分类层、输出层。</p>
</blockquote>
<h2><a name="t4"></a><a id="4_12"></a>4.性能评估</h2>
<h2><a name="t5"></a><a id="5__13"></a>5. 用测试数据,验证模型</h2>
<h1><a name="t6"></a><a id="_15"></a><strong><code>下面开始代码实现啦</code></strong></h1>
<h1><a name="t7"></a><a id="_17"></a>二.数据准备</h1>
<h2><a name="t8"></a><a id="_18"></a>导入模块</h2>
<pre class="prettyprint"><code class="has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;">import sys
import os
import cv2
import dlib
import matplotlib
import time
start=time.time()
<div class="hljs-button {2}" data-title="复制(为作者贡献原力分)" data-report-click="{"spm":"1001.2101.3001.4259"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li></ul></pre>
<h2><a name="t9"></a><a id="1_29"></a>1.获取数据</h2>
<pre class="prettyprint"><code class="has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"># 1.定义输入、输出目录,文件解压到当前目录,my_faces目录下,output_dir_myself 为检测以后我的的头像
input_dir_myself = 'face_recog/my_faces'
output_dir_myself = 'my_faces'
size = 64
*# 2.判断输出目录是否存在,不存在,则创建
if not os.path.exists(output_dir_myself):
os.makedirs(output_dir_myself)
# 3.利用 dlib 的人脸特征提取器,使用 dlib 自带的 frontal_face_detector 作为我们的特征提取器
detector = dlib.get_frontal_face_detector()
<div class="hljs-button {2}" data-title="复制(为作者贡献原力分)" data-report-click="{"spm":"1001.2101.3001.4259"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li></ul></pre>
<h2><a name="t10"></a><a id="2_43"></a>2.预处理数据</h2>
<pre class="prettyprint"><code class="has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"># 接下来使用 dlib 来批量识别图片中的人脸部分,并对原图像进行预处理,并保存到指定目录下。
#1.预处理我的头像
index = 1
for (path, dirnames, filenames) in os.walk(input_dir_myself):
for filename in filenames:
if filename.endswith('.jpg'):
print('Being processed picture %s' % index)
img_path = path+'/'+filename
# 从文件读取图片
img = cv2.imread(img_path)
# 转为灰度图片
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 使用 detector 进行人脸检测 dets 为返回的结果
dets = detector(gray_img, 1)
#使用 enumerate 函数遍历序列中的元素以及它们的下标
#下标 i 即为人脸序号
#left:人脸左边距离图片左边界的距离 ;right:人脸右边距离图片左边界的距离
#top:人脸上边距离图片上边界的距离 ;bottom:人脸下边距离图片上边界的距离
for i, d in enumerate(dets):
x1 = d.top() if d.top() > 0 else 0
y1 = d.bottom() if d.bottom() > 0 else 0
x2 = d.left() if d.left() > 0 else 0
y2 = d.right() if d.right() > 0 else 0
# img[y:y+h,x:x+w]
face = img[x1:y1,x2:y2]
# 调整图片的尺寸
face = cv2.resize(face, (size,size))
cv2.imshow('image',face)
# 保存图片
cv2.imwrite(output_dir_myself + '/' + str(index) + '.jpg', face)
index += 1
# 不断刷新图像,频率时间为 30ms
key = cv2.waitKey(30) & 0xff
if key == 27:
sys.exit(0)
# 2.用同样方法预处理别人的头像(我只选用别人部分头像)
# 定义输入、输出目录,文件解压到当前目录,other_faces目录下
input_dir_other = 'face_recog/other_faces'
output_dir_other = 'other_faces'
size = 64
# 判断输出目录是否存在,不存在,则创建
if not os.path.exists(output_dir_other):
os.makedirs(output_dir_other)
#使用 dlib 自带的 frontal_face_detector 作为我们的特征提取器
detector = dlib.get_frontal_face_detector()
# 预处理别人的头像
index = 1
for (path, dirnames, filenames) in os.walk(input_dir_other):
for filename in filenames:
if filename.endswith('.jpg'):
print('Being processed picture %s' % index)
img_path = path+'/'+filename
# 从文件读取图片
img = cv2.imread(img_path)
# 转为灰度图片
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 使用 detector 进行人脸检测 dets 为返回的结果
dets = detector(gray_img, 1)
#使用 enumerate 函数遍历序列中的元素以及它们的下标
#下标 i 即为人脸序号
#left:人脸左边距离图片左边界的距离 ;right:人脸右边距离图片左边界的距离
#top:人脸上边距离图片上边界的距离 ;bottom:人脸下边距离图片上边界的距离
for i, d in enumerate(dets):
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Python项目源码21_神经网络实现人脸识别.rar (1个子文件)
Python项目源码21_神经网络实现人脸识别
人脸识别.html 34KB
共 1 条
- 1
资源评论
mylife512
- 粉丝: 1461
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功