东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[课堂笔记] 10、OpenCV + Dlib人脸检测与对人脸位置进行校准与裁剪

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14439
QQ
跳转到指定楼层
楼主
发表于 2020-2-24 15:41:09 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
10、OpenCV + Dlib人脸检测与对人脸位置进行校准与裁剪


人脸校准:使用 Dlib 的实时姿势估计与 OpenCV 的仿射变换来尝试使眼睛和下唇在每个图像上出现在相同位置



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

  6. import numpy as np
  7. import os
  8. import cv2
  9. import dlib

  10. # current word dict
  11. print(os.getcwd())
  12. # 定义人脸检测器
  13. detector = dlib.get_frontal_face_detector()
  14. predictor_path = 'models/shape_predictor_5_face_landmarks.dat'

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

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

  19. # img_path = '/home/dfy888/DataSets/CelebA/img_align_celeba/100205.jpg'
  20. img_path = './testimgs/img01.png'
  21. # img_path = '/home/dfy888/DataSets/VOCdevkit_widerface_dfy/VOC2020/JPEGImages/9--Press_Conference_9_Press_Conference_Press_Conference_9_91.jpg'
  22. img_data = cv2.imread(img_path)
  23. print(img_data.shape)


  24. img = img_data[:, :, ::-1]
  25. # 1、人脸检测:使用 Dlib 中预先训练的模型检测面部
  26. # 这里 0 或 1的区别是?  0是只检测一个人脸 1是所有的
  27. dets = detector(img, 1)

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

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


  51. cv2.imshow('origianl+face', img_data)
  52. cv2.waitKey(0)


复制代码



对人脸位置进行校准.png (540.33 KB, 下载次数: 299)

对人脸位置进行校准.png

summary.jpg (91.92 KB, 下载次数: 290)

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

使用道具 举报

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14439
QQ
沙发
 楼主| 发表于 2020-3-24 15:15:45 | 只看该作者
用cnn的人脸检测器 无法校准

  1. import numpy as np
  2. import os
  3. import cv2
  4. import dlib
  5. import json
  6. import random

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

  8. # current word dict
  9. print(os.getcwd())
  10. # 定义人脸检测器
  11. # detector = dlib.get_frontal_face_detector()
  12. cnn_face_detector = dlib.cnn_face_detection_model_v1('./models/mmod_human_face_detector.dat')

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

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

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

  18. # img_path = '/home/dfy888/DataSets/CelebA/img_align_celeba/100205.jpg'
  19. # img_path = './testimgs/img01.png'
  20. # # img_path = '/home/dfy888/DataSets/VOCdevkit_widerface_dfy/VOC2020/JPEGImages/9--Press_Conference_9_Press_Conference_Press_Conference_9_91.jpg'
  21. # img_data = cv2.imread(img_path)
  22. print(img_data.shape)


  23. img = img_data[:, :, ::-1]
  24. # 1、人脸检测:使用 Dlib 中预先训练的模型检测面部
  25. # 这里 0 或 1的区别是?  0是只检测一个人脸 1是所有的
  26. dets = cnn_face_detector(img, 1)

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

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


  50. cv2.imshow('origianl+face', img_data)
  51. cv2.waitKey(0)



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

使用道具 举报

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14439
QQ
板凳
 楼主| 发表于 2020-3-24 16:24:26 | 只看该作者
# detector检测那块,后面的数字代表我们对图片进行上采样,
# 然后检测,0不做处理,1上采样一倍。相对来说图片越大,检测到的可能性越大,但是速度会越慢
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14439
QQ
地板
 楼主| 发表于 2020-3-24 16:24:40 | 只看该作者
# Dlib提供的这两个检测模型都只能检测脸部区域在70*70以上的图片,对于脸部区域太小的图片无法检测
# Dlib检测出的脸部区域对于下巴和额头区域会做过多的裁剪,这也是这两个模型的缺点
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14439
QQ
5#
 楼主| 发表于 2020-3-24 17:09:29 | 只看该作者
东方耀 发表于 2020-3-24 15:15
用cnn的人脸检测器 无法校准

用cnn的人脸检测器 无法校准
只需要将dets = cnn_face_detector(img, 1)中
1改为0即可
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-13 03:31 , Processed in 0.181638 second(s), 21 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

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