'''活体检测'''
# import the necessary packages
from scipy.spatial import distance as dist
from imutils.video import FileVideoStream
from imutils.video import VideoStream
from imutils import face_utils
import numpy as np
import argparse
import imutils
import time
import dlib
import cv2
import random
def eye_aspect_ratio(eye):
# compute the euclidean distances between the two sets of vertical eye landmarks (x, y)-coordinates
# 计算两组垂直眼睛地标(x,y)坐标之间的欧氏距离
A = dist.euclidean(eye[1], eye[5]) # (43, 47) 垂直欧几里德距离
B = dist.euclidean(eye[2], eye[4]) # (44, 46)
# compute the euclidean distance between the horizontal eye landmark (x, y)-coordinates
# 计算水平眼睛地标(x,y)坐标之间的欧氏距离
C = dist.euclidean(eye[0], eye[3]) # (42, 45) 水平欧几里德距离
# compute the eye aspect ratio 长宽比
ear = (A + B) / (2.0 * C)
# return the eye aspect ratio
return ear
def mouth_aspect_ratio(mouth):
A = dist.euclidean(mouth[1], mouth[11])
B = dist.euclidean(mouth[2], mouth[10])
C = dist.euclidean(mouth[3], mouth[9])
D = dist.euclidean(mouth[4], mouth[8])
E = dist.euclidean(mouth[5], mouth[7])
F = dist.euclidean(mouth[0], mouth[6]) # 水平欧几里德距离
ratio = (A + B + C + D + E) / (5.0 * F)
return ratio
def left_right_face_ratio(face):
rightA = dist.euclidean(face[0], face[27])
rightB = dist.euclidean(face[2], face[30])
rightC = dist.euclidean(face[4], face[48])
leftA = dist.euclidean(face[16], face[27])
leftB = dist.euclidean(face[14], face[30])
leftC = dist.euclidean(face[12], face[54])
ratioA = rightA / leftA
ratioB = rightB / leftB
ratioC = rightC / leftC
ratio = (ratioA + ratioB + ratioC) / 3
return ratio # 左转大于2.0,右转小于0.5
# define two constants, one for the eye aspect ratio to indicate
# blink and then a second constant for the number of consecutive
# frames the eye must be below the threshold
EYE_AR_THRESH = 0.25
EYE_AR_CONSEC_FRAMES = 2
# initialize the frame counters and the total number of blinks
COUNTER = 0
TOTAL = 0
OPEN_MOUTH_COUNTER = 0
MOUTH_TOTAL = 0
TRUN_LEFT_TOTAL = 0
TRUN_RIGHT_TOTAL = 0
TRUN_LEFT_COUNTER = 0
TRUN_RIGHT_COUNTER = 0
random_number = random.randint(1, 2)
# initialize dlib's face detector (HOG-based) and then create
# the facial landmark predictor
print("[INFO] loading facial landmark predictor...")
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(r'D:\Python\OpenCV\shape_predictor_68_face_landmarks.dat') # 这里需要修改成你自己的路径
# grab the indexes of the facial landmarks for the left and
# right eye, respectively
(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"] # (42, 48)
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"] # (36, 42)
(mStart, mEnd) = face_utils.FACIAL_LANDMARKS_IDXS["mouth"] # (48, 68)
# start the video stream thread
print("[INFO] starting video stream thread...")
#vs = FileVideoStream(args["video"]).start()
fileStream = True
# vs = VideoStream(src=0).start()
video_capture = cv2.VideoCapture(0)
fileStream = False
textColor = (255, 0, 0)
# loop over frames from the video stream
while True:
ret,frame = video_capture.read()
frame = imutils.resize(frame) # , width=450)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# detect faces in the grayscale frame
rects = detector(gray, 0)
# loop over the face detections
for rect in rects:
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
# extract the left and right eye coordinates, then use the
# coordinates to compute the eye aspect ratio for both eyes
leftEye = shape[lStart:lEnd]
rightEye = shape[rStart:rEnd]
mouth = shape[mStart:mEnd]
leftEAR = eye_aspect_ratio(leftEye)
rightEAR = eye_aspect_ratio(rightEye)
mouthRatio = mouth_aspect_ratio(mouth)
leftRightRatio = left_right_face_ratio(shape)
# average the eye aspect ratio together for both eyes
ear = (leftEAR + rightEAR) / 2.0 # 欧几里德距离,垂直/水平比
# compute the convex hull for the left and right eye, then
# visualize each of the eyes
leftEyeHull = cv2.convexHull(leftEye)
rightEyeHull = cv2.convexHull(rightEye)
mouthHull = cv2.convexHull(mouth)
cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1)
cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)
cv2.drawContours(frame, [mouthHull], -1, (0, 255, 0), 1)
print("leftRightRatio:",leftRightRatio) #
# print("mouthRatio:",mouthRatio) # 0.387004875451 1.01599394304
if mouthRatio > 0.7:
OPEN_MOUTH_COUNTER += 1
else:
if OPEN_MOUTH_COUNTER >= 2:
MOUTH_TOTAL += 1
OPEN_MOUTH_COUNTER = 0
if leftRightRatio >= 2.0:
TRUN_LEFT_COUNTER += 1
elif leftRightRatio <= 0.5:
TRUN_RIGHT_COUNTER += 1
else:
if TRUN_LEFT_COUNTER >= 2:
TRUN_LEFT_TOTAL += 1
if TRUN_RIGHT_COUNTER >= 2:
TRUN_RIGHT_TOTAL += 1
TRUN_LEFT_COUNTER = 0
TRUN_RIGHT_COUNTER = 0
# check to see if the eye aspect ratio is below the blink
# threshold, and if so, increment the blink frame counter
if ear < EYE_AR_THRESH:
COUNTER += 1
# otherwise, the eye aspect ratio is not below the blink
# threshold
else:
# if the eyes were closed for a sufficient number of
# then increment the total number of blinks 闭眼时间大于等于2帧图片
if COUNTER >= EYE_AR_CONSEC_FRAMES:
TOTAL += 1
# 检测完成后,眨眼重新检测
if TRUN_RIGHT_TOTAL >= 1 and TRUN_LEFT_TOTAL >= 1 and MOUTH_TOTAL >= 1:
TRUN_LEFT_TOTAL = 0
TRUN_RIGHT_TOTAL = 0
MOUTH_TOTAL = 0
random_number = random.randint(1, 2)
# reset the eye frame counter
COUNTER = 0
if random_number == 1:
if TRUN_LEFT_TOTAL > 0:
if TRUN_RIGHT_TOTAL > 0:
if MOUTH_TOTAL > 0:
cv2.putText(frame, "live detection finish", (200, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, textColor, 2)
else:
cv2.putText(frame, "Open Mouth", (200, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, textColor, 2)
else:
cv2.putText(frame, "Trun Right Face", (200, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, textColor, 2)
MOUTH_TOTAL = 0
else:
cv2.putText(frame, "Trun Left Face", (200, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, textColor, 2)
MOUTH_TOTAL = 0
TRUN_RIGHT_TOTAL = 0
elif random_number == 2:
if MOUTH_TOTAL > 0:
if TRUN_RIGHT_TOTAL > 0:
if TRUN_LEFT_TOTAL > 0:
cv2.putText(frame, "live detection finish", (200, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, textColor, 2)
else:
cv2.putText(frame, "Trun Left Face", (200, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, textColor, 2)
else:
cv2.putText(frame, "Trun Right Face", (200, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, textColor, 2)
TRUN_LEFT_TOTAL = 0
else:
cv2.putText(frame, "Open Mouth", (200, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, textColor, 2)
TRUN_LEFT_TOTAL = 0
TRUN_RIGHT_TOTAL = 0
if len(rects) == 0:
cv2.putText(frame, "No Face Detected!", (200, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0 ,255), 2)
# show the frame
cv2.imshow("Frame", frame)
if cv2.waitKey(30) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
评论5
最新资源