当前位置:网站首页>Dlib detects blink times based on video stream
Dlib detects blink times based on video stream
2022-07-06 15:02:00 【gmHappy】
Eye aspect ratio (EAR)
In the discussion EAR Before , Have a look first 68 Personal face feature points :
The algorithm of facial feature point detection itself is very complex ,dlib The related implementation is given in .
Each eye consists of 6 individual (x,y) Coordinate representation , Start at the left corner of the eye , Then it displays clockwise around the rest of the area :
Based on this description , We should get to the point : Of these coordinates Width and Height There is a relationship between them .
Soukupová and Čech In its 2016 Year paper “ Real time eye blink detection using facial signs ” The job of , We can deduce the equation reflecting this relationship , be called Eye aspect ratio (EAR):
among p1,...,p6 yes 2D Location of facial landmarks .
The molecule of this equation is to calculate the distance between vertical eye marks , And the denominator is to calculate the distance between horizontal eye marks , Because only One Group horizontal point , however Yes Two sets of vertical points , So the weighted denominator .
Why is this equation so interesting ?
We will find out , The aspect ratio of the eyes is roughly constant when the eyes are open , But in the blink of an eye, it will quickly fall to zero .
Use this simple equation , We It can avoid using image processing technology , Simply rely on Eye landmark distance leave Of The proportion To determine whether a person blinks .
To make it clear , Look at the picture below :
stay Bottom In the figure, the curve of eye aspect ratio over time is drawn . As we can see , The aspect ratio of the eyes is constant , Then it drops rapidly to near zero , And then add , Indicates that a single blink has occurred .
Code implementation
# -*- coding: utf-8 -*-
# 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
def eye_aspect_ratio(eye):
# Calculate the Euclidean distance between two sets
# Vertical eye sign (X,Y) coordinate
A = dist.euclidean(eye[1], eye[5])
B = dist.euclidean(eye[2], eye[4])
# Calculate the Euclidean distance between levels
# Horizontal eye mark (X,Y) coordinate
C = dist.euclidean(eye[0], eye[3])
# Calculation of eye aspect ratio
ear = (A + B) / (2.0 * C)
# Return to the aspect ratio of glasses
return ear
# Define two constants
# Eye aspect ratio
# Flicker threshold
EYE_AR_THRESH = 0.2
EYE_AR_CONSEC_FRAMES = 3
# Initialize the frame counter and total blinks
COUNTER = 0
TOTAL = 0
# initialization DLIB Face detector (HOG), Then create a facial marker prediction
print("[INFO] loading facial landmark predictor...")
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
# Get the indexes of left and right eye facial signs respectively
(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]
cap = cv2.VideoCapture(1)
# Loop frames from video stream
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces in gray frames
rects = detector(gray, 0)
# Face detection cycle
for rect in rects:
# Determine the facial signs of the face area , Then put the facial logo (x,y) Coordinate conversion to digital array
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
# Extract left eye and right eye coordinates , Then use coordinates to calculate the eye aspect ratio of both eyes
leftEye = shape[lStart:lEnd]
rightEye = shape[rStart:rEnd]
leftEAR = eye_aspect_ratio(leftEye)
rightEAR = eye_aspect_ratio(rightEye)
# Average aspect ratio of both eyes
ear = (leftEAR + rightEAR) / 2.0
# Calculate the marker points of the left eye and the right eye and draw
leftEyeHull = cv2.convexHull(leftEye)
rightEyeHull = cv2.convexHull(rightEye)
cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1)
cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)
# Mark the face in the picture , And display
left = rect.left()
top = rect.top()
right = rect.right()
bottom = rect.bottom()
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 3)
'''
The first step is to check whether the aspect ratio of the eye is lower than our blink threshold , If it is , We increment the number of consecutive frames indicating that blinking is occurring . otherwise , We will deal with cases where the aspect ratio of the eye is not lower than the blink threshold , We check it ,
See if a sufficient number of consecutive frames contain blink rates below our predefined threshold . If the inspection passes , We increase the total number of flashes . Then we reset the number of consecutive flashes COUNTER.
'''
if ear < EYE_AR_THRESH:
COUNTER += 1
else:
# If the eyes are closed enough , Then the total number of blinks increases
if COUNTER >= EYE_AR_CONSEC_FRAMES:
TOTAL += 1
# Reset eye frame counter
COUNTER = 0
for (x, y) in shape:
cv2.circle(frame, (x, y), 1, (0, 0, 255), -1)
cv2.putText(frame, "COUNTER: {}".format(COUNTER), (150, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
cv2.putText(frame, "Blinks: {}".format(TOTAL), (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
cv2.putText(frame, "EAR: {:.2f}".format(ear), (300, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
cv2.imshow("Frame", frame)
print(len(rects))
# if the `q` key was pressed, break from the loop
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# do a bit of cleanup
cv2.destroyAllWindows()
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
effect
边栏推荐
- Four methods of exchanging the values of a and B
- 函数:用牛顿迭代法求方程的根
- Global and Chinese market of maleic acid modified rosin esters 2022-2028: Research Report on technology, participants, trends, market size and share
- Get started with Matplotlib drawing
- Statistics, 8th Edition, Jia Junping, Chapter 6 Summary of knowledge points of statistics and sampling distribution and answers to exercises after class
- 指针--剔除字符串中的所有数字
- C language learning summary (I) (under update)
- Function: calculates the number of uppercase letters in a string
- With 27K successful entry ByteDance, this "software testing interview notes" has benefited me for life
- Global and Chinese markets of MPV ACC ECU 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
Express
C language do while loop classic Level 2 questions
DVWA exercise 05 file upload file upload
Réponses aux devoirs du csapp 7 8 9
Want to learn how to get started and learn software testing? I'll give you a good chat today
Query method of database multi table link
后台登录系统,JDBC连接数据库,做小案例练习
Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary
Don't you even look at such a detailed and comprehensive written software test question?
Es full text index
随机推荐
c语言学习总结(上)(更新中)
Function: find the root of the equation by Newton iterative method
Statistics 8th Edition Jia Junping Chapter IX summary of knowledge points of classified data analysis and answers to exercises after class
Statistics 8th Edition Jia Junping Chapter 1 after class exercises and answers summary
1. Payment system
[pointer] delete all spaces in the string s
【指针】求字符串的长度
Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class
STC-B学习板蜂鸣器播放音乐
函数:用牛顿迭代法求方程的根
刷视频的功夫,不如看看这些面试题你掌握了没有,慢慢积累月入过万不是梦。
Database monitoring SQL execution
{1,2,3,2,5} duplicate checking problem
Express
Login the system in the background, connect the database with JDBC, and do small case exercises
四元数---基本概念(转载)
Why can swing implement a form program by inheriting the JFrame class?
Description of Vos storage space, bandwidth occupation and PPS requirements
移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
C language do while loop classic Level 2 questions