DeepActionPotential commited on
Commit
c9280e3
·
verified ·
1 Parent(s): b8741b9

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -1,35 +1,4 @@
1
- *.7z filter=lfs diff=lfs merge=lfs -text
2
- *.arrow filter=lfs diff=lfs merge=lfs -text
3
- *.bin filter=lfs diff=lfs merge=lfs -text
4
- *.bz2 filter=lfs diff=lfs merge=lfs -text
5
- *.ckpt filter=lfs diff=lfs merge=lfs -text
6
- *.ftz filter=lfs diff=lfs merge=lfs -text
7
- *.gz filter=lfs diff=lfs merge=lfs -text
8
- *.h5 filter=lfs diff=lfs merge=lfs -text
9
- *.joblib filter=lfs diff=lfs merge=lfs -text
10
- *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
- *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
- *.model filter=lfs diff=lfs merge=lfs -text
13
- *.msgpack filter=lfs diff=lfs merge=lfs -text
14
- *.npy filter=lfs diff=lfs merge=lfs -text
15
- *.npz filter=lfs diff=lfs merge=lfs -text
16
- *.onnx filter=lfs diff=lfs merge=lfs -text
17
- *.ot filter=lfs diff=lfs merge=lfs -text
18
- *.parquet filter=lfs diff=lfs merge=lfs -text
19
- *.pb filter=lfs diff=lfs merge=lfs -text
20
- *.pickle filter=lfs diff=lfs merge=lfs -text
21
- *.pkl filter=lfs diff=lfs merge=lfs -text
22
- *.pt filter=lfs diff=lfs merge=lfs -text
23
- *.pth filter=lfs diff=lfs merge=lfs -text
24
- *.rar filter=lfs diff=lfs merge=lfs -text
25
- *.safetensors filter=lfs diff=lfs merge=lfs -text
26
- saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
- *.tar.* filter=lfs diff=lfs merge=lfs -text
28
- *.tar filter=lfs diff=lfs merge=lfs -text
29
- *.tflite filter=lfs diff=lfs merge=lfs -text
30
- *.tgz filter=lfs diff=lfs merge=lfs -text
31
- *.wasm filter=lfs diff=lfs merge=lfs -text
32
- *.xz 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
 
