您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 苏州分类信息网,免费分类信息发布

使用Dlib + python进行面部映射(标注)

2023/12/18 7:11:26发布29次查看
识别照片或视频中的面孔非常酷,但这不足以创建强大的应用程序,我们需要更多关于人脸的信息,如坐标,嘴是打开还是关闭,眼睛是否睁开或关闭,在这篇文章中,我将以快速和客观的方式向您呈现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()
之后,运行脚本。
苏州分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录