【OpenCV-Python】36.OpenCV的人脸关键点检测——dlib库

36.OpenCV的人脸关键点检测——dlib库



前言

  dlib库是一个十分好用的机器学习库,其源码均由C++实现,并提供了Python 接口,可广泛适用于很多场景。本实例仅简单示范dlib库中关于人脸关键点技术的Python应用。


一、dlib库的安装

  可以使用这个代码在windows环境中进行安装:

pip install dlib-bin -i https://pypi.douban.com/simple/

二、基于dlib库的人脸关键点检测(图片和视频)

  dlib封装了一个人脸关键点提取算法,下载好模型后可直接调用。可以实现的68个点的关键点检测:
在这里插入图片描述
  对图片中的人脸进行检测:

import cv2
import dlib

image = cv2.imread("Tom2.jpeg")

detector = dlib.get_frontal_face_detector()

predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

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

faces = detector(gray, 1)

for face in faces: #(x, y, w, h)
  
    cv2.rectangle(image, (face.left(), face.top()), (face.right(), face.bottom()), (0,255,0), 5)

    shape = predictor(image, face)

    for pt in shape.parts():
        
        pt_position = (pt.x, pt.y)
        
        cv2.circle(image, pt_position, 2, (0, 0, 255), -1)
    cv2.imshow("resulr", image)

cv2.waitKey(0)
cv2.destroyAllWindows()

在这里插入图片描述
  对视频中的人脸进行检测:

import cv2
import dlib

capture = cv2.VideoCapture(0)

detector = dlib.get_frontal_face_detector()

predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

while True:
    
    ret, frame = capture.read()
    
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    
    faces = detector(gray, 1)
    
    for face in faces:
        
        cv2.rectangle(frame, (face.left(), face.top()), (face.right(), face.bottom()), (0,255,0), 3)
        
        shape = predictor(gray, face)
        
        for pt in shape.parts():
            
            pt_position = (pt.x, pt.y)
            
            cv2.circle(frame, pt_position, 3, (0,0,255), -1)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
  
    cv2.imshow("face detection landmark", frame)
capture.release()
cv2.destroyAllWindows()

三、OpenCV-Python资源下载

OpenCV-Python测试用图片、中文官方文档、opencv-4.5.4源码


总结

  以上内容简单的介绍了OpenCV-Python的人脸关键点检测,有关Python、数据科学、人工智能等文章后续会不定期发布,请大家多多关注,一键三连哟(●’◡’●)。

是的,你可以使用OpenCV-Pythondlib实现嘴巴和眼睛的分割。以下是基本步骤: 1. 使用dlib中的人脸检测器检测人脸。 2. 使用dlib中的关键点检测器检测人脸的关键点,其中包括眼睛和嘴巴的关键点。 3. 根据关键点的位置,使用OpenCV中的形态学操作和阈值处理来分割眼睛和嘴巴。 你可以使用以下代码片段开始: ```python import cv2 import dlib # 加载人脸检测器和关键点检测器 detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat') # 读取图像 image = cv2.imread('test.jpg') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 检测人脸 faces = detector(gray, 0) for face in faces: # 检测关键点 landmarks = predictor(gray, face) # 提取嘴巴关键点 mouth_points = [] for i in range(48, 68): x = landmarks.part(i).x y = landmarks.part(i).y mouth_points.append((x, y)) # 提取左眼关键点 left_eye_points = [] for i in range(36, 42): x = landmarks.part(i).x y = landmarks.part(i).y left_eye_points.append((x, y)) # 提取右眼关键点 right_eye_points = [] for i in range(42, 48): x = landmarks.part(i).x y = landmarks.part(i).y right_eye_points.append((x, y)) # 分割嘴巴 mouth_mask = np.zeros((image.shape[0], image.shape[1]), dtype=np.uint8) mouth_hull = cv2.convexHull(np.array(mouth_points)) cv2.drawContours(mouth_mask, [mouth_hull], -1, 255, -1) mouth = cv2.bitwise_and(image, image, mask=mouth_mask) # 分割左眼 left_eye_mask = np.zeros((image.shape[0], image.shape[1]), dtype=np.uint8) left_eye_hull = cv2.convexHull(np.array(left_eye_points)) cv2.drawContours(left_eye_mask, [left_eye_hull], -1, 255, -1) left_eye = cv2.bitwise_and(image, image, mask=left_eye_mask) # 分割右眼 right_eye_mask = np.zeros((image.shape[0], image.shape[1]), dtype=np.uint8) right_eye_hull = cv2.convexHull(np.array(right_eye_points)) cv2.drawContours(right_eye_mask, [right_eye_hull], -1, 255, -1) right_eye = cv2.bitwise_and(image, image, mask=right_eye_mask) # 显示结果 cv2.imshow('Mouth', mouth) cv2.imshow('Left Eye', left_eye) cv2.imshow('Right Eye', right_eye) cv2.waitKey(0) cv2.destroyAllWindows() ``` 请注意,此代码片段仅用于演示目的,并且可能需要进行更改以适应您的特定情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

机器视觉小学徒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值