koesan commited on
Commit
9b81ce3
·
1 Parent(s): b57bc8a

Add traffic sign application file

Browse files
Files changed (7) hide show
  1. Dockerfile +33 -0
  2. app.py +207 -0
  3. image.jpg +0 -0
  4. labels.csv +44 -0
  5. requirements.txt +7 -0
  6. tabela_tespit.h5 +3 -0
  7. templates/index.html +496 -0
Dockerfile ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9-slim
2
+
3
+ WORKDIR /app
4
+
5
+ # Install system dependencies for OpenCV
6
+ RUN apt-get update && apt-get install -y \
7
+ libglib2.0-0 \
8
+ libsm6 \
9
+ libxext6 \
10
+ libxrender-dev \
11
+ libgomp1 \
12
+ libgl1 \
13
+ && rm -rf /var/lib/apt/lists/*
14
+
15
+ # Copy requirements and install Python dependencies
16
+ COPY requirements.txt .
17
+ RUN pip install --no-cache-dir -r requirements.txt
18
+
19
+ # Copy application files
20
+ COPY app.py .
21
+ COPY tabela_tespit.h5 .
22
+ COPY labels.csv .
23
+ COPY templates/ templates/
24
+ COPY image.jpg .
25
+
26
+ # Create uploads directory
27
+ RUN mkdir -p uploads && chmod 777 uploads
28
+
29
+ # Expose port
30
+ EXPOSE 7860
31
+
32
+ # Run the application
33
+ CMD ["python", "app.py"]
app.py ADDED
@@ -0,0 +1,207 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ import numpy as np
4
+ import pandas as pd
5
+ import base64
6
+ from flask import Flask, render_template, request, jsonify
7
+ from werkzeug.utils import secure_filename
8
+ from io import BytesIO
9
+ from PIL import Image
10
+ import tensorflow as tf
11
+ from tensorflow.keras.models import load_model
12
+
13
+ # Suppress warnings
14
+ os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
15
+ os.environ['MPLCONFIGDIR'] = '/tmp/matplotlib'
16
+
17
+ app = Flask(__name__)
18
+ app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max
19
+ app.config['UPLOAD_FOLDER'] = 'uploads'
20
+ app.config['ALLOWED_EXTENSIONS'] = {'png', 'jpg', 'jpeg'}
21
+
22
+ os.makedirs(app.config['UPLOAD_FOLDER'], mode=0o777, exist_ok=True)
23
+
24
+ # Load model
25
+ print("Loading traffic sign classification model...")
26
+ model = load_model('tabela_tespit.h5', compile=False)
27
+ model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
28
+ print("✓ Model loaded successfully!")
29
+
30
+ # Load labels
31
+ labels_df = pd.read_csv('labels.csv')
32
+ print(f"✓ Loaded {len(labels_df)} traffic sign classes")
33
+
34
+ def allowed_file(filename):
35
+ return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
36
+
37
+ def preprocess_image(image_path):
38
+ """Preprocess image for CNN model (32x32 grayscale)"""
39
+ try:
40
+ # Read image as grayscale
41
+ img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
42
+
43
+ if img is None:
44
+ raise ValueError("Could not read image")
45
+
46
+ print(f"Original image shape: {img.shape}")
47
+
48
+ # Resize to 32x32
49
+ img_resized = cv2.resize(img, (32, 32))
50
+
51
+ # Apply histogram equalization
52
+ img_equalized = cv2.equalizeHist(img_resized)
53
+
54
+ # Normalize to [0, 1]
55
+ img_normalized = img_equalized / 255.0
56
+
57
+ # Add dimensions: (1, 32, 32, 1)
58
+ img_input = img_normalized.reshape(1, 32, 32, 1)
59
+
60
+ print(f"Model input shape: {img_input.shape}")
61
+
62
+ # For display - convert to RGB
63
+ img_display = cv2.cvtColor(img_resized, cv2.COLOR_GRAY2RGB)
64
+
65
+ return img_input, img_display
66
+
67
+ except Exception as e:
68
+ raise ValueError(f"Failed to preprocess image: {str(e)}")
69
+
70
+ def img_to_base64(img):
71
+ """Convert numpy image to base64 string"""
72
+ img_pil = Image.fromarray(img.astype('uint8'))
73
+ buf = BytesIO()
74
+ img_pil.save(buf, format='PNG')
75
+ buf.seek(0)
76
+ img_base64 = base64.b64encode(buf.getvalue()).decode('utf-8')
77
+ return f'data:image/png;base64,{img_base64}'
78
+
79
+ @app.route('/')
80
+ def index():
81
+ return render_template('index.html')
82
+
83
+ @app.route('/predict', methods=['POST'])
84
+ def predict():
85
+ try:
86
+ if 'file' not in request.files:
87
+ return jsonify({'error': 'No file uploaded'}), 400
88
+
89
+ file = request.files['file']
90
+
91
+ if file.filename == '':
92
+ return jsonify({'error': 'No file selected'}), 400
93
+
94
+ if not allowed_file(file.filename):
95
+ return jsonify({'error': 'Invalid file type. Please upload PNG, JPG, or JPEG'}), 400
96
+
97
+ # Save file
98
+ filename = secure_filename(file.filename)
99
+ filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
100
+ file.save(filepath)
101
+
102
+ print(f"Processing: {filename}")
103
+
104
+ # Preprocess
105
+ img_input, img_display = preprocess_image(filepath)
106
+
107
+ # Predict
108
+ print("Making prediction...")
109
+ predictions = model.predict(img_input, verbose=0)
110
+
111
+ # Get top prediction
112
+ class_id = np.argmax(predictions)
113
+ confidence = np.max(predictions)
114
+ class_name = labels_df.loc[class_id, 'Name']
115
+
116
+ # Get top 5 predictions
117
+ top5_indices = np.argsort(predictions[0])[-5:][::-1]
118
+ top5_predictions = []
119
+ for idx in top5_indices:
120
+ top5_predictions.append({
121
+ 'class_id': int(idx),
122
+ 'class_name': labels_df.loc[idx, 'Name'],
123
+ 'confidence': float(predictions[0][idx])
124
+ })
125
+
126
+ # Convert image to base64
127
+ img_base64 = img_to_base64(img_display)
128
+
129
+ # Clean up
130
+ os.remove(filepath)
131
+
132
+ result = {
133
+ 'predicted_class': class_name,
134
+ 'class_id': int(class_id),
135
+ 'confidence': float(confidence),
136
+ 'top5_predictions': top5_predictions,
137
+ 'image': img_base64
138
+ }
139
+
140
+ print(f"✓ Prediction: {class_name} (Confidence: {confidence:.2%})")
141
+
142
+ return jsonify(result)
143
+
144
+ except Exception as e:
145
+ print(f"Error during prediction: {e}")
146
+ import traceback
147
+ traceback.print_exc()
148
+ if os.path.exists(filepath):
149
+ os.remove(filepath)
150
+ return jsonify({'error': str(e)}), 500
151
+
152
+ @app.route('/test-example', methods=['POST'])
153
+ def test_example():
154
+ """Test with example image"""
155
+ try:
156
+ example_path = 'image.jpg'
157
+
158
+ if not os.path.exists(example_path):
159
+ return jsonify({'error': 'Example image not found'}), 404
160
+
161
+ print(f"Testing with example: {example_path}")
162
+
163
+ # Preprocess
164
+ img_input, img_display = preprocess_image(example_path)
165
+
166
+ # Predict
167
+ print("Making prediction on example...")
168
+ predictions = model.predict(img_input, verbose=0)
169
+
170
+ # Get top prediction
171
+ class_id = np.argmax(predictions)
172
+ confidence = np.max(predictions)
173
+ class_name = labels_df.loc[class_id, 'Name']
174
+
175
+ # Get top 5 predictions
176
+ top5_indices = np.argsort(predictions[0])[-5:][::-1]
177
+ top5_predictions = []
178
+ for idx in top5_indices:
179
+ top5_predictions.append({
180
+ 'class_id': int(idx),
181
+ 'class_name': labels_df.loc[idx, 'Name'],
182
+ 'confidence': float(predictions[0][idx])
183
+ })
184
+
185
+ # Convert image to base64
186
+ img_base64 = img_to_base64(img_display)
187
+
188
+ result = {
189
+ 'predicted_class': class_name,
190
+ 'class_id': int(class_id),
191
+ 'confidence': float(confidence),
192
+ 'top5_predictions': top5_predictions,
193
+ 'image': img_base64
194
+ }
195
+
196
+ print(f"✓ Example prediction: {class_name} (Confidence: {confidence:.2%})")
197
+
198
+ return jsonify(result)
199
+
200
+ except Exception as e:
201
+ print(f"Error during example prediction: {e}")
202
+ import traceback
203
+ traceback.print_exc()
204
+ return jsonify({'error': str(e)}), 500
205
+
206
+ if __name__ == '__main__':
207
+ app.run(host='0.0.0.0', port=7860, debug=False)
image.jpg ADDED
labels.csv ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ClassId,Name
2
+ 0,Speed limit (20km/h)
3
+ 1,Speed limit (30km/h)
4
+ 2,Speed limit (50km/h)
5
+ 3,Speed limit (60km/h)
6
+ 4,Speed limit (70km/h)
7
+ 5,Speed limit (80km/h)
8
+ 6,End of speed limit (80km/h)
9
+ 7,Speed limit (100km/h)
10
+ 8,Speed limit (120km/h)
11
+ 9,No passing
12
+ 10,No passing veh over 3.5 tons
13
+ 11,Right-of-way at intersection
14
+ 12,Priority road
15
+ 13,Yield
16
+ 14,Stop
17
+ 15,No vehicles
18
+ 16,Veh > 3.5 tons prohibited
19
+ 17,No entry
20
+ 18,General caution
21
+ 19,Dangerous curve left
22
+ 20,Dangerous curve right
23
+ 21,Double curve
24
+ 22,Bumpy road
25
+ 23,Slippery road
26
+ 24,Road narrows on the right
27
+ 25,Road work
28
+ 26,Traffic signals
29
+ 27,Pedestrians
30
+ 28,Children crossing
31
+ 29,Bicycles crossing
32
+ 30,Beware of ice/snow
33
+ 31,Wild animals crossing
34
+ 32,End speed + passing limits
35
+ 33,Turn right ahead
36
+ 34,Turn left ahead
37
+ 35,Ahead only
38
+ 36,Go straight or right
39
+ 37,Go straight or left
40
+ 38,Keep right
41
+ 39,Keep left
42
+ 40,Roundabout mandatory
43
+ 41,End of no passing
44
+ 42,End no passing veh > 3.5 tons
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ flask==2.3.0
2
+ tensorflow==2.13.0
3
+ opencv-python-headless==4.8.0.74
4
+ numpy==1.24.3
5
+ pandas==2.0.3
6
+ pillow==10.0.0
7
+ werkzeug==2.3.0
tabela_tespit.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:20b34afbb88b23645c1bfe9060dbb48a04fe8729a16773663cd09a291f249704
3
+ size 3212592
templates/index.html ADDED
@@ -0,0 +1,496 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Traffic Sign Classification</title>
7
+ <style>
8
+ * {
9
+ margin: 0;
10
+ padding: 0;
11
+ box-sizing: border-box;
12
+ }
13
+
14
+ body {
15
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
16
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
17
+ min-height: 100vh;
18
+ padding: 20px;
19
+ }
20
+
21
+ .container {
22
+ max-width: 1200px;
23
+ margin: 0 auto;
24
+ background: white;
25
+ border-radius: 20px;
26
+ box-shadow: 0 20px 60px rgba(0,0,0,0.3);
27
+ overflow: hidden;
28
+ }
29
+
30
+ .header {
31
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
32
+ color: white;
33
+ padding: 40px;
34
+ text-align: center;
35
+ }
36
+
37
+ .header h1 {
38
+ font-size: 2.5em;
39
+ margin-bottom: 10px;
40
+ }
41
+
42
+ .header p {
43
+ font-size: 1.2em;
44
+ opacity: 0.9;
45
+ margin-bottom: 20px;
46
+ }
47
+
48
+ .github-link {
49
+ display: inline-flex;
50
+ align-items: center;
51
+ gap: 8px;
52
+ color: white;
53
+ text-decoration: none;
54
+ font-weight: 600;
55
+ font-size: 1.1em;
56
+ padding: 12px 24px;
57
+ background: rgba(255, 255, 255, 0.2);
58
+ border: 2px solid white;
59
+ border-radius: 10px;
60
+ transition: all 0.3s;
61
+ }
62
+
63
+ .github-link:hover {
64
+ background: white;
65
+ color: #667eea;
66
+ transform: translateY(-2px);
67
+ box-shadow: 0 5px 15px rgba(0,0,0,0.2);
68
+ }
69
+
70
+ .content {
71
+ padding: 40px;
72
+ }
73
+
74
+ .info-section {
75
+ background: #f8f9fa;
76
+ border-radius: 15px;
77
+ padding: 30px;
78
+ margin-bottom: 30px;
79
+ border-left: 5px solid #667eea;
80
+ }
81
+
82
+ .info-section h2 {
83
+ color: #667eea;
84
+ margin-bottom: 15px;
85
+ font-size: 1.8em;
86
+ }
87
+
88
+ .info-section p {
89
+ color: #495057;
90
+ line-height: 1.8;
91
+ font-size: 1.05em;
92
+ }
93
+
94
+ .upload-section {
95
+ background: #f8f9fa;
96
+ border-radius: 15px;
97
+ padding: 40px;
98
+ text-align: center;
99
+ margin-bottom: 30px;
100
+ border: 3px dashed #667eea;
101
+ transition: all 0.3s;
102
+ }
103
+
104
+ .upload-section:hover {
105
+ border-color: #764ba2;
106
+ background: #f0f2ff;
107
+ }
108
+
109
+ .upload-icon {
110
+ font-size: 4em;
111
+ color: #667eea;
112
+ margin-bottom: 20px;
113
+ }
114
+
115
+ .file-input {
116
+ display: none;
117
+ }
118
+
119
+ .upload-button {
120
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
121
+ color: white;
122
+ padding: 15px 40px;
123
+ border: none;
124
+ border-radius: 30px;
125
+ font-size: 1.2em;
126
+ font-weight: bold;
127
+ cursor: pointer;
128
+ transition: transform 0.2s;
129
+ margin: 10px;
130
+ }
131
+
132
+ .upload-button:hover {
133
+ transform: scale(1.05);
134
+ }
135
+
136
+ .test-button {
137
+ background: #28a745;
138
+ color: white;
139
+ padding: 15px 40px;
140
+ border: none;
141
+ border-radius: 30px;
142
+ font-size: 1.2em;
143
+ font-weight: bold;
144
+ cursor: pointer;
145
+ transition: transform 0.2s;
146
+ margin: 10px;
147
+ }
148
+
149
+ .test-button:hover {
150
+ background: #218838;
151
+ transform: scale(1.05);
152
+ }
153
+
154
+ .results-section {
155
+ display: none;
156
+ }
157
+
158
+ .result-card {
159
+ background: white;
160
+ border-radius: 15px;
161
+ padding: 30px;
162
+ box-shadow: 0 5px 20px rgba(0,0,0,0.1);
163
+ margin-bottom: 30px;
164
+ }
165
+
166
+ .result-header {
167
+ text-align: center;
168
+ margin-bottom: 30px;
169
+ }
170
+
171
+ .result-image {
172
+ max-width: 200px;
173
+ border-radius: 10px;
174
+ box-shadow: 0 5px 15px rgba(0,0,0,0.2);
175
+ margin: 0 auto 20px;
176
+ display: block;
177
+ }
178
+
179
+ .prediction-main {
180
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
181
+ color: white;
182
+ padding: 30px;
183
+ border-radius: 15px;
184
+ text-align: center;
185
+ margin-bottom: 30px;
186
+ }
187
+
188
+ .prediction-main h2 {
189
+ font-size: 2em;
190
+ margin-bottom: 10px;
191
+ }
192
+
193
+ .confidence {
194
+ font-size: 3em;
195
+ font-weight: bold;
196
+ margin: 20px 0;
197
+ }
198
+
199
+ .confidence-bar {
200
+ background: rgba(255,255,255,0.3);
201
+ height: 30px;
202
+ border-radius: 15px;
203
+ overflow: hidden;
204
+ margin-top: 20px;
205
+ }
206
+
207
+ .confidence-fill {
208
+ background: white;
209
+ height: 100%;
210
+ transition: width 0.5s;
211
+ }
212
+
213
+ .top5-predictions {
214
+ margin-top: 30px;
215
+ }
216
+
217
+ .top5-predictions h3 {
218
+ color: #667eea;
219
+ margin-bottom: 20px;
220
+ font-size: 1.5em;
221
+ }
222
+
223
+ .prediction-item {
224
+ display: flex;
225
+ justify-content: space-between;
226
+ align-items: center;
227
+ padding: 15px;
228
+ background: #f8f9fa;
229
+ border-radius: 10px;
230
+ margin-bottom: 10px;
231
+ transition: transform 0.2s;
232
+ }
233
+
234
+ .prediction-item:hover {
235
+ transform: translateX(5px);
236
+ background: #e9ecef;
237
+ }
238
+
239
+ .prediction-name {
240
+ font-weight: 600;
241
+ color: #495057;
242
+ }
243
+
244
+ .prediction-confidence {
245
+ font-size: 1.2em;
246
+ font-weight: bold;
247
+ color: #667eea;
248
+ }
249
+
250
+ .loading {
251
+ display: none;
252
+ text-align: center;
253
+ padding: 40px;
254
+ }
255
+
256
+ .spinner {
257
+ border: 5px solid #f3f3f3;
258
+ border-top: 5px solid #667eea;
259
+ border-radius: 50%;
260
+ width: 60px;
261
+ height: 60px;
262
+ animation: spin 1s linear infinite;
263
+ margin: 0 auto 20px;
264
+ }
265
+
266
+ @keyframes spin {
267
+ 0% { transform: rotate(0deg); }
268
+ 100% { transform: rotate(360deg); }
269
+ }
270
+
271
+ .error {
272
+ display: none;
273
+ background: #f8d7da;
274
+ color: #721c24;
275
+ padding: 20px;
276
+ border-radius: 10px;
277
+ border-left: 5px solid #dc3545;
278
+ margin-bottom: 20px;
279
+ }
280
+
281
+ .reset-button {
282
+ background: #6c757d;
283
+ color: white;
284
+ padding: 12px 30px;
285
+ border: none;
286
+ border-radius: 25px;
287
+ font-size: 1.1em;
288
+ cursor: pointer;
289
+ margin-top: 20px;
290
+ transition: transform 0.2s;
291
+ }
292
+
293
+ .reset-button:hover {
294
+ background: #5a6268;
295
+ transform: scale(1.05);
296
+ }
297
+ </style>
298
+ </head>
299
+ <body>
300
+ <div class="container">
301
+ <div class="header">
302
+ <h1>🚦 Traffic Sign Classification</h1>
303
+ <p>AI-Powered Recognition of 43 Traffic Sign Categories</p>
304
+ <a href="https://github.com/koesan/Traffic_Sign_Classification" target="_blank" class="github-link">
305
+ <svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
306
+ <path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z"/>
307
+ </svg>
308
+ View on GitHub
309
+ </a>
310
+ </div>
311
+
312
+ <div class="content">
313
+ <!-- Info Section -->
314
+ <div class="info-section">
315
+ <h2>📖 About</h2>
316
+ <p style="font-size: 1.1em; line-height: 1.8;">
317
+ <strong>CNN-based traffic sign recognition</strong> system with 99.27% accuracy across 43 sign categories.
318
+ Upload an image and the AI model will identify the traffic sign type with confidence scores.
319
+ </p>
320
+ <p style="font-size: 1.05em; color: #6c757d; margin-top: 15px;">
321
+ <strong>Usage:</strong> Click "Choose Image" or "Test Example" → View classification results
322
+ </p>
323
+ </div>
324
+
325
+ <!-- Error Message -->
326
+ <div class="error" id="errorMessage"></div>
327
+
328
+ <!-- Upload Section -->
329
+ <div class="upload-section" id="uploadSection">
330
+ <div class="upload-icon">📤</div>
331
+ <h2 style="color: #667eea; margin-bottom: 15px;">Upload Traffic Sign Image</h2>
332
+ <p style="color: #6c757d; margin-bottom: 20px;">
333
+ Supported formats: PNG, JPG, JPEG<br>
334
+ Images will be automatically resized to 32×32 pixels
335
+ </p>
336
+ <input type="file" id="fileInput" class="file-input" accept="image/*">
337
+ <button class="upload-button" onclick="document.getElementById('fileInput').click()">
338
+ Choose Image
339
+ </button>
340
+ <button class="test-button" onclick="testExample()">
341
+ 🧪 Test Example
342
+ </button>
343
+ </div>
344
+
345
+ <!-- Loading -->
346
+ <div class="loading" id="loading">
347
+ <div class="spinner"></div>
348
+ <p style="color: #667eea; font-size: 1.2em;">Analyzing traffic sign...</p>
349
+ </div>
350
+
351
+ <!-- Results -->
352
+ <div class="results-section" id="resultsSection">
353
+ <div class="result-card">
354
+ <div class="result-header">
355
+ <img id="resultImage" class="result-image" alt="Traffic Sign">
356
+ </div>
357
+
358
+ <div class="prediction-main">
359
+ <h2>🚦 Predicted Sign</h2>
360
+ <h1 id="predictedClass" style="margin: 20px 0; font-size: 2.5em;"></h1>
361
+ <div class="confidence" id="confidenceValue"></div>
362
+ <div class="confidence-bar">
363
+ <div class="confidence-fill" id="confidenceFill"></div>
364
+ </div>
365
+ </div>
366
+
367
+ <div class="top5-predictions">
368
+ <h3>📊 Top 5 Predictions</h3>
369
+ <div id="top5List"></div>
370
+ </div>
371
+
372
+ <div style="text-align: center;">
373
+ <button class="reset-button" onclick="reset()">
374
+ 🔄 Classify Another Image
375
+ </button>
376
+ </div>
377
+ </div>
378
+ </div>
379
+ </div>
380
+ </div>
381
+
382
+ <script>
383
+ document.getElementById('fileInput').addEventListener('change', function(e) {
384
+ const file = e.target.files[0];
385
+ if (file) {
386
+ analyzeImage(file);
387
+ }
388
+ });
389
+
390
+ async function analyzeImage(file) {
391
+ if (!file.type.startsWith('image/')) {
392
+ showError('Please upload a valid image file (PNG, JPG, JPEG)');
393
+ return;
394
+ }
395
+
396
+ // Show loading
397
+ document.getElementById('loading').style.display = 'block';
398
+ document.getElementById('uploadSection').style.display = 'none';
399
+ document.getElementById('resultsSection').style.display = 'none';
400
+ document.getElementById('errorMessage').style.display = 'none';
401
+
402
+ const formData = new FormData();
403
+ formData.append('file', file);
404
+
405
+ try {
406
+ const response = await fetch('/predict', {
407
+ method: 'POST',
408
+ body: formData
409
+ });
410
+
411
+ const data = await response.json();
412
+
413
+ document.getElementById('loading').style.display = 'none';
414
+
415
+ if (data.error) {
416
+ showError(data.error);
417
+ } else {
418
+ showResults(data);
419
+ }
420
+ } catch (error) {
421
+ document.getElementById('loading').style.display = 'none';
422
+ showError('An error occurred: ' + error.message);
423
+ }
424
+ }
425
+
426
+ async function testExample() {
427
+ // Show loading
428
+ document.getElementById('loading').style.display = 'block';
429
+ document.getElementById('uploadSection').style.display = 'none';
430
+ document.getElementById('resultsSection').style.display = 'none';
431
+ document.getElementById('errorMessage').style.display = 'none';
432
+
433
+ try {
434
+ const response = await fetch('/test-example', {
435
+ method: 'POST'
436
+ });
437
+
438
+ const data = await response.json();
439
+
440
+ document.getElementById('loading').style.display = 'none';
441
+
442
+ if (data.error) {
443
+ showError(data.error);
444
+ } else {
445
+ showResults(data);
446
+ }
447
+ } catch (error) {
448
+ document.getElementById('loading').style.display = 'none';
449
+ showError('An error occurred: ' + error.message);
450
+ }
451
+ }
452
+
453
+ function showResults(data) {
454
+ document.getElementById('resultImage').src = data.image;
455
+ document.getElementById('predictedClass').textContent = data.predicted_class;
456
+ document.getElementById('confidenceValue').textContent = (data.confidence * 100).toFixed(1) + '%';
457
+ document.getElementById('confidenceFill').style.width = (data.confidence * 100) + '%';
458
+
459
+ // Show top 5 predictions
460
+ const top5List = document.getElementById('top5List');
461
+ top5List.innerHTML = '';
462
+
463
+ data.top5_predictions.forEach((pred, index) => {
464
+ const item = document.createElement('div');
465
+ item.className = 'prediction-item';
466
+ item.innerHTML = `
467
+ <span class="prediction-name">
468
+ <strong>${index + 1}.</strong> ${pred.class_name}
469
+ </span>
470
+ <span class="prediction-confidence">
471
+ ${(pred.confidence * 100).toFixed(1)}%
472
+ </span>
473
+ `;
474
+ top5List.appendChild(item);
475
+ });
476
+
477
+ document.getElementById('resultsSection').style.display = 'block';
478
+ }
479
+
480
+ function showError(message) {
481
+ const errorElement = document.getElementById('errorMessage');
482
+ errorElement.textContent = '❌ Error: ' + message;
483
+ errorElement.style.display = 'block';
484
+ document.getElementById('uploadSection').style.display = 'block';
485
+ }
486
+
487
+ function reset() {
488
+ document.getElementById('fileInput').value = '';
489
+ document.getElementById('uploadSection').style.display = 'block';
490
+ document.getElementById('resultsSection').style.display = 'none';
491
+ document.getElementById('errorMessage').style.display = 'none';
492
+ document.getElementById('loading').style.display = 'none';
493
+ }
494
+ </script>
495
+ </body>
496
+ </html>