【人脸识别】提取68维特征值与正脸操作

我做的项目大题有两种思路,一是用opencv库直接训练预测,第二种是通过dlib库提取特征值在根据特征值用其它的机器学习方法预测,此篇先介绍几个技术点



技术点一,检测人脸提取68维特征值:

import dlib
import cv2


#源程序是用sys.argv从命令行参数去获取训练模型,精简版我直接把路径写在程序中了
predictor_path = "C:\\Users\\51530\\Desktop\\openFace\\shape_predictor_68_face_landmarks.dat"

faces_path = "C:\\Users\\51530\\Desktop\\openFace\\timg.jpg"
#与人脸检测相同,使用dlib自带的frontal_face_detector作为人脸检测器
detector = dlib.get_frontal_face_detector()

#使用官方提供的模型构建特征提取器
predictor = dlib.shape_predictor(predictor_path)
#读取图片
img = cv2.imread(faces_path)

#与人脸检测程序相同,使用detector进行人脸检测 dets为返回的结果
dets = detector(img, 1)
#使用enumerate 函数遍历序列中的元素以及它们的下标
#下标k即为人脸序号
#left:人脸左边距离图片左边界的距离 ;right:人脸右边距离图片左边界的距离
#top:人脸上边距离图片上边界的距离 ;bottom:人脸下边距离图片上边界的距离
for k, d in enumerate(dets):
    print("dets{}".format(d))
    print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
    k, d.left(), d.top(), d.right(), d.bottom()))

    #使用predictor进行人脸关键点识别 shape为返回的结果
    shape = predictor(img, d)
    #获取第一个和第二个点的坐标(相对于图片而不是框出来的人脸)
    print("Part 0: {}, Part 1: {} ...".format(shape.part(0),  shape.part(1)))

    #绘制特征点
    for index, pt in enumerate(shape.parts()):
        print('Part {}: {}'.format(index, pt))
        pt_pos = (pt.x, pt.y)
        cv2.circle(img, pt_pos, 2, (255, 0, 0), 1)



cv2.imshow('test2', img)
k = cv2.waitKey(0)
cv2.destroyAllWindows()
#等待点击

原理论文:

Kazemi V, Sullivan J. One millisecond face alignment with an ensemble of regression trees[C]//Computer Vision and Pattern Recognition (CVPR), 2014 IEEE Conference on. IEEE, 2014: 1867-1874


技术点二,正脸操作

import dlib
import cv2
import numpy as np


#源程序是用sys.argv从命令行参数去获取训练模型,精简版我直接把路径写在程序中了
predictor_path = "C:\\Users\\51530\\Desktop\\openFace\\shape_predictor_68_face_landmarks.dat"

faces_path = "C:\\Users\\51530\\Desktop\\openFace\\timg.jpg"
#与人脸检测相同,使用dlib自带的frontal_face_detector作为人脸检测器
detector = dlib.get_frontal_face_detector()

#使用官方提供的模型构建特征提取器
predictor = dlib.shape_predictor(predictor_path)
#读取图片
bgr_img = cv2.imread(faces_path)
rgb_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB)

#与人脸检测程序相同,使用detector进行人脸检测 dets为返回的结果
dets = detector(rgb_img, 1)
for k, face in enumerate(dets):
    left = face.left()
    top = face.top()
    right = face.right()
    bottom = face.bottom()
    resized = cv2.resize(bgr_img[top: bottom, left: right], (300, 300), interpolation=cv2.INTER_AREA)
    cv2.imshow('1',resized)

# 识别人脸特征点,并保存下来
faces = dlib.full_object_detections()
for det in dets:
    faces.append(predictor(rgb_img, det))
#人脸对齐
images = dlib.get_face_chips(rgb_img, faces, size=300)
for image in images:
   cv_rgb_image = np.array(image).astype(np.uint8)# 先转换为numpy数组
   print(cv_rgb_image.shape)
   bgr = cv2.cvtColor(cv_rgb_image, cv2.COLOR_RGB2BGR)
   cv2.imshow('2',bgr)
cv2.waitKey(0)
cv2.destroyAllWindows()

(正脸前)

(正脸后)


  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值