asrcoddeploy commited on
Commit
37b6eff
Β·
verified Β·
1 Parent(s): 6151085

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +164 -0
app.py ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # =========================================================
2
+ # HUGGINGFACE DRIVER STATE DETECTION APP
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
17
+ import numpy as np
18
+ import cv2
19
+
20
+ from tensorflow.keras.models import load_model
21
+ from tensorflow.keras.preprocessing.image import img_to_array
22
+
23
+ # =========================================================
24
+ # LOAD MODEL
25
+ # =========================================================
26
+
27
+ model = load_model("final_driver_state_model.h5")
28
+
29
+ CLASS_NAMES = [
30
+ "alert",
31
+ "sleepy",
32
+ "slowBlink",
33
+ "yawning"
34
+ ]
35
+
36
+ # =========================================================
37
+ # RISK MAPPING
38
+ # =========================================================
39
+
40
+ RISK_LEVELS = {
41
+ "alert": "SAFE",
42
+ "sleepy": "HIGH RISK",
43
+ "slowBlink": "MEDIUM RISK",
44
+ "yawning": "LOW RISK"
45
+ }
46
+
47
+ # =========================================================
48
+ # PREDICTION FUNCTION
49
+ # =========================================================
50
+
51
+ def predict_driver_state(image):
52
+
53
+ # =============================================
54
+ # IMAGE PREPROCESSING
55
+ # =============================================
56
+
57
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
58
+
59
+ image = cv2.resize(image, (224,224))
60
+
61
+ image = image.astype("float32") / 255.0
62
+
63
+ image = img_to_array(image)
64
+
65
+ image = np.expand_dims(image, axis=0)
66
+
67
+ # =============================================
68
+ # MODEL PREDICTION
69
+ # =============================================
70
+
71
+ prediction = model.predict(image, verbose=0)
72
+
73
+ class_index = np.argmax(prediction)
74
+
75
+ predicted_class = CLASS_NAMES[class_index]
76
+
77
+ confidence = float(np.max(prediction))
78
+
79
+ risk_level = RISK_LEVELS[predicted_class]
80
+
81
+ # =============================================
82
+ # ALL CLASS PROBABILITIES
83
+ # =============================================
84
+
85
+ confidence_scores = {}
86
+
87
+ for i, class_name in enumerate(CLASS_NAMES):
88
+
89
+ confidence_scores[class_name] = float(
90
+ prediction[0][i]
91
+ )
92
+
93
+ # =============================================
94
+ # RESULT TEXT
95
+ # =============================================
96
+
97
+ result = f"""
98
+ πŸš— DRIVER STATE ANALYSIS
99
+
100
+ Prediction: {predicted_class}
101
+
102
+ Confidence: {confidence:.2f}
103
+
104
+ Risk Level: {risk_level}
105
+ """
106
+
107
+ return result, confidence_scores
108
+
109
+ # =========================================================
110
+ # GRADIO UI
111
+ # =========================================================
112
+
113
+ title = "πŸš— AI Driver Safety Detection System"
114
+
115
+ description = """
116
+ Upload a driver image to detect fatigue state using Deep Learning.
117
+
118
+ ### Supported Driver States
119
+ - Alert
120
+ - Sleepy
121
+ - Slow Blink
122
+ - Yawning
123
+
124
+ ### AI Features
125
+ βœ… CNN-Based Classification
126
+ βœ… Fatigue Risk Analysis
127
+ βœ… Real-Time Prediction Engine
128
+ """
129
+
130
+ # =========================================================
131
+ # INTERFACE
132
+ # =========================================================
133
+
134
+ interface = gr.Interface(
135
+
136
+ fn=predict_driver_state,
137
+
138
+ inputs=gr.Image(
139
+ type="numpy",
140
+ label="Upload Driver Image"
141
+ ),
142
+
143
+ outputs=[
144
+ gr.Textbox(
145
+ label="Prediction Result"
146
+ ),
147
+
148
+ gr.Label(
149
+ label="Class Confidence Scores"
150
+ )
151
+ ],
152
+
153
+ title=title,
154
+
155
+ description=description,
156
+
157
+ theme="soft"
158
+ )
159
+
160
+ # =========================================================
161
+ # LAUNCH
162
+ # =========================================================
163
+
164
+ interface.launch()