当前位置:网站首页>Dlib library blink
Dlib library blink
2022-06-30 08:03:00 【emplace_ back】
See the comments
import cv2 as cv
import dlib
import imutils
import numpy as np
from imutils import face_utils as fu
def eye_aspect_ratio(eye):
# Calculate the Euclidean distance up and down
a = np.linalg.norm(eye[1] - eye[5])
b = np.linalg.norm(eye[2] - eye[4])
# Calculate the left and right Euclidean distance
c = np.linalg.norm(eye[0] - eye[3])
# Calculate the aspect ratio and return
d = (a + b) / (2.0 * c)
return d
COUNTER = 0
TOTAL = 0
# Turn on the camera
cap = cv.VideoCapture(0, cv.CAP_DSHOW)
# Establish a face detector
detector = dlib.get_frontal_face_detector()
# establish 68 Key point detector
predictor = dlib.shape_predictor('./1.dat')
# Returns the left and right eyes of the face 68 Start and end in key points
(lStart, lEnd) = fu.FACIAL_LANDMARKS_IDXS["left_eye"]
(rStart, rEnd) = fu.FACIAL_LANDMARKS_IDXS["right_eye"]
# As long as the camera can be turned on correctly
while cap.isOpened():
# Get every frame
_, frame = cap.read()
# Set output width
frame = imutils.resize(frame, width=750)
# Convert to gray image to speed up recognition
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
# Return all face frames of this frame
faces = detector(gray, 0)
# Traverse all frames
for face in faces:
# Returns the of the box 68 A coordinate
shape = predictor(gray, face)
# Into a coordinate matrix
shape = fu.shape_to_np(shape)
# Returns the coordinates of the left and right eyes
leftEye = shape[lStart:lEnd]
rightEye = shape[rStart:rEnd]
# Calculate the aspect ratio of the left eye and the right eye
leftEAR = eye_aspect_ratio(leftEye)
rightEAR = eye_aspect_ratio(rightEye)
# Average.
earAVG = (leftEAR + rightEAR) / 2.0
# Calculate the convex hull of the left and right eyes
leftEyeHull = cv.convexHull(leftEye)
rightEyeHull = cv.convexHull(rightEye)
# Circle the convex hull , That is, the scope of the eyes
cv.drawContours(frame, [leftEyeHull], -1, (255, 255, 255), 1)
cv.drawContours(frame, [rightEyeHull], -1, (255, 255, 255), 1)
''' If the aspect ratio of the eye is less than 0.3 And continuity 3 frame I think I close my eyes '''
if earAVG < 0.3:
COUNTER += 1
else:
if COUNTER >= 3:
TOTAL += 1
COUNTER = 0
# Next is the processing of the output
# Output blink times in the upper left corner
cv.putText(
frame,
"Blinks:{0}".format(TOTAL),
(10, 20),
cv.FONT_HERSHEY_COMPLEX_SMALL,
1,
(255, 255, 255),
2,
cv.LINE_AA
)
# Immediate real time earAVG
cv.putText(
frame,
"earAVG:{0}".format(earAVG),
(200, 20),
cv.FONT_HERSHEY_COMPLEX_SMALL,
1,
(255, 255, 255),
2,
cv.LINE_AA
)
# The lower right corner prompts the exit message
cv.putText(
frame,
"Press 'Esc' to Quit",
(515, 550),
cv.FONT_HERSHEY_COMPLEX_SMALL,
1,
(255, 255, 255),
2,
cv.LINE_AA
)
# Output every frame
cv.imshow('camera', frame)
# Press Esc sign out
if cv.waitKey(1) & 0xff == 27:
break
# Close all windows
cv.destroyAllWindows()
# Release camera
cap.release()
边栏推荐
- Efga design open source framework openlane series (I) development environment construction
- Deep learning - bounding box prediction
- Projection point of point on line
- Deep learning vocabulary representation
- 想问问,炒股怎么选择证券公司?网上开户安全么?
- Personal blog one article multi post tutorial - basic usage of openwriter management tool
- Self study notes -- use of 74h573
- 期末複習-PHP學習筆記5-PHP數組
- 牛客小白月賽52
- F12抓包用于做postman接口测试的全过程解析
猜你喜欢

鲸探NFT数字臧品系统开发技术分享
![2021.11.20 [reading notes] | differential variable splicing events and DTU analysis](/img/02/6971454e51c015990b5b60b357ee1d.jpg)
2021.11.20 [reading notes] | differential variable splicing events and DTU analysis

深度学习——序列模型and数学符号

AcrelEMS能效管理平台为高层小区用电安全保驾护航

Do you know the IP protocol?

December 4, 2021 - Introduction to macro genome analysis process tools

Armv8 (coretex-a53) debugging based on openocd and ft2232h
![July 30, 2021 [wgs/gwas] - whole genome analysis process (Part I)](/img/37/ae0f7ca03ef564b029c9c709779231.jpg)
July 30, 2021 [wgs/gwas] - whole genome analysis process (Part I)

Simple application of generating function

Halcon12+vs2013 C # configuration
随机推荐
Introduction to opencv (II): image color space conversion and image saving
Experiment 2 LED button PWM 2021/11/15
【花雕体验】13 搭建ESP32C3之PlatformIO IDE开发环境
Go 数据类型篇之基本数据类型之间的转化
2021-10-29 [microbiology] a complete set of 16s/its analysis process based on qiime2 tool (Part I)
25岁,从天坑行业提桶跑路,在经历千辛万苦转行程序员,属于我的春天终于来了
Solve the linear equation of a specified point and a specified direction
December 19, 2021 [reading notes] - bioinformatics and functional genomics (Chapter 5 advanced database search)
Construction of module 5 of actual combat Battalion
鲸探NFT数字臧品系统开发技术分享
Deep learning -- language model and sequence generation
期末复习-PHP学习笔记1
Deep learning - residual networks resnets
[flower carving experience] 13 build the platformio ide development environment of esp32c3
Self study notes -- use of 74h573
MySQL加索引语句不加锁:ALGORITHM=INPLACE, LOCK=NONE
Introduction notes to pytorch deep learning (10) neural network convolution layer
深度学习——Bounding Box预测
JS代码案例
Niuke Xiaobai month race 52