计算机视觉40例:案例33绘制关键点案例34勾勒五官轮廓
案例33绘制关键点
# 使用dlib进行人脸关键点检测(68个特征点)
## 完整代码
```python
import numpy as np
import cv2
import dlib
# 读取图像
img = cv2.imread("people.jpg")
# Step 1:构造人脸检测器(dlib初始化)
detector = dlib.get_frontal_face_detector()
# Step 2:检测人脸框(使用人脸检测器返回检测到的人脸框)
faces = detector(img, 0)
# Step 3:载入模型(加载预测器)
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
# Step 4:获取每一张脸的关键点(实现检测)
for face in faces:
# 获取关键点
shape = predictor(img, face)
# 将关键点转换为坐标(x,y)的形式
landmarks = np.matrix([[p.x, p.y] for p in shape.parts()])
# Step 5:绘制每一张脸的关键点(绘制shape中的每个点)
for idx, point in enumerate(landmarks):
# 当前关键点的坐标
pos = (point[0, 0], point[0, 1])
# 针对当前关键点,绘制一个实心圆
cv2.circle(img, pos, 2, color=(0, 255, 0), thickness=-1)
# 字体
font = cv2.FONT_HERSHEY_SIMPLEX
# 利用cv2.putText输出1-68,索引序号加1,显示时从1开始
cv2.putText(img, str(idx + 1), pos, font, 0.4, (255, 255, 255), 1, cv2.LINE_AA)
# 绘制结果
cv2.imshow("img", img)
cv2.waitKey()
cv2.destroyAllWindows()
案例34勾勒五官轮廓
```python import numpy as np import dlib import cv2 # 模型初始化 shape_predictor= "shape_predictor_68_face_landmarks.dat" #dace_landmark detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(shape_predictor) # 自定义函数drawLine,将指定的点连接起来 def drawLine(start,end): # 获取点集 pts = shape[start:end] # 遍历点集,将各个点用直线连接起来 for l in range(1, len(pts)): ptA = tuple(pts[l - 1]) ptB = tuple(pts[l]) cv2.line(image, ptA, ptB, (0, 255, 0), 2) # 自定义函数,将指定的点构成一个凸包、绘制其轮廓 def drawConvexHull(start,end): # 注意,凸包用来绘制眼睛、嘴 # 眼睛、嘴也可以用drawLine通过直线绘制 # 但是,使用凸包绘制轮廓,更方便进行颜色填充等设置 # 获取某个特定五官的点集 Facial = shape[start:end] # 针对该五官构造凸包 mouthHull = cv2.convexHull(Facial) # 把凸包轮廓绘制出来 cv2.drawContours(image, [mouthHull], -1, (0, 255, 0), 2) # 读取图像 image=cv2.imread("people.jpg") # 色彩空间转换彩色(BGR)-->灰度(Gray) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 获取人脸 faces = detector(gray, 0) # 对检测到的rects,逐个遍历 for face in faces: # 针对脸部的关键点进行处理,构成坐标(x,y)形式 shape = np.matrix([[p.x, p.y] for p in predictor(gray, face).parts()]) # ============使用函数drawConexHull绘制嘴、眼睛========================= #获取嘴部的关键点集(在整个脸部索引中,其索引范围为[48,60],不包含61) drawConvexHull(48,59) # 嘴内部 drawConvexHull(60,68) # 左眼 drawConvexHull(42,48) # 右眼 drawConvexHull(36,42) # ============使用函数drawLine绘制脸颊、眉毛、鼻子========================= # 将shape转换为np.array shape=np.array(shape) # 绘制脸颊,把脸颊的各个关键点(索引0-16,不含17)用线条连接起来 drawLine(0,17) # 绘制左眉毛,通过将关键点连接实现(索引18-21) drawLine(17,22) # 绘制右眉毛(索引23-26) drawLine(22,27) # 鼻子(索引27-36) drawLine(27,36) cv2.imshow("Frame", image) cv2.waitKey() cv2.destroyAllWindows() ```