Subhash Vadaparthi commited on
Commit ·
a68bb2b
1
Parent(s): ef86b0d
Initial commit with model and app
Browse filesFiles changed (5)
- .gitattributes+1
- README.md+66-4
- app.pyAdded+124
- requirements.txtAdded+5
- shape_predictor_68_face_landmarks.datAdded+3
32 unmodified lines33343532 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text32 unmodified lines3334353632 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text*.dat filter=lfs diff=lfs merge=lfs -text
123456789101112---title: Driver Drowsiness Detectionemoji: 🏆colorFrom: bluecolorTo: graysdk: gradiosdk_version: 6.10.0app_file: app.pypinned: false---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374---title: Driver Drowsiness Detectionemoji: 🚗colorFrom: bluecolorTo: redsdk: gradiosdk_version: 4.21.0app_file: app.pypinned: falselicense: apache-2.0---# 🚗 Driver Drowsiness Monitoring SystemAn AI-powered real-time Driver Drowsiness Detection System that monitors a driver’s eye movements using computer vision and alerts them when signs of drowsiness are detected.## 📌 OverviewDriver fatigue is one of the major causes of road accidents worldwide. This project uses Computer Vision and Facial Landmark detection to monitor eye closure and alert the driver.The system continuously monitors the driver's face through a webcam and identifies potential drowsiness using the Eye Aspect Ratio (EAR).## 🎯 Features* **👁 Real-time eye detection:** Utilizes webcam feed for continuous monitoring.* **🧠 Facial Landmark Analysis:** Accurate detection using `dlib`'s 68-point landmark model.* **🔔 Live Analytics:** Displays EAR, blink count, and status on an interactive dashboard.* **⚡ Fast and lightweight:** Optimized for real-time web inference.* **🖥 Easy Cloud/Local setup:** Run it as a Hugging Face Space or locally with Gradio.## 🛠️ Tech Stack* **Python*** **OpenCV*** **Dlib** (Facial Landmarks)* **Gradio** (Web Interface)* **SciPy** (EAR Calculation)## 🏗️ Project Structure```textDriverDrowsinessDetection/│├── shape_predictor_68_face_landmarks.dat # Dlib model file├── app.py # Main Gradio application├── requirements.txt # Dependencies└── README.md```## 🚀 Installation & Setup### 1️⃣ Run as Hugging Face Space* Create a new Space on [Hugging Face](https://huggingface.co/new-space).* Select **Gradio** as the SDK.* Push this repository to the Space.### 2️⃣ Local Setup```bash# Clone the repositorygit clone https://github.com/KL2300030695/DriverDrowsinessDetection.gitcd DriverDrowsinessDetection# Install dependenciespip install -r requirements.txt# Run the apppython app.py```## 👨💻 Author**Subhash Vadaparthi**B.Tech Computer Science EngineeringKoneru Lakshmaiah Education FoundationAI & Cloud Enthusiast
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124import cv2import dlibimport numpy as npimport gradio as grfrom scipy.spatial import distanceimport time
# Load dlib modelstry: detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")except Exception as e: print(f"Error loading model: {e}") detector = None predictor = None
def calculate_EAR(eye): A = distance.euclidean(eye[1], eye[5]) B = distance.euclidean(eye[2], eye[4]) C = distance.euclidean(eye[0], eye[3]) return (A + B) / (2.0 * C)
def process_stream(frame, threshold, state): if frame is None: return frame, state, "No Frame Detected" if detector is None or predictor is None: return frame, state, "Model Not Loaded"
# Ensure state is a dictionary if not isinstance(state, dict): state = {"counter": 0, "blink_count": 0, "last_time": time.time()}
counter = state.get("counter", 0) blink_count = state.get("blink_count", 0) last_time = state.get("last_time", time.time()) # Calculate FPS current_time = time.time() dt = current_time - last_time fps = 1.0 / dt if dt > 0 else 0 state["last_time"] = current_time
# Gradio provided image is already RGB gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) faces = detector(gray) status = "Awake" color = (0, 255, 0) # Green for Awake (RGB)
for face in faces: # Draw face box x, y, w, h = face.left(), face.top(), face.width(), face.height() cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2) # Blue box in RGB
landmarks = predictor(gray, face) leftEye = [(landmarks.part(n).x, landmarks.part(n).y) for n in range(36, 42)] rightEye = [(landmarks.part(n).x, landmarks.part(n).y) for n in range(42, 48)] leftEAR = calculate_EAR(leftEye) rightEAR = calculate_EAR(rightEye) EAR = (leftEAR + rightEAR) / 2.0 # Draw eyes for pt in leftEye + rightEye: cv2.circle(frame, pt, 2, (0, 255, 0), -1)
# Alert logic if EAR < threshold: counter += 1 if counter >= 10: # Reduced for faster response in web app status = "Drowsy" color = (255, 0, 0) # Red status (RGB) cv2.putText(frame, "!! DROWSY !!", (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2) else: if counter >= 3: # Blink detection blink_count += 1 counter = 0
cv2.putText(frame, f"EAR: {EAR:.2f}", (x+w-80, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1)
state["counter"] = counter state["blink_count"] = blink_count
# UI Overlays cv2.putText(frame, f"Status: {status}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2) cv2.putText(frame, f"Blinks: {blink_count}", (10, 70), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2) cv2.putText(frame, f"FPS: {fps:.1f}", (frame.shape[1]-100, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 0), 2)
return frame, state, f"Current Status: {status} | Total Blinks: {blink_count}"
# Define custom CSS for a premium lookcustom_css = """.container { max-width: 1000px; margin: auto; }footer { visibility: hidden; }"""
with gr.Blocks(css=custom_css, title="Driver Drowsiness Detector") as demo: gr.Markdown("<h1 style='text-align: center;'>🚗 Driver Drowsiness Monitoring System</h1>") gr.Markdown("<p style='text-align: center;'>AI-powered real-time detection for safer driving. Use the webcam feed below.</p>") app_state = gr.State({"counter": 0, "blink_count": 0, "last_time": time.time()}) with gr.Row(): with gr.Column(scale=2): # webcam input with streaming enabled input_img = gr.Image(sources=["webcam"], label="Webcam Feed", type="numpy", streaming=True) threshold = gr.Slider(0.15, 0.35, value=0.25, step=0.01, label="Detection Sensitivity (EAR Threshold)") with gr.Column(scale=2): output_img = gr.Image(label="Live Analytics") status_text = gr.Textbox(label="Real-time Metrics")
# Connect the stream for continuous processing input_img.stream( fn=process_stream, inputs=[input_img, threshold, app_state], outputs=[output_img, app_state, status_text], stream_every=0.05 # Process every 50ms for a "live" feel )
if __name__ == "__main__": demo.launch()12345opencv-pythondlibnumpyscipygradio123version https://git-lfs.github.com/spec/v1oid sha256:fbdc2cb80eb9aa7a758672cbfdda32ba6300efe9b6e6c7a299ff7e736b11b92fsize 99693937.gitattributes+1
README.md+66-4
app.pyAdded+124
requirements.txtAdded+5
shape_predictor_68_face_landmarks.datAdded+3