Python+dlib+opencv实现简单的人脸识别

本文介绍了如何使用Python、dlib和opencv建立人脸数据集,包括采集多角度人脸图像和对应的特征点数组。接着,通过人脸识别实验证明了该方法的有效性,当特征值与平均特征值的差距小于0.4时,系统判断为同一人。总结中强调了人脸特征采集的重要性,并提供了参考资料。
摘要由CSDN通过智能技术生成

接上一篇博客基于dlib+opencv3.4+python3.7的人脸特征提取

一、建立人脸数据集

1.采集人脸

建立自己的人脸数据集:建议采集多角度的20张人脸

import cv2
import dlib
import os
import sys
import random
# 存储位置
output_dir = 'D:/face/631907060410/4me' #采集人脸的路径:其中2***为人名(英文方式)方便对比,第二次编译时修改为3***,进行多人比对
size = 100
 
if not os.path.exists(output_dir):
    os.makedirs(output_dir)
# 改变图片的亮度与对比度
 
def relight(img, light=1, bias=0):
    w = img.shape[1]
    h = img.shape[0]
    #image = []
    for i in range(0,w):
        for j in range(0,h):
            for c in range(3):
                tmp = int(img[j,i,c]*light + bias)
                if tmp > 255:
                    tmp = 255
                elif tmp < 0:
                    tmp = 0
                img[j,i,c] = tmp
    return img
 
#使用dlib自带的frontal_face_detector作为我们的特征提取器
detector = dlib.get_frontal_face_detector()
# 打开摄像头 参数为输入流,可以为摄像头或视频文件
camera = cv2.VideoCapture(0) #打开摄像头方式
#camera = cv2.VideoCapture('2.mp4') #采集视频的人脸

index = 1
while True:
    if (index <= 20):#存储20张人脸特征图像
        print('Being processed picture %s' % index)
        # 从摄像头读取照片
        success, img = camera.read()
        # 转为灰度图片
        gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        # 使用detector进行人脸检测
        dets = detector(gray_img, 1)
 
        for i, d in enumerate(dets):
            x1 = d.top() if d.top() > 0 else 0
            y1 = d.bottom() if d.bottom() > 0 else 0
            x2 = d.left() if d.left() > 0 else 0
            y2 = d.right() if d.right() > 0 else 0
 
            face = img[x1:y1,x2:y2]
            # 调整图片的对比度与亮度, 对比度与亮度值都取随机数,这样能增加样本的多样性
            face = relight(face, random.uniform(0.5, 1.5), random.randint(-50, 50))
 
            face = cv2.resize(face, (size,size))
 
            cv2.imshow('image', face)
 
            cv2.imwrite(output_dir+'/'+str(index)+'.jpg', face)
 
            index += 1
        key = cv2.waitKey(30) & 0xff
        if key == 27:
            break
    else:
        print('Finished!')
        # 释放摄像头 release camera
        camera.release()
        # 删除建立的窗口 delete all the windows
        cv2.destroyAllWindows()
        break

在这里插入图片描述

我采集了三组:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.采集对应20张图片的68个特征点数组和平均特征数组

由于我采集了三组数据,所以会有60个数组(当然,如果有无效图的话就没有60组了)。

当光线过亮或者过暗,五官没有在采集到的图片内或模糊不清时,这张图就无效

from cv2 import cv2 as cv2
import os
import dlib
from skimage import io
import csv
import numpy as np
 
# 要读取人脸图像文件的路径
path_images_from_camera = "D:/face/631907060410/"

# Dlib 正向人脸检测器
detector = dlib.get_frontal_face_detector()

# Dlib 人脸预测器
predictor = dlib.shape_predictor("C:/Users/86150/JupyterProject/shape_predictor_68_face_landmarks.dat") #我这是在同一路径下的,shape_predictor_68_face_landmarks.dat不在同一路径的话
#要写为绝对路径:"D:/****/****/shape_predictor_68_face_landmarks.dat"

# Dlib 人脸识别模型
# Face recognition model, the object maps human faces into 128D vectors
face_rec = dlib.face_recognition_model_v1("C:/Users/86150/JupyterProject/dlib_face_recognition_resnet_model_v1.dat")#我这是在同一路径下的,dlib_face_recognition_resnet_model_v1.dat不在
#要写为绝对路径:"D:/****/****/dlib_face_recognition_resnet_model_v1.dat"

# 返回单张图像的 128D 特征
def return_128d_features(path_img):
    img_rd = io.imread(path_img)
    s=path_img
    a
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值