Ai
4 Star 1 Fork 0

wjwilliam/software_engineering-dlib_face_recognition

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
LivenessDetection.py 3.17 KB
一键复制 编辑 原始数据 按行查看 历史
import cv2, numpy as np
from PyQt6.QtCore import QThread
from scipy.spatial import distance as dist
from imutils import face_utils
from src.utils.GlobalVariable import models
class LivenessDetection(QThread):
def __init__(self):
super().__init__()
self.img1 = np.random.randint(255, size=(900, 800, 3), dtype=np.uint8)
self.img2 = np.random.randint(255, size=(900, 800, 3), dtype=np.uint8)
self.EYE_AR_THRESH = 0.3 #小于0.3时认为是闭眼状态
self.MAR_THRESH = 0.5 #大于于0.5时认为是张嘴状态
#68个人脸特征中眼睛的位置
self.lStart, self.lEnd = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
self.rStart, self.rEnd = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]
#68个人脸特征中嘴巴的位置
self.mStart, self.mEnd = face_utils.FACIAL_LANDMARKS_IDXS["mouth"]
def eye_aspect_ratio(self, eye):
"""
计算眼睛大小
"""
A = dist.euclidean(eye[1], eye[5])
B = dist.euclidean(eye[2], eye[4])
C = dist.euclidean(eye[0], eye[3])
ear = (A + B) / (2.0 * C) #眼睛大小值
return ear
#计算嘴巴张开大小
def mouth__aspect_ratio(self, mouth):
A = dist.euclidean(mouth[2], mouth[9]) # 51, 59
B = dist.euclidean(mouth[4], mouth[7]) # 53, 57
C = dist.euclidean(mouth[0], mouth[6]) # 49, 55
mar = (A + B) / (2.0 * C) #嘴巴大小值
return mar
def compare2faces(self, list_img): #对比两张人脸照片对比是否发生眨眼。两张照片眼睛距离大于0.1时认为发生眨眼
gray1 = cv2.cvtColor(list_img[0], cv2.COLOR_RGB2GRAY)
gray2 = cv2.cvtColor(list_img[1], cv2.COLOR_RGB2GRAY)
rect1 = models.detector(gray1, 0)
rect2 = models.detector(gray2, 0)
list = []
if (len(rect1) == 1) and (len(rect2)) == 1:
list.append(self.comput_eye(gray1, rect1))
list.append(self.comput_eye(gray2, rect2))
result = abs(list[0] - list[1])
print("eye dis: {0}".format(result))
if result >= 0.065:
return True
else:
return False
return False
#判断是否眨眼
def comput_eye(self, gray, rect):
shape = models.predictor(gray, rect[0])
shape = face_utils.shape_to_np(shape) #68个人脸特征坐标
leftEye = shape[self.lStart:self.lEnd]
rightEye = shape[self.rStart:self.rEnd]
leftEAR = self.eye_aspect_ratio(leftEye)
rightEAR = self.eye_aspect_ratio(rightEye)
ear = (leftEAR + rightEAR) / 2.0 # 两个眼睛大小平均值
return ear
#判断是否张开嘴巴
def comput_mouth(self, img):
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
rect = models.detector(gray, 0)
if (len(rect) == 1):
shape = models.predictor(gray, rect[0])
shape = face_utils.shape_to_np(shape) #68个人脸特征坐标
mouth = shape[self.mStart:self.mEnd]
mouth = self.mouth__aspect_ratio(mouth)
if mouth > 0.5:
return True
return False
return False
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/wjwilliam/software_engineering-dlib_face_recognition.git
git@gitee.com:wjwilliam/software_engineering-dlib_face_recognition.git
wjwilliam
software_engineering-dlib_face_recognition
software_engineering-dlib_face_recognition
master

搜索帮助