通过 dlib 人脸关键点按照眼球和嘴巴内部轮廓截取图片的 Python 代码
时间: 2024-02-15 09:04:17 浏览: 145
以下是通过 dlib 人脸关键点按照眼球和嘴巴内部轮廓截取图片的 Python 代码:
```python
import dlib
import cv2
import numpy as np
# 加载人脸检测器和关键点检测器
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
# 读取图像
img = cv2.imread("test.jpg")
# 检测人脸
faces = detector(img)
# 获取人脸关键点
for face in faces:
landmarks = predictor(img, face)
# 提取眼球
left_eye_points = np.array([(landmarks.part(36).x, landmarks.part(36).y),
(landmarks.part(37).x, landmarks.part(37).y),
(landmarks.part(38).x, landmarks.part(38).y),
(landmarks.part(39).x, landmarks.part(39).y),
(landmarks.part(40).x, landmarks.part(40).y),
(landmarks.part(41).x, landmarks.part(41).y)], dtype=np.int32)
left_eye_mask = np.zeros(img.shape[:2], dtype=np.uint8)
cv2.drawContours(left_eye_mask, [left_eye_points], -1, 255, -1)
left_eye = cv2.bitwise_and(img, img, mask=left_eye_mask)
right_eye_points = np.array([(landmarks.part(42).x, landmarks.part(42).y),
(landmarks.part(43).x, landmarks.part(43).y),
(landmarks.part(44).x, landmarks.part(44).y),
(landmarks.part(45).x, landmarks.part(45).y),
(landmarks.part(46).x, landmarks.part(46).y),
(landmarks.part(47).x, landmarks.part(47).y)], dtype=np.int32)
right_eye_mask = np.zeros(img.shape[:2], dtype=np.uint8)
cv2.drawContours(right_eye_mask, [right_eye_points], -1, 255, -1)
right_eye = cv2.bitwise_and(img, img, mask=right_eye_mask)
# 提取嘴巴内部
mouth_points = np.array([(landmarks.part(60).x, landmarks.part(60).y),
(landmarks.part(61).x, landmarks.part(61).y),
(landmarks.part(62).x, landmarks.part(62).y),
(landmarks.part(63).x, landmarks.part(63).y),
(landmarks.part(64).x, landmarks.part(64).y),
(landmarks.part(65).x, landmarks.part(65).y),
(landmarks.part(66).x, landmarks.part(66).y),
(landmarks.part(67).x, landmarks.part(67).y)], dtype=np.int32)
mouth_mask = np.zeros(img.shape[:2], dtype=np.uint8)
cv2.drawContours(mouth_mask, [mouth_points], -1, 255, -1)
mouth = cv2.bitwise_and(img, img, mask=mouth_mask)
# 截取眼球和嘴巴内部
left_eye_crop = left_eye[np.min(left_eye_points[:, 1]):np.max(left_eye_points[:, 1]), np.min(left_eye_points[:, 0]):np.max(left_eye_points[:, 0])]
right_eye_crop = right_eye[np.min(right_eye_points[:, 1]):np.max(right_eye_points[:, 1]), np.min(right_eye_points[:, 0]):np.max(right_eye_points[:, 0])]
mouth_crop = mouth[np.min(mouth_points[:, 1]):np.max(mouth_points[:, 1]), np.min(mouth_points[:, 0]):np.max(mouth_points[:, 0])]
# 显示结果
cv2.imshow("Left Eye Crop", left_eye_crop)
cv2.imshow("Right Eye Crop", right_eye_crop)
cv2.imshow("Mouth Crop", mouth_crop)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
代码中,首先加载了人脸检测器和关键点检测器。然后读取待处理的图像,检测人脸,获取人脸关键点。
接着,使用 `cv2.drawContours()` 绘制出眼球和嘴巴内部的轮廓,然后使用 `cv2.bitwise_and()` 取出对应区域的图像。
最后,通过对左右眼球和嘴巴内部的轮廓坐标进行裁剪,截取出相应的区域图像,并使用 `cv2.imshow()` 显示结果。
阅读全文