发布于2019-08-19 17:26 阅读(327) 评论(0) 点赞(1) 收藏(3)
ps.这个步骤不能pip直接安装,需要先下载boosting和cmake搭建环境,再解压安装获得dlib库文件,最后复制关键文件到python安装目录下!
需要下载的文件:
具体安装教程:
python3.7添加dlib模块——需要耐心安装注意:
b2 -a –with-python address-model=64 toolset=msvc runtime-link=static
这一句运行时需要删去-with
b2 -a -python address-model=64 toolset=msvc runtime-link=static其他步骤根据教程安装即可!
基于dlib库对人脸特征进行提取,在视频流中抓取人脸特征、并保存为64x64大小的图片文件。
注意的是:因为我们后面会对人脸数据集进行训练识别,因此,这一步非常重要。
尽量同一设备——采取数据集和做人脸识别
。源码:
import cv2
import dlib
import os
import sys
import random
# 存储位置
output_dir = 'D:/myworkspace/JupyterNotebook/People/person/person1'
size = 64
if 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 = 255
elif tmp < 0:
tmp = 0
img[j,i,c] = tmp
return 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 <= 15):#存储15张人脸特征图像
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 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
face = 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 += 1
key = cv2.waitKey(30) & 0xff
if key == 27:
break
else:
print('Finished!')
# 释放摄像头 release camera
camera.release()
# 删除建立的窗口 delete all the windows
cv2.destroyAllWindows()
break
作者:343ueru
链接:https://www.pythonheidong.com/blog/article/48864/ceb6ea9bf3b9cff3c538/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!