1
+ models/model.pth filter=lfs diff=lfs merge=lfs -text
2
+ assets/1.png filter=lfs diff=lfs merge=lfs -text
3
+ assets/2.png filter=lfs diff=lfs merge=lfs -text
4
+ assets/drowsy_demo.mp4 filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
LICENCE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Eslam Tarek
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
README.md CHANGED
@@ -1,19 +1,102 @@
1
- ---
2
- title: DrowSeeAi
3
- emoji: 🚀
4
- colorFrom: red
5
- colorTo: red
6
- sdk: docker
7
- app_port: 8501
8
- tags:
9
- - streamlit
10
- pinned: false
11
- short_description: Is a deep learning solution for detecting driver drowsiness
12
- ---
13
-
14
- # Welcome to Streamlit!
15
-
16
- Edit `/src/streamlit_app.py` to customize this app to your heart's desire. :heart:
17
-
18
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
19
- forums](https://discuss.streamlit.io).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Drowsy Detector
2
+
3
+ ## About the Project
4
+
5
+ Drowsy Detector is an end-to-end deep learning solution for detecting driver drowsiness from facial images. The project leverages transfer learning with a pre-trained ResNet50 model, custom data preprocessing, and a user-friendly Streamlit web interface for real-time predictions. It is designed to help improve road safety by providing an automated tool to identify signs of driver fatigue. The repository includes all code, a training and evaluation notebook, and demo media for easy testing and demonstration.
6
+
7
+ ## About the Dataset
8
+
9
+ This project is built upon the [Drowsiness Prediction Dataset](https://www.kaggle.com/datasets/rakibuleceruet/drowsiness-prediction-dataset) from Kaggle. The dataset contains thousands of labeled facial images of drivers, divided into two categories: "Fatigue Subjects" (drowsy) and "Active Subjects" (alert). Images are collected under various lighting conditions, backgrounds, and driver poses, making the dataset robust and suitable for real-world drowsiness detection. The dataset is organized into folders by class, and each image is labeled for supervised learning. This diversity and structure allow for effective training and evaluation of deep learning models for driver monitoring systems.
10
+
11
+ ## Notebook Summary
12
+
13
+ The notebook provides a comprehensive, step-by-step workflow for building a deep learning-based driver drowsiness detection system:
14
+ - **Data Exploration:** Visualizes the dataset structure, displays sample images from each class, and analyzes class distribution to ensure balanced training.
15
+ - **Data Preparation:** Implements a custom PyTorch dataset and DataLoader, applies image preprocessing (resizing, normalization), and uses data augmentation to improve model generalization.
16
+ - **Model Architecture:** Utilizes transfer learning with a pre-trained ResNet50 model, adapting its final layers for binary classification and fine-tuning the last few layers.
17
+ - **Training Loop:** Sets up the training process with early stopping to prevent overfitting, tracks loss and accuracy metrics, and saves the best-performing model.
18
+ - **Evaluation:** Assesses model performance on validation and test sets, visualizes results with confusion matrices, and plots training/validation loss curves for diagnostics.
19
+
20
+ ## Model Results
21
+
22
+ ### Preprocessing
23
+ - **Image Resizing:** All images are resized to 224x224 pixels to match ResNet50's input requirements.
24
+ - **Normalization:** Images are converted to tensors and normalized to standardize input distributions.
25
+ - **Augmentation:** The training set uses augmentations such as random flips and rotations to improve generalization and robustness.
26
+
27
+ ### Training
28
+ - **Transfer Learning:** The model uses a pre-trained ResNet50 backbone. All layers are frozen except the last three, which are fine-tuned on the drowsiness dataset.
29
+ - **Custom Classifier Head:** The final fully connected layer is replaced with a sequence of linear, ReLU, and dropout layers, ending with a two-class output.
30
+ - **Loss Function:** Cross-entropy loss is used for binary classification.
31
+ - **Optimizer:** Adam optimizer is employed with a learning rate of 1e-4.
32
+ - **Early Stopping:** Training is monitored on the validation set and stops early if validation loss does not improve for several epochs.
33
+
34
+ ### Evaluation
35
+ - **Accuracy:** The model achieves up to 96% accuracy on the test set, demonstrating strong performance in distinguishing between drowsy and alert drivers.
36
+ - **Confusion Matrix:** The confusion matrix shows high true positive and true negative rates, with minimal misclassifications.
37
+ - **Loss Curves:** Training and validation loss curves are plotted to visualize convergence and detect any signs of overfitting or underfitting.
38
+
39
+ ## How to Install
40
+
41
+ 1. **Clone the repository:**
42
+ ```bash
43
+ git clone <repo-url>
44
+ cd DrowsyDetector
45
+ ```
46
+ 2. **Create and activate a virtual environment:**
47
+ ```bash
48
+ python -m venv venv
49
+ # On Windows:
50
+ venv\Scripts\activate
51
+ # On macOS/Linux:
52
+ source venv/bin/activate
53
+ ```
54
+ 3. **Install dependencies:**
55
+ ```bash
56
+ pip install -r requirements.txt
57
+ ```
58
+ This will install PyTorch, torchvision, Streamlit, Pillow, NumPy, and other dependencies.
59
+
60
+ 4. **Download the dataset:**
61
+ - Download the dataset from [Kaggle](https://www.kaggle.com/datasets/rakibuleceruet/drowsiness-prediction-dataset).
62
+ - Extract it and place it in the appropriate directory as referenced in the notebook.
63
+
64
+ ## How to Use the Software
65
+
66
+ 1. **Demo Application:**
67
+ - The project includes a Streamlit-based web application for real-time drowsiness detection.
68
+ - To launch the demo, run:
69
+ ```bash
70
+ streamlit run app.py
71
+ ```
72
+ - The app allows you to upload an image of a driver and predicts whether the driver is drowsy or alert.
73
+
74
+ 2. **Using Demo Media:**
75
+ - Simply upload any image from the `assets` directory via the web interface and click predict button.
76
+
77
+ ## [demo-video](assets/drowsy_demo.mp4)
78
+
79
+ - **Demo Images:**
80
+ ![demo1](assets/1.png)
81
+ ![demo2](assets/2.png)
82
+
83
+
84
+ 3. **Notebook Usage:**
85
+ - Open the notebook in Jupyter or VS Code and run all cells to reproduce the training and evaluation process.
86
+ - You can modify paths and parameters as needed to experiment with different settings.
87
+
88
+ ## Technologies Used
89
+
90
+ - **PyTorch:** The primary deep learning framework used for model definition, training, and inference. PyTorch provides flexibility for custom dataset handling and model customization.
91
+ - **Torchvision:** Supplies pre-trained models (ResNet50), image transforms, and utility functions for computer vision tasks.
92
+ - **Pandas & NumPy:** Used for data manipulation, analysis, and efficient numerical computations.
93
+ - **Matplotlib:** For visualizing images, loss curves, and confusion matrices during exploration and evaluation.
94
+ - **scikit-learn:** Provides metrics such as confusion matrix and accuracy score for model evaluation.
95
+ - **Streamlit:** Enables rapid development of interactive web applications for model deployment and demonstration.
96
+ - **Pillow:** Used for image loading and processing within the custom dataset class.
97
+
98
+ These technologies together enable efficient data handling, model training, evaluation, and deployment in a user-friendly interface.
99
+
100
+ ## License
101
+
102
+ This project is licensed under the MIT License. You are free to use, modify, and distribute this software for personal or commercial purposes, provided that you include the original copyright and license.
active-fatigue-resnet50-accuracy-96.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from ui import upload_image
3
+ from utils import load_model, predict
4
+
5
+ # -------------------------------
6
+ # 1) Set the path to your saved model file:
7
+ # Change this to the correct path where you saved your .pth/.pt
8
+ # -------------------------------
9
+ MODEL_PATH = "./models/model.pth" # ← replace with your actual path
10
+
11
+ # -------------------------------
12
+ # 2) Cache the model load so it isn't reloaded on every run:
13
+ # -------------------------------
14
+ @st.cache_resource
15
+ def get_model():
16
+ """
17
+ Load and cache the PyTorch model so that Streamlit does not reload it on every interaction.
18
+ """
19
+ model = load_model(MODEL_PATH)
20
+ return model
21
+
22
+ # -------------------------------
23
+ # 3) Main Streamlit UI
24
+ # -------------------------------
25
+ def main():
26
+
27
+ # apply the styles.css here
28
+ with open("./styles.css") as f:
29
+ st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
30
+
31
+ # Load the model once
32
+ model = get_model()
33
+
34
+ # Let the user upload an image via ui.upload_image()
35
+ image = upload_image()
36
+
37
+ if image is not None:
38
+ # Only show the “Predict” button if an image has been uploaded
39
+ if st.button("Predict Drowsiness"):
40
+ # Run inference
41
+ label = predict(model, image)
42
+
43
+ # Display results
44
+ if label == 1:
45
+ st.error("🚨 Drowsiness Detected (1)")
46
+ else:
47
+ st.success("✅ Not Drowsy (0)")
48
+
49
+ if __name__ == "__main__":
50
+ main()
assets/1.png ADDED

Git LFS Details

  • SHA256: fa0bb70f15a8a9e7b2ac278d868b3af5200165209ba4089b4c9457b4c335ad3b
  • Pointer size: 131 Bytes
  • Size of remote file: 148 kB
assets/2.png ADDED

Git LFS Details

  • SHA256: 61621ea304b4839db919c136ce56b9ed0142ce088cfc4da54cdccc277c1aefdb
  • Pointer size: 132 Bytes
  • Size of remote file: 1.03 MB
assets/drowsy_demo.mp4 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ee8e868b4eb6b1e5d8df56e033070584ef2641cf0be29610b27cb8cbb2347944
3
+ size 4426144
models/model.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5b534e193015cfc8e612760de3786a3206fd7e0a78f4f6a84196a5de42a8db2f
3
+ size 104857690
requirements.txt CHANGED
@@ -1,3 +1,5 @@
1
- altair
2
- pandas
3
- streamlit
 
 
 
1
+ torch==2.2.2
2
+ torchvision==0.17.2
3
+ streamlit==1.34.0
4
+ pillow==10.3.0
5
+ numpy==1.26.4
styles.css ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ /* Hide Streamlit default UI elements */
2
+ #MainMenu, header, footer {
3
+ visibility: hidden;
4
+ }
5
+
ui.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+
4
+ def upload_image():
5
+ """
6
+ Display a Streamlit file uploader. If an image is uploaded, show a preview and return it.
7
+
8
+ Returns:
9
+ PIL.Image.Image or None: The uploaded image as a PIL Image (RGB), or None if nothing uploaded.
10
+ """
11
+ st.title("🛏️ Drowsiness Detection App")
12
+ st.write("Upload a face image and the model will predict whether the person is drowsy (1) or not (0).")
13
+
14
+ uploaded_file = st.file_uploader(
15
+ label="Choose an image file (JPG/PNG)",
16
+ type=["jpg", "jpeg", "png"]
17
+ )
18
+
19
+ if uploaded_file is not None:
20
+ # Convert the uploaded file to a PIL image
21
+ image = Image.open(uploaded_file).convert("RGB")
22
+ st.image(image, caption="Uploaded Image", use_column_width=True)
23
+ return image
24
+
25
+ return None
utils.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision.transforms as transforms
3
+ from PIL import Image
4
+
5
+
6
+ val_test_transform = transforms.Compose([
7
+ transforms.Resize((224, 224)),
8
+ transforms.ToTensor(),
9
+
10
+ ])
11
+
12
+
13
+
14
+ def load_model(model_path: str):
15
+ """
16
+ Load a trained PyTorch model from disk (saved via torch.save(model, path))
17
+ and set it to eval() mode.
18
+
19
+ Args:
20
+ model_path (str): Path to the .pth or .pt file containing your trained model.
21
+
22
+ Returns:
23
+ torch.nn.Module: The loaded model in eval mode (on CPU).
24
+ """
25
+
26
+
27
+ model = torch.load(
28
+ model_path,
29
+ map_location=torch.device("cpu"),
30
+ weights_only=False, # Allow loading the entire saved model object
31
+ )
32
+ model.eval()
33
+ return model
34
+
35
+
36
+
37
+ def predict(model: torch.nn.Module, image: Image.Image) -> int:
38
+ """
39
+ Given a loaded model and a PIL.Image, return 0 (not drowsy) or 1 (drowsy).
40
+
41
+ Args:
42
+ model (torch.nn.Module): Your trained PyTorch model in eval() mode.
43
+ image (PIL.Image.Image): A PIL image (RGB) of a human face.
44
+
45
+ Returns:
46
+ int: 0 if non-drowsy, 1 if drowsy.
47
+ """
48
+ # Apply the validation/test transform:
49
+ image_tensor = val_test_transform(image) # [3, 224, 224]
50
+ image_tensor = image_tensor.unsqueeze(0) # [1, 3, 224, 224]
51
+
52
+ with torch.no_grad():
53
+ outputs = model(image_tensor) # assume shape [1, 2] or [1, 1]
54
+ # If your model outputs two logits (for classes 0 vs 1):
55
+ if outputs.dim() == 2 and outputs.shape[1] == 2:
56
+ # e.g. softmax‐based two‐class output
57
+ _, predicted = torch.max(outputs, dim=1)
58
+ return int(predicted.item())
59
+ else:
60
+ # If your model outputs a single logit (e.g. using `nn.Linear(…) -> [1, 1]`):
61
+ # apply a sigmoid threshold of 0.5
62
+ prob = torch.sigmoid(outputs).item()
63
+ return 1 if prob >= 0.5 else 0