使用Dlib + python进行面部映射(标注)
导读:本文共1644.5字符,通常情况下阅读需要5分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要:识别照片或视频中的面孔非常酷,但这不足以创建强大的应用程序,我们需要更多关于人脸的信息,如坐标,嘴是打开还是关闭,眼睛是否睁开或关闭,在这篇文章中,我将以快速和客观的方式向您呈现Dlib,一个能够为您提供68点(坐标)的类库。什么是Dlib?这是一个具有预训练模型的地标检测器,dlib用于估计68个坐标(x,y)的位置,这些坐标映射人脸上的面部点,如下图所示。示例代码在这个“Hello World... ...
目录
(为您整理了一些要点),点击可以直达。识别照片或视频中的面孔非常酷,但这不足以创建强大的应用程序,我们需要更多关于人脸的信息,如坐标,嘴是打开还是关闭,眼睛是否睁开或关闭,在这篇文章中,我将以快速和客观的方式向您呈现Dlib,一个能够为您提供68点(坐标)的类库。
什么是Dlib?
这是一个具有预训练模型的地标检测器,dlib用于估计68个坐标(x,y)的位置,这些坐标映射人脸上的面部点,如下图所示。
示例代码
在这个“Hello World”中我们将使用:
numpy
OpenCV
imutils
在本教程中,我将用dlib编写一个简单的例子。我们在图像上识别和绘制脸上的点。
安装依赖关系
pip install numpy opencv-python dlib imutils
从我们将要处理的图像捕捉开始,我们将使用OpenCV以“无限”循环捕捉图像的网络摄像头,从而给人一种观看视频的印象
import cv2
# if (you have only 1 webcam){ set device = 0} else{ chose your favorite webcam setting device = 1, 2 ,3 ... }
cap = cv2.VideoCapture(0)
while True:
# Getting our image by webcam and converting it into a gray image scale
_, image = cap.read()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# show the gray image
cv2.imshow("Output", image)
#key to give up the app.
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
cap.release()
运行您的脚本并确保您的网络摄像头的图像正在被捕捉。
得到我们的照片后,让我们做主要代码部分。
提醒:我们正在使用已经训练好的模型,我们需要下载shape_predictor_68_face_landmarks.dat文件,您可以在这里找到它(https://github/italojs/facial-landmarks-recognition-/blob/master/shape_predictor_68_face_landmarks.dat)。
from imutils import face_utils
import dlib
import cv2
# Vamos inicializar um detector de faces (HOG) para ento
# let's go code an faces detector(HOG) and after detect the
# landmarks on this detected face
# p = our pre-treined model directory, on my case, it's on the same script's diretory.
p = "shape_predictor_68_face_landmarks.dat"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(p)
cap = cv2.VideoCapture(0)
while True:
# Getting out image by webcam
_, image = cap.read()
# Converting the image to gray scale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Get faces into webcam's image
rects = detector(gray, 0)
# For each detected face, find the landmark.
for (i, rect) in enumerate(rects):
# Make the prediction and transfom it to numpy array
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
# Draw on our image, all the finded cordinate points (x,y)
for (x, y) in shape:
cv2.circle(image, (x, y), 2, (0, 255, 0), -1)
# Show the image
cv2.imshow("Output", image)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
cap.release()
之后,运行脚本。
使用Dlib + python进行面部映射(标注)的详细内容,希望对您有所帮助,信息来源于网络。