python學習(day9)科研項目——人臉識別檢測疲勞程度(一)


人臉識別檢測運用模塊:opencv-python、dlib

opencv-python調用攝像頭,儲存圖像。dlib調用已訓練好的人臉特征圖,與圖像結合。

以下代碼為初始學習代碼,我的科研目的是,在此基礎上加入其他特征,提高疲勞精確度。

            臉譜64特征點

#開始導入需要的模塊
import cv2#調用攝像頭
import dlib#調用識別檢測庫
from math import hypot
import time
import winsound#調用設備(筆記本)音響,提示疲勞

cap = cv2.VideoCapture(0)#打開筆記本的內置攝像頭,參數改為視頻位置則為打開視頻文件
detector = dlib.get_frontal_face_detector()#獲取人臉分類器
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")#獲取人臉檢測器,提取特征點數
font = cv2.FONT_HERSHEY_PLAIN#設置寫入文字的字體(在屏幕上的字體)

#用於求上眼皮與下眼皮的重點
def midpoint(p1 ,p2):
    return int((p1.x + p2.x)/2), int((p1.y + p2.y)/2)

#用於計算眼睛長寬比,獲取比值
def get_blinking_ratio(eye_points, facial_landmarks):
    left_point = (facial_landmarks.part(eye_points[0]).x, facial_landmarks.part(eye_points[0]).y)
    right_point = (facial_landmarks.part(eye_points[3]).x, facial_landmarks.part(eye_points[3]).y)
    #利用臉譜特征圖上的點,獲得人臉上眼睛兩邊的坐標

    center_top = midpoint(facial_landmarks.part(eye_points[1]), facial_landmarks.part(eye_points[2]))
    center_bottom = midpoint(facial_landmarks.part(eye_points[5]), facial_landmarks.part(eye_points[4]))
    #利用臉譜特征圖上的點,獲得人臉上眼睛上下眼皮的坐標,同時計算中間點的坐標

    hor_line = cv2.line(frame, left_point, right_point, (0,255,0), 3)
    ver_line = cv2.line(frame, center_top, center_bottom, (0,255,255), 3)
    #將眼睛左右與上下連成線,方便觀測

    hor_line_lenght = hypot((left_point[0] - right_point[0]), (left_point[1] - right_point[1]))
    ver_line_lenght = hypot((center_top[0] - center_bottom[0]), (center_top[1] - center_bottom[1]))
    #利用hypot函數計算得出線段的長度

    ratio = hor_line_lenght / ver_line_lenght
    #得到長寬比
    return ratio

#主程序,一直檢測眼睛睜眨,長寬比與一個定值(臨界點)比較,判斷是否疲勞且發出提示音
while True:
    _, frame = cap.read()#這個read是cv2中的方法,作用:按幀讀取畫面,返回兩個值(True,frame)有畫面是True,且賦值給frame
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)#方法,作用:將攝像頭捕獲的視頻轉換為灰色並且保存,這樣方便判斷面部特征點
    faces = detector(gray)#利用dlib庫,處理獲取的人臉畫面

    #循環每一個畫面
    for face in faces:
        landmarks = predictor(gray, face)
        left_eye_ratio = get_blinking_ratio([36, 37, 38, 39, 40, 41], landmarks)
        right_eye_ratio = get_blinking_ratio([42, 43, 44, 45, 46, 47], landmarks)
        #利用函數獲得左右眼的比值

        blinking_ratio = (left_eye_ratio + right_eye_ratio) / 2
        #取平均數

        end = time.time()#記時,判斷閉眼時間

        #檢測眼睛狀況
        if blinking_ratio > 4.5:
            cv2.putText(frame, "CLOSE", (75, 250), font, 7, (255, 0, 255)) #方法,作用:在圖像上打印文字,設置字體,顏色,大小
        else :
            cv2.putText(frame, "OPEN", (75, 250), font, 7, (0, 255, 0))
            start = time.time()#記時
        print("閉眼時間:%.2f秒"%(end-start))#獲取睜閉眼時間差

        #判斷是否疲勞
        if (end-start) > 2 :
            cv2.putText(frame, "TIRED", (200, 325), font, 7, (0, 0, 255))
            duration = 1000
            freq = 1000
            winsound.Beep(freq, duration)#調用喇叭,設置聲音大小,與時間長短

    cv2.imshow("Frame", frame)#方法,作用,創建一個窗口,將畫面投影到窗口中

    #推出鍵設置,按Ese鍵退出
    key = cv2.waitKey(1)
    if key == 27:
        break

#釋放窗口,關閉攝像頭
cap.release()
cv2.destroyAllWindows()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM