先上实验结果
- '''
- 人脸关键点检测——dlib
- 1 dlib.get_frontal_face_detector() 获取人脸检测器
- 2 dlib.shape_predictor() 预测人脸关键点
- '''
- # import cv2
- #上面的无法自动补全代码
- import cv2.cv2 as cv2
- import matplotlib.pyplot as plt
- import dlib
- import numpy as np
-
-
- #读取图片
- img = cv2.imread("6.png")
-
- #调用人脸检测器
- detector = dlib.get_frontal_face_detector()
-
- #加载 预测关键点模型(68个关键点)
- predictor = dlib.shape_predictor("shape_predictor_81_face_landmarks.dat")
-
- #灰度化
- gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
-
- #人脸检测
- faces = detector(gray, 1) # 1:将图片放大一倍,便于识别人脸; 0 原始图像
-
- a = np.arange(81)
- print("a:",a)
- b = np.arange(81)
-
- if (len(faces) != 0):
- # 找到脸颊区域
- for i in range(len(faces)):
- landmarks = np.matrix([[p.x, p.y] for p in predictor(img, faces[i]).parts()])
- for idx, point in enumerate(landmarks):
- # 68点的坐标
- pos = (point[0, 0], point[0, 1])
- # ROI:脸颊区域
- a = a.tolist() # 将矩阵转换成列表。
- b = b.tolist()
- a[idx] = point[0, 0]
- b[idx] = point[0, 1]
- a = np.array(a) # 将数据转化为矩阵
- b = np.array(b)
-
- # 额头区域
- x1 = int(a[76])
- y1 = int(b[19])
- x2 = int(a[73])
- y2 = int(b[73])
- if (x1 < 0): # 防止人脸转动,x1位置超出图像
- x1 = 0
- forehead_ROI = img[y2:y1, x1:x2]
- cv2.imshow("forehead_ROI",forehead_ROI)
- cv2.waitKey()
-
- # 脸颊区域
- x1 = int((a[2] + a[4]) / 2)
- y1 = int((b[2] + b[4]) / 2)
- y2 = int((b[2] + b[23]) / 2)
- x2 = int((a[12] + a[14]) / 2)
- if (x1 < 0):
- x1 = 0
- cheek_ROI = img[y2:y1, x1:x2]
- cv2.imshow("cheek_ROI",cheek_ROI)
- cv2.waitKey()
- # 检测整个人脸区域
- for k, d in enumerate(faces):
- [x1, x2, y1, y2] = [d.left(), d.right(), d.top(), d.bottom()]
- face_ROI = img[y1:y2, x1:x2]
-
- cv2.imshow("face_ROI",face_ROI)
- cv2.waitKey()
-
- #显示整个效果
- plt.imshow(img[:, :, ::-1]) # BGR 2 RGB
- plt.title("81_face_landmarks")
- plt.axis("off")
- # cv2.imwrite("dlib_81_face_landmarks10.jpg", img)
- plt.show()
-
- '''
- 人脸关键点检测——dlib
- 1 dlib.get_frontal_face_detector() 获取人脸检测器
- 2 dlib.shape_predictor() 预测人脸关键点
- '''
- # import cv2
- #上面的无法自动补全代码
- import cv2.cv2 as cv2
- import matplotlib.pyplot as plt
- import dlib
- import numpy as np
-
- #读取图片
- img = cv2.imread("6.png")
-
- #调用人脸检测器
- detector = dlib.get_frontal_face_detector()
-
- #加载 预测关键点模型(68个关键点)
- predictor = dlib.shape_predictor("shape_predictor_81_face_landmarks.dat")
-
- #灰度化
- gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
-
- #人脸检测
- faces = detector(gray, 1) # 1:将图片放大一倍,便于识别人脸; 0 原始图像
-
- a = np.arange(81)
- b = np.arange(81)
-
- if (len(faces) != 0):
- # 找到脸颊区域
- for i in range(len(faces)):
- landmarks = np.matrix([[p.x, p.y] for p in predictor(img, faces[i]).parts()])
- for idx, point in enumerate(landmarks):
- # 68点的坐标
- pos = (point[0, 0], point[0, 1])
- # ROI:脸颊区域
- a = a.tolist() # 将矩阵转换成列表。
- b = b.tolist()
- a[idx] = point[0, 0]
- b[idx] = point[0, 1]
- a = np.array(a) # 将数据转化为矩阵
- b = np.array(b)
-
- # 额头区域
- x1 = int(a[76])
- y1 = int(b[19])
- x2 = int(a[73])
- y2 = int(b[73])
- if (x1 < 0): # 防止人脸转动,x1位置超出图像
- x1 = 0
- forehead_ROI = img[y2:y1, x1:x2]
- cv2.rectangle(img, (x1,y2), (x2,y1), (0,255,0), 1)
-
- # cv2.imshow("forehead_ROI",forehead_ROI)
- # cv2.waitKey()
-
-
- # 脸颊区域
- x1 = int((a[2] + a[4]) / 2)
- y1 = int((b[2] + b[4]) / 2)
- y2 = int((b[2] + b[23]) / 2)
- x2 = int((a[12] + a[14]) / 2)
- if (x1 < 0):
- x1 = 0
- cheek_ROI = img[y2:y1, x1:x2]
- cv2.rectangle(img, (x1,y2), (x2,y1), (0,255,0), 1)
- # cv2.imshow("cheek_ROI",cheek_ROI)
- # cv2.waitKey()
-
- # 检测整个人脸区域
- for k, d in enumerate(faces):
- [x1, x2, y1, y2] = [d.left(), d.right(), d.top(), d.bottom()]
-
- face_ROI = img[y1:y2, x1:x2]
- cv2.rectangle(img, (x1, y2), (x2, y1), (255, 0, 0), 1) # 蓝色
- # cv2.imshow("face_ROI",face_ROI)
- # cv2.waitKey()
-
- cv2.imshow("ROI",img)
- cv2.waitKey()
-
这个ROI区域确定是根据关键点索引,比较麻烦。引入imutils库,更加简单