Drunk Driving

Help me , d lib isnt importing
Anonymous
plain_text
11/28/2024 7:08 PM
3.3 KB
34
Indexable
import cv2
import dlib
import numpy
import imutils
from imutils import face_utils
from scipy.spatial import distance as dist

def EAR(eye):
    p2p6distance = dist.euclidean(eye[1], eye[5])
    p3p5distance = dist.euclidean(eye[2], eye[4])
    p1p4distance = dist.euclidean(eye[0], eye[3])
    ear = (p2p6distance + p3p5distance) / (2.0 * p1p4distance)
    return ear

Face_LandMarks = "shape_predictor_68_face_landmarks.dat"  
MINIMUM_EAR = 0.2
MAXIMUM_FRAME_COUNT = 10
#EAR---> Eye Aspect Ratio, basically the limiting value/degree of how your eyes close or blah blah blah
# I Set a threshold value of 0.2 so when ur EAR reaches below 0.2 the alram blares
# Maximum frame count is a precaution since blinking != drowsiness, so if ur eyes remain closed for 10 video frames, the alarm blares 
faceDetector = dlib.get_frontal_face_detector()
landmarkFinder = dlib.shape_predictor(Face_LandMarks)
webcamFeed = cv2.VideoCapture(0)

(LEYEStart, LEYEStop) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
(REYEStart, REYEEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]

EYE_CLOSED_COUNTER = 0
try:
    while True:
        (status, image) = webcamFeed.read()
        image = imutils.resize(image, width=800)
        grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        faces = faceDetector(grayImage, 0)

        for face in faces:
            faceLandmarks = landmarkFinder(grayImage, face)
            faceLandmarks = face_utils.shape_to_np(faceLandmarks)

            leftEye = faceLandmarks[LEYEStart:LEYEStop]
            rightEye = faceLandmarks[REYEStart:REYEEnd]

            leftEAR = EAR(leftEye)
            rightEAR = EAR(rightEye)

            ear = (leftEAR + rightEAR) / 2.0

            leftEyeHull = cv2.convexHull(leftEye)
            rightEyeHull = cv2.convexHull(rightEye)

            cv2.drawContours(image, [leftEyeHull], -1, (255, 0, 0), 2)
            cv2.drawContours(image, [rightEyeHull], -1, (255, 0, 0), 2)

            if ear < MINIMUM_EAR:
                EYE_CLOSED_COUNTER += 1
            else:
                EYE_CLOSED_COUNTER = 0

            cv2.putText(image, "EAR: {}".format(round(ear, 1)), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)

            if EYE_CLOSED_COUNTER >= MAXIMUM_FRAME_COUNT:
                cv2.putText(image, "Drowsiness", (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)

        cv2.imshow("Frame", image)
        cv2.waitKey(1)
except:
    pass
Editor is loading...
Leave a Comment