20行代码实现人脸检测

2019-07-23  本文已影响0人  前端极客技术

如今,“刷脸”已经成为人们生活中的日常,刷脸支付、人脸解锁、门禁等,都运用了人脸识别技术。人脸识别技术已广泛应用于金融、司法、公安、教育、医疗等诸多领域,同时也涌现出如:旷视科技、商汤科技等一批优秀的企业。

人脸识别算法主要分为三个流程:

本文我们主要针对人脸检测这一部分,利用OpenCV和face_recognition库分别实现图片中的人脸检测。

开发环境

使用OpenCV进行人脸检测

OpenCV实现人脸检测的主要思路为:

图片转为灰度图

使用OpenCV的cvtColor()方法转换图片颜色

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

使用训练分类器查找人脸

在使用OpenCV的人脸检测之前,需要一个人脸训练模型,格式为xml。

我们这里使用OpenCV提供好的人脸分类模型xml,下载地址如下:
https://github.com/opencv/opencv/tree/master/data/haarcascades

图片上画矩形

使用OpenCV的rectangle()绘制矩形

检测结果

detection_with_face_recognition_result.png

完整代码

# 使用OpenCV进行人脸检测
import cv2
from matplotlib import pyplot as plt

imagePath = './test_face_detection.jpg'
# 引入OpenCV提供的人脸分类模型xml
cascPath = './haarcascade_frontalface_default.xml'
# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)

# 读取图像并转为灰度图
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 检测图片中的人脸
faces = faceCascade.detectMultiScale(
  gray,
  scaleFactor=1.1,
  minNeighbors=4,
  minSize=(30, 30)
)
color = (0, 255, 0)
# 用矩形框将人脸框出来
for (x, y, w, h) in faces:
  cv2.rectangle(image, (x, y), (x+w, y+h), color, 2)

plt.title("Found {0} faces!".format(len(faces)))
plt.axis("off")
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.show()

参数说明

使用face_recognition库进行人脸检测

face_recognition库是使用Dlib最先进的面部识别功能构建而成,具有深度学习功能。该模型在LFW上的准确率为99.38%。

检测结果

detection_with_opencv_result.png

从上面的结果来看,face_recognition库的检测准确率比OpenCV的高。

完整代码

# 使用face_recognition库进行人脸检测
import face_recognition
import cv2
from matplotlib import pyplot as plt

imagePath = './test_face_detection.jpg'

# 使用face_recognition加载图片,并检测人脸
image = face_recognition.load_image_file(imagePath)
#检测图片中所有人脸
face_locations = face_recognition.face_locations(image)

# 用矩形框框出检测到的人脸
for (top, right, bottom, left) in face_locations:
  cv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2)

plt.title("Found {0} faces!".format(len(face_locations)))
plt.axis("off")
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.show()

识别人脸关键点

调用face_recognition.face_landmarks()方法即可得到人脸特征点, 返回一个字典, 下图是返回的数据, 包括chin(下巴), left_eye(左眼)等。

{'chin': [(184, 182), (187, 198), (191, 213), (196, 229), (202, 243), (211, 256), (223, 266), (236, 275), (251, 277), (267, 273), (281, 263), (295, 251), (304, 236), (308, 219), (309, 201), (311, 184), (311, 167)], 'left_eyebrow': [(192, 170), (199, 163), (210, 162), (220, 163), (231, 166)], 'right_eyebrow': [(246, 163), (258, 158), (269, 155), (281, 155), (291, 160)], 'nose_bridge': [(240, 177), (240, 187), (240, 197), (241, 208)], 'nose_tip': [(233, 217), (238, 218), (243, 219), (248, 217), (254, 215)], 'left_eye': [(205, 181), (211, 177), (219, 177), (226, 180), (219, 183), (211, 183)], 'right_eye': [(258, 177), (264, 172), (272, 172), (279, 174), (273, 178), (265, 178)], 'top_lip': [(228, 242), (234, 236), (240, 231), (245, 232), (250, 230), (258, 233), (267, 238), (263, 238), (251, 237), (246, 238), (241, 238), (232, 242)], 'bottom_lip': [(267, 238), (259, 243), (252, 247), (247, 248), (241, 248), (235, 246), (228, 242), (232, 242), (241, 239), (246, 239), (251, 238), (263, 238)]}

人脸关键点检测结果

test_face_landmarks_result.png

完整代码

# 调用face_recognition.face_landmarks()方法得到人脸特征点
import face_recognition
import cv2
from matplotlib import pyplot as plt

imagePath = './test_face_landmarks.jpg'

image = face_recognition.load_image_file(imagePath)
face_landmarks_list = face_recognition.face_landmarks(image)

for each in face_landmarks_list:
  for i in each.keys():
    for any in each[i]:
      image = cv2.circle(image, any, 2, (0, 255, 0), 1)

plt.title("Face landmarks")
plt.axis("off")
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.show()

本文只是使用OpenCV和face_recognition库进行实现简单的人脸检测,基于此我们还可以进行许多好玩的事情,比如:脸部轮廓绘制、数字化妆、头像特效合成等等。

上述人脸检测demo的github地址:
https://github.com/Hanpeng-Chen/tensorflow-learning/tree/master/face_detection

上一篇下一篇

猜你喜欢

热点阅读