File size: 4,748 Bytes
37b6eff
3360661
 
37b6eff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3360661
fa59a09
 
 
3360661
 
37b6eff
 
 
 
 
 
 
 
3360661
37b6eff
 
 
 
 
 
 
 
 
3360661
fa59a09
3360661
 
 
 
 
 
 
 
 
37b6eff
fa59a09
 
 
37b6eff
 
fa59a09
37b6eff
fa59a09
 
 
 
3360661
 
37b6eff
 
 
 
 
 
 
fa59a09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3360661
 
 
37b6eff
fa59a09
 
 
 
37b6eff
fa59a09
 
 
 
 
37b6eff
 
 
 
 
 
 
3360661
 
 
 
 
37b6eff
 
 
 
 
 
 
 
 
3360661
37b6eff
3360661
37b6eff
 
 
 
3360661
 
37b6eff
3360661
 
37b6eff
3360661
 
37b6eff
 
 
 
 
fa59a09
37b6eff
 
 
 
 
3360661
37b6eff
3360661
 
 
 
 
37b6eff
3360661
 
37b6eff
3360661
37b6eff
 
 
 
fa59a09
37b6eff
 
 
 
 
 
 
 
 
 
 
 
3360661
37b6eff
 
 
 
 
3360661
37b6eff
 
 
 
 
fa59a09
37b6eff
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# =========================================================
# AI DRIVER SAFETY DETECTION SYSTEM
# HuggingFace Gradio App
# =========================================================

import gradio as gr
import numpy as np
import cv2

from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array

# =========================================================
# LOAD MODEL
# =========================================================

model = load_model("final_driver_state_model.h5")

# =========================================================
# CLASS LABELS
# IMPORTANT:
# Must match training class order exactly
# =========================================================

CLASS_NAMES = [
    "alert",
    "sleepy",
    "slowBlink",
    "yawning"
]

# =========================================================
# RISK LEVELS
# =========================================================

RISK_LEVELS = {
    "alert": "SAFE",
    "sleepy": "HIGH RISK",
    "slowBlink": "MEDIUM RISK",
    "yawning": "LOW RISK"
}

# =========================================================
# EMOJIS
# =========================================================

RISK_EMOJIS = {
    "SAFE": "🟒",
    "LOW RISK": "🟑",
    "MEDIUM RISK": "🟠",
    "HIGH RISK": "πŸ”΄"
}

# =========================================================
# IMAGE PREPROCESSING
# IMPORTANT:
# Match training preprocessing
# =========================================================

def preprocess_image(image):

    # -----------------------------------------------------
    # Gradio already provides RGB image
    # DO NOT use cvtColor
    # -----------------------------------------------------

    image = cv2.resize(image, (224, 224))

    image = image.astype("float32") / 255.0

    image = img_to_array(image)

    image = np.expand_dims(image, axis=0)

    return image

# =========================================================
# PREDICTION FUNCTION
# =========================================================

def predict_driver_state(image):

    if image is None:

        return (
            "Please upload an image.",
            {}
        )

    # =====================================================
    # PREPROCESS
    # =====================================================

    processed_image = preprocess_image(image)

    # =====================================================
    # PREDICTION
    # =====================================================

    prediction = model.predict(
        processed_image,
        verbose=0
    )

    # =====================================================
    # RESULTS
    # =====================================================

    class_index = int(np.argmax(prediction))

    predicted_class = CLASS_NAMES[class_index]

    confidence = float(np.max(prediction))

    risk_level = RISK_LEVELS[predicted_class]

    emoji = RISK_EMOJIS[risk_level]

    # =====================================================
    # CONFIDENCE SCORES
    # =====================================================

    confidence_scores = {}

    for i, class_name in enumerate(CLASS_NAMES):

        confidence_scores[class_name] = float(
            prediction[0][i]
        )

    # =====================================================
    # RESULT TEXT
    # =====================================================

    result = f"""
πŸš— DRIVER STATE ANALYSIS

Prediction:
{predicted_class.upper()}

Confidence:
{confidence:.2f}

Risk Level:
{emoji} {risk_level}
"""

    return result, confidence_scores

# =========================================================
# TITLE & DESCRIPTION
# =========================================================

title = "πŸš— AI Driver Safety Detection System"

description = """
Upload a driver image to analyze fatigue and attention state using Deep Learning.

## Supported Driver States
- 🟒 Alert
- πŸ”΄ Sleepy
- 🟠 Slow Blink
- 🟑 Yawning

## AI Features
βœ… CNN-Based Driver State Classification  
βœ… Fatigue Risk Analysis  
βœ… Deep Learning Inference  
βœ… Real-Time Prediction Engine  
"""

# =========================================================
# GRADIO INTERFACE
# =========================================================

interface = gr.Interface(

    fn=predict_driver_state,

    inputs=gr.Image(
        type="numpy",
        label="Upload Driver Image"
    ),

    outputs=[

        gr.Textbox(
            label="Prediction Result"
        ),

        gr.Label(
            label="Confidence Scores"
        )
    ],

    title=title,

    description=description
)

# =========================================================
# LAUNCH
# =========================================================

interface.launch()