高科技眨眼偵測技術,打開人機互動新視界!

b.加載人臉檢測器:使用 dlib 提供的人臉檢測器來識別圖片中的人臉。

import dlib

# 加載前置人臉檢測器
detector = dlib.get_frontal_face_detector()

c.加載特徵點預測器:為了獲取臉部特徵點,需要加載一個預訓練的特徵點預測模型,模型可以在官網下載。dlib 有一個基於 68 個特徵點的預訓練模型。

# 加載特徵點預測器
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

d.識別特徵點:使用人臉檢測器找到圖片中的人臉,然後用特徵點預測器來識別臉部的特徵點。

import cv2

# 讀取圖片
image = cv2.imread("your_image.jpg")

# 檢測人臉
faces = detector(image, 1)

for face in faces:
    # 獲取臉部特徵點
    landmarks = predictor(image, face)
    # 這裡可以根據 landmarks 來獲取眼睛周圍的特徵點座標

二.算法介紹

  • 眼睛長寬比(EAR)的計算方法
  • EAR是基於眼睛的幾何特性,透過特定的幾點計算得到一個數值,以此來判斷眼睛是否閉合。EAR計算公式如下:
  • $EAR=\frac{{∣∣p2​−p6​∣∣+∣∣p3​−p5​∣∣}​}{2×∣∣p1​−p4​∣∣}$
  • 這裡,$p1​$到$p6​$代表眼睛周圍的六個特徵點。$p2​$到$p6​$是眼瞼的點,而$p1​$和$p4​$是眼角的點。公式中的||p2 – p6||和||p3 – p5||計算的是眼瞼之間的距離,而2 × ||p1 – p4||則是計算眼睛開口的寬度的兩倍。

    三.計算EAR sample

  • 睜眼:Left EAR: 0.37, Right EAR: 0.36, Average EAR: 0.36
  • 閉眼:Left EAR: 0.15, Right EAR: 0.15, Average EAR: 0.15
  • 四.完整範例程式碼

import cv2
import dlib
import numpy as np

def eye_aspect_ratio(eye_points):
    # 根據眼睛的6個特徵點計算EAR
    A = np.linalg.norm(eye_points[1] - eye_points[5])
    B = np.linalg.norm(eye_points[2] - eye_points[4])
    C = np.linalg.norm(eye_points[0] - eye_points[3])
    ear = (A + B) / (2.0 * C)
    return ear

# 加載人臉檢測和特徵點檢測器
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

# 讀取圖像
image_path = "your_image_path.jpg"  # 替換成您的圖片路徑
img = cv2.imread(image_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 檢測圖像中的人臉
faces = detector(gray)
for face in faces:
    # 獲取臉部特徵點
    landmarks = predictor(gray, face)
    
    # 獲取左眼和右眼的特徵點
    left_eye_points = np.array([(landmarks.part(n).x, landmarks.part(n).y) for n in range(36, 42)])
    right_eye_points = np.array([(landmarks.part(n).x, landmarks.part(n).y) for n in range(42, 48)])
    
    # 計算EAR
    left_ear = eye_aspect_ratio(left_eye_points)
    right_ear = eye_aspect_ratio(right_eye_points)
    ear = (left_ear + right_ear) / 2.0
    
    # 在圖像上標記左眼和右眼的特徵點
    for point in left_eye_points:
        cv2.circle(img, tuple(point), 1, (0, 255, 0), -1)
    for point in right_eye_points:
        cv2.circle(img, tuple(point), 1, (0, 255, 0), -1)
    
    print(f"Left EAR: {left_ear:.2f}, Right EAR: {right_ear:.2f}, Average EAR: {ear:.2f}")

# 顯示結果圖像
cv2.imshow("Eye Aspect Ratio and Landmarks", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

五.參考資料

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments