0% found this document useful (0 votes)
13 views2 pages

Ai Proj

Uploaded by

Tayyaba zia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Ai Proj

Uploaded by

Tayyaba zia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import cv2

import imutils
from [Link] import distance
from pygame import mixer

[Link]()
[Link]("[Link]")

def eye_aspect_ratio(eye):
A = [Link](eye[1], eye[5])
B = [Link](eye[2], eye[4])
C = [Link](eye[0], eye[3])
ear = (A + B) / (2.0 * C)
return ear

thresh = 0.25
frame_check = 20

(lStart, lEnd) = (42, 48) # Indices for left eye landmarks


(rStart, rEnd) = (36, 42) # Indices for right eye landmarks

# Load the Haarcascades for face and eye detection


face_cascade = [Link]([Link] +
'haarcascade_frontalface_default.xml')
eye_cascade = [Link]([Link] + 'haarcascade_eye.xml')

cap = [Link](0)
flag = 0

while True:
ret, frame = [Link]()
frame = [Link](frame, width=450)
gray = [Link](frame, cv2.COLOR_BGR2GRAY)

# Detect faces using Haarcascade


faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5,
minSize=(30, 30))

for (x, y, w, h) in faces:


[Link](frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]

# Detect eyes within the face region


eyes = eye_cascade.detectMultiScale(roi_gray)

leftEye = []
rightEye = []

for (ex, ey, ew, eh) in eyes:


if ey < h / 2:
if ex < w / 2:
[Link]((x + ex, y + ey))
else:
[Link]((x + ex, y + ey))

if len(leftEye) == 6 and len(rightEye) == 6:


leftEAR = eye_aspect_ratio(leftEye)
rightEAR = eye_aspect_ratio(rightEye)
ear = (leftEAR + rightEAR) / 2.0

leftEye = leftEye[:6]
rightEye = rightEye[:6]

for i in range(1, 6):


[Link](frame, leftEye[i - 1], leftEye[i], (0, 255, 0), 1)
[Link](frame, rightEye[i - 1], rightEye[i], (0, 255, 0), 1)

if ear < thresh:


flag += 1
if flag >= frame_check:
[Link](frame, "****************ALERT!****************",
(10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
[Link](frame, "****************ALERT!****************",
(10, 325),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
[Link]()
else:
flag = 0
else:
flag = 0

[Link]("Frame", frame)
key = [Link](1) & 0xFF
if key == ord("q"):
break

[Link]()
[Link]()

You might also like