Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,6 @@
|
|
| 1 |
# =========================================================
|
| 2 |
-
#
|
| 3 |
-
#
|
| 4 |
-
#
|
| 5 |
-
# Upload driver image
|
| 6 |
-
# β Predict fatigue state
|
| 7 |
-
#
|
| 8 |
-
# Classes:
|
| 9 |
-
# - alert
|
| 10 |
-
# - sleepy
|
| 11 |
-
# - slowBlink
|
| 12 |
-
# - yawning
|
| 13 |
-
#
|
| 14 |
# =========================================================
|
| 15 |
|
| 16 |
import gradio as gr
|
|
@@ -26,6 +16,10 @@ from tensorflow.keras.preprocessing.image import img_to_array
|
|
| 26 |
|
| 27 |
model = load_model("final_driver_state_model.h5")
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
CLASS_NAMES = [
|
| 30 |
"alert",
|
| 31 |
"sleepy",
|
|
@@ -34,7 +28,7 @@ CLASS_NAMES = [
|
|
| 34 |
]
|
| 35 |
|
| 36 |
# =========================================================
|
| 37 |
-
# RISK
|
| 38 |
# =========================================================
|
| 39 |
|
| 40 |
RISK_LEVELS = {
|
|
@@ -44,19 +38,38 @@ RISK_LEVELS = {
|
|
| 44 |
"yawning": "LOW RISK"
|
| 45 |
}
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
# =========================================================
|
| 48 |
# PREDICTION FUNCTION
|
| 49 |
# =========================================================
|
| 50 |
|
| 51 |
def predict_driver_state(image):
|
| 52 |
|
| 53 |
-
# =============================================
|
| 54 |
-
#
|
| 55 |
-
# =============================================
|
| 56 |
|
| 57 |
-
|
|
|
|
| 58 |
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
image = image.astype("float32") / 255.0
|
| 62 |
|
|
@@ -64,9 +77,9 @@ def predict_driver_state(image):
|
|
| 64 |
|
| 65 |
image = np.expand_dims(image, axis=0)
|
| 66 |
|
| 67 |
-
# =============================================
|
| 68 |
-
#
|
| 69 |
-
# =============================================
|
| 70 |
|
| 71 |
prediction = model.predict(image, verbose=0)
|
| 72 |
|
|
@@ -78,9 +91,11 @@ def predict_driver_state(image):
|
|
| 78 |
|
| 79 |
risk_level = RISK_LEVELS[predicted_class]
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
# =============================================
|
|
|
|
|
|
|
| 84 |
|
| 85 |
confidence_scores = {}
|
| 86 |
|
|
@@ -90,40 +105,50 @@ def predict_driver_state(image):
|
|
| 90 |
prediction[0][i]
|
| 91 |
)
|
| 92 |
|
| 93 |
-
# =============================================
|
| 94 |
# RESULT TEXT
|
| 95 |
-
# =============================================
|
| 96 |
|
| 97 |
result = f"""
|
| 98 |
π DRIVER STATE ANALYSIS
|
| 99 |
|
| 100 |
-
Prediction:
|
|
|
|
| 101 |
|
| 102 |
-
Confidence:
|
|
|
|
| 103 |
|
| 104 |
-
Risk Level:
|
|
|
|
| 105 |
"""
|
| 106 |
|
| 107 |
return result, confidence_scores
|
| 108 |
|
| 109 |
# =========================================================
|
| 110 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
# =========================================================
|
| 112 |
|
| 113 |
title = "π AI Driver Safety Detection System"
|
| 114 |
|
| 115 |
description = """
|
| 116 |
-
Upload a driver image to
|
| 117 |
|
| 118 |
-
##
|
| 119 |
-
- Alert
|
| 120 |
-
- Sleepy
|
| 121 |
-
- Slow Blink
|
| 122 |
-
- Yawning
|
| 123 |
|
| 124 |
-
##
|
| 125 |
-
β
CNN-Based Classification
|
| 126 |
β
Fatigue Risk Analysis
|
|
|
|
| 127 |
β
Real-Time Prediction Engine
|
| 128 |
"""
|
| 129 |
|
|
@@ -141,12 +166,13 @@ interface = gr.Interface(
|
|
| 141 |
),
|
| 142 |
|
| 143 |
outputs=[
|
|
|
|
| 144 |
gr.Textbox(
|
| 145 |
label="Prediction Result"
|
| 146 |
),
|
| 147 |
|
| 148 |
gr.Label(
|
| 149 |
-
label="
|
| 150 |
)
|
| 151 |
],
|
| 152 |
|
|
@@ -154,7 +180,11 @@ interface = gr.Interface(
|
|
| 154 |
|
| 155 |
description=description,
|
| 156 |
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
)
|
| 159 |
|
| 160 |
# =========================================================
|
|
|
|
| 1 |
# =========================================================
|
| 2 |
+
# AI DRIVER SAFETY DETECTION SYSTEM
|
| 3 |
+
# HuggingFace Gradio App
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
# =========================================================
|
| 5 |
|
| 6 |
import gradio as gr
|
|
|
|
| 16 |
|
| 17 |
model = load_model("final_driver_state_model.h5")
|
| 18 |
|
| 19 |
+
# =========================================================
|
| 20 |
+
# CLASS NAMES
|
| 21 |
+
# =========================================================
|
| 22 |
+
|
| 23 |
CLASS_NAMES = [
|
| 24 |
"alert",
|
| 25 |
"sleepy",
|
|
|
|
| 28 |
]
|
| 29 |
|
| 30 |
# =========================================================
|
| 31 |
+
# RISK LEVELS
|
| 32 |
# =========================================================
|
| 33 |
|
| 34 |
RISK_LEVELS = {
|
|
|
|
| 38 |
"yawning": "LOW RISK"
|
| 39 |
}
|
| 40 |
|
| 41 |
+
# =========================================================
|
| 42 |
+
# COLORS
|
| 43 |
+
# =========================================================
|
| 44 |
+
|
| 45 |
+
RISK_EMOJIS = {
|
| 46 |
+
"SAFE": "π’",
|
| 47 |
+
"LOW RISK": "π‘",
|
| 48 |
+
"MEDIUM RISK": "π ",
|
| 49 |
+
"HIGH RISK": "π΄"
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
# =========================================================
|
| 53 |
# PREDICTION FUNCTION
|
| 54 |
# =========================================================
|
| 55 |
|
| 56 |
def predict_driver_state(image):
|
| 57 |
|
| 58 |
+
# =====================================================
|
| 59 |
+
# VALIDATION
|
| 60 |
+
# =====================================================
|
| 61 |
|
| 62 |
+
if image is None:
|
| 63 |
+
return "Please upload an image.", None
|
| 64 |
|
| 65 |
+
# =====================================================
|
| 66 |
+
# PREPROCESSING
|
| 67 |
+
# IMPORTANT:
|
| 68 |
+
# Gradio already gives RGB image
|
| 69 |
+
# DO NOT use cvtColor here
|
| 70 |
+
# =====================================================
|
| 71 |
+
|
| 72 |
+
image = cv2.resize(image, (224, 224))
|
| 73 |
|
| 74 |
image = image.astype("float32") / 255.0
|
| 75 |
|
|
|
|
| 77 |
|
| 78 |
image = np.expand_dims(image, axis=0)
|
| 79 |
|
| 80 |
+
# =====================================================
|
| 81 |
+
# PREDICTION
|
| 82 |
+
# =====================================================
|
| 83 |
|
| 84 |
prediction = model.predict(image, verbose=0)
|
| 85 |
|
|
|
|
| 91 |
|
| 92 |
risk_level = RISK_LEVELS[predicted_class]
|
| 93 |
|
| 94 |
+
emoji = RISK_EMOJIS[risk_level]
|
| 95 |
+
|
| 96 |
+
# =====================================================
|
| 97 |
+
# CONFIDENCE SCORES
|
| 98 |
+
# =====================================================
|
| 99 |
|
| 100 |
confidence_scores = {}
|
| 101 |
|
|
|
|
| 105 |
prediction[0][i]
|
| 106 |
)
|
| 107 |
|
| 108 |
+
# =====================================================
|
| 109 |
# RESULT TEXT
|
| 110 |
+
# =====================================================
|
| 111 |
|
| 112 |
result = f"""
|
| 113 |
π DRIVER STATE ANALYSIS
|
| 114 |
|
| 115 |
+
Prediction:
|
| 116 |
+
{predicted_class.upper()}
|
| 117 |
|
| 118 |
+
Confidence:
|
| 119 |
+
{confidence:.2f}
|
| 120 |
|
| 121 |
+
Risk Level:
|
| 122 |
+
{emoji} {risk_level}
|
| 123 |
"""
|
| 124 |
|
| 125 |
return result, confidence_scores
|
| 126 |
|
| 127 |
# =========================================================
|
| 128 |
+
# EXAMPLES
|
| 129 |
+
# =========================================================
|
| 130 |
+
|
| 131 |
+
examples = []
|
| 132 |
+
|
| 133 |
+
# =========================================================
|
| 134 |
+
# UI
|
| 135 |
# =========================================================
|
| 136 |
|
| 137 |
title = "π AI Driver Safety Detection System"
|
| 138 |
|
| 139 |
description = """
|
| 140 |
+
Upload a driver image to analyze fatigue and attention state using Deep Learning.
|
| 141 |
|
| 142 |
+
## Supported Driver States
|
| 143 |
+
- π’ Alert
|
| 144 |
+
- π΄ Sleepy
|
| 145 |
+
- π Slow Blink
|
| 146 |
+
- π‘ Yawning
|
| 147 |
|
| 148 |
+
## AI Features
|
| 149 |
+
β
CNN-Based Driver State Classification
|
| 150 |
β
Fatigue Risk Analysis
|
| 151 |
+
β
Deep Learning Inference
|
| 152 |
β
Real-Time Prediction Engine
|
| 153 |
"""
|
| 154 |
|
|
|
|
| 166 |
),
|
| 167 |
|
| 168 |
outputs=[
|
| 169 |
+
|
| 170 |
gr.Textbox(
|
| 171 |
label="Prediction Result"
|
| 172 |
),
|
| 173 |
|
| 174 |
gr.Label(
|
| 175 |
+
label="Confidence Scores"
|
| 176 |
)
|
| 177 |
],
|
| 178 |
|
|
|
|
| 180 |
|
| 181 |
description=description,
|
| 182 |
|
| 183 |
+
examples=examples,
|
| 184 |
+
|
| 185 |
+
theme="soft",
|
| 186 |
+
|
| 187 |
+
allow_flagging="never"
|
| 188 |
)
|
| 189 |
|
| 190 |
# =========================================================
|