当前位置:网站首页>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
边栏推荐
- [oiclass] share prizes
- 【指针】删除字符串s中的所有空格
- Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
- What level do 18K test engineers want? Take a look at the interview experience of a 26 year old test engineer
- “人生若只如初见”——RISC-V
- Don't you even look at such a detailed and comprehensive written software test question?
- Install and run tensorflow object detection API video object recognition system of Google open source
- How to use Moment. JS to check whether the current time is between 2 times
- 函数:计算字符串中大写字母的个数
- 函数:求方程的根
猜你喜欢
Wang Shuang's detailed learning notes of assembly language II: registers
How to transform functional testing into automated testing?
Vysor uses WiFi wireless connection for screen projection_ Operate the mobile phone on the computer_ Wireless debugging -- uniapp native development 008
Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class
5分钟掌握机器学习鸢尾花逻辑回归分类
Database monitoring SQL execution
数字电路基础(一)数制与码制
王爽汇编语言学习详细笔记一:基础知识
The common methods of servlet context, session and request objects and the scope of storing data in servlet.
基于485总线的评分系统双机实验报告
随机推荐
servlet中 servlet context与 session与 request三个对象的常用方法和存放数据的作用域。
[pointer] use the insertion sorting method to arrange n numbers from small to large
[pointer] solve the last person left
High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
Database monitoring SQL execution
CSAPP homework answers chapter 789
Function: find 1-1/2+1/3-1/4+1/5-1/6+1/7-... +1/n
Common Oracle commands
Statistics 8th Edition Jia Junping Chapter 1 after class exercises and answers summary
Interview Essentials: what is the mysterious framework asking?
【指针】删除字符串s中的所有空格
数字电路基础(一)数制与码制
Functions: Finding Roots of equations
Keil5-MDK的格式化代码工具及添加快捷方式
指针 --按字符串相反次序输出其中的所有字符
刷视频的功夫,不如看看这些面试题你掌握了没有,慢慢积累月入过万不是梦。
Function: string storage in reverse order
Fundamentals of digital circuit (V) arithmetic operation circuit
数字电路基础(三)编码器和译码器
Software testing interview summary - common interview questions