python通过Dlib库实现人脸68点特征点标记

1、使用到的库

import cv2                    # 图像处理库
import dlib                   # 人脸识别库
from skimage import io        # 图像处理库

2、代码片段详解

2.1 dlib.get_frontal_face_detector()

功能:人脸检测画框
参数:无
返回值:默认的人脸检测器

detector = dlib.get_frontal_face_detector()
print(detector)
# <dlib.fhog_object_detector object at 0x0000026536162F70>

2.2 dlib.shape_predictor()

功能:标记人脸关键点
参数:shape_predictor_68_face_landmarks.dat:68个关键点模型地址
返回值:人脸关键点预测器
下载链接:http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2

predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
print(predictor)
# <dlib.shape_predictor object at 0x00000265361F77B0>

2.3 io.imread()

skimage.io.imread: 直接返回numpy.ndarray 对象,通道顺序为RGB(注意cv2.imread()生成的是BGR),通道值默认范围0-255。

img = cv2.imread("ma_si_ke.jpg")   # RGB
print(img)

[[[ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]
  ...
  [ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]]

 [[ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]
  ...
  [ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]]

 [[ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]
  ...
  [ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]]

 ...

 [[43 37 30]
  [42 36 29]
  [41 35 28]
  ...
  [ 9  4  5]
  [ 9  4  5]
  [ 9  4  5]]

 [[43 37 30]
  [42 36 29]
  [41 35 28]
  ...
  [ 9  4  5]
  [ 9  4  5]
  [ 9  4  5]]

 [[43 37 30]
  [42 36 29]
  [41 35 28]
  ...
  [ 9  4  5]
  [ 9  4  5]
  [ 9  4  5]]]

2.4 cv2.cvtColor()

cv2.cvtColor(p1,p2) 是颜色空间转换函数,p1是需要转换的图片,p2是转换成何种格式。
cv2.COLOR_BGR2RGB 将BGR格式转换成RGB格式
cv2.COLOR_BGR2GRAY 将BGR格式转换成灰度图片

2.5 detector(img, 0)

dets = detector(img, 0)

功能:对图像画人脸框
参数:img:输入的图片
返回值:人脸检测矩形框4点坐标

# 特征提取器的实例化
dets = detector(img, 0)
print(dets)
print("人脸数:", len(dets))

# rectangles[[(62, 62) (211, 211)]]
# 人脸数: 1

2.6 predictor(img, d)

shape = predictor(img, d)

功能:定位人脸关键点
参数:img:一个numpy ndarray,包含8位灰度或RGB图像
d:开始内部形状预测的边界框
返回值:68个关键点的位置

shape = predictor(img, d)
print(shape)
# <dlib.full_object_detection object at 0x0000025923EB1E30>

2.7 cv2.circle()

cv2.circle(img, (shape.part(i).x, shape.part(i).y), 2, (0, 255, 0), -1, 1)

cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]])
参数说明

img:输入的图片data
center:圆心位置
radius:圆的半径
color:圆的颜色
thickness:圆形轮廓的粗细(如果为正)。负厚度表示要绘制实心圆。
lineType: 圆边界的类型。
shift:中心坐标和半径值中的小数位数。

2.8 cv2.putText()

cv2.putText(img, str(i), (shape.part(i).x, shape.part(i).y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255))

putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) -> img
文本内容,起点坐标,字体,字体大小,颜色,线宽,线类型(cv2.LINE_AA)

2.9 cv2.imshow()

函数cv2.imshow() 显示图像,窗口会自动调整为图像大小。第一个参数是窗口的名字,其次才是我们的图像。你可以创建多个窗口,只要你喜欢,但是必须给他们不同的名字。

cv2.imshow('face_detector_68', img)

2.10 cv2.waitKey(0)

关于cv2.waitKey()的详细说明

3、实现代码

import cv2                    # 图像处理库
import dlib                   # 人脸识别库
from skimage import io        # 图像处理库

# 使用特征提取器get_frontal_face_detector
detector = dlib.get_frontal_face_detector()

# dlib的68点模型,使用官方训练好的特征预测器
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

# 图片所在路径
img = cv2.imread("ma_si_ke.jpg")   # RGB
# img = cv2.imread() # BGR
# img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)

# 特征提取器的实例化
dets = detector(img, 0)
print("人脸数:", len(dets))

