import face_recognition
from imutils import face_utils
from PIL import Image, ImageDraw,ImageFont
import numpy as np
import threading #导入threading模块
import yagmail
import dlib
import datetime
import time
import cv2
import os
import sys
# 初始化眨眼次数
blink_total = 0
# 初始化张嘴次数
mouth_total = 0
# 设置图片存储路径
# pic_path = './dataset'
# 图片数量
pic_total = 0
# 初始化眨眼的连续帧数以及总的眨眼次数
blink_counter = 0
# 初始化张嘴状态为闭嘴
mouth_status_open = 0
# 眼长宽比例值
EAR_THRESH = 0.15
EAR_CONSEC_FRAMES_MIN = 1
EAR_CONSEC_FRAMES_MAX = 5 # 当EAR小于阈值时,接连多少帧一定发生眨眼动作
# 嘴长宽比例值
MAR_THRESH = 0.15
# 人脸检测器
detector = dlib.get_frontal_face_detector()
# 特征点检测器
predictor = dlib.shape_predictor("modles/shape_predictor_68_face_landmarks.dat")
# 获取左眼的特征点
(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
# 获取右眼的特征点
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]
# 获取嘴巴特征点
(mStart, mEnd) = face_utils.FACIAL_LANDMARKS_IDXS["inner_mouth"]
class Recorder:
pass
red_dict = {}
unknownjpg = []
def sendemail(title,contents,fileslist):#将照片和访问记录上传云端
# yag = yagmail.SMTP("发件人邮箱",'密码' ,'smtp.qq.com', 465)
# yag.send(['收件人邮箱1','收件人邮箱2'],title,contents,fileslist)
yag=yagmail.SMTP("xxx@qq.com",'mbiwgdukvqaadfei','smtp.qq.com',465)#发件人邮箱
yag.send(['xxx@163.com','xxx@163.com'],title,contents,fileslist)#收件人邮箱(注意和上面邮箱不同)
def dicttostr():#生成来访记录列表
strlist = []
listkey =list(sorted(red_dict.keys()))#取字典的key
for item in listkey:#通过循环,合成每一条来访记录
strlist.extend([item + ','+str(onetime) for onetime in red_dict[item].times])
return strlist
flagover = 0#全局标志,用来控制是否保持来访记录
def saveRecorder(name, frame):#保存和添加来访记录
global red_dict
global flagover
global unknownjpg
if flagover == 1:#响应全局标志,如果为1时,关闭来访记录
return
try:
red = red_dict[name]#如果多次识别,比较时间
secondsDiff = (datetime.datetime.now() - red.times[-1]).total_seconds()
if secondsDiff < 5*60: # 如果两次识别在5分钟内,将被过滤掉
return
red.times.append(datetime.datetime.now())
print('更新记录', red_dict, red.times)
except (KeyError):
newRed = Recorder()
newRed.times = [datetime.datetime.now()]
red_dict[name] = newRed
print('添加记录', red_dict, newRed.times)
if name == 'Unknown':
s = str(red_dict[name].times[-1])
print('写入', s[:10] + s[-6:])
filename = s[:10] + s[-6:] + '.jpg'
cv2.imwrite(filename, frame)
unknownjpg.append(filename)
def loop_timer_headle(): # 定时器循环触发函数
print('————————Timer headle!————————', str(datetime.datetime.now()))
global timer2
global flagover
global red_dict
global unknownjpg
flagover = 1
timer2 = threading.Timer(60 * 5, loop_timer_headle) # 创建定时器 5分钟
timer2.start()
# 发送邮件
sendemail("来访统计记录", '\n'.join(dicttostr()), unknownjpg)
red_dict.clear()
unknownjpg.clear()
print("清空")
time.sleep(10)
print("重新开始")
flagover = 0
timer2 = threading.Timer(2, loop_timer_headle)
timer2.start()
def load_img(sample_dir):#导入数据库照片
print('loading sample face..')
for (dirpath, dirnames, filenames) in os.walk(sample_dir): # 一级一级的文件夹递归
print(dirpath, dirnames, filenames)
facelib = []
for filename in filenames:
filename_path = os.sep.join([dirpath, filename])
print(filename_path)
faceimage = face_recognition.load_image_file(filename_path)
# 由于我们每个图像只有一个脸,我只关心每个图像中的第一个编码,所以我取索引0
face_encoding = face_recognition.face_encodings(faceimage)[0]
facelib.append(face_encoding)
return facelib, filenames
# def getFaceEncoding(src):#获取人脸编码
# image = face_recognition.load_image_file(src) # 加载人脸图片
# # 获取图片人脸定位[(top,right,bottom,left )]
# face_locations = face_recognition.face_locations(image)
# img_ = image[face_locations[0][0]:face_locations[0][2], face_locations[0][3]:face_locations[0][1]]
# img_ = cv2.cvtColor(img_, cv2.COLOR_BGR2RGB)
# # display(img_)
# face_encoding = face_recognition.face_encodings(image, face_locations)[0] # 默认人脸数为1,对人脸图片进行编码
# return face_encoding
#对比两张照片距离
# def simcos(a, b):
# a = np.array(a)
# b = np.array(b)
# dist = np.linalg.norm(a - b) # 二范数
# sim = 1.0 / (1.0 + dist) #
# return sim
# 提供对外比对的接口 返回比对的相似度
# def comparison(face_src1, face_src2):
# xl1 = getFaceEncoding(face_src1)
# xl2 = getFaceEncoding(face_src2)
# value = simcos(xl1, xl2)
# print(value)
# 眼长宽比例
def eye_aspect_ratio(eye):
# (|e1-e5|+|e2-e4|) / (2|e0-e3|)
A = np.linalg.norm(eye[1] - eye[5])
B = np.linalg.norm(eye[2] - eye[4])
C = np.linalg.norm(eye[0] - eye[3])
ear = (A + B) / (2.0 * C)
return ear
# 嘴长宽比例
def mouth_aspect_ratio(mouth):
A = np.linalg.norm(mouth[1] - mouth[7]) # 61, 67
B = np.linalg.norm(mouth[3] - mouth[5]) # 63, 65
C = np.linalg.norm(mouth[0] - mouth[4]) # 60, 64
mar = (A + B) / (2.0 * C)
return mar
def main():
global blink_total # 使用global声明blink_total,在函数中就可以修改全局变量的值
global mouth_total
global pic_total
global blink_counter
global mouth_status_open
# video_path, src = sys.argv[1], sys.argv[2]
facelib, facename = load_img('dataset')
vs = cv2.VideoCapture(0)
face_locations = [] # 定义列表存放人脸位置
face_encodings = [] # 定义列表存放人脸特征编码
process_this_frame = True # 定义信号量
while True:
ret, frame = vs.read() # 捕获一帧图片
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) # 将图片缩小1/4,为人脸识别提速
rgb_small_frame = small_frame[:, :, ::-1] # 将opencv的BGR格式转为RGB格式
if process_this_frame: # 使用信号量对当前的处理进行保护
# 找到人脸位置,并生成特征码
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
face_names = [] # 定义列表,放置识别结果
for face_encoding in face_encodings: # 循环多张人脸
matches = face_recognition.compare_faces(facelib, face_encoding) # 人脸识别
name = "Unknown" # 定义默认的识别结果为Unknown
if True in matches: # 如果识别出来,就将名称取出
first_match_index = matches.index(True)
name = facename[first_match_index][:-4]
face_names.append(name) # 保存识别结果
process_this_frame = not process_this_frame # 信号量保护结束
# 显示结果
for (top, right, bottom, left), name in zip(face_locations, face_names):
top *= 4 # 还原人脸的原始尺寸
没有合适的资源?快使用搜索试试~ 我知道了~
使用dlib实现人脸识别+活体检测

共28个文件
bmp:22个
jpg:4个
py:1个

需积分: 10 323 浏览量
2022-12-19
22:46:25
上传
评论 1
收藏 68.47MB RAR 举报
温馨提示
使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实现人脸识别+活体检测使用dlib实
资源推荐
资源详情
资源评论

















收起资源包目录

































共 28 条
- 1
资源评论


学习是种信仰啊
- 粉丝: 91
- 资源: 14
上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


会员权益专享
安全验证
文档复制为VIP权益,开通VIP直接复制
