pratik1000 commited on
Commit
66f8308
Β·
verified Β·
1 Parent(s): a6282b7

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +88 -10
src/streamlit_app.py CHANGED
@@ -60,7 +60,81 @@
60
  # else:
61
  # st.info("Please upload an image to get started.")
62
 
63
- import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  from tensorflow.keras.models import load_model
65
  from tensorflow.keras.preprocessing import image
66
  import numpy as np
@@ -93,7 +167,7 @@ Model accuracy β‰ˆ 85%, so it may sometimes make mistakes.πŸ‘‡
93
  uploaded_file = st.file_uploader("πŸ“Έ Choose an image...", type=["jpg", "jpeg", "png"])
94
 
95
  # ================================================================
96
- # STEP 3: Prediction Logic
97
  # ================================================================
98
  if uploaded_file is not None:
99
  try:
@@ -110,24 +184,28 @@ if uploaded_file is not None:
110
  # Predict
111
  pred = model.predict(x)[0][0]
112
 
113
- # Confidence calculation
114
- dhoni_conf = 1 - pred
115
- kohli_conf = pred
116
 
 
117
  st.write(f"**Raw Prediction Value:** {pred:.4f}")
 
118
 
119
  # Decision logic
120
- if kohli_conf > 0.65:
 
 
 
 
121
  st.success(f"🏏 Predicted: **Virat Kohli** (Confidence: {kohli_conf:.2f})")
122
  st.progress(float(kohli_conf))
123
- elif dhoni_conf > 0.65:
 
124
  st.success(f"🏏 Predicted: **MS Dhoni** (Confidence: {dhoni_conf:.2f})")
125
  st.progress(float(dhoni_conf))
126
- else:
127
- st.warning("πŸ€” The model is **not confident enough**. Might be someone **else**.")
128
- st.progress(0.5)
129
 
130
  except Exception as e:
131
  st.error(f"❌ Error during prediction: {e}")
132
  else:
133
  st.info("Please upload an image to get started.")
 
 
60
  # else:
61
  # st.info("Please upload an image to get started.")
62
 
63
+ # import streamlit as st
64
+ # from tensorflow.keras.models import load_model
65
+ # from tensorflow.keras.preprocessing import image
66
+ # import numpy as np
67
+ # from PIL import Image
68
+ # import os
69
+
70
+ # # ================================================================
71
+ # # STEP 1: Load Model
72
+ # # ================================================================
73
+ # @st.cache_resource
74
+ # def load_trained_model():
75
+ # MODEL_PATH = os.path.join(os.path.dirname(__file__), "dhoni_vs_kohli_model.h5")
76
+ # model = load_model(MODEL_PATH)
77
+ # return model
78
+
79
+ # model = load_trained_model()
80
+
81
+ # # ================================================================
82
+ # # STEP 2: App UI
83
+ # # ================================================================
84
+ # st.set_page_config(page_title="Dhoni vs Kohli Classifier 🏏", layout="centered")
85
+ # st.title("🏏 Dhoni vs Kohli Image Classifier")
86
+ # st.markdown(
87
+ # """
88
+ # Upload an image, and the model will tell whether it's **MS Dhoni**, **Virat Kohli**, or someone **else**.
89
+ # Model accuracy β‰ˆ 85%, so it may sometimes make mistakes.πŸ‘‡
90
+ # """
91
+ # )
92
+
93
+ # uploaded_file = st.file_uploader("πŸ“Έ Choose an image...", type=["jpg", "jpeg", "png"])
94
+
95
+ # # ================================================================
96
+ # # STEP 3: Prediction Logic
97
+ # # ================================================================
98
+ # if uploaded_file is not None:
99
+ # try:
100
+ # # Show uploaded image
101
+ # img = Image.open(uploaded_file).convert("RGB")
102
+ # st.image(img, caption="Uploaded Image", use_container_width=True)
103
+ # st.write("πŸ” Analyzing...")
104
+
105
+ # # Preprocess
106
+ # img = img.resize((150, 150)) # βœ… same size as training
107
+ # x = image.img_to_array(img)
108
+ # x = np.expand_dims(x, axis=0) / 255.0
109
+
110
+ # # Predict
111
+ # pred = model.predict(x)[0][0]
112
+
113
+ # # Confidence calculation
114
+ # dhoni_conf = 1 - pred
115
+ # kohli_conf = pred
116
+
117
+ # st.write(f"**Raw Prediction Value:** {pred:.4f}")
118
+
119
+ # # Decision logic
120
+ # if kohli_conf > 0.65:
121
+ # st.success(f"🏏 Predicted: **Virat Kohli** (Confidence: {kohli_conf:.2f})")
122
+ # st.progress(float(kohli_conf))
123
+ # elif dhoni_conf > 0.65:
124
+ # st.success(f"🏏 Predicted: **MS Dhoni** (Confidence: {dhoni_conf:.2f})")
125
+ # st.progress(float(dhoni_conf))
126
+ # else:
127
+ # st.warning("πŸ€” The model is **not confident enough**. Might be someone **else**.")
128
+ # st.progress(0.5)
129
+
130
+ # except Exception as e:
131
+ # st.error(f"❌ Error during prediction: {e}")
132
+ # else:
133
+ # st.info("Please upload an image to get started.")
134
+
135
+
136
+
137
+ import streamlit as st
138
  from tensorflow.keras.models import load_model
139
  from tensorflow.keras.preprocessing import image
140
  import numpy as np
 
167
  uploaded_file = st.file_uploader("πŸ“Έ Choose an image...", type=["jpg", "jpeg", "png"])
168
 
169
  # ================================================================
170
+ # STEP 3: Prediction Logic with Unknown Handling
171
  # ================================================================
172
  if uploaded_file is not None:
173
  try:
 
184
  # Predict
185
  pred = model.predict(x)[0][0]
186
 
187
+ # Confidence distance from midpoint (0.5)
188
+ confidence_distance = abs(pred - 0.5) * 2 # 0 β†’ uncertain, 1 β†’ confident
 
189
 
190
+ # Show raw prediction for debugging
191
  st.write(f"**Raw Prediction Value:** {pred:.4f}")
192
+ st.write(f"**Confidence Distance:** {confidence_distance:.2f}")
193
 
194
  # Decision logic
195
+ if confidence_distance < 0.4:
196
+ st.warning("πŸ€” Not confident β€” This might not be Dhoni or Kohli.")
197
+ st.progress(0.4)
198
+ elif pred > 0.5:
199
+ kohli_conf = pred
200
  st.success(f"🏏 Predicted: **Virat Kohli** (Confidence: {kohli_conf:.2f})")
201
  st.progress(float(kohli_conf))
202
+ else:
203
+ dhoni_conf = 1 - pred
204
  st.success(f"🏏 Predicted: **MS Dhoni** (Confidence: {dhoni_conf:.2f})")
205
  st.progress(float(dhoni_conf))
 
 
 
206
 
207
  except Exception as e:
208
  st.error(f"❌ Error during prediction: {e}")
209
  else:
210
  st.info("Please upload an image to get started.")
211
+