Skip to content

Commit 65d76b1

Browse files
Merge pull request #821 from 2222k3060/patch-11
Create computer Vision Project.py
2 parents 421376f + df81ddd commit 65d76b1

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

computer Vision Project.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from scipy.spatial import distance
2+
from imutils import face_utils
3+
from pygame import mixer
4+
import imutils
5+
import dlib
6+
import cv2
7+
8+
mixer.init()
9+
mixer.music.load("C:\\Users\\Kawin M.S\\Downloads\\alarm.mp3")
10+
11+
def eye_aspect_ratio(eye):
12+
A = distance.euclidean(eye[1], eye[5])
13+
B = distance.euclidean(eye[2], eye[4])
14+
C = distance.euclidean(eye[0], eye[3])
15+
ear = (A + B) / (2.0 * C)
16+
return ear
17+
18+
thresh = 0.25
19+
frame_check = 20
20+
detect = dlib.get_frontal_face_detector()
21+
predict = dlib.shape_predictor("D:\\projects @\\shape_predictor_68_face_landmarks.dat")
22+
23+
(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_68_IDXS["left_eye"]
24+
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_68_IDXS["right_eye"]
25+
cap=cv2.VideoCapture(0)
26+
flag=0
27+
while True:
28+
ret, frame=cap.read()
29+
frame = imutils.resize(frame,width=600)
30+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
31+
subjects = detect(gray, 0)
32+
for subject in subjects:
33+
shape = predict(gray, subject)
34+
shape = face_utils.shape_to_np(shape)
35+
leftEye = shape[lStart:lEnd]
36+
rightEye = shape[rStart:rEnd]
37+
leftEAR = eye_aspect_ratio(leftEye)
38+
rightEAR = eye_aspect_ratio(rightEye)
39+
ear = (leftEAR + rightEAR) / 2.0
40+
leftEyeHull = cv2.convexHull(leftEye)
41+
rightEyeHull = cv2.convexHull(rightEye)
42+
cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1)
43+
cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)
44+
if ear < thresh:
45+
flag += 1
46+
print (flag)
47+
if flag >= frame_check:
48+
49+
cv2.putText(frame, "****************ALERT!****************", (10, 30),
50+
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
51+
cv2.putText(frame, "****************ALERT!****************", (10,325),
52+
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
53+
mixer.music.play()
54+
else:
55+
flag = 0
56+
cv2.imshow("Frame", frame)
57+
key = cv2.waitKey(1) & 0xFF
58+
if key == ord("q"):
59+
break
60+
cv2.destroyAllWindows()
61+
cap.release()

0 commit comments

Comments
 (0)