dlib库标注特征点以及裁剪人脸组件

一时失言乱红尘 2024-05-24 01:02 136阅读 0赞

1、安装dlib

当在anaconda下使用命令 pip install dlib 安装dlib库时,提示错误安装不成功,可能是版本不匹配。在虚拟环境下查看Python版本,高版本不一定能和当前python版本兼容。可以将dlib wheel下载到本地,再手动安装。
dlib库:链接:https://pan.baidu.com/s/14FKO_wiMG85s2kgo-1aEZg 提取码:hg5m

在这里插入图片描述

2、标注人脸特征点

  1. # _*_ coding:utf-8 _*_
  2. import numpy as np
  3. import cv2
  4. import dlib
  5. from PIL import Image
  6. import os
  7. import time
  8. detector = dlib.get_frontal_face_detector()
  9. predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
  10. # dictory_name = "E:\code\celeba-1024"
  11. dictory_name = './imgs'
  12. if not os.path.exists('./eye'):
  13. os.mkdir('./eye')
  14. if not os.path.exists('./nose'):
  15. os.mkdir('./nose')
  16. if not os.path.exists('./mouse'):
  17. os.mkdir('./mouse')
  18. # 摄像头捕捉人脸图像
  19. def cap_face():
  20. cap = cv2.VideoCapture(0)
  21. while (cap.isOpened()):
  22. ret, frame = cap.read()
  23. frame = cv2.resize(frame, (800, 650))
  24. start = time.time()
  25. frame = cv2.flip(frame, 1)
  26. dets = detector(frame, 0)
  27. for k, point in enumerate(dets):
  28. shape = predictor(frame, point)
  29. landmarks = np.matrix([[p.x, p.y] for p in shape.parts()])
  30. for num in range(shape.num_parts):
  31. cv2.circle(frame, (shape.parts()[num].x, shape.parts()[num].y), 1, (0, 255, 0), -1)
  32. print("time ->", time.time() - start)
  33. cv2.imshow('frame', frame)
  34. if cv2.waitKey(1) & 0xFF == ord('q'):
  35. break
  36. cap.release()
  37. cv2.destroyAllWindows()
  38. def landmark_face():
  39. img = cv2.imread(dictory_name + "\\" + "000085.jpg.jpg")
  40. # img = cv2.resize(img[:, :, ::-1],dsize=(256, 256))
  41. # 取灰度
  42. # img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
  43. # 人脸数 rects
  44. rects = detector(img, 0)
  45. for k, point in enumerate(rects):
  46. shape = predictor(img, point)
  47. landmarks = np.matrix([[p.x, p.y] for p in shape.parts()])
  48. for num in range(shape.num_parts):
  49. cv2.circle(img, (shape.parts()[num].x, shape.parts()[num].y), 3, (0, 255, 0), -1)
  50. cv2.imshow('image', img)
  51. cv2.imwrite("./imgs/landmark_face.jpg",img)
  52. cv2.waitKey(0)
  53. def crop_face():
  54. for filename in os.listdir(dictory_name):
  55. # cv2读取图像
  56. img = cv2.imread(dictory_name + "\\" + filename)
  57. img = cv2.resize(img[:, :, ::-1], dsize=(256, 256))
  58. # 取灰度
  59. # img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
  60. # 人脸数 rects
  61. rects = detector(img, 0)
  62. for k, point in enumerate(rects):
  63. shape = predictor(img, point)
  64. landmarks = np.matrix([[p.x, p.y] for p in shape.parts()])
  65. for num in range(shape.num_parts):
  66. cv2.circle(img, (shape.parts()[num].x, shape.parts()[num].y), 1, (0, 255, 0), -1)
  67. # cv2.imshow('image', img)
  68. # if len(rects) == 0:
  69. # 眼睛+眉毛
  70. # x_eye_left_up = 63 # 3 23
  71. # y_eye_left_up = 95 # 18 18
  72. # x_eye_right_down = 186 # 92 50
  73. # y_eye_right_down = 134 # 43 43
  74. #
  75. # eye_box = (x_eye_left_up, y_eye_left_up, x_eye_right_down, y_eye_right_down)
  76. # eye_crop = Image.fromarray(img).crop(eye_box)
  77. # eye_crop.save("./eye3" + "/" + filename)
  78. # 鼻子
  79. # x_nose_left_up = 98
  80. # y_nose_left_up = 130
  81. # x_nose_right_down = 155
  82. # y_nose_right_down = 173
  83. #
  84. # nose_box = (x_nose_left_up, y_nose_left_up, x_nose_right_down, y_nose_right_down)
  85. # nose_crop = Image.fromarray(img).crop(nose_box)
  86. # nose_crop.save("./nose" + "/" + filename)
  87. # 嘴巴
  88. # x_mouse_left_up = 90
  89. # y_mouse_left_up = 173
  90. # x_mouse_right_down = 160
  91. # y_mouse_right_down = 210
  92. #
  93. # mouse_box = (x_mouse_left_up, y_mouse_left_up, x_mouse_right_down, y_mouse_right_down)
  94. # mouse_crop = Image.fromarray(img).crop(mouse_box)
  95. # mouse_crop.save("./mouse" + "/" + filename)
  96. # else:
  97. # for i in range(len(rects)): # 超过一个人脸时
  98. # landmarks = np.matrix([[p.x, p.y] for p in predictor(img, rects[i]).parts()])
  99. # # print(type(landmarks)) # numpy.matrix
  100. #
  101. # # 眼睛+眉毛
  102. # x_left_up = landmarks[17, 0]
  103. # y_left_up = landmarks[18, 1] - (landmarks[29, 1] - landmarks[28, 1])*2
  104. # x_right_down = landmarks[26, 0]
  105. # y_right_down = landmarks[28, 1] + (landmarks[29, 1] - landmarks[28, 1])
  106. #
  107. # # 嘴巴+鼻子
  108. # # x_left_up = landmarks[4, 0] + (landmarks[48, 0] - landmarks[4, 0]) / 2
  109. # # y_left_up = landmarks[29, 1]
  110. # # x_right_down = landmarks[54, 0] + (landmarks[12, 0] - landmarks[54, 0]) / 2
  111. # # y_right_down = landmarks[57, 1] + (landmarks[8, 1] - landmarks[57, 1]) / 2
  112. #
  113. # # 眼睛+眉毛+鼻子
  114. # # x_left_up = landmarks[17, 0]
  115. # # y_left_up = landmarks[18, 1] - (landmarks[29, 1] - landmarks[28, 1])*2
  116. # # x_right_down = landmarks[26, 0]
  117. # # y_right_down = landmarks[33, 1] + (landmarks[51, 1] - landmarks[33, 1]) / 2
  118. #
  119. # # 眼睛+眉毛+鼻子+嘴巴
  120. # # x_left_up = landmarks[17, 0]
  121. # # y_left_up = landmarks[18, 1] - (landmarks[29, 1] - landmarks[28, 1]) * 2
  122. # # x_right_down = landmarks[54, 0] + (landmarks[12, 0] - landmarks[54, 0])
  123. # # y_right_down = landmarks[57, 1] + (landmarks[8, 1] - landmarks[57, 1]) / 2
  124. #
  125. # box = (x_left_up, y_left_up, x_right_down, y_right_down)
  126. # image_crop = Image.fromarray(img).crop(box)
  127. # image_crop.save("./eye3" + "/" + filename)
  128. if __name__ == '__main__':
  129. # cap_face()
  130. # crop_face()
  131. landmark_face()

