Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import alt as alt
|
2 |
+
import streamlit as st
|
3 |
+
import pandas as pd
|
4 |
+
import tensorflow as tf
|
5 |
+
import altair as alt
|
6 |
+
from utils import load_and_prep, get_classes, preprocess_data # Import the preprocess_data function
|
7 |
+
import time
|
8 |
+
|
9 |
+
# @st.cache_data(suppress_st_warning=True)
|
10 |
+
def predicting(image, model):
|
11 |
+
image = load_and_prep(image)
|
12 |
+
image = tf.cast(tf.expand_dims(image, axis=0), tf.int16)
|
13 |
+
preds = model.predict(image)
|
14 |
+
pred_class = class_names[tf.argmax(preds[0])]
|
15 |
+
pred_conf = tf.reduce_max(preds[0])
|
16 |
+
top_5_i = sorted((preds.argsort())[0][-5:][::-1])
|
17 |
+
values = preds[0][top_5_i] * 100
|
18 |
+
labels = []
|
19 |
+
for x in range(5):
|
20 |
+
labels.append(class_names[top_5_i[x]])
|
21 |
+
df = pd.DataFrame({"Top 5 Predictions": labels,
|
22 |
+
"F1 Scores": values,
|
23 |
+
'color': ['#EC5953', '#EC5953', '#EC5953', '#EC5953', '#EC5953']})
|
24 |
+
df = df.sort_values('F1 Scores')
|
25 |
+
return pred_class, pred_conf, df
|
26 |
+
|
27 |
+
|
28 |
+
class_names = get_classes()
|
29 |
+
|
30 |
+
st.set_page_config(page_title="Dish Decoder",
|
31 |
+
page_icon="π")
|
32 |
+
|
33 |
+
#### SideBar ####
|
34 |
+
|
35 |
+
st.sidebar.title("What's Dish Decoder ?")
|
36 |
+
st.sidebar.write("""
|
37 |
+
Dish Decoder is an end-to-end **CNN Image Classification Model** which identifies the food in your image.
|
38 |
+
|
39 |
+
- It can identify over 100 different food classes
|
40 |
+
|
41 |
+
- It is based upon a pre-trained Image Classification Model that comes with Keras and then retrained on the infamous **Food101 Dataset**.
|
42 |
+
|
43 |
+
- The Model actually beats the DeepFood Paper's model which also trained on the same dataset.
|
44 |
+
|
45 |
+
- The Accuracy acquired by DeepFood was 77.4% and our model's 85%.
|
46 |
+
|
47 |
+
- Difference of 8% ain't much, but the interesting thing is, DeepFood's model took 2-3 days to train while our's barely took 90min.
|
48 |
+
|
49 |
+
**Accuracy :** **`85%`**
|
50 |
+
|
51 |
+
**Model :** **`EfficientNetB1`**
|
52 |
+
|
53 |
+
**Dataset :** **`Food101`**
|
54 |
+
|
55 |
+
""")
|
56 |
+
|
57 |
+
#### Main Body ####
|
58 |
+
|
59 |
+
st.title("Dish Decoder πποΈ")
|
60 |
+
st.header("Discover, Decode, Delight !")
|
61 |
+
file = st.file_uploader(label="Upload an image of food.",
|
62 |
+
type=["jpg", "jpeg", "png"])
|
63 |
+
|
64 |
+
model = tf.keras.models.load_model("FoodVision.hdf5")
|
65 |
+
|
66 |
+
st.sidebar.markdown("Created by **Sparsh Goyal**")
|
67 |
+
|
68 |
+
st.markdown(
|
69 |
+
"""
|
70 |
+
<div style="position: fixed; bottom: 0; right: 10px; padding: 10px; color: white;">
|
71 |
+
<a href="https://github.com/sg-sparsh-goyal" target="_blank" style="color: white; text-decoration: none;">
|
72 |
+
β¨ Github
|
73 |
+
</a><br>
|
74 |
+
</div>
|
75 |
+
""",
|
76 |
+
unsafe_allow_html=True
|
77 |
+
)
|
78 |
+
|
79 |
+
if not file:
|
80 |
+
st.warning("Please upload an image")
|
81 |
+
st.stop()
|
82 |
+
else:
|
83 |
+
st.info("Uploading your image...")
|
84 |
+
|
85 |
+
# Add a loading bar
|
86 |
+
progress_bar = st.progress(0)
|
87 |
+
image = file.read()
|
88 |
+
|
89 |
+
# Simulate image processing with a 2-second delay
|
90 |
+
for percent_complete in range(100):
|
91 |
+
time.sleep(0.02)
|
92 |
+
progress_bar.progress(percent_complete + 1)
|
93 |
+
|
94 |
+
st.success("Image upload complete!")
|
95 |
+
|
96 |
+
st.image(image, use_column_width=True)
|
97 |
+
pred_button = st.button("Predict")
|
98 |
+
|
99 |
+
if pred_button:
|
100 |
+
pred_class, pred_conf, df = predicting(image, model)
|
101 |
+
st.success(f'Prediction : {pred_class} \nConfidence : {pred_conf * 100:.2f}%')
|
102 |
+
chart = alt.Chart(df).mark_bar(color='#00FF00').encode(
|
103 |
+
x=alt.X('F1 Scores', axis=alt.Axis(title=None)),
|
104 |
+
y=alt.Y('Top 5 Predictions', sort=None, axis=alt.Axis(title=None)),
|
105 |
+
text='F1 Scores'
|
106 |
+
).properties(width=600, height=400)
|
107 |
+
st.altair_chart(chart, use_container_width=True)
|