Spaces:
Build error
Build error
Upload 6 files
Browse filesFiles changed (7)
- .gitattributes+1
- app.pyAdded+310
- drowsiness_detection.pyAdded+248
- haarcascade_frontalface_default.xmlAdded
- requirements.txt+9-3
- shape_predictor_68_face_landmarks.datAdded+3
- video_processor.pyAdded+142
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 -textshape_predictor_68_face_landmarks.dat filter=lfs diff=lfs merge=lfs -text
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310import streamlit as st
from PIL import Image
import numpy as np
import subprocess
import time
import tempfile
import os
from ultralytics import YOLO
import cv2 as cv
import pandas as pd
model_path="/home/bacancy/POCs/Driver-Distraction-Detection-main/models/best2.pt"
# --- Page Configuration ---
st.set_page_config(
page_title="Driver Distraction System",
page_icon="🚗",
layout="wide",
initial_sidebar_state="expanded",
)
# --- Sidebar ---
st.sidebar.title("🚗 Driver Distraction System")
st.sidebar.write("Choose an option below:")
# Sidebar navigation
page = st.sidebar.radio("Select Feature", [
"Distraction System",
"Real-time Drowsiness Detection",
"Video Drowsiness Detection"
])
# --- Class Labels (for YOLO model) ---
class_names = ['drinking', 'hair and makeup', 'operating the radio', 'reaching behind',
'safe driving', 'talking on the phone', 'talking to passenger', 'texting']
# Sidebar Class Name Display
st.sidebar.subheader("Class Names")
for idx, class_name in enumerate(class_names):
st.sidebar.write(f"{idx}: {class_name}")
# --- Feature: YOLO Distraction Detection ---
if page == "Distraction System":
st.title("Driver Distraction System")
st.write("Upload an image or video to detect distractions using YOLO model.")
# File type selection
file_type = st.radio("Select file type:", ["Image", "Video"])
if file_type == "Image":
uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
image = Image.open(uploaded_file).convert('RGB')
image_np = np.array(image)
col1, col2 = st.columns([1, 1])
with col1:
st.subheader("Uploaded Image")
st.image(image, caption="Original Image", use_container_width=True)
with col2:
st.subheader("Detection Results")
model = YOLO(model_path)
start_time = time.time()
results = model(image_np)
end_time = time.time()
prediction_time = end_time - start_time
result = results[0]
if len(result.boxes) > 0:
boxes = result.boxes
confidences = boxes.conf.cpu().numpy()
classes = boxes.cls.cpu().numpy()
class_names_dict = result.names
max_conf_idx = confidences.argmax()
predicted_class = class_names_dict[int(classes[max_conf_idx])]
confidence_score = confidences[max_conf_idx]
st.markdown(f"### Predicted Class: **{predicted_class}**")
st.markdown(f"### Confidence Score: **{confidence_score:.4f}** ({confidence_score*100:.1f}%)")
st.markdown(f"Inference Time: {prediction_time:.2f} seconds")
else:
st.warning("No distractions detected.")
else: # Video processing
uploaded_video = st.file_uploader("Upload Video", type=["mp4", "avi", "mov", "mkv", "webm"])
if uploaded_video is not None:
tfile = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
tfile.write(uploaded_video.read())
temp_input_path = tfile.name
temp_output_path = tempfile.mktemp(suffix="_distraction_detected.mp4")
st.subheader("Video Information")
cap = cv.VideoCapture(temp_input_path)
fps = cap.get(cv.CAP_PROP_FPS)
width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))
total_frames = int(cap.get(cv.CAP_PROP_FRAME_COUNT))
duration = total_frames / fps if fps > 0 else 0
cap.release()
col1, col2 = st.columns(2)
with col1:
st.metric("Duration", f"{duration:.2f} seconds")
st.metric("Original FPS", f"{fps:.2f}")
with col2:
st.metric("Resolution", f"{width}x{height}")
st.metric("Total Frames", total_frames)
st.subheader("Original Video Preview")
st.video(uploaded_video)
if st.button("Process Video for Distraction Detection"):
TARGET_PROCESSING_FPS = 10
# --- NEW: Hyperparameter for the temporal smoothing logic ---
PERSISTENCE_CONFIDENCE_THRESHOLD = 0.40 # Stick with old class if found with >= 40% confidence
st.info(f"🚀 For faster results, video will be processed at ~{TARGET_PROCESSING_FPS} FPS.")
st.info(f"🧠 Applying temporal smoothing to reduce status flickering (Persistence Threshold: {PERSISTENCE_CONFIDENCE_THRESHOLD*100:.0f}%).")
progress_bar = st.progress(0, text="Starting video processing...")
with st.spinner(f"Processing video... This may take a while."):
model = YOLO(model_path)
cap = cv.VideoCapture(temp_input_path)
fourcc = cv.VideoWriter_fourcc(*'mp4v')
out = cv.VideoWriter(temp_output_path, fourcc, fps, (width, height))
frame_skip_interval = max(1, round(fps / TARGET_PROCESSING_FPS))
frame_count = 0
last_best_box_coords = None
last_best_box_label = ""
last_status_text = "Status: Initializing..."
last_status_color = (128, 128, 128)
# --- NEW: State variable to store the last confirmed class ---
last_confirmed_class_name = 'safe driving'
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame_count += 1
progress = int((frame_count / total_frames) * 100) if total_frames > 0 else 0
progress_bar.progress(progress, text=f"Analyzing frame {frame_count}/{total_frames}")
annotated_frame = frame.copy()
if frame_count % frame_skip_interval == 0:
results = model(annotated_frame)
result = results[0]
last_best_box_coords = None # Reset box for this processing cycle
if len(result.boxes) > 0:
boxes = result.boxes
class_names_dict = result.names
confidences = boxes.conf.cpu().numpy()
classes = boxes.cls.cpu().numpy()
# --- NEW STABILITY LOGIC ---
final_box_to_use = None
# 1. Check if the last known class exists with reasonable confidence
for i in range(len(boxes)):
current_class_name = class_names_dict[int(classes[i])]
if current_class_name == last_confirmed_class_name and confidences[i] >= PERSISTENCE_CONFIDENCE_THRESHOLD:
final_box_to_use = boxes[i]
break
# 2. If not, fall back to the highest confidence detection in the current frame
if final_box_to_use is None:
max_conf_idx = confidences.argmax()
final_box_to_use = boxes[max_conf_idx]
# --- END OF NEW LOGIC ---
# Now, process the determined "final_box_to_use"
x1, y1, x2, y2 = final_box_to_use.xyxy[0].cpu().numpy()
confidence = final_box_to_use.conf[0].cpu().numpy()
class_id = int(final_box_to_use.cls[0].cpu().numpy())
class_name = class_names_dict[class_id]
# Update the state for the next frames
last_confirmed_class_name = class_name
last_best_box_coords = (int(x1), int(y1), int(x2), int(y2))
last_best_box_label = f"{class_name}: {confidence:.2f}"
if class_name != 'safe driving':
last_status_text = f"Status: {class_name.replace('_', ' ').title()}"
last_status_color = (0, 0, 255)
else:
last_status_text = "Status: Safe Driving"
last_status_color = (0, 128, 0)
else:
# No detections, reset to safe driving
last_confirmed_class_name = 'safe driving'
last_status_text = "Status: Safe Driving"
last_status_color = (0, 128, 0)
# Draw annotations on EVERY frame using the last known data
if last_best_box_coords:
cv.rectangle(annotated_frame, (last_best_box_coords[0], last_best_box_coords[1]),
(last_best_box_coords[2], last_best_box_coords[3]), (0, 255, 0), 2)
cv.putText(annotated_frame, last_best_box_label,
(last_best_box_coords[0], last_best_box_coords[1] - 10),
cv.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
# Draw status text
font_scale, font_thickness = 1.0, 2
(text_w, text_h), _ = cv.getTextSize(last_status_text, cv.FONT_HERSHEY_SIMPLEX, font_scale, font_thickness)
padding = 10
rect_start = (padding, padding)
rect_end = (padding + text_w + padding, padding + text_h + padding)
cv.rectangle(annotated_frame, rect_start, rect_end, last_status_color, -1)
text_pos = (padding + 5, padding + text_h + 5)
cv.putText(annotated_frame, last_status_text, text_pos, cv.FONT_HERSHEY_SIMPLEX, font_scale, (255, 255, 255), font_thickness)
out.write(annotated_frame)
cap.release()
out.release()
progress_bar.progress(100, text="Video processing completed!")
st.success("Video processed successfully!")
if os.path.exists(temp_output_path):
with open(temp_output_path, "rb") as file:
video_bytes = file.read()
st.download_button(
label="📥 Download Processed Video",
data=video_bytes,
file_name=f"distraction_detected_{uploaded_video.name}",
mime="video/mp4",
key="download_distraction_video"
)
st.subheader("Sample Frame from Processed Video")
cap_out = cv.VideoCapture(temp_output_path)
ret, frame = cap_out.read()
if ret:
frame_rgb = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
st.image(frame_rgb, caption="Sample frame with distraction detection", use_container_width=True)
cap_out.release()
try:
os.unlink(temp_input_path)
if os.path.exists(temp_output_path): os.unlink(temp_output_path)
except Exception as e:
st.warning(f"Failed to clean up temporary files: {e}")
# --- Feature: Real-time Drowsiness Detection ---
elif page == "Real-time Drowsiness Detection":
st.title("🧠 Real-time Drowsiness Detection")
st.write("This will open your webcam and run the detection script.")
if st.button("Start Drowsiness Detection"):
with st.spinner("Launching webcam..."):
subprocess.Popen(["python3", "drowsiness_detection.py", "--mode", "webcam"])
st.success("Drowsiness detection started in a separate window. Press 'q' in that window to quit.")
# --- Feature: Video Drowsiness Detection ---
elif page == "Video Drowsiness Detection":
st.title("📹 Video Drowsiness Detection")
st.write("Upload a video file to detect drowsiness and download the processed video.")
uploaded_video = st.file_uploader("Upload Video", type=["mp4", "avi", "mov", "mkv", "webm"])
if uploaded_video is not None:
tfile = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
tfile.write(uploaded_video.read())
temp_input_path = tfile.name
temp_output_path = tempfile.mktemp(suffix="_processed.mp4")
st.subheader("Original Video Preview")
st.video(uploaded_video)
if st.button("Process Video for Drowsiness Detection"):
progress_bar = st.progress(0, text="Preparing to process video...")
with st.spinner("Processing video... This may take a while."):
process = subprocess.Popen([
"python3", "drowsiness_detection.py",
"--mode", "video",
"--input", temp_input_path,
"--output", temp_output_path
], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
stdout, stderr = process.communicate()
if process.returncode == 0:
progress_bar.progress(100, text="Video processing completed!")
if os.path.exists(temp_output_path):
st.success("Video processed successfully!")
if stdout: st.code(stdout)
with open(temp_output_path, "rb") as file: video_bytes = file.read()
st.download_button(
label="📥 Download Processed Video",
data=video_bytes,
file_name=f"drowsiness_detected_{uploaded_video.name}",
mime="video/mp4",
key="download_processed_video"
)
st.subheader("Sample Frame from Processed Video")
cap = cv.VideoCapture(temp_output_path)
ret, frame = cap.read()
if ret: st.image(cv.cvtColor(frame, cv.COLOR_BGR2RGB), caption="Sample frame with drowsiness detection", use_container_width=True)
cap.release()
else:
st.error("Error: Processed video file not found.")
if stderr: st.code(stderr)
else:
st.error("An error occurred during video processing.")
if stderr: st.code(stderr)
try:
if os.path.exists(temp_input_path): os.unlink(temp_input_path)
if os.path.exists(temp_output_path): os.unlink(temp_output_path)
except Exception as e:
st.warning(f"Failed to clean up temporary files: {e}")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248# PREP DEPENDENCIESfrom scipy.spatial import distance as distfrom imutils import face_utilsfrom threading import Threadimport numpy as npimport cv2 as cvimport imutilsimport dlibimport pygame # Used for playing alarm sounds cross-platformimport argparseimport os
# --- INITIALIZE MODELS AND CONSTANTS ---
# Haar cascade classifier for face detectionhaar_cascade_face_detector = "dependencies/haarcascade_frontalface_default.xml"face_detector = cv.CascadeClassifier(haar_cascade_face_detector)
# Dlib facial landmark detectordlib_facial_landmark_predictor = "dependencies/shape_predictor_68_face_landmarks.dat"landmark_predictor = dlib.shape_predictor(dlib_facial_landmark_predictor)
# Important Variablesfont = cv.FONT_HERSHEY_SIMPLEX# --- INITIALIZE MODELS AND CONSTANTS ---# Eye Drowsiness DetectionEYE_ASPECT_RATIO_THRESHOLD = 0.25EYE_CLOSED_THRESHOLD = 20EYE_THRESH_COUNTER = 0DROWSY_COUNTER = 0drowsy_alert = False
# Mouth Yawn DetectionMOUTH_ASPECT_RATIO_THRESHOLD = 0.5MOUTH_OPEN_THRESHOLD = 15YAWN_THRESH_COUNTER = 0YAWN_COUNTER = 0yawn_alert = False
# NEW: Head Not Visible DetectionFACE_LOST_THRESHOLD = 25 # Conseq. frames face must be lost to trigger alertFACE_LOST_COUNTER = 0HEAD_DOWN_COUNTER = 0 # Renaming for clarityhead_down_alert = False
# --- AUDIO SETUP (using Pygame) ---pygame.mixer.init()drowsiness_sound = pygame.mixer.Sound("dependencies/audio/drowsiness-detected.mp3")yawn_sound = pygame.mixer.Sound("dependencies/audio/yawning-detected.mp3")# head_down_sound = pygame.mixer.Sound("dependencies/audio/head-down-detected.mp3")
# --- CORE FUNCTIONS ---def play_alarm(sound_to_play): if not pygame.mixer.get_busy(): sound_to_play.play()
def generate_alert(final_eye_ratio, final_mouth_ratio): global EYE_THRESH_COUNTER, YAWN_THRESH_COUNTER global drowsy_alert, yawn_alert global DROWSY_COUNTER, YAWN_COUNTER
# Drowsiness check if final_eye_ratio < EYE_ASPECT_RATIO_THRESHOLD: EYE_THRESH_COUNTER += 1 if EYE_THRESH_COUNTER >= EYE_CLOSED_THRESHOLD: if not drowsy_alert: DROWSY_COUNTER += 1 drowsy_alert = True Thread(target=play_alarm, args=(drowsiness_sound,)).start() else: EYE_THRESH_COUNTER = 0 drowsy_alert = False
# Yawn check if final_mouth_ratio > MOUTH_ASPECT_RATIO_THRESHOLD: YAWN_THRESH_COUNTER += 1 if YAWN_THRESH_COUNTER >= MOUTH_OPEN_THRESHOLD: if not yawn_alert: YAWN_COUNTER += 1 yawn_alert = True Thread(target=play_alarm, args=(yawn_sound,)).start() else: YAWN_THRESH_COUNTER = 0 yawn_alert = False
def detect_facial_landmarks(x, y, w, h, gray_frame): face = dlib.rectangle(int(x), int(y), int(x + w), int(y + h)) face_landmarks = landmark_predictor(gray_frame, face) face_landmarks = face_utils.shape_to_np(face_landmarks) return face_landmarks
def eye_aspect_ratio(eye): A = dist.euclidean(eye[1], eye[5]) B = dist.euclidean(eye[2], eye[4]) C = dist.euclidean(eye[0], eye[3]) ear = (A + B) / (2.0 * C) return ear
def final_eye_aspect_ratio(shape): (lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"] (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"] left_eye = shape[lStart:lEnd] right_eye = shape[rStart:rEnd] left_ear = eye_aspect_ratio(left_eye) right_ear = eye_aspect_ratio(right_eye) final_ear = (left_ear + right_ear) / 2.0 return final_ear, left_eye, right_eye
def mouth_aspect_ratio(mouth): A = dist.euclidean(mouth[2], mouth[10]) B = dist.euclidean(mouth[4], mouth[8]) C = dist.euclidean(mouth[0], mouth[6]) mar = (A + B) / (2.0 * C) return mar
def final_mouth_aspect_ratio(shape): (mStart, mEnd) = face_utils.FACIAL_LANDMARKS_IDXS["mouth"] mouth = shape[mStart:mEnd] return mouth_aspect_ratio(mouth), mouth
def head_pose_ratio(shape): nose_tip = shape[30] chin_tip = shape[8] left_face_corner = shape[0] right_face_corner = shape[16] nose_to_chin_dist = dist.euclidean(nose_tip, chin_tip) face_width = dist.euclidean(left_face_corner, right_face_corner) if face_width == 0: return 0.0 hpr = nose_to_chin_dist / face_width return hpr
def reset_counters(): global EYE_THRESH_COUNTER, YAWN_THRESH_COUNTER, FACE_LOST_COUNTER global DROWSY_COUNTER, YAWN_COUNTER, HEAD_DOWN_COUNTER global drowsy_alert, yawn_alert, head_down_alert EYE_THRESH_COUNTER, YAWN_THRESH_COUNTER, FACE_LOST_COUNTER = 0, 0, 0 DROWSY_COUNTER, YAWN_COUNTER, HEAD_DOWN_COUNTER = 0, 0, 0 drowsy_alert, yawn_alert, head_down_alert = False, False, False
def process_frame(frame): global FACE_LOST_COUNTER, head_down_alert, HEAD_DOWN_COUNTER frame = imutils.resize(frame, width=640) gray_frame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) faces = face_detector.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv.CASCADE_SCALE_IMAGE) if len(faces) > 0: FACE_LOST_COUNTER = 0 head_down_alert = False (x, y, w, h) = faces[0] face_landmarks = detect_facial_landmarks(x, y, w, h, gray_frame) final_ear, left_eye, right_eye = final_eye_aspect_ratio(face_landmarks) final_mar, mouth = final_mouth_aspect_ratio(face_landmarks) # left_eye_hull, right_eye_hull, mouth_hull = cv.convexHull(left_eye), cv.convexHull(right_eye), cv.convexHull(mouth) # cv.drawContours(frame, [left_eye_hull], -1, (0, 255, 0), 1) # cv.drawContours(frame, [right_eye_hull], -1, (0, 255, 0), 1) # cv.drawContours(frame, [mouth_hull], -1, (0, 255, 0), 1) generate_alert(final_ear, final_mar) cv.putText(frame, f"EAR: {final_ear:.2f}", (10, 30), font, 0.7, (0, 0, 255), 2) cv.putText(frame, f"MAR: {final_mar:.2f}", (10, 60), font, 0.7, (0, 0, 255), 2) else: FACE_LOST_COUNTER += 1 if FACE_LOST_COUNTER >= FACE_LOST_THRESHOLD and not head_down_alert: HEAD_DOWN_COUNTER += 1 head_down_alert = True cv.putText(frame, f"Drowsy: {DROWSY_COUNTER}", (480, 30), font, 0.7, (255, 255, 0), 2) cv.putText(frame, f"Yawn: {YAWN_COUNTER}", (480, 60), font, 0.7, (255, 255, 0), 2) cv.putText(frame, f"Head Down: {HEAD_DOWN_COUNTER}", (480, 90), font, 0.7, (255, 255, 0), 2) if drowsy_alert: cv.putText(frame, "DROWSINESS ALERT!", (150, 30), font, 0.9, (0, 0, 255), 2) if yawn_alert: cv.putText(frame, "YAWN ALERT!", (200, 60), font, 0.9, (0, 0, 255), 2) if head_down_alert: cv.putText(frame, "HEAD NOT VISIBLE!", (180, 90), font, 0.9, (0, 0, 255), 2) return frame
def process_video(input_path, output_path=None): reset_counters() video_stream = cv.VideoCapture(input_path) if not video_stream.isOpened(): print(f"Error: Could not open video file {input_path}") return False fps = int(video_stream.get(cv.CAP_PROP_FPS)) width = int(video_stream.get(cv.CAP_PROP_FRAME_WIDTH)) height = int(video_stream.get(cv.CAP_PROP_FRAME_HEIGHT)) print(f"Processing video: {input_path}") print(f"Original Res: {width}x{height}, FPS: {fps}") video_writer = None if output_path: fourcc = cv.VideoWriter_fourcc(*'mp4v') # --- FIX: Calculate correct output dimensions to prevent corruption --- # The process_frame function resizes frames to a fixed width of 640. output_width = 640 # Maintain aspect ratio output_height = int(height * (output_width / float(width))) output_dims = (output_width, output_height) video_writer = cv.VideoWriter(output_path, fourcc, fps, output_dims) print(f"Outputting video with Res: {output_dims[0]}x{output_dims[1]}")
while True: ret, frame = video_stream.read() if not ret: break processed_frame = process_frame(frame) if video_writer: video_writer.write(processed_frame) video_stream.release() if video_writer: video_writer.release() print("Video processing complete!") print(f"Final Stats - Drowsy: {DROWSY_COUNTER}, Yawn: {YAWN_COUNTER}, Head Down: {HEAD_DOWN_COUNTER}") return True
def run_webcam(): reset_counters() video_stream = cv.VideoCapture(0) if not video_stream.isOpened(): print("Error: Could not open webcam") return False while True: ret, frame = video_stream.read() if not ret: print("Failed to grab frame") break processed_frame = process_frame(frame) cv.imshow("Live Drowsiness and Yawn Detection", processed_frame) if cv.waitKey(1) & 0xFF == ord('q'): break video_stream.release() cv.destroyAllWindows() return True
# --- MAIN EXECUTION LOOP ---if __name__ == "__main__": parser = argparse.ArgumentParser(description='Drowsiness Detection System') parser.add_argument('--mode', choices=['webcam', 'video'], default='webcam', help='Mode of operation') parser.add_argument('--input', type=str, help='Input video file path for video mode') parser.add_argument('--output', type=str, help='Output video file path for video mode') args = parser.parse_args() if args.mode == 'webcam': print("Starting webcam detection...") run_webcam() elif args.mode == 'video': if not args.input: print("Error: --input argument is required for video mode.") elif not os.path.exists(args.input): print(f"Error: Input file not found at {args.input}") else: process_video(args.input, args.output)123altairpandasstreamlit123456789dlibimutilsnumpyopencv-pythonpillowpygamescipystreamlitultralytics
123version https://git-lfs.github.com/spec/v1oid sha256:fbdc2cb80eb9aa7a758672cbfdda32ba6300efe9b6e6c7a299ff7e736b11b92fsize 99693937123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142"""Video Processing Utility for Drowsiness DetectionThis script provides a more robust video processing interface"""
import cv2 as cvimport osimport jsonfrom datetime import datetimeimport argparse
def get_video_info(video_path): """Get detailed video information""" cap = cv.VideoCapture(video_path) if not cap.isOpened(): return None info = { 'fps': cap.get(cv.CAP_PROP_FPS), 'width': int(cap.get(cv.CAP_PROP_FRAME_WIDTH)), 'height': int(cap.get(cv.CAP_PROP_FRAME_HEIGHT)), 'total_frames': int(cap.get(cv.CAP_PROP_FRAME_COUNT)), 'duration': cap.get(cv.CAP_PROP_FRAME_COUNT) / cap.get(cv.CAP_PROP_FPS) if cap.get(cv.CAP_PROP_FPS) > 0 else 0, 'codec': int(cap.get(cv.CAP_PROP_FOURCC)), 'file_size': os.path.getsize(video_path) } cap.release() return info
def create_processing_report(input_path, output_path, stats): """Create a JSON report of the processing results""" report = { 'timestamp': datetime.now().isoformat(), 'input_file': input_path, 'output_file': output_path, 'video_info': get_video_info(input_path), 'detection_stats': stats, 'processing_info': { 'software': 'Drowsiness Detection System', 'version': '1.0' } } report_path = output_path.replace('.mp4', '_report.json') with open(report_path, 'w') as f: json.dump(report, f, indent=2) return report_path
def process_video_with_progress(input_path, output_path, progress_callback=None): """ Process video with progress callback progress_callback: function that takes (current_frame, total_frames) """ # Import the drowsiness detection functions from drowsiness_detection import process_frame, reset_counters from drowsiness_detection import DROWSY_COUNTER, YAWN_COUNTER, HEAD_DOWN_COUNTER reset_counters() # Open video file video_stream = cv.VideoCapture(input_path) if not video_stream.isOpened(): raise ValueError(f"Could not open video file {input_path}") # Get video properties fps = int(video_stream.get(cv.CAP_PROP_FPS)) width = int(video_stream.get(cv.CAP_PROP_FRAME_WIDTH)) height = int(video_stream.get(cv.CAP_PROP_FRAME_HEIGHT)) total_frames = int(video_stream.get(cv.CAP_PROP_FRAME_COUNT)) # Setup video writer fourcc = cv.VideoWriter_fourcc(*'mp4v') video_writer = cv.VideoWriter(output_path, fourcc, fps, (640, 480)) frame_count = 0 try: while True: ret, frame = video_stream.read() if not ret: break frame_count += 1 # Process frame processed_frame = process_frame(frame) # Write frame to output video video_writer.write(processed_frame) # Call progress callback if provided if progress_callback: progress_callback(frame_count, total_frames) # Get final stats stats = { 'total_frames': frame_count, 'drowsy_events': DROWSY_COUNTER, 'yawn_events': YAWN_COUNTER, 'head_down_events': HEAD_DOWN_COUNTER } return stats finally: video_stream.release() video_writer.release()
def main(): parser = argparse.ArgumentParser(description='Video Processing Utility for Drowsiness Detection') parser.add_argument('--input', '-i', required=True, help='Input video file path') parser.add_argument('--output', '-o', help='Output video file path (optional)') parser.add_argument('--report', '-r', action='store_true', help='Generate processing report') parser.add_argument('--info', action='store_true', help='Show video information only') args = parser.parse_args() if not os.path.exists(args.input): print(f"Error: Input file {args.input} does not exist") return # Show video info if args.info: info = get_video_info(args.input) if info: print(f"Video Information for: {args.input}") print(f"Resolution: {info['width']}x{info['height']}") print(f"FPS: {info['fps']:.2f}") print(f"Duration: {info['duration']:.2f} seconds") print(f"Total Frames: {info['total_frames']}") print(f"File Size: {info['file_size'] / (1024*1024):.2f} MB") else: print("Error: Could not read video file") return # Generate output path if not provided if not args.output: base_name.gitattributes+1
app.pyAdded+310
drowsiness_detection.pyAdded+248
haarcascade_frontalface_default.xmlAdded
requirements.txt+9-3
shape_predictor_68_face_landmarks.datAdded+3
video_processor.pyAdded+142