利用dlib库进行人脸识别
现如今人脸识别的技术已经十分先进了,识别率很高,dlib也是人脸识别常用的一个库,可以检测出人脸上的68个点,并且进行标注,当我们准备自己的人脸数据时,常常用dlib进行数据提取。
首先需要在python中安装dlib
pip install dlib==19.6.1
如果提示无法编译的错误,则需要在python环境中安装cmake,之后再安装dlib就能够成功
dlib人脸检测使用分为两种,一种是对人脸检测后提取整个人脸区域,一种是在人脸上标注特征点
人脸区域
import cv2
import numpy as np
import time
import os
from matplotlib import pyplot as plt
import dlib
detector = dlib.get_frontal_face_detector()
img = cv2.imread('E:\\private\\deeplearning\\dlib\\timg.jpg')
img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
faces = detector(img_gray, 1)
for index, face in enumerate(faces):
left = face.left()
top = face.top()
right = face.right()
bottom = face.bottom()
cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0))
cv2.imwrite('E:\\private\\deeplearning\\dlib\\timg1.jpg', img)
女神镇楼
import cv2
import numpy as np
import time
import os
from matplotlib import pyplot as plt
import dlib
detector = dlib.get_frontal_face_detector()
img = cv2.imread('E:\\private\\deeplearning\\dlib\\timg.jpg')
img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
faces = detector(img_gray, 1)
for face in faces:
shape = predictor(img, face) # 寻找人脸的68个标定点
# 遍历所有点,打印出其坐标,并圈出来
for pt in shape.parts():
pt_pos = (pt.x, pt.y)
cv2.circle(img, pt_pos, 2, (0, 255, 0), 2)
cv2.imwrite('E:\\private\\deeplearning\\dlib\\timg1.jpg', img)