调用landmark_face(),标注图像结果如下:
在这里插入图片描述

3、裁剪人脸组件

crop_face()方法:采用的固定大小的尺度去裁剪人脸组件(眼睛+眉毛,鼻子,嘴巴),使用PIL保存裁剪的结果,如下图所示:
眼睛:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
鼻子:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
嘴巴:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
PIL库中的crop方法:采用两个像素点的坐标(x,y)去裁剪组件,两个点的位置分别为矩形框的左上角和右下角。注意x的方向为向右,y为向下,图像左上角的起始坐标为(0,0)。

4、后记

思考:借助dlib库检测人脸的特征点来辅助裁剪人脸组件,存在哪些问题?

  • 当人脸有姿态和遮挡时,采用固定size的尺度裁剪,无法精准裁剪。优点是裁剪后大小一致,以便网络训练,以及进一步处理。
  • 根据标注的特征点,利用其中的几个特征点来crop人脸组件,特征点中位置要进行加减一定的像素,但是裁剪后的图像大小不一致。当输入给神经网络时,需要resize处理,这样的裁剪好处是能将人脸组件精确裁剪下来。

由于作者水平有限,文中若有不正确的地方,欢迎大家指出,若有任何问题,请在下方讨论。

发表评论

表情:
评论列表 (有 0 条评论,136人围观)

还没有评论,来说两句吧...

相关阅读

    相关 人脸特征检测--基于DLIB

      Dlib是一个C++编写的工具包,它包含了机器学习算法以及一些用来解决现实复杂问题的工具,可以广泛应用于机器人、嵌入式设备、手机,甚至高性能计算中,可以在其官网了解更多。这

    相关 人脸关键提取(dlib)

    作者使用开发环境Ubuntu16.04+Python3 摘要 人脸关键点识别在机器视觉领域已经发展的相当成熟。比如商汤科技人脸关键点定位 毫秒级别眼、口、鼻轮廓等人

    相关 利用dlib进行人脸识别

    现如今人脸识别的技术已经十分先进了,识别率很高,dlib也是人脸识别常用的一个库,可以检测出人脸上的68个点,并且进行标注,当我们准备自己的人脸数据时,常常用dlib进行数据提