0% found this document useful (0 votes)
7 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)
7 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
You are on page 1/ 2

import cv2

import imutils
from scipy.spatial import distance
from pygame import mixer

mixer.init()
mixer.music.load("music.wav")

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

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 = cv2.CascadeClassifier(cv2.data.haarcascades +
'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')

cap = cv2.VideoCapture(0)
flag = 0

while True:
ret, frame = cap.read()
frame = imutils.resize(frame, width=450)
gray = cv2.cvtColor(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:


cv2.rectangle(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:
leftEye.append((x + ex, y + ey))
else:
rightEye.append((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):


cv2.line(frame, leftEye[i - 1], leftEye[i], (0, 255, 0), 1)
cv2.line(frame, rightEye[i - 1], rightEye[i], (0, 255, 0), 1)

if ear < thresh:


flag += 1
if flag >= frame_check:
cv2.putText(frame, "****************ALERT!****************",
(10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
cv2.putText(frame, "****************ALERT!****************",
(10, 325),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
mixer.music.play()
else:
flag = 0
else:
flag = 0

cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break

cv2.destroyAllWindows()
cap.release()

You might also like