Update app.py
Browse files
app.py
CHANGED
|
@@ -1,364 +1,427 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
import json
|
| 6 |
import os
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
def top_5_accuracy(y_true, y_pred):
|
| 13 |
-
return tf.keras.metrics.top_k_categorical_accuracy(y_true, y_pred, k=5)
|
| 14 |
-
|
| 15 |
-
# Register custom functions with Keras
|
| 16 |
-
@tf.keras.utils.register_keras_serializable()
|
| 17 |
-
def top_3_accuracy_serializable(y_true, y_pred):
|
| 18 |
-
return tf.keras.metrics.top_k_categorical_accuracy(y_true, y_pred, k=3)
|
| 19 |
-
|
| 20 |
-
@tf.keras.utils.register_keras_serializable()
|
| 21 |
-
def top_5_accuracy_serializable(y_true, y_pred):
|
| 22 |
-
return tf.keras.metrics.top_k_categorical_accuracy(y_true, y_pred, k=5)
|
| 23 |
-
|
| 24 |
-
# Custom objects dictionary
|
| 25 |
-
custom_objects = {
|
| 26 |
-
'top_3_accuracy': top_3_accuracy,
|
| 27 |
-
'top_5_accuracy': top_5_accuracy,
|
| 28 |
-
'top_3_accuracy_serializable': top_3_accuracy_serializable,
|
| 29 |
-
'top_5_accuracy_serializable': top_5_accuracy_serializable
|
| 30 |
-
}
|
| 31 |
|
| 32 |
# Page configuration
|
| 33 |
st.set_page_config(
|
| 34 |
-
page_title="
|
| 35 |
page_icon="๐",
|
| 36 |
layout="wide",
|
| 37 |
initial_sidebar_state="expanded"
|
| 38 |
)
|
| 39 |
|
| 40 |
-
# Custom CSS for
|
| 41 |
st.markdown("""
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
| 45 |
}
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
padding: 2rem;
|
| 49 |
-
|
| 50 |
-
color: white;
|
| 51 |
-
border-radius: 10px;
|
| 52 |
margin-bottom: 2rem;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
}
|
|
|
|
|
|
|
| 54 |
.prediction-card {
|
| 55 |
-
background
|
|
|
|
| 56 |
padding: 1.5rem;
|
| 57 |
-
border-radius: 10px;
|
| 58 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 59 |
margin: 1rem 0;
|
|
|
|
|
|
|
| 60 |
}
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
background: linear-gradient(135deg, #006400 0%, #008000 100%);
|
| 63 |
color: white;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
padding: 1rem;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
border-radius: 10px;
|
| 66 |
-
|
| 67 |
}
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
border
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
}
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
margin: 0.5rem 0;
|
| 80 |
-
border-left: 5px solid #8bc34a;
|
| 81 |
}
|
| 82 |
-
|
|
|
|
|
|
|
| 83 |
text-align: center;
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
margin: 1rem 0;
|
| 86 |
}
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
""", unsafe_allow_html=True)
|
| 89 |
|
| 90 |
-
#
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
# Paths for model files
|
| 94 |
-
MODEL_PATH = "dish_classifier_final.keras"
|
| 95 |
-
CLASSES_PATH = "class_names.json"
|
| 96 |
-
|
| 97 |
@st.cache_resource
|
| 98 |
def load_model():
|
| 99 |
-
"""Load the trained model
|
| 100 |
-
|
| 101 |
-
# Check if model exists
|
| 102 |
-
if not os.path.exists(MODEL_PATH):
|
| 103 |
-
st.error(f"""
|
| 104 |
-
โ Model file not found: `{MODEL_PATH}`
|
| 105 |
-
|
| 106 |
-
**Please upload the model file:**
|
| 107 |
-
1. Click on **"Files"** tab above
|
| 108 |
-
2. Click **"Add file"** โ **"Upload file"**
|
| 109 |
-
3. Upload `dish_classifier_final.keras`
|
| 110 |
-
4. Upload `class_names.json`
|
| 111 |
-
5. Refresh this page
|
| 112 |
-
|
| 113 |
-
**Note:** The model file should be around 80-100 MB.
|
| 114 |
-
""")
|
| 115 |
-
return None
|
| 116 |
-
|
| 117 |
try:
|
| 118 |
-
|
| 119 |
-
model = tf.keras.models.load_model(
|
| 120 |
-
MODEL_PATH,
|
| 121 |
-
custom_objects=custom_objects,
|
| 122 |
-
compile=False
|
| 123 |
-
)
|
| 124 |
-
|
| 125 |
-
# Recompile the model with the same metrics
|
| 126 |
-
model.compile(
|
| 127 |
-
optimizer='adam',
|
| 128 |
-
loss='categorical_crossentropy',
|
| 129 |
-
metrics=['accuracy', top_3_accuracy, top_5_accuracy]
|
| 130 |
-
)
|
| 131 |
-
|
| 132 |
-
st.success("โ
Model loaded successfully!")
|
| 133 |
return model
|
| 134 |
-
except
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
@st.cache_data
|
| 140 |
def load_class_names():
|
| 141 |
-
"""Load class names
|
| 142 |
-
if not os.path.exists(CLASSES_PATH):
|
| 143 |
-
st.error(f"โ Class names file not found: `{CLASSES_PATH}`")
|
| 144 |
-
st.info("Please upload `class_names.json` file")
|
| 145 |
-
return None
|
| 146 |
-
|
| 147 |
try:
|
| 148 |
-
with open(
|
| 149 |
class_names = json.load(f)
|
| 150 |
-
st.success(f"โ
Loaded {len(class_names)} Pakistani food classes!")
|
| 151 |
return class_names
|
| 152 |
-
except
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
-
def preprocess_image(image):
|
| 157 |
"""Preprocess image for model prediction"""
|
| 158 |
-
# Convert to RGB if needed
|
| 159 |
if image.mode != 'RGB':
|
| 160 |
image = image.convert('RGB')
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
image = image.resize((224, 224))
|
| 164 |
-
|
| 165 |
-
# Convert to array
|
| 166 |
-
img_array = np.array(image)
|
| 167 |
-
|
| 168 |
-
# Normalize to [0,1] range
|
| 169 |
-
img_array = img_array / 255.0
|
| 170 |
-
|
| 171 |
-
# Expand dimensions to create batch
|
| 172 |
img_array = np.expand_dims(img_array, axis=0)
|
| 173 |
-
|
| 174 |
return img_array
|
| 175 |
|
| 176 |
-
def
|
| 177 |
-
"""
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
-
return
|
| 189 |
|
|
|
|
|
|
|
|
|
|
| 190 |
def main():
|
| 191 |
-
# Header
|
| 192 |
st.markdown("""
|
| 193 |
-
<div class="
|
| 194 |
-
<
|
| 195 |
-
|
| 196 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
</div>
|
| 198 |
""", unsafe_allow_html=True)
|
| 199 |
|
| 200 |
-
# Sidebar
|
| 201 |
with st.sidebar:
|
| 202 |
-
st.markdown("###
|
| 203 |
st.info("""
|
| 204 |
-
- **
|
| 205 |
-
- **Classes:** 80 Pakistani Dishes
|
| 206 |
-
- **Accuracy:**
|
| 207 |
-
- **
|
| 208 |
-
- **Top-5 Accuracy:** 84.62%
|
| 209 |
""")
|
| 210 |
|
| 211 |
-
st.markdown("
|
| 212 |
-
st.markdown(""
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
- **Daal Chawal**
|
| 221 |
-
- **Naan** (Garlic, Plain, Roghni)
|
| 222 |
-
- **Halwa Puri**
|
| 223 |
-
- **Gulab Jamun**
|
| 224 |
-
- **Kheer**
|
| 225 |
-
- **And 68 more...**
|
| 226 |
-
""")
|
| 227 |
|
| 228 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
st.markdown("""
|
| 230 |
-
1.
|
| 231 |
-
2.
|
| 232 |
-
3.
|
| 233 |
-
4.
|
| 234 |
""")
|
| 235 |
|
| 236 |
st.markdown("---")
|
| 237 |
-
st.markdown("###
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
st.markdown("**Top-3 Accuracy**")
|
| 245 |
-
st.progress(0.77)
|
| 246 |
-
st.caption("77.38%")
|
| 247 |
-
|
| 248 |
-
st.markdown("**Top-5 Accuracy**")
|
| 249 |
-
st.progress(0.85)
|
| 250 |
-
st.caption("84.62%")
|
| 251 |
|
| 252 |
st.markdown("---")
|
| 253 |
-
st.markdown("Made with โค๏ธ for
|
| 254 |
|
| 255 |
# Main content area
|
| 256 |
col1, col2 = st.columns([1, 1])
|
| 257 |
|
| 258 |
with col1:
|
| 259 |
-
st.markdown("### ๐ค Upload
|
| 260 |
uploaded_file = st.file_uploader(
|
| 261 |
"Choose an image...",
|
| 262 |
type=['jpg', 'jpeg', 'png', 'webp', 'gif'],
|
| 263 |
-
help="Upload a clear image of Pakistani
|
| 264 |
)
|
| 265 |
|
| 266 |
if uploaded_file is not None:
|
| 267 |
-
# Display uploaded image
|
| 268 |
image = Image.open(uploaded_file)
|
| 269 |
-
st.image(image, caption='๐ธ Uploaded Image', use_container_width=True)
|
| 270 |
|
| 271 |
-
#
|
| 272 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
|
| 274 |
with col2:
|
| 275 |
if uploaded_file is not None:
|
| 276 |
-
st.markdown("### ๐
|
| 277 |
|
| 278 |
-
with st.spinner(
|
| 279 |
-
# Load model and
|
| 280 |
model = load_model()
|
| 281 |
class_names = load_class_names()
|
| 282 |
|
| 283 |
if model is not None and class_names is not None:
|
| 284 |
-
#
|
| 285 |
-
|
|
|
|
| 286 |
|
| 287 |
-
#
|
| 288 |
-
|
|
|
|
|
|
|
| 289 |
|
| 290 |
-
#
|
| 291 |
st.markdown(f"""
|
| 292 |
-
<div class="
|
| 293 |
-
<
|
| 294 |
-
<h2>
|
| 295 |
-
<
|
| 296 |
</div>
|
| 297 |
""", unsafe_allow_html=True)
|
| 298 |
|
| 299 |
-
#
|
| 300 |
-
st.markdown(
|
| 301 |
-
<div class="top2">
|
| 302 |
-
<strong>๐ฅ Most Likely:</strong><br>
|
| 303 |
-
<span style="font-size: 1.1rem;">{top_5_labels[1]}</span><br>
|
| 304 |
-
<span style="color: #666;">Confidence: {top_5_probs[1]:.2f}%</span>
|
| 305 |
-
</div>
|
| 306 |
-
""", unsafe_allow_html=True)
|
| 307 |
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
|
| 317 |
-
#
|
| 318 |
-
st.markdown(
|
| 319 |
-
|
| 320 |
-
<p><strong>4๏ธโฃ Other possibilities:</strong> {top_5_labels[3]} ({top_5_probs[3]:.1f}%)</p>
|
| 321 |
-
<p><strong>5๏ธโฃ Also possible:</strong> {top_5_labels[4]} ({top_5_probs[4]:.1f}%)</p>
|
| 322 |
-
</div>
|
| 323 |
-
""", unsafe_allow_html=True)
|
| 324 |
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
# Add confidence visualization
|
| 328 |
-
st.markdown("### ๐ Confidence Level")
|
| 329 |
-
if top_5_probs[0] > 70:
|
| 330 |
-
confidence_color = "๐ข High Confidence"
|
| 331 |
-
elif top_5_probs[0] > 50:
|
| 332 |
-
confidence_color = "๐ก Medium Confidence"
|
| 333 |
-
else:
|
| 334 |
-
confidence_color = "๐ Low Confidence"
|
| 335 |
|
| 336 |
-
|
|
|
|
|
|
|
| 337 |
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
|
|
|
|
|
|
|
|
|
| 341 |
|
| 342 |
-
#
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
|
| 355 |
# Footer
|
| 356 |
-
st.markdown("---")
|
| 357 |
st.markdown("""
|
| 358 |
-
<div
|
| 359 |
-
<p>๐ต๐ฐ Celebrating
|
| 360 |
-
<p>โ ๏ธ Note:
|
| 361 |
-
|
| 362 |
</div>
|
| 363 |
""", unsafe_allow_html=True)
|
| 364 |
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Indian/Pakistani Food Classifier
|
| 3 |
+
A deep learning model to identify 80+ Indian and Pakistani dishes
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
import streamlit as st
|
| 7 |
import tensorflow as tf
|
| 8 |
import numpy as np
|
| 9 |
from PIL import Image
|
| 10 |
import json
|
| 11 |
import os
|
| 12 |
+
import plotly.graph_objects as go
|
| 13 |
+
import pandas as pd
|
| 14 |
+
from datetime import datetime
|
| 15 |
+
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Page configuration
|
| 18 |
st.set_page_config(
|
| 19 |
+
page_title="Pakistani & Indian Food Classifier",
|
| 20 |
page_icon="๐",
|
| 21 |
layout="wide",
|
| 22 |
initial_sidebar_state="expanded"
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# Custom CSS for beautiful UI
|
| 26 |
st.markdown("""
|
| 27 |
+
<style>
|
| 28 |
+
/* Main container */
|
| 29 |
+
.main {
|
| 30 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 31 |
}
|
| 32 |
+
|
| 33 |
+
/* Header styling */
|
| 34 |
+
.header-container {
|
| 35 |
+
background: linear-gradient(135deg, #006400 0%, #008000 50%, #ffffff 100%);
|
| 36 |
padding: 2rem;
|
| 37 |
+
border-radius: 20px;
|
|
|
|
|
|
|
| 38 |
margin-bottom: 2rem;
|
| 39 |
+
text-align: center;
|
| 40 |
+
box-shadow: 0 10px 30px rgba(0,0,0,0.1);
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
.header-title {
|
| 44 |
+
font-size: 3rem;
|
| 45 |
+
font-weight: bold;
|
| 46 |
+
color: white;
|
| 47 |
+
margin-bottom: 0.5rem;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
.header-subtitle {
|
| 51 |
+
font-size: 1.2rem;
|
| 52 |
+
color: rgba(255,255,255,0.9);
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
.pakistan-flag {
|
| 56 |
+
font-size: 2rem;
|
| 57 |
+
margin-bottom: 1rem;
|
| 58 |
}
|
| 59 |
+
|
| 60 |
+
/* Card styling */
|
| 61 |
.prediction-card {
|
| 62 |
+
background: white;
|
| 63 |
+
border-radius: 15px;
|
| 64 |
padding: 1.5rem;
|
|
|
|
|
|
|
| 65 |
margin: 1rem 0;
|
| 66 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
| 67 |
+
transition: transform 0.3s;
|
| 68 |
}
|
| 69 |
+
|
| 70 |
+
.prediction-card:hover {
|
| 71 |
+
transform: translateY(-5px);
|
| 72 |
+
box-shadow: 0 6px 12px rgba(0,0,0,0.15);
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
/* Top prediction styling */
|
| 76 |
+
.top-prediction {
|
| 77 |
background: linear-gradient(135deg, #006400 0%, #008000 100%);
|
| 78 |
color: white;
|
| 79 |
+
border-radius: 15px;
|
| 80 |
+
padding: 1.5rem;
|
| 81 |
+
margin: 1rem 0;
|
| 82 |
+
text-align: center;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
.top-prediction h2 {
|
| 86 |
+
font-size: 2.5rem;
|
| 87 |
+
margin: 0;
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
.confidence-score {
|
| 91 |
+
font-size: 1.2rem;
|
| 92 |
+
margin-top: 0.5rem;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
/* Other predictions */
|
| 96 |
+
.other-prediction {
|
| 97 |
+
background: #f8f9fa;
|
| 98 |
+
border-left: 5px solid #006400;
|
| 99 |
+
border-radius: 10px;
|
| 100 |
padding: 1rem;
|
| 101 |
+
margin: 0.8rem 0;
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
/* Sidebar styling */
|
| 105 |
+
.sidebar-content {
|
| 106 |
+
background: #f0f2f6;
|
| 107 |
border-radius: 10px;
|
| 108 |
+
padding: 1rem;
|
| 109 |
}
|
| 110 |
+
|
| 111 |
+
/* Button styling */
|
| 112 |
+
.stButton > button {
|
| 113 |
+
background: linear-gradient(135deg, #006400 0%, #008000 100%);
|
| 114 |
+
color: white;
|
| 115 |
+
border: none;
|
| 116 |
+
padding: 0.5rem 2rem;
|
| 117 |
+
border-radius: 25px;
|
| 118 |
+
font-weight: bold;
|
| 119 |
+
transition: all 0.3s;
|
| 120 |
}
|
| 121 |
+
|
| 122 |
+
.stButton > button:hover {
|
| 123 |
+
transform: scale(1.05);
|
| 124 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
|
|
|
|
|
|
|
| 125 |
}
|
| 126 |
+
|
| 127 |
+
/* Footer */
|
| 128 |
+
.footer {
|
| 129 |
text-align: center;
|
| 130 |
+
padding: 2rem;
|
| 131 |
+
color: #666;
|
| 132 |
+
font-size: 0.8rem;
|
| 133 |
+
margin-top: 3rem;
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
/* Success/Error messages */
|
| 137 |
+
.success-message {
|
| 138 |
+
background: #d4edda;
|
| 139 |
+
color: #155724;
|
| 140 |
+
padding: 1rem;
|
| 141 |
+
border-radius: 10px;
|
| 142 |
margin: 1rem 0;
|
| 143 |
}
|
| 144 |
+
|
| 145 |
+
.info-message {
|
| 146 |
+
background: #d1ecf1;
|
| 147 |
+
color: #0c5460;
|
| 148 |
+
padding: 1rem;
|
| 149 |
+
border-radius: 10px;
|
| 150 |
+
margin: 1rem 0;
|
| 151 |
+
}
|
| 152 |
+
</style>
|
| 153 |
""", unsafe_allow_html=True)
|
| 154 |
|
| 155 |
+
# ============================================================
|
| 156 |
+
# LOAD MODEL AND CLASSES
|
| 157 |
+
# ============================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
@st.cache_resource
|
| 159 |
def load_model():
|
| 160 |
+
"""Load the trained model"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
try:
|
| 162 |
+
model = tf.keras.models.load_model('indian_food_classifier.keras')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
return model
|
| 164 |
+
except:
|
| 165 |
+
try:
|
| 166 |
+
model = tf.keras.models.load_model('/kaggle/working/indian_food_classifier.keras')
|
| 167 |
+
return model
|
| 168 |
+
except:
|
| 169 |
+
st.error("โ ๏ธ Model file not found. Please upload 'indian_food_classifier.keras'")
|
| 170 |
+
return None
|
| 171 |
|
| 172 |
@st.cache_data
|
| 173 |
def load_class_names():
|
| 174 |
+
"""Load class names"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
try:
|
| 176 |
+
with open('class_names.json', 'r') as f:
|
| 177 |
class_names = json.load(f)
|
|
|
|
| 178 |
return class_names
|
| 179 |
+
except:
|
| 180 |
+
try:
|
| 181 |
+
with open('/kaggle/working/class_names.json', 'r') as f:
|
| 182 |
+
class_names = json.load(f)
|
| 183 |
+
return class_names
|
| 184 |
+
except:
|
| 185 |
+
st.error("โ ๏ธ class_names.json not found. Please upload the file.")
|
| 186 |
+
return None
|
| 187 |
|
| 188 |
+
def preprocess_image(image, target_size=(224, 224)):
|
| 189 |
"""Preprocess image for model prediction"""
|
|
|
|
| 190 |
if image.mode != 'RGB':
|
| 191 |
image = image.convert('RGB')
|
| 192 |
+
image = image.resize(target_size)
|
| 193 |
+
img_array = np.array(image) / 255.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
img_array = np.expand_dims(img_array, axis=0)
|
|
|
|
| 195 |
return img_array
|
| 196 |
|
| 197 |
+
def format_food_name(name):
|
| 198 |
+
"""Format food name for display"""
|
| 199 |
+
return name.replace('_', ' ').title()
|
| 200 |
+
|
| 201 |
+
def create_confidence_chart(confidences, labels, top_n=5):
|
| 202 |
+
"""Create an interactive confidence chart"""
|
| 203 |
+
fig = go.Figure(data=[
|
| 204 |
+
go.Bar(
|
| 205 |
+
x=confidences[:top_n],
|
| 206 |
+
y=[format_food_name(l) for l in labels[:top_n]],
|
| 207 |
+
orientation='h',
|
| 208 |
+
marker=dict(
|
| 209 |
+
color=confidences[:top_n],
|
| 210 |
+
colorscale='Greens',
|
| 211 |
+
showscale=True,
|
| 212 |
+
colorbar=dict(title="Confidence (%)")
|
| 213 |
+
),
|
| 214 |
+
text=[f"{c:.1f}%" for c in confidences[:top_n]],
|
| 215 |
+
textposition='outside'
|
| 216 |
+
)
|
| 217 |
+
])
|
| 218 |
|
| 219 |
+
fig.update_layout(
|
| 220 |
+
title="Top Predictions Confidence Score",
|
| 221 |
+
xaxis_title="Confidence (%)",
|
| 222 |
+
yaxis_title="Food Item",
|
| 223 |
+
height=400,
|
| 224 |
+
margin=dict(l=0, r=0, t=40, b=0),
|
| 225 |
+
paper_bgcolor='rgba(0,0,0,0)',
|
| 226 |
+
plot_bgcolor='rgba(0,0,0,0)'
|
| 227 |
+
)
|
| 228 |
|
| 229 |
+
return fig
|
| 230 |
|
| 231 |
+
# ============================================================
|
| 232 |
+
# MAIN APP
|
| 233 |
+
# ============================================================
|
| 234 |
def main():
|
| 235 |
+
# Header
|
| 236 |
st.markdown("""
|
| 237 |
+
<div class="header-container">
|
| 238 |
+
<div class="pakistan-flag">
|
| 239 |
+
๐ต๐ฐ ๐ฎ๐ณ ๐ต๐ฐ
|
| 240 |
+
</div>
|
| 241 |
+
<div class="header-title">
|
| 242 |
+
๐ Pakistani & Indian Food Classifier
|
| 243 |
+
</div>
|
| 244 |
+
<div class="header-subtitle">
|
| 245 |
+
AI-powered dish recognition for 80+ South Asian delicacies
|
| 246 |
+
</div>
|
| 247 |
</div>
|
| 248 |
""", unsafe_allow_html=True)
|
| 249 |
|
| 250 |
+
# Sidebar
|
| 251 |
with st.sidebar:
|
| 252 |
+
st.markdown("### ๐ Model Information")
|
| 253 |
st.info("""
|
| 254 |
+
- **Architecture:** EfficientNetV2S
|
| 255 |
+
- **Classes:** 80 Indian/Pakistani Dishes
|
| 256 |
+
- **Accuracy:** 59.25%
|
| 257 |
+
- **Input Size:** 224x224 pixels
|
|
|
|
| 258 |
""")
|
| 259 |
|
| 260 |
+
st.markdown("---")
|
| 261 |
+
st.markdown("### ๐ฝ๏ธ Popular Dishes")
|
| 262 |
+
|
| 263 |
+
# Random popular dishes
|
| 264 |
+
popular_dishes = [
|
| 265 |
+
"Biryani", "Nihari", "Butter Chicken", "Aloo Gobi",
|
| 266 |
+
"Samosa", "Gulab Jamun", "Naan", "Haleem",
|
| 267 |
+
"Karahi", "Seekh Kebab", "Dal Makhani", "Ras Malai"
|
| 268 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
|
| 270 |
+
for dish in random.sample(popular_dishes, min(6, len(popular_dishes))):
|
| 271 |
+
st.markdown(f"โข {dish}")
|
| 272 |
+
|
| 273 |
+
st.markdown("---")
|
| 274 |
+
st.markdown("### ๐ How It Works")
|
| 275 |
st.markdown("""
|
| 276 |
+
1. ๐ธ Upload a clear photo of food
|
| 277 |
+
2. ๐ค AI analyzes the image
|
| 278 |
+
3. ๐ฏ Get top 5 predictions with confidence scores
|
| 279 |
+
4. ๐ View detailed confidence chart
|
| 280 |
""")
|
| 281 |
|
| 282 |
st.markdown("---")
|
| 283 |
+
st.markdown("### ๐ก Tips for Best Results")
|
| 284 |
+
st.markdown("""
|
| 285 |
+
- Use well-lit photos
|
| 286 |
+
- Focus on the main dish
|
| 287 |
+
- Avoid cluttered backgrounds
|
| 288 |
+
- Single dish per photo works best
|
| 289 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
|
| 291 |
st.markdown("---")
|
| 292 |
+
st.markdown("Made with โค๏ธ for South Asian Cuisine")
|
| 293 |
|
| 294 |
# Main content area
|
| 295 |
col1, col2 = st.columns([1, 1])
|
| 296 |
|
| 297 |
with col1:
|
| 298 |
+
st.markdown("### ๐ค Upload Food Image")
|
| 299 |
uploaded_file = st.file_uploader(
|
| 300 |
"Choose an image...",
|
| 301 |
type=['jpg', 'jpeg', 'png', 'webp', 'gif'],
|
| 302 |
+
help="Upload a clear image of Pakistani or Indian food"
|
| 303 |
)
|
| 304 |
|
| 305 |
if uploaded_file is not None:
|
|
|
|
| 306 |
image = Image.open(uploaded_file)
|
|
|
|
| 307 |
|
| 308 |
+
# Display image with styling
|
| 309 |
+
st.markdown("#### Preview")
|
| 310 |
+
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 311 |
+
|
| 312 |
+
# Image info
|
| 313 |
+
st.caption(f"๐ Image size: {image.size[0]} x {image.size[1]} pixels")
|
| 314 |
|
| 315 |
with col2:
|
| 316 |
if uploaded_file is not None:
|
| 317 |
+
st.markdown("### ๐ Analysis Results")
|
| 318 |
|
| 319 |
+
with st.spinner("๐ Analyzing your food image..."):
|
| 320 |
+
# Load model and classes
|
| 321 |
model = load_model()
|
| 322 |
class_names = load_class_names()
|
| 323 |
|
| 324 |
if model is not None and class_names is not None:
|
| 325 |
+
# Preprocess and predict
|
| 326 |
+
processed_img = preprocess_image(image)
|
| 327 |
+
predictions = model.predict(processed_img, verbose=0)[0]
|
| 328 |
|
| 329 |
+
# Get top 5 predictions
|
| 330 |
+
top_5_idx = np.argsort(predictions)[-5:][::-1]
|
| 331 |
+
top_5_names = [class_names[idx] for idx in top_5_idx]
|
| 332 |
+
top_5_confidences = [predictions[idx] * 100 for idx in top_5_idx]
|
| 333 |
|
| 334 |
+
# Display top prediction (highlighted)
|
| 335 |
st.markdown(f"""
|
| 336 |
+
<div class="top-prediction">
|
| 337 |
+
<div style="font-size: 1.2rem;">๐ฅ Top Prediction</div>
|
| 338 |
+
<h2>{format_food_name(top_5_names[0])}</h2>
|
| 339 |
+
<div class="confidence-score">Confidence: {top_5_confidences[0]:.2f}%</div>
|
| 340 |
</div>
|
| 341 |
""", unsafe_allow_html=True)
|
| 342 |
|
| 343 |
+
# Display other predictions
|
| 344 |
+
st.markdown("#### Other Possibilities")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 345 |
|
| 346 |
+
for i in range(1, min(5, len(top_5_names))):
|
| 347 |
+
confidence_percent = top_5_confidences[i]
|
| 348 |
+
|
| 349 |
+
# Determine emoji based on rank
|
| 350 |
+
if i == 1:
|
| 351 |
+
emoji = "๐ฅ"
|
| 352 |
+
elif i == 2:
|
| 353 |
+
emoji = "๐ฅ"
|
| 354 |
+
else:
|
| 355 |
+
emoji = f"{i+1}๏ธโฃ"
|
| 356 |
+
|
| 357 |
+
st.markdown(f"""
|
| 358 |
+
<div class="other-prediction">
|
| 359 |
+
<strong>{emoji} {format_food_name(top_5_names[i])}</strong><br>
|
| 360 |
+
<span style="color: #666;">Confidence: {confidence_percent:.2f}%</span>
|
| 361 |
+
</div>
|
| 362 |
+
""", unsafe_allow_html=True)
|
| 363 |
|
| 364 |
+
# Confidence chart
|
| 365 |
+
st.markdown("---")
|
| 366 |
+
st.markdown("### ๐ Confidence Analysis")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 367 |
|
| 368 |
+
fig = create_confidence_chart(top_5_confidences, top_5_names, top_n=5)
|
| 369 |
+
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
|
| 371 |
+
# Confidence meter for top prediction
|
| 372 |
+
st.markdown("#### Confidence Meter")
|
| 373 |
+
confidence_level = top_5_confidences[0]
|
| 374 |
|
| 375 |
+
if confidence_level > 70:
|
| 376 |
+
st.success(f"๐ฏ High confidence! The AI is very sure this is {format_food_name(top_5_names[0])}")
|
| 377 |
+
elif confidence_level > 50:
|
| 378 |
+
st.warning(f"๐ค Medium confidence. The AI thinks it's {format_food_name(top_5_names[0])}")
|
| 379 |
+
else:
|
| 380 |
+
st.info(f"๐ก Low confidence. Try uploading a clearer photo for better results")
|
| 381 |
|
| 382 |
+
# Footer with additional information
|
| 383 |
+
st.markdown("---")
|
| 384 |
+
|
| 385 |
+
col1, col2, col3 = st.columns(3)
|
| 386 |
+
|
| 387 |
+
with col1:
|
| 388 |
+
st.markdown("""
|
| 389 |
+
### ๐ฏ Supported Cuisines
|
| 390 |
+
- Punjabi
|
| 391 |
+
- Mughlai
|
| 392 |
+
- South Indian
|
| 393 |
+
- Sindhi
|
| 394 |
+
- Kashmiri
|
| 395 |
+
- Hyderabadi
|
| 396 |
+
""")
|
| 397 |
+
|
| 398 |
+
with col2:
|
| 399 |
+
st.markdown("""
|
| 400 |
+
### ๐ Dish Categories
|
| 401 |
+
- Curries & Gravies
|
| 402 |
+
- Rice Dishes (Biryani)
|
| 403 |
+
- Breads (Naan, Roti)
|
| 404 |
+
- Desserts & Sweets
|
| 405 |
+
- Snacks & Appetizers
|
| 406 |
+
- Beverages
|
| 407 |
+
""")
|
| 408 |
+
|
| 409 |
+
with col3:
|
| 410 |
+
st.markdown("""
|
| 411 |
+
### ๐ Model Performance
|
| 412 |
+
- 59.25% Top-1 Accuracy
|
| 413 |
+
- 80+ Food Classes
|
| 414 |
+
- 3,200 Training Images
|
| 415 |
+
- EfficientNetV2S Backbone
|
| 416 |
+
- Real-time Predictions
|
| 417 |
+
""")
|
| 418 |
|
| 419 |
# Footer
|
|
|
|
| 420 |
st.markdown("""
|
| 421 |
+
<div class="footer">
|
| 422 |
+
<p>๐ต๐ฐ Celebrating the rich culinary heritage of Pakistan and India ๐ฎ๐ณ</p>
|
| 423 |
+
<p>โ ๏ธ Note: For best results, use clear, well-lit images of individual dishes. The model works best on traditional South Asian cuisine.</p>
|
| 424 |
+
<p>Made with Streamlit & TensorFlow | Model trained on 80+ dishes</p>
|
| 425 |
</div>
|
| 426 |
""", unsafe_allow_html=True)
|
| 427 |
|