資源簡介
基于python dlib和sklearn的眨眼檢測完整工程,詳細請參考我的博客:http://blog.csdn.net/hongbin_xu/article/details/79033116。
代碼片段和文件信息
#coding=utf-8??
import?numpy?as?np?
import?cv2
import?dlib
from?scipy.spatial?import?distance
import?os
from?imutils?import?face_utils
#?計算EAR
def?eye_aspect_ratio(eye):
#?print(eye)
A?=?distance.euclidean(eye[1]?eye[5])
B?=?distance.euclidean(eye[2]?eye[4])
C?=?distance.euclidean(eye[0]?eye[3])
ear?=?(A?+?B)?/?(2.0?*?C)
return?ear
pwd?=?os.getcwd()#?獲取當前路徑
model_path?=?os.path.join(pwd?‘model‘)#?模型文件夾路徑
shape_detector_path?=?os.path.join(model_path?‘shape_predictor_68_face_landmarks.dat‘)#?人臉特征點檢測模型路徑
detector?=?dlib.get_frontal_face_detector()#?人臉檢測器
predictor?=?dlib.shape_predictor(shape_detector_path)#?人臉特征點檢測器
EYE_AR_THRESH?=?0.3#?EAR閾值
EYE_AR_CONSEC_frameS?=?3#?當EAR小于閾值時,接連多少幀一定發生眨眼動作
#?對應特征點的序號
RIGHT_EYE_START?=?37?-?1
RIGHT_EYE_END?=?42?-?1
LEFT_EYE_START?=?43?-?1
LEFT_EYE_END?=?48?-?1
frame_counter?=?0
blink_counter?=?0
cap?=?cv2.VideoCapture(1)
while(1):
ret?img?=?cap.read()
gray?=?cv2.cvtColor(img?cv2.COLOR_BGR2GRAY)
rects?=?detector(gray?0)
for?rect?in?rects:
print(‘-‘*20)
shape?=?predictor(gray?rect)
points?=?face_utils.shape_to_np(shape)#?convert?the?facial?landmark?(x?y)-coordinates?to?a?NumPy?array
#?points?=?shape.parts()
leftEye?=?points[LEFT_EYE_START:LEFT_EYE_END?+?1]
rightEye?=?points[RIGHT_EYE_START:RIGHT_EYE_END?+?1]
leftEAR?=?eye_aspect_ratio(leftEye)
rightEAR?=?eye_aspect_ratio(rightEye)
print(‘leftEAR?=?{0}‘.format(leftEAR))
print(‘rightEAR?=?{0}‘.format(rightEAR))
ear?=?(leftEAR?+?rightEAR)?/?2.0
leftEyeHull?=?cv2.convexHull(leftEye)
rightEyeHull?=?cv2.convexHull(rightEye)
cv2.drawContours(img?[leftEyeHull]?-1?(0?255?0)?1)
cv2.drawContours(img?[rightEyeHull]?-1?(0?255?0)?1)
#?for?pt?in?leftEye:
#? pt_pos?=?(pt[0]?pt[1])
#? cv2.circle(img?pt_pos?2?(255?0?0)?1)
#?for?pt?in?rightEye:
#? pt_pos?=?(pt[0]?pt[1])
#? cv2.circle(img?pt_pos?2?(0?255?0)?1)
if?ear? frame_counter?+=?1
else:
if?frame_counter?>=?EYE_AR_CONSEC_frameS:
blink_counter?+=?1
frame_counter?=?0
cv2.putText(img?“blinks:{0}“.format(blink_counter)?(10?30)?cv2.FONT_HERSHEY_SIMPLEX?0.7?(00255)?2)
cv2.putText(img?“EAR:{:.2f}“.format(ear)?(300?30)?cv2.FONT_HERSHEY_SIMPLEX?0.7?(00255)?2)
cv2.imshow(“frame“?img)
if?cv2.waitKey(1)?&?0xFF?==?ord(“q“):
break
cap.release()
cv2.destroyAllWindows()
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-01-11?12:07??bl
?????目錄???????????0??2018-01-11?12:07??bl
?????文件????????1077??2018-01-11?12:05??bl
?????文件??????????96??2018-01-11?12:05??bl
?????文件????????1784??2018-01-11?12:05??bl
?????文件????????3488??2018-01-11?12:05??bl
?????文件??????????80??2018-01-11?12:05??bl
?????文件??????????96??2018-01-11?12:05??bl
?????文件??????????88??2018-01-11?12:05??bl
?????文件????????3488??2018-01-11?12:05??bl
?????文件????????3488??2018-01-11?12:05??bl
?????文件??????????88??2018-01-11?12:05??bl
?????文件??????????88??2018-01-11?12:05??bl
?????文件??????????80??2018-01-11?12:05??bl
?????文件?????????890??2018-01-10?23:24??bl
?????文件?????????515??2018-01-10?23:23??bl
?????文件????????5705??2018-01-11?00:28??bl
?????文件????????5705??2018-01-11?00:28??bl
?????文件????????5690??2018-01-11?00:29??bl
?????文件????????5690??2018-01-11?00:29??bl
?????目錄???????????0??2018-01-11?11:34??bl
?????文件????????1077??2018-01-11?11:23??bl
?????文件??????????96??2018-01-11?11:23??bl
?????文件????????1656??2018-01-11?11:23??bl
?????文件????????3232??2018-01-11?11:23??bl
?????文件??????????80??2018-01-11?11:23??bl
?????文件??????????96??2018-01-11?11:23??bl
?????文件??????????88??2018-01-11?11:23??bl
?????文件???????41056??2018-01-11?11:23??bl
?????文件????????3232??2018-01-11?11:23??bl
?????文件??????????88??2018-01-11?11:23??bl
............此處省略63個文件信息
評論
共有 條評論