Python Eye Blinker

Python Eye Blinker

Şbt 22, 2024 - 17:21
Haz 20, 2024 - 00:14
 0  35
Python Eye Blinker
Python Eye blinker
import cv2
import dlib
from imutils import face_utils
from scipy.spatial import distance

def eye_aspect_ratio(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

EAR_THRESHOLD = 0.3
EAR_CONSEC_FRAMES = 3

# Load the facial landmark detector from dlib
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

# Define the indexes for the left and right eye landmarks
(lstart, lend) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
(rstart, rend) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]

# Start the video stream
vs = cv2.VideoCapture(0)
blink_count = 0
blink_flag = False

while True:
    ret, frame = vs.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Detect faces in the frame
    faces = detector(gray, 0)

    for face in faces:
        shape = predictor(gray, face)
        shape = face_utils.shape_to_np(shape)

        left_eye = shape[lstart:lend]
        right_eye = shape[rstart:rend]

        left_ear = eye_aspect_ratio(left_eye)
        right_ear = eye_aspect_ratio(right_eye)

        average_ear = (left_ear + right_ear) / 2.0

        left_eye_hull = cv2.convexHull(left_eye)
        right_eye_hull = cv2.convexHull(right_eye)
        cv2.drawContours(frame, [left_eye_hull], -1, (0, 255, 0), 1)
        cv2.drawContours(frame, [right_eye_hull], -1, (0, 255, 0), 1)

        if average_ear < EAR_THRESHOLD:
            if not blink_flag:
                blink_count += 1
                blink_flag = True
        else:
            blink_flag = False

    cv2.putText(frame, "Blinks: {}".format(blink_count), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)

    cv2.imshow("Frame", frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

vs.release()
cv2.destroyAllWindows()

Bu Yazıya Tepkin Nedir?

like

dislike

love

funny

angry

sad

wow