for k, d in enumerate(dets):
        print("第", k+1, "个人脸d的坐标:",
              "left:", d.left(),
              "right:", d.right(),
              "top:", d.top(),
              "bottom:", d.bottom())

        width = d.right() - d.left()
        heigth = d.bottom() - d.top()

        print('人脸面积为:',(width*heigth))

        # 利用预测器预测
        shape = predictor(img, d)
        # print(shape)
        # 标出68个点的位置
        for i in range(68):
            cv2.circle(img, (shape.part(i).x, shape.part(i).y), 2, (0, 255, 0), -1, 1)
            cv2.putText(img, str(i), (shape.part(i).x, shape.part(i).y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255))
        
        # 显示一下处理的图片,然后销毁窗口
        cv2.imshow('face_detector_68', img)
        cv2.waitKey(0)

4、成果展示

在这里插入图片描述

  • 13
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Pythondlib实现简单瘦脸功能,你可以按照以下步骤进行: 1. 安装dlib 你可以在终端中使用pip命令安装dlib: ``` pip install dlib ``` 2. 下载预训练的人脸关键检测器 你可以从dlib的官方网站下载预训练的人脸关键检测器。这个检测器是一个文件,可以用于检测人脸,并标记出面部的关键。 下载地址:http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2 3. 导入必要的Python代码中,你需要导入必要的: ```python import dlib import cv2 import numpy as np ``` 4. 加载人脸检测器和关键检测器 在Python中,你可以使用dlib提供的人脸检测器和关键检测器。你需要在代码中加载这两个检测器: ```python detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") ``` 5. 加载图像并进行人脸检测 在Python中,你可以使用OpenCV加载图像,并使用dlib人脸检测器检测人脸: ```python img = cv2.imread("input.jpg") gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = detector(gray) ``` 6. 获取面部关键并进行瘦脸 一旦检测到人脸,你可以使用dlib的关键检测器获取面部关键。一旦你有这些关键,你可以使用NumPy中的数组操作来瘦脸。 下面是一个简单的示例,使用NumPy来轻微地瘦脸: ```python for face in faces: landmarks = predictor(gray, face) landmarks = np.array([[p.x, p.y] for p in landmarks.parts()]) # 绘制面部关键 for landmark in landmarks: cv2.circle(img, tuple(landmark), 2, (0, 255, 0), -1) # 瘦脸 jawline = landmarks[0:17] left_brow = landmarks[17:22] right_brow = landmarks[22:27] nose = landmarks[27:31] left_eye = landmarks[36:42] right_eye = landmarks[42:48] lips = landmarks[48:60] # 将左眼、右眼和嘴巴的中心作为瘦脸的参考 left_eye_center = left_eye.mean(axis=0).astype("int") right_eye_center = right_eye.mean(axis=0).astype("int") mouth_center = lips.mean(axis=0).astype("int") # 计算瘦脸的偏移量 d = (right_eye_center[0] - left_eye_center[0]) // 2 offset_left = (-d, 0) offset_right = (d, 0) offset_mouth = (0, d // 2) # 应用瘦脸的偏移量 jawline += offset_left + offset_right left_brow += offset_left right_brow += offset_right nose += offset_left + offset_right left_eye += offset_left right_eye += offset_right lips += offset_mouth # 绘制瘦脸后的面部关键 for landmark in jawline: cv2.circle(img, tuple(landmark), 2, (255, 0, 0), -1) for landmark in left_brow: cv2.circle(img, tuple(landmark), 2, (255, 0, 0), -1) for landmark in right_brow: cv2.circle(img, tuple(landmark), 2, (255, 0, 0), -1) for landmark in nose: cv2.circle(img, tuple(landmark), 2, (255, 0, 0), -1) for landmark in left_eye: cv2.circle(img, tuple(landmark), 2, (255, 0, 0), -1) for landmark in right_eye: cv2.circle(img, tuple(landmark), 2, (255, 0, 0), -1) for landmark in lips: cv2.circle(img, tuple(landmark), 2, (255, 0, 0), -1) ``` 7. 保存瘦脸后的图像 最后,你可以使用OpenCV中的imwrite函数保存瘦脸后的图像: ```python cv2.imwrite("output.jpg", img) ``` 这就是使用Pythondlib实现简单瘦脸功能的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值