Upload 4 files
Browse files- .gitattributes +1 -0
- finalbgtt.py +120 -0
- haarcascade_frontalface_default.xml +0 -0
- requirements.txt.txt +6 -0
- shape_predictor_68_face_landmarks.dat +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
shape_predictor_68_face_landmarks.dat filter=lfs diff=lfs merge=lfs -text
|
finalbgtt.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import dlib
|
| 5 |
+
from scipy.spatial import distance as dist
|
| 6 |
+
from imutils import face_utils
|
| 7 |
+
|
| 8 |
+
# Constants
|
| 9 |
+
EYE_AR_THRESH = 0.3
|
| 10 |
+
EYE_AR_CONSEC_FRAMES = 30
|
| 11 |
+
YAWN_THRESH = 20
|
| 12 |
+
|
| 13 |
+
# Global variables
|
| 14 |
+
COUNTER = 0
|
| 15 |
+
|
| 16 |
+
# Functions
|
| 17 |
+
def eye_aspect_ratio(eye):
|
| 18 |
+
A = dist.euclidean(eye[1], eye[5])
|
| 19 |
+
B = dist.euclidean(eye[2], eye[4])
|
| 20 |
+
C = dist.euclidean(eye[0], eye[3])
|
| 21 |
+
ear = (A + B) / (2.0 * C)
|
| 22 |
+
return ear
|
| 23 |
+
|
| 24 |
+
def final_ear(shape):
|
| 25 |
+
(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
|
| 26 |
+
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]
|
| 27 |
+
leftEye = shape[lStart:lEnd]
|
| 28 |
+
rightEye = shape[rStart:rEnd]
|
| 29 |
+
leftEAR = eye_aspect_ratio(leftEye)
|
| 30 |
+
rightEAR = eye_aspect_ratio(rightEye)
|
| 31 |
+
ear = (leftEAR + rightEAR) / 2.0
|
| 32 |
+
return (ear, leftEye, rightEye)
|
| 33 |
+
|
| 34 |
+
def lip_distance(shape):
|
| 35 |
+
top_lip = shape[50:53]
|
| 36 |
+
top_lip = np.concatenate((top_lip, shape[61:64]))
|
| 37 |
+
low_lip = shape[56:59]
|
| 38 |
+
low_lip = np.concatenate((low_lip, shape[65:68]))
|
| 39 |
+
top_mean = np.mean(top_lip, axis=0)
|
| 40 |
+
low_mean = np.mean(low_lip, axis=0)
|
| 41 |
+
distance = abs(top_mean[1] - low_mean[1])
|
| 42 |
+
return distance
|
| 43 |
+
|
| 44 |
+
# Load detector and predictor
|
| 45 |
+
detector = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')
|
| 46 |
+
predictor = dlib.shape_predictor('./shape_predictor_68_face_landmarks.dat')
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# Streamlit UI
|
| 50 |
+
st.title("Sleep Detection using OpenCV")
|
| 51 |
+
st.markdown("**Check the box below to start the camera:**")
|
| 52 |
+
|
| 53 |
+
FRAME_WINDOW = st.image([])
|
| 54 |
+
run = st.checkbox("Run Camera", key="run_camera")
|
| 55 |
+
|
| 56 |
+
# Video capture
|
| 57 |
+
if "cap" not in st.session_state:
|
| 58 |
+
st.session_state.cap = None
|
| 59 |
+
|
| 60 |
+
if run:
|
| 61 |
+
if st.session_state.cap is None:
|
| 62 |
+
st.session_state.cap = cv2.VideoCapture(0)
|
| 63 |
+
st.success("Camera Started!")
|
| 64 |
+
|
| 65 |
+
while run:
|
| 66 |
+
ret, frame = st.session_state.cap.read()
|
| 67 |
+
if not ret:
|
| 68 |
+
st.error("Failed to open webcam.")
|
| 69 |
+
break
|
| 70 |
+
|
| 71 |
+
frame = cv2.resize(frame, (450, 300))
|
| 72 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 73 |
+
|
| 74 |
+
rects = detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)
|
| 75 |
+
|
| 76 |
+
for (x, y, w, h) in rects:
|
| 77 |
+
rect = dlib.rectangle(int(x), int(y), int(x + w), int(y + h))
|
| 78 |
+
shape = predictor(gray, rect)
|
| 79 |
+
shape = face_utils.shape_to_np(shape)
|
| 80 |
+
|
| 81 |
+
ear, leftEye, rightEye = final_ear(shape)
|
| 82 |
+
distance = lip_distance(shape)
|
| 83 |
+
|
| 84 |
+
leftEyeHull = cv2.convexHull(leftEye)
|
| 85 |
+
rightEyeHull = cv2.convexHull(rightEye)
|
| 86 |
+
cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1)
|
| 87 |
+
cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)
|
| 88 |
+
|
| 89 |
+
lip = shape[48:60]
|
| 90 |
+
cv2.drawContours(frame, [lip], -1, (0, 255, 0), 1)
|
| 91 |
+
|
| 92 |
+
if ear < EYE_AR_THRESH:
|
| 93 |
+
COUNTER += 1
|
| 94 |
+
if COUNTER >= EYE_AR_CONSEC_FRAMES:
|
| 95 |
+
cv2.putText(frame, "DROWSINESS", (10, 30),
|
| 96 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
|
| 97 |
+
else:
|
| 98 |
+
COUNTER = 0
|
| 99 |
+
|
| 100 |
+
if distance > YAWN_THRESH:
|
| 101 |
+
cv2.putText(frame, "YAWN", (10, 60),
|
| 102 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
|
| 103 |
+
|
| 104 |
+
cv2.putText(frame, f"EAR: {ear:.2f}", (300, 30),
|
| 105 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
|
| 106 |
+
cv2.putText(frame, f"YAWN: {distance:.2f}", (300, 60),
|
| 107 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
|
| 108 |
+
|
| 109 |
+
FRAME_WINDOW.image(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 110 |
+
|
| 111 |
+
st.session_state.cap.release()
|
| 112 |
+
st.session_state.cap = None
|
| 113 |
+
FRAME_WINDOW.image([])
|
| 114 |
+
st.checkbox("Run Camera", value=False, key="run_camera") # Uncheck the checkbox automatically
|
| 115 |
+
else:
|
| 116 |
+
if st.session_state.cap is not None:
|
| 117 |
+
st.session_state.cap.release()
|
| 118 |
+
st.session_state.cap = None
|
| 119 |
+
FRAME_WINDOW.image([])
|
| 120 |
+
st.info("Check 'Run Camera' to start detection.")
|
haarcascade_frontalface_default.xml
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
opencv-python-headless
|
| 3 |
+
numpy
|
| 4 |
+
dlib
|
| 5 |
+
imutils
|
| 6 |
+
scipy
|
shape_predictor_68_face_landmarks.dat
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fbdc2cb80eb9aa7a758672cbfdda32ba6300efe9b6e6c7a299ff7e736b11b92f
|
| 3 |
+
size 99693937
|