一、dlib库
1. 简介
- Dlib是一个包含机器学习算法的C++开源工具包。
- Dlib可以帮助您创建很多复杂的机器学习方面的软件来帮助解决实际问题。
- 目前Dlib已经被广泛的用在行业和学术领域,包括机器人,嵌入式设备,移动电话和大型高性能计算环境。
Dlib是开源的、免费的;官网和git地址:
官网 : http://dlib.net/
github : https://github.com/davisking/dlib
特点
- 文档齐全
- 高质量的可移植代码
- 提供大量的机器学习 / 图像处理算法
2. 环境配置
- 添加环境变量到Path中
3. dlib库与OpenCV安装
- 使用命令行安装dlib库
根据所使用的Python版本选用适配的dlib库
pip install dlib-19.19.0-cp38-cp38-win_amd64.whl
- 使用Python库中OpenCV,通过
pip指令
进行基本安装
pip install opencv_python==3.4.11.45
问题
遇到这种问题,可能是pip指令未升级,可进行指令升级
修正
python -m pip install --upgrade pip
二、人脸信息采集
- 导入所需的相关包
import numpy as np
import cv2
import dlib
import os
import sys
import random
- 设定图片的亮度等,方便对于人脸68点的采集
output_dir = 'D:/faces'
size = 64
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# 改变图片的亮度与对比度
- 设定图片的基本格式,并调用dlib库的提取器
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('E:\\Ai\\face\\人脸.png')
ok = True
- 调用已调整好的人脸68点采集函数
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('E:\\Ai\\face\\shape_predictor_68_face_landmarks.dat')
- 函数调用,完成人脸图像点采集
while ok:
# 读取摄像头中的图像,ok为是否读取成功的判断参数
ok, img = camera.read()
# 转换成灰度图像
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
rects = detector(img_gray, 0)
for i in range(len(rects)):
landmarks = np.matrix([[p.x, p.y] for p in predictor(img,rects[i]).parts()])
for idx, point in enumerate(landmarks):
# 68点的坐标
pos = (point[0, 0], point[0, 1])
print(idx,pos)
# 利用cv2.circle给每个特征点画一个圈,共68个
cv2.circle(img, pos, 2, color=(0, 255, 0))
# 利用cv2.putText输出1-68
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, str(idx+1), pos, font, 0.2, (0, 0, 255), 1,cv2.LINE_AA)
cv2.imshow('video', img)
k = cv2.waitKey(1)
if k == 27: # press 'ESC' to quit
break
camera.release()
cv2.destroyAllWindows()
7. 运行结果
三、人像墨镜绘制
- 导入所需的相关的包
# 导入包
import numpy as np
import cv2
import dlib
import os
import sys
import random
- 添加函数,获得默认的人脸检测器和训练好的人脸68特征点检测器
def get_detector_and_predicyor():
#使用dlib自带的frontal_face_detector作为我们的特征提取器
detector = dlib.get_frontal_face_detector()
"""
功能:人脸检测画框
参数:PythonFunction和in Classes
in classes表示采样次数,次数越多获取的人脸的次数越多,但更容易框错
返回值是矩形的坐标,每个矩形为一个人脸(默认的人脸检测器)
"""
#返回训练好的人脸68特征点检测器
predictor = dlib.shape_predictor('E:\\Ai\\face\\shape_predictor_68_face_landmarks.dat')
return detector,predictor
- 获取所需的检测器
#获取检测器
detector,predictor=get_detector_and_predicyor()
- 通过
获取特征点函数
,找到人物眼睛的特征点
,并通过Circle函数
对该点进行绘制墨镜
def painting_sunglasses(img,detector,predictor):
#给人脸带上墨镜
rects = detector(img_gray, 0)
for i in range(len(rects)):
landmarks = np.matrix([[p.x, p.y] for p in predictor(img,rects[i]).parts()])
right_eye_x=0
right_eye_y=0
left_eye_x=0
left_eye_y=0
for i in range(36,42):#右眼范围
#将坐标相加
right_eye_x+=landmarks[i][0,0]
right_eye_y+=landmarks[i][0,1]
#取眼睛的中点坐标
pos_right=(int(right_eye_x/6),int(right_eye_y/6))
"""
利用circle函数画圆
函数原型
cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]])
img:输入的图片data
center:圆心位置
radius:圆的半径
color:圆的颜色
thickness:圆形轮廓的粗细(如果为正)。负厚度表示要绘制实心圆。
lineType: 圆边界的类型。
shift:中心坐标和半径值中的小数位数。
"""
cv2.circle(img=img, center=pos_right, radius=30, color=(0,0,0),thickness=-1)
for i in range(42,48):#左眼范围
#将坐标相加
left_eye_x+=landmarks[i][0,0]
left_eye_y+=landmarks[i][0,1]
#取眼睛的中点坐标
pos_left=(int(left_eye_x/6),int(left_eye_y/6))
cv2.circle(img=img, center=pos_left, radius=30, color=(0,0,0),thickness=-1)
- 进行函数调用,获取最后的摄像头权限等,并采集人物图像点
camera = cv2.VideoCapture(0)#打开摄像头
ok=True
# 打开摄像头 参数为输入流,可以为摄像头或视频文件
while ok:
ok,img = camera.read()
# 转换成灰度图像
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#display_feature_point(img,detector,predictor)
painting_sunglasses(img,detector,predictor)#调用画墨镜函数
cv2.imshow('video', img)
k = cv2.waitKey(1)
if k == 27: # press 'ESC' to quit
break
camera.release()
cv2.destroyAllWindows()
- 运行结果
四、总结
- 在此次实验中,主要是通过OpenCV对于摄像头权限进行调用
- 使用dlib对人脸的特征点进行识别提取
五、参考
Py之dlib:Python库之dlib库的简介、安装、使用方法详细攻略
dlib.shape_predictor()关键点检测器
人脸特征提取(dlib+opencv3.4+python3.8)
Dlib库介绍(一)