資源簡介
基于OpenCV和Python的Dlib庫駕駛疲勞監測代碼。包括面部標定、眨眼檢測、疲勞監測,包含shape_predictor_68_face_landmarks.dat文件,代碼可以運行。
代碼片段和文件信息
“““
瞌睡檢測
“““
import?numpy?as?np
import?cv2?as?cv
import?dlib
from?scipy.spatial?import?distance
#?調用人臉檢測器
detector?=?dlib.get_frontal_face_detector()
predictor?=?dlib.shape_predictor(“shape_predictor_68_face_landmarks.dat“)
#?設定人眼標定點
LeftEye_Start?=?36
LeftEye_End?=?41
RightEye_Start?=?42
RightEye_End?=?47
Radio?=?0.25??#?橫縱比閾值
Low_radio_constant?=?30??#?意味著連續多少幀橫縱比小于Radio小于閾值時,判斷疲勞
def?calculate_Ratio(eye):
????“““
????計算眼睛橫縱比
????“““
????d1?=?distance.euclidean(eye[1]?eye[5])
????d2?=?distance.euclidean(eye[2]?eye[4])
????d3?=?distance.euclidean(eye[0]?eye[3])
????ratio?=?(d1?+?d2)?/?(2?*?d3)
????return?ratio
def?main():
????“““
????主函數
????“““
????alarm?=?False??#?初始化警報
????frame_counter?=?0??#?連續幀計數
????cap?=?cv.VideoCapture(0)??#?0攝像頭攝像
????while?cap.isOpened():
????????ret?frame?=?cap.read()??#?讀取每一幀
????????frame?=?cv.flip(frame?1)
????????if?ret:
????????????gray?=?cv.cvtColor(frame?cv.COLOR_BGR2GRAY)
????????????rects?=?detector(gray?0)??#?人臉檢測
????????????for?rect?in?rects:
????????????????shape?=?predictor(gray?rect)
????????????????points?=?np.zeros((68?2)?dtype=int)
????????????????for?i?in?range(68):
????????????????????points[i]?=?(shape.part(i).x?shape.part(i).y)
????????????????#?獲取眼睛特征點
????????????????Lefteye?=?points[LeftEye_Start:?LeftEye_End?+?1]
????????????????Righteye?=?points[RightEye_Start:?RightEye_End?+?1]
????????????????#?計算眼睛橫縱比
????????????????Lefteye_Ratio?=?calculate_Ratio(Lefteye)
????????????????Righteye_Ratio?=?calculate_Ratio(Righteye)
????????????????mean_Ratio?=?(Lefteye_Ratio?+?Righteye_Ratio)?/?2??#?計算兩眼平均比例
????????????????#?計算凸包
????????????????left_eye_hull?=?cv.convexHull(Lefteye)
????????????????right_eye_hull?=?cv.convexHull(Righteye)
????????????????#?繪制輪廓
????????????????cv.drawContours(frame?[left_eye_hull]?-1?[0?255?0]?1)
????????????????cv.drawContours(frame?[right_eye_hull]?-1?[0?255?0]?1)
????????????????#?眨眼判斷
????????????????if?mean_Ratio?????????????????????frame_counter?+=?1
????????????????????if?frame_counter?>=?Low_radio_constant:
????????????????????????#?發出警報
????????????????????????if?not?alarm:
????????????????????????????alarm?=?True
????????????????????????cv.putText(frame?“DROWSINESS?ALERT!“?(10?30)
???????????????????????????????????cv.FONT_HERSHEY_SIMPLEX?0.7?(0?0?255)?2)
????????????????else:
????????????????????alarm?=?False
????????????????????frame_counter?=?0
????????????????#?顯示結果
????????????????cv.putText(frame?“Ratio{:.2f}“.format(mean_Ratio)?(300?30)
???????????????????????????cv.FONT_HERSHEY_SIMPLEX?0.7?[0?0?255]?2)
????????????cv.imshow(“瞌睡檢測“?frame)
????????????if?cv.waitKey(1)?&?0xFF?==?ord(‘q‘):
????????????????break
????cap.release()
????cv.destroyAllWindows()
if?__name__?==?‘__main__‘:
????main()
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????1102??2018-12-11?15:39??facial_landmarks3.py
?????文件????????2091??2018-12-06?16:35??facial_landmarks4.py
?????文件????99693937??2018-12-04?14:34??shape_predictor_68_face_landmarks.dat
?????文件????????3157??2018-12-10?16:50??Drowsiness_detection.py
?????文件????????3018??2018-12-11?15:39??eye_bl
評論
共有 條評論