This repo contains two training notebooks (a baseline CNN and an “industry-grade” EfficientNet pipeline) and a real-time webcam inference script to classify driver behavior into 6 classes:
DangerousDrivingDistractedDrinkingSafeDrivingSleepyDrivingYawn
driver_drowsing_detection_model_notebook.ipynb— Baseline CNN trained from scratch; exportsdriving_behavior_model.h5.driver_drowsiness_pipeline.ipynb— Transfer-learning pipeline (EfficientNetB0 + fine-tuning, augmentation, mixed precision, richer evaluation); exports EfficientNet models.real_time_detection.py— Webcam inference + on-screen labels + CSV logging.driving_behavior_model.h5— Trained Keras model (used byreal_time_detection.py).driving_behavior_model.onnx— Exported ONNX version of the model (not used byreal_time_detection.pyas-is).requirements.txt— Dependency placeholder (see notes below).checkpoints/— Training checkpoints (used by the pipeline notebook).
- Windows (tested paths in notebooks are Windows-style)
- Python 3.9+ recommended
- A working webcam (for real-time inference)
This project uses:
numpy,pandasopencv-pythonmatplotlib,seabornscikit-learntensorflow
Note:
requirements.txtcurrently includes some entries that are not pip-installable packages (e.g.,random,warnings,cv2,matplotlib.pyplot). Ifpip install -r requirements.txtfails, install the packages above directly.
- Create and activate a virtual environment.
- Install dependencies.
If you want a quick, reliable install, use (PowerShell):
python -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install --upgrade pip
python -m pip install numpy pandas matplotlib seaborn opencv-python scikit-learn tensorflowThe notebooks expect the Driver Inattention Detection Dataset (as referenced in the notebook paths). You can:
- Download it to any location, then
- Update the dataset paths in the notebooks.
In driver_drowsiness_pipeline.ipynb, edit the single configuration cell:
BASE_DIRTRAIN_DIR,TEST_DIR,VALID_DIRTRAIN_ANNOT,TEST_ANNOT,VALID_ANNOT
In driver_drowsing_detection_model_notebook.ipynb, update:
train_images_pathannotation_file_pathtrain_folder,test_folder,valid_folder
Open and run driver_drowsing_detection_model_notebook.ipynb.
At the end, it saves:
driving_behavior_model.h5
This file matches the default model name expected by real_time_detection.py.
Open and run driver_drowsiness_pipeline.ipynb.
It trains in two phases (frozen backbone → fine-tuning), evaluates with classification metrics, and exports:
driver_drowsiness_efficientnet.kerasdriver_drowsiness_efficientnet.h5saved_model/
The script loads a Keras model and runs inference on frames from your webcam.
From the repo folder:
python real_time_detection.pyCommon options:
python real_time_detection.py --model driving_behavior_model.h5
python real_time_detection.py --webcam 0
python real_time_detection.py --save
python real_time_detection.py --normalize- Press
qto quit. - Predictions are appended to
drowsiness_predictions_log.csv.
This repo includes two model artifacts:
driving_behavior_model.h5(Keras / TensorFlow) — works withreal_time_detection.py.driving_behavior_model.onnx(ONNX) — useful for deployment in other runtimes, but the current script does not load ONNX.
If you want to run the ONNX model, you’ll need a separate inference path (typically via onnxruntime) or adapt real_time_detection.py accordingly.
real_time_detection.py currently preprocesses frames as:
- BGR → RGB
- Resize to
224x224 - Optional
/255.0normalization via--normalize
This matches the baseline CNN notebook (which uses raw pixel values unless you explicitly normalized during training).
If you want to run the EfficientNet model exported from driver_drowsiness_pipeline.ipynb, you must ensure the webcam preprocessing matches EfficientNet’s expected preprocessing (the notebook uses tensorflow.keras.applications.efficientnet.preprocess_input, which scales to roughly [-1, 1]).
Practical options:
- Use the baseline
driving_behavior_model.h5forreal_time_detection.pyas-is, or - Update
real_time_detection.pyto apply EfficientNet preprocessing before prediction.
- On-screen overlay: predicted class, confidence, FPS
- CSV log:
drowsiness_predictions_log.csvwith columnstimestamp, iso, class, confidence - Optional video output:
output_with_labels.avi(when using--save)
- Webcam won’t open: try a different
--webcamindex (0, 1, 2…). Close other apps using the camera. - Model file not found: pass an explicit path via
--model. - TensorFlow install issues on Windows: ensure you’re using a supported Python version and upgraded
pip. - Wrong predictions / low confidence: verify that preprocessing at inference matches the preprocessing used during training (normalization and/or EfficientNet
preprocess_input).
- The
checkpoints/directory is used by the EfficientNet pipeline notebook to save best models during training. - Class ordering must stay consistent between training and inference.
real_time_detection.pyuses:['DangerousDriving','Distracted','Drinking','SafeDriving','SleepyDriving','Yawn'].
Add a license if you plan to distribute this project.