識別照片或視頻中的面孔非???,但這不足以創建強大的應用程序,我們需要更多關于人臉的信息,如坐標,嘴是打開還是關閉,眼睛是否睜開或關閉,在這篇文章中,我將以快速和客觀的方式向您呈現Dlib,一個能夠為您提供68點(坐標)的類庫。
這是一個具有預訓練模型的地標檢測器,dlib用于估計68個坐標(x,y)的位置,這些坐標映射人臉上的面部點,如下圖所示。
在這個“Hello World”中我們將使用:
在本教程中,我將用dlib編寫一個簡單的例子。我們在圖像上識別和繪制臉上的點。
安裝依賴關系
pip install numpy opencv-python dlib imutils
從我們將要處理的圖像捕捉開始,我們將使用OpenCV以“無限”循環捕捉圖像的網絡攝像頭,從而給人一種觀看視頻的印象
import cv2
# if (you have only 1 webcam){ set device=0} else{ chose your favorite webcam setting device=1, 2 ,3 ... }
cap=cv2.VideoCapture(0)
while True:
# Getting our image by webcam and converting it into a gray image scale
_, image=cap.read()
gray=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# show the gray image
cv2.imshow("Output", image)
#key to give up the app.
k=cv2.waitKey(5) & 0xFF
if k==27:
break
cv2.destroyAllWindows()
cap.release()
運行您的腳本并確保您的網絡攝像頭的圖像正在被捕捉。
得到我們的照片后,讓我們做主要代碼部分。
提醒:我們正在使用已經訓練好的模型,我們需要下載shape_predictor_68_face_landmarks.dat文件,您可以在這里找到它(https://github.com/italojs/facial-landmarks-recognition-/blob/master/shape_predictor_68_face_landmarks.dat)。
from imutils import face_utils
import dlib
import cv2
# Vamos inicializar um detector de faces (HOG) para ent?o
# let's go code an faces detector(HOG) and after detect the
# landmarks on this detected face
# p=our pre-treined model directory, on my case, it's on the same script's diretory.
p="shape_predictor_68_face_landmarks.dat"
detector=dlib.get_frontal_face_detector()
predictor=dlib.shape_predictor(p)
cap=cv2.VideoCapture(0)
while True:
# Getting out image by webcam
_, image=cap.read()
# Converting the image to gray scale
gray=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Get faces into webcam's image
rects=detector(gray, 0)
# For each detected face, find the landmark.
for (i, rect) in enumerate(rects):
# Make the prediction and transfom it to numpy array
shape=predictor(gray, rect)
shape=face_utils.shape_to_np(shape)
# Draw on our image, all the finded cordinate points (x,y)
for (x, y) in shape:
cv2.circle(image, (x, y), 2, (0, 255, 0), -1)
# Show the image
cv2.imshow("Output", image)
k=cv2.waitKey(5) & 0xFF
if k==27:
break
cv2.destroyAllWindows()
cap.release()
之后,運行腳本。
摘要: 圖像識別的新思路:眼睛縱橫比,看看大牛如果用這種思路玩轉識別眨眼動作!
今天我們來使用面部標志和OpenCV 檢測和計算視頻流中的眨眼次數。
為了構建我們的眨眼檢測器,我們將計算一個稱為眼睛縱橫比(EAR)的指標,由Soukupová和?ech在其2016年的論文“使用面部標志實時眼睛眨眼檢測”中介紹。
今天介紹的這個方法與傳統的計算眨眼圖像處理方法是不同的,使用眼睛的長寬比是更為簡潔的解決方案,它涉及到基于眼睛的面部標志之間的距離比例是一個非常簡單的計算。
用OpenCV,Python和dlib進行眼睛眨眼檢測
我們的眨眼檢測實驗分為四個部分:
第一步,我們將討論眼睛的縱橫比以及如何用它來確定一個人是否在給定的視頻幀中閃爍。
第二步,我們將編寫Python,OpenCV和dlib代碼來執行面部標志檢測和檢測視頻流中的眨眼。
第三步,基于代碼,我們將應用我們的方法來檢測示例攝像頭流中的眨眼以及視頻文件。
最后,我將通過討論改進我們的眨眼檢測器的方法來結束。
1.了解“眼睛縱橫比”(EAR)
我們可以應用面部標志檢測來定位臉部的重要區域,包括眼睛,眉毛,鼻子,耳朵和嘴巴:
這也意味著我們可以通過了解特定臉部的索引來提取特定的臉部結構:
在眨眼檢測方面,我們眼睛結構感興趣。
每只眼睛由6個(x,y)坐標表示,從眼睛的左角開始,然后圍繞該區域的其余部分順時針顯示:
基于這個描述,我們應該抓住重點:這些坐標的寬度和高度之間有一個關系。
Soukupová和?ech在其2016年的論文“使用面部標志實時眼睛眨眼檢測”的工作,我們可以推導出反映這種關系的方程,稱為眼睛縱橫比(EAR):
其中p1,...,p6是2D面部地標位置。
這個方程的分子是計算垂直眼睛標志之間的距離,而分母是計算水平眼睛標志之間的距離,因為只有一組水平點,但是有兩組垂直點,所以進行加權分母。
為什么這個方程如此有趣?
我們將會發現,眼睛的長寬比在眼睛張開的時候大致是恒定的,但是在發生眨眼時會迅速下降到零。
使用這個簡單的方程,我們可以避免使用圖像處理技術,簡單地依靠眼睛地標距離的比例來確定一個人是否眨眼。
為了更清楚地說明,看下面的圖:
在底部圖中繪出了眼縱橫比隨時間的視頻剪輯的曲線圖。正如我們所看到的,眼睛縱橫比是恒定的,然后迅速下降到接近零,然后再增加,表明一個單一的眨眼已經發生。
2.用面部標志和OpenCV檢測眨眼(代碼篇)
請打開一個新文件并將其命名為detect_blinks.py。插入以下代碼:
# import the necessary packagesfrom scipy.spatial import distance as distfrom imutils.video import FileVideoStreamfrom imutils.video import VideoStreamfrom imutils import face_utilsimport numpy as npimport argparseimport imutilsimport timeimport dlibimport cv2
要訪問磁盤上的視頻文件(FileVideoStream)或內置的網絡攝像頭/ USB攝像頭/Raspberry Pi攝像頭模塊(VideoStream),我們需要使用imutils庫,它可以使OpenCV更容易工作。
如果您的系統上沒有安裝 imutils,請確保使用以下命令安裝/升級:
pip install --upgrade imutils
注意:如果您正在使用Python虛擬環境(OpenCV安裝教程),請確保使用 workon命令首先訪問您的虛擬環境,然后安裝/升級 imutils。
例外的是dlib庫,如果您的系統上沒有安裝dlib,請按照我的dlib安裝教程配置您的機器。
接下來,我們將定義eye_aspect_ratio函數:
def eye_aspect_ratio(eye):
# compute the euclidean distances between the two sets of
# vertical eye landmarks (x, y)-coordinates
A=dist.euclidean(eye[1], eye[5])
B=dist.euclidean(eye[2], eye[4])
# compute the euclidean distance between the horizontal
# eye landmark (x, y)-coordinates
C=dist.euclidean(eye[0], eye[3])
# compute the eye aspect ratio
ear=(A + B) / (2.0 * C)
# return the eye aspect ratio
return ear
這個函數接受單一的參數,即給定的眼睛面部標志的(x,y)坐標 。
A,B是計算兩組垂直眼睛標志之間的距離,而C是計算水平眼睛標志之間的距離。
最后,將分子和分母相結合,得出最終的眼睛縱橫比。然后將眼圖長寬比返回給調用函數。
讓我們繼續解析我們的命令行參數:
# construct the argument parse and parse the argumentsap=argparse.ArgumentParser()ap.add_argument("-p", "--shape-predictor", required=True,
help="path to facial landmark predictor")ap.add_argument("-v", "--video", type=str, default="",
help="path to input video file")args=vars(ap.parse_args())
detect_blinks.py腳本需要一個命令行參數,然后第二個是可選的參數:
1.--shape-predictor:這是dlib的預訓練面部標志檢測器的路徑。
2.--video:它控制駐留在磁盤上的輸入視頻文件的路徑。如果您想要使用實時視頻流,則需在執行腳本時省略此開關。
我們現在需要設置兩個重要的常量,您可能需要調整實現,并初始化其他兩個重要的變量。
# 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 thresholdEYE_AR_THRESH=0.3EYE_AR_CONSEC_FRAMES=3# initialize the frame counters and the total number of blinksCOUNTER=0TOTAL=0
當確定視頻流中是否發生眨眼時,我們需要計算眼睛的長寬比。
如果眼睛長寬比低于一定的閾值,然后超過閾值,那么我們將記錄一個“眨眼” -EYE_AR_THRESH是這個閾值,我們默認它的值為 0.3,您也可以為自己的應用程序調整它。另外,我們有一個重要的常量,EYE_AR_CONSEC_FRAME,這個值被設置為 3,表明眼睛長寬比小于3時,接著三個連續的幀一定發生眨眼動作。
同樣,取決于視頻的幀處理吞吐率,您可能需要提高或降低此數字以供您自己實施。
接著初始化兩個計數器,COUNTER是眼圖長寬比小于EYE_AR_THRESH的連續幀的總數,而 TOTAL則是腳本運行時發生的眨眼的總次數。
現在我們的輸入,命令行參數和常量都已經寫好了,接著可以初始化dlib的人臉檢測器和面部標志檢測器:
# initialize dlib's face detector (HOG-based) and then create# the facial landmark predictorprint("[INFO] loading facial landmark predictor...")detector=dlib.get_frontal_face_detector()predictor=dlib.shape_predictor(args["shape_predictor"])
dlib庫使用一個預先訓練的人臉檢測器,該檢測器基于對用于對象檢測的定向梯度直方圖+線性SVM方法的修改。然后,我們初始化的實際面部標志預測值predictor。
您可以在本博客文章中了解更多關于dlib的面部標志性探測器(即它是如何工作的,它在哪些數據集上進行了訓練等)。
由dlib生成的面部標記遵循可索引列表,正如我所描述的那樣:
因此,我們可以確定為下面的左眼和右眼提?。▁,y)坐標的起始和結束數組切片索引值:
# grab the indexes of the facial landmarks for the left and# right eye, respectively(lStart, lEnd)=face_utils.FACIAL_LANDMARKS_IDXS["left_eye"](rStart, rEnd)=face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]
使用這些索引,我們將能夠毫不費力地提取眼部區域。
接下來,我們需要決定是否使用基于文件的視頻流或實時USB/網絡攝像頭/ Raspberry Pi攝像頭視頻流:
# start the video stream threadprint("[INFO] starting video stream thread...")vs=FileVideoStream(args["video"]).start()fileStream=True# vs=VideoStream(src=0).start()# vs=VideoStream(usePiCamera=True).start()# fileStream=Falsetime.sleep(1.0)
如果您使用的是文件視頻流,請保持原樣。
如果您想使用內置攝像頭或USB攝像頭,取消注釋:# vs=VideoStream(src=0).start()。
Raspberry Pi相機模塊,取消注釋:# vs=VideoStream(usePiCamera=True).start()。
如果您未注釋上述兩個,你可以取消注釋# fileStream=False以及以表明你是不是從磁盤讀取視頻文件。
最后,我們已經完成了我們腳本的主要循環:
# loop over frames from the video streamwhile True:
# if this is a file video stream, then we need to check if
# there any more frames left in the buffer to process
if fileStream and not vs.more():
break
# grab the frame from the threaded video file stream, resize
# it, and convert it to grayscale
# channels)
frame=vs.read()
frame=imutils.resize(frame, width=450)
gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# detect faces in the grayscale frame
rects=detector(gray, 0)
在while處我們開始從視頻流循環幀。
如果我們正在訪問視頻文件流,并且視頻中沒有剩余的幀,我們從循環中斷。
從我們的視頻流中讀取下一幀,然后調整大小并將其轉換為灰度。然后,我們通過dlib內置的人臉檢測器檢測灰度幀中的人臉。
我們現在需要遍歷幀中的每個面,然后對其中的每個面應用面部標志檢測:
# loop over the face detections
for rect in rects:
# determine the facial landmarks for the face region, then
# convert the facial landmark (x, y)-coordinates to a NumPy
# array
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]
leftEAR=eye_aspect_ratio(leftEye)
rightEAR=eye_aspect_ratio(rightEye)
# average the eye aspect ratio together for both eyes
ear=(leftEAR + rightEAR) / 2.0
shape確定面部區域的面部標志,接著將這些(x,y)坐標轉換成NumPy陣列。
使用我們之前在這個腳本中的數組切片技術,我們可以分別為左眼left eye和右眼提取(x,y)坐標,然后我們計算每只眼睛的眼睛長寬比 。
下一個代碼塊簡單地處理可視化眼部區域的面部標志:
# compute the convex hull for the left and right eye, then
# visualize each of the eyes
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)
我們已經計算了我們的(平均的)眼睛長寬比,但是我們并沒有真正確定是否發生了眨眼,這在下一部分中將得到關注:
# 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
if COUNTER >=EYE_AR_CONSEC_FRAMES:
TOTAL +=1
# reset the eye frame counter
COUNTER=0
第一步檢查眼睛縱橫比是否低于我們的眨眼閾值,如果是,我們遞增指示正在發生眨眼的連續幀數。否則,我們將處理眼高寬比不低于眨眼閾值的情況,我們對其進行檢查,看看是否有足夠數量的連續幀包含低于我們預先定義的閾值的眨眼率。如果檢查通過,我們增加總的閃爍次數。然后我們重新設置連續閃爍次數 COUNTER。
我們的最終代碼塊只是簡單地處理在輸出幀上繪制閃爍的次數,以及顯示當前的眼圖高寬比:
# draw the total number of blinks on the frame along with# the computed eye aspect ratio for the framecv2.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)# show the framecv2.imshow("Frame", frame)key=cv2.waitKey(1) & 0xFF# if the `q` key was pressed, break from the loopif key==ord("q"):
break# do a bit of cleanupcv2.destroyAllWindows()vs.stop()
3.眨眼檢測結果
在執行之前,請務必使用本指南“下載”源代碼+示例視頻+預訓練的dlib面部標記預測器。
要將我們的眨眼檢測器應用于示例視頻,只需執行以下命令:
$python detect_blinks.py \
--shape-predictor shape_predictor_68_face_landmarks.dat \
--video blink_detection_demo.mp4
后來,在旅館里,我記錄下了眨眼檢測器的實時流,并將其變成了屏幕錄像。
要訪問我的內置攝像頭,我執行了下面的命令(注意取消注釋正確的VideoStream類,如上所述):
$python detect_blinks.py \ --shape-predictor shape_predictor_68_face_landmarks.dat
4.改進我們的眨眼檢測器
我們只關注眼睛縱橫比作為定量指標,以確定一個人是否在視頻流中眨了眨眼睛。
為了使我們的眨眼檢測器更加強大的顯示中可能挑戰,Soukupová和?ech建議:為了改善我們的眨眼檢測器,Soukupová和?ech建議構建眼長寬比(第N幀,第 N-6 幀和第 N + 6幀)的13維特征向量,然后將該特征向量饋送到線性SVM分類。
如果你有什么問題,可以查看原文,在評論區與作者交流,作者是一個熱心腸的人。完整代碼,請查看原文。
本文由阿里云云棲社區組織翻譯。
文章原標題《
eye-blink-detection-opencv-python-dlib》,
作者:Adrian Rosebrock