东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 5206|回复: 4
打印 上一主题 下一主题

[课堂笔记] 19、Dlib中人脸配准有两种方式一种是使用 get_face_chip()方法

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14431
QQ
跳转到指定楼层
楼主
发表于 2020-3-24 17:15:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
19、Dlib中人脸配准有两种方式一种是使用 get_face_chip()方法

  1. # -*- coding: utf-8 -*-
  2. __author__ = u'东方耀 微信:dfy_88888'
  3. __date__ = '2020/3/24 下午4:28'
  4. __product__ = 'PyCharm'
  5. __filename__ = 'dfy_demo03'

  6. import cv2
  7. import dlib

  8. '''
  9. 人脸配准
  10. 人脸配准是很多其他功能的前提,比如我们最近做的一个脸型预测功能,就必须对人脸进行配准之后才能得到更好的准确率,
  11. 否则脸在水平平面和垂直水平平面进行旋转都会影响准确率的计算。
  12. Dlib中人脸配准有两种方式一种是使用 get_face_chip()方法,使用5个关键点模型来进行配准,
  13. 这种方法Dlib已经提供了完整的接口,
  14. 另一种是自己使用68点关键点模型,根据关键点信息求解变换矩阵,然后把变换矩阵应用到整个图像上。
  15. 先看get_face_chip()方法:

  16. 先检测人脸
  17. 用得到的人脸位置信息来预测关键点
  18. 再使用get_face_chips方法返回配准好的人脸
  19. 这个get_face_chip()方法内部是怎么实现的,我现在还不知道

  20. '''

  21. img_path = '/home/dfy888/DataSets/64_CASIA-FaceV5/images_test/000/000_0.png'

  22. predictor_path = './models/shape_predictor_5_face_landmarks.dat'

  23. # 定义人脸关键点检测器
  24. sp = dlib.shape_predictor(predictor_path)

  25. hog_detector = dlib.get_frontal_face_detector()
  26. cnn_detector = dlib.cnn_face_detection_model_v1('./models/mmod_human_face_detector.dat')


  27. img_data = cv2.imread(img_path)
  28. cv2.imshow('orig', img_data)
  29. cv2.waitKey(0)

  30. rgb_img2 = img_data[:, :, ::-1]


  31. def align_face(detector, five_landmarks_predictor, rgb_img, height=512, width=0):
  32.     # gray_img = cv2.cvtColor(rgb_img, cv2.COLOR_RGB2GRAY)
  33.     faceRects = detector(rgb_img, 0)
  34.     print('检测出%d个人脸' % len(faceRects))
  35.     faces = dlib.full_object_detections()
  36.     faces.append(five_landmarks_predictor(rgb_img, faceRects[0].rect))
  37.     # for faceRect in faceRects:
  38.     #     faces.append(five_landmarks_predictor(rgb_img, faceRect.rect))
  39.     if len(faces) != 0:
  40.         face = dlib.get_face_chip(rgb_img, faces[0], size=139, padding=0.25)
  41.         print(face.shape)
  42.         cv2.imshow("aligned_face", face[:, :, ::-1])


  43. align_face(cnn_detector, sp, rgb_img2)
  44. cv2.waitKey(0)
复制代码








让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14431
QQ
沙发
 楼主| 发表于 2020-3-24 17:21:56 | 只看该作者
成功跑起来的例子:

  1. img_data = cv2.imread('/home/dfy888/DataSets/64_CASIA-FaceV5/images_test/000/000_0.png')

  2. # current word dict
  3. print(os.getcwd())
  4. # 定义人脸检测器
  5. # detector = dlib.get_frontal_face_detector()
  6. cnn_face_detector = dlib.cnn_face_detection_model_v1('./models/mmod_human_face_detector.dat')

  7. predictor_path = './models/shape_predictor_5_face_landmarks.dat'

  8. # 定义人脸关键点检测器
  9. sp = dlib.shape_predictor(predictor_path)

  10. # OpenCV + Dlib对人脸位置进行校准  查找脸部位置
  11. fo = dlib.full_object_detections()

  12. # img_path = '/home/dfy888/DataSets/CelebA/img_align_celeba/100205.jpg'
  13. # img_path = './testimgs/img01.png'
  14. # # img_path = '/home/dfy888/DataSets/VOCdevkit_widerface_dfy/VOC2020/JPEGImages/9--Press_Conference_9_Press_Conference_Press_Conference_9_91.jpg'
  15. # img_data = cv2.imread(img_path)
  16. print(img_data.shape)


  17. img = img_data[:, :, ::-1]
  18. # 1、人脸检测:使用 Dlib 中预先训练的模型检测面部

  19. dets = cnn_face_detector(img, 0)
  20. # dets = detector(img, 1)

  21. num_faces = len(dets)
  22. print('检测出%d个人脸' % num_faces)

  23. if num_faces == 1:
  24.     # 获取人脸的坐标  注意img与img_data的区别
  25.     x1 = dets[0].rect.left()
  26.     y1 = dets[0].rect.top()
  27.     x2 = dets[0].rect.right()
  28.     y2 = dets[0].rect.bottom()
  29.     cv2.rectangle(img_data, (x1, y1), (x2, y2), (0, 0, 255), 2)
  30.     landmarks = np.matrix([[p.x, p.y] for p in sp(img, dets[0].rect).parts()])
  31.     print('每个人脸的关键点个数:', len(landmarks))
  32.     for point in landmarks:
  33.         pos = (point[0, 0], point[0, 1])
  34.         print('pos-', pos)
  35.         cv2.circle(img_data, pos, 2, color=(0, 255, 255), thickness=3)
  36.     # 下面开始clip 查找脸部位置
  37.     fo.append(sp(img, dets[0].rect))
  38.     image = dlib.get_face_chip(img, fo[0], size=139)
  39.     fo.pop()
  40.     image = image[:, :, ::-1]
  41.     cv2.imshow('chip shape:%s' % str(image.shape), image)
  42.     cv2.waitKey(0)
  43.     pass


  44. cv2.imshow('origianl+face', img_data)
  45. cv2.waitKey(0)


复制代码
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

0

主题

13

帖子

33

积分

新手上路

Rank: 1

积分
33
板凳
发表于 2020-3-27 16:03:45 | 只看该作者
谢谢楼主分享代码
回复

使用道具 举报

0

主题

46

帖子

155

积分

注册会员

Rank: 2

积分
155
地板
发表于 2020-4-5 00:14:08 | 只看该作者

感谢楼主的分享
回复

使用道具 举报

0

主题

7

帖子

32

积分

新手上路

Rank: 1

积分
32
5#
发表于 2021-5-24 11:09:39 | 只看该作者
感谢楼主的分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-4-25 05:01 , Processed in 0.175059 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表