0

我创建了我自己的面部标志模型,只取名为“eye_predictor3.dat”的眼睛部分。接下来,我要计算EAR(Eye Aspect Ratio)公式来检测眨眼。但我很困惑在眼睛周围画一条线并制作公式 EAR。这是我的程序 在此处输入图像描述

from imutils.video import VideoStream
from imutils.video import FPS
from imutils import face_utils
from scipy.spatial import distance as dist
import numpy as np
import pickle
import cv2
import time
import imutils
import face_recognition
import dlib
   
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

EYE_AR_THRESH = 0.3
EYE_AR_CONSEC_FRAMES = 3
COUNTER = 0
TOTAL = 0

print("[INFO] loading face detector...")
detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
recognizer = pickle.loads(open('recognizer.pickle', "rb").read())
le = pickle.loads(open('le.pickle', "rb").read())
print("[INFO] loading facial landmark predictor...")
predictor = dlib.shape_predictor('eye_predictor3.dat')
detector2 = dlib.get_frontal_face_detector()
print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()
time.sleep(2.0)
knownEncodings = []
fps2 = 0
fps_start = 0
while True:
    fps_end = time.time()
    fps_current = fps_end - fps_start
    fps_final = 1/ (fps_current)
    fps_start = fps_end
    frame = vs.read()
    frame = imutils.resize(frame, width=640)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    rects = detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30),flags=cv2.CASCADE_SCALE_IMAGE)
    rect = detector2(gray,1)
    for (x, y, w, h) in rects:
        encodings = face_recognition.face_encodings(rgb)
        boxes = gray[y:y+h,x:x+w]
        for boxes in rect:
            shape = predictor(gray,boxes)
            shape = face_utils.shape_to_np(shape)
            for (sX, sY) in shape:
                cv2.circle(frame, (sX, sY), 1, (0, 0, 255), -1)
                for encoding in encodings:
                    knownEncodings.append(encoding)
                    preds = recognizer.predict_proba(encodings)[0]
                    j = np.argmax(preds)
                    proba = preds[j]
                    name = le.classes_[j]
                    text = "{}: {:.2f}%".format(name, proba*100)
                    fps_text = "FPS: {:.2f}".format(fps_final)
                    Y = y - 10 if y - 10 > 10 else y + 10
                    cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
                    cv2.putText(frame, text, (x, Y), cv2.FONT_HERSHEY_SIMPLEX, 0.45,(0, 0, 255), 2)
                    cv2.putText(frame, fps_text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.45,(0, 0, 255), 2)
                
    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break

cv2.destroyAllWindows()
vs.stop()

我应该怎么办?谢谢大家

4

0 回答 0