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("[email protected]",'mbiwgdukvqaadfei','smtp.qq.com',465)#发件人邮箱
yag.send(['[email protected]','[email protected]'],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 # 还原人脸的原始尺寸

入世浮尘
- 粉丝: 242
最新资源
- 软件测试基础理论介绍.pdf
- 通信设备制造行业发展有利因素及不利因素分析报告.docx
- 我国医疗信息化服务行业创新业务模式与市场空间预测分析.docx
- 基于web数字媒体技术专业网络教学平台的方案与实现.doc
- excel制作柏拉图教程.doc
- 依托物联网推动产业结构升级-赤峰市物联网技术应用现状调查.docx
- 软件项目需求申请.doc
- 基于 OpenCV 实现的 C++ 与 Python 版 4 种 YOLO 目标检测(仅依赖 OpenCV 库)
- 大数据驱动与专业导师引导双引擎助力的智能专业人才培养.docx
- 物联网技术在医疗质量管理中的应用研究.docx
- 大数据时代环境下新闻版权的保护.docx
- 互联网+背景下医院诚信档案建设的价值取向及实现路径.docx
- 使用 OpenCV 自带 YOLO 模型实现图片与视频目标检测
- 大学生设计方案MATLAB在数据处理中应用.doc
- 阿里大数据运维经验分享-阿里巴巴.pdf
- 传统企业互联网化发展的突破口.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


