SVM深入理解&人脸特征提取

一、下载dlib库 

1.下载链接

提取码(1111)

2.下载好后打开cmd

输入以下代码进行安装

pip install dlib-19.17.99-cp37-cp37m-win_amd64.whl

安装OpenCV库(注意安装你python对应版本号):

pip install opencv_python==3.4.11.45

13.环境配置

二、利用dlib库在眼睛处绘制黑色的实心圆 

1、下载地址

数据集(提取码1111)

2.导入包


import numpy as np
import cv2
import dlib
import os
import sys
import random

2.获得默认的人脸检测器和训练好的人脸68特征点检测器

 

def get_detector_and_predicyor():
    #使用dlib自带的frontal_face_detector作为我们的特征提取器
    detector = dlib.get_frontal_face_detector()
    """
    功能:人脸检测画框
    参数:PythonFunction和in Classes
    in classes表示采样次数,次数越多获取的人脸的次数越多,但更容易框错
    返回值是矩形的坐标,每个矩形为一个人脸(默认的人脸检测器)
    """
    #返回训练好的人脸68特征点检测器
    predictor = dlib.shape_predictor('..\\source\\shape_predictor_68_face_landmarks.dat')
    return detector,predictor

获取检测器:

#获取检测器
detector,predictor=get_detector_and_predicyor()

给眼睛画圆的函数,这个就是找到眼睛周围的特征点,然后确认中心点,后面用cirecle函数画出来就行了

def painting_sunglasses(img,detector,predictor):   
    #给人脸带上墨镜
    rects = detector(img_gray, 0)  
    for i in range(len(rects)):
        landmarks = np.matrix([[p.x, p.y] for p in predictor(img,rects[i]).parts()])
        right_eye_x=0
        right_eye_y=0
        left_eye_x=0
        left_eye_y=0
        for i in range(36,42):#右眼范围
            #将坐标相加
            right_eye_x+=landmarks[i][0,0]
            right_eye_y+=landmarks[i][0,1]
        #取眼睛的中点坐标
        pos_right=(int(right_eye_x/6),int(right_eye_y/6))
        """
        利用circle函数画圆
        函数原型      
        cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]])
        img:输入的图片data
        center:圆心位置
        radius:圆的半径
        color:圆的颜色
        thickness:圆形轮廓的粗细(如果为正)。负厚度表示要绘制实心圆。
        lineType: 圆边界的类型。
        shift:中心坐标和半径值中的小数位数。
        """
        cv2.circle(img=img, center=pos_right, radius=30, color=(0,0,0),thickness=-1)
        for i in range(42,48):#左眼范围
           #将坐标相加
            left_eye_x+=landmarks[i][0,0]
            left_eye_y+=landmarks[i][0,1]
        #取眼睛的中点坐标
        pos_left=(int(left_eye_x/6),int(left_eye_y/6))
        cv2.circle(img=img, center=pos_left, radius=30, color=(0,0,0),thickness=-1)

调用函数:

camera = cv2.VideoCapture(0)#打开摄像头
ok=True
# 打开摄像头 参数为输入流,可以为摄像头或视频文件
while ok:
    ok,img = camera.read()
     # 转换成灰度图像
    img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    #display_feature_point(img,detector,predictor)
    painting_sunglasses(img,detector,predictor)#调用画墨镜函数
    cv2.imshow('video', img)
    k = cv2.waitKey(1)
    if k == 27:    # press 'ESC' to quit
        break
camera.release()
cv2.destroyAllWindows()

7的意志:

三、总结

Dlib是一个很经典的用于图像处理的开源库,shape_predictor_68_face_landmarks.dat是一个用于人脸68个关键点检测的dat模型库,使用这个模型库可以很方便地进行人脸检测,并进行简单的应用。

四、参考

人脸特征提取(给眼睛画圆)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值