使用Dlib + python进行面部映射(标注)(python中如何注释,关键词优化)

时间:2024-05-06 11:50:56 作者 : 石家庄SEO 分类 : 关键词优化
  • TAG :

    %E4%BD%BF%E7%94%A8Dlib+%2B+python%E8%BF%9B%E8%A1%8C%E9%9D%A2%E9%83%A8%E6%98%A0%E5%B0%84%EF%BC%88%E6%A0%87%E6%B3%A8%EF%BC%89

识别照片或视频中的面孔非常酷,但这不足以创建强大的应用程序,我们需要更多关于人脸的信息,如坐标,嘴是打开还是关闭,眼睛是否睁开或关闭,在这篇文章中,我将以快速和客观的方式向您呈现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进行面部映射(标注)的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:在Python 2.7即将停止支持时,我们为你准备了一份3.x迁移指南下一篇:

9 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18