File size: 7,727 Bytes
51eabe8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66f8308
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
812cf26
 
 
 
51eabe8
 
6a1c334
 
 
812cf26
 
6f99ef0
51eabe8
812cf26
 
 
 
6a1c334
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66f8308
6a1c334
812cf26
51eabe8
6a1c334
 
 
 
 
 
 
 
 
 
 
 
 
66f8308
 
6a1c334
66f8308
6a1c334
66f8308
6a1c334
 
66f8308
 
 
 
 
6a1c334
 
66f8308
 
6a1c334
 
 
51eabe8
6a1c334
812cf26
6a1c334
66f8308
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# import streamlit as st
# from tensorflow.keras.models import load_model
# from tensorflow.keras.preprocessing import image
# import numpy as np
# from PIL import Image

# # ================================================================
# # STEP 1: Load Model
# # ================================================================
# @st.cache_resource
# def load_trained_model():
#     model = load_model('src/dhoni_vs_kohli_final_model.h5')
#     return model

# model = load_trained_model()

# # ================================================================
# # STEP 2: App UI
# # ================================================================
# st.set_page_config(page_title="Dhoni vs Kohli Classifier 🏏", layout="centered")
# st.title("🏏 Dhoni vs Kohli Image Classifier")
# st.markdown("Upload an image, and the model will tell whether it's **MS Dhoni**, **Virat Kohli**, or someone **else**,the model accuracy is 85% so some time it make mistakeπŸ‘‡")

# uploaded_file = st.file_uploader("πŸ“Έ Choose an image...", type=["jpg", "jpeg", "png"])

# # ================================================================
# # STEP 3: Prediction Logic
# # ================================================================
# if uploaded_file is not None:
#     # Show uploaded image
#     img = Image.open(uploaded_file)
#     st.image(img, caption="Uploaded Image", use_column_width=True)
#     st.write("πŸ” Analyzing...")

#     # Preprocess
#     img = img.resize((150, 150))
#     x = image.img_to_array(img)
#     x = np.expand_dims(x, axis=0) / 255.0

#     # Predict
#     pred = model.predict(x)[0][0]

#     # Define confidence thresholds
#     dhoni_conf = 1 - pred
#     kohli_conf = pred

#     st.write(f"Raw Prediction Value: {pred:.4f}")

#     # Decision Logic
#     if kohli_conf > 0.75:
#         st.success(f"🏏 Predicted: **Virat Kohli** (Confidence: {kohli_conf:.2f})")
#         st.progress(float(kohli_conf))
#     elif dhoni_conf > 0.75:
#         st.success(f"🏏 Predicted: **MS Dhoni** (Confidence: {dhoni_conf:.2f})")
#         st.progress(float(dhoni_conf))
#     else:
#         st.warning("πŸ€” The model is **not confident enough**. This might be **someone else (Unknown Person)**.")
#         st.progress(0.5)

# else:
#     st.info("Please upload an image to get started.")

# import streamlit as st
# from tensorflow.keras.models import load_model
# from tensorflow.keras.preprocessing import image
# import numpy as np
# from PIL import Image
# import os

# # ================================================================
# # STEP 1: Load Model
# # ================================================================
# @st.cache_resource
# def load_trained_model():
#     MODEL_PATH = os.path.join(os.path.dirname(__file__), "dhoni_vs_kohli_model.h5")
#     model = load_model(MODEL_PATH)
#     return model

# model = load_trained_model()

# # ================================================================
# # STEP 2: App UI
# # ================================================================
# st.set_page_config(page_title="Dhoni vs Kohli Classifier 🏏", layout="centered")
# st.title("🏏 Dhoni vs Kohli Image Classifier")
# st.markdown(
#     """
# Upload an image, and the model will tell whether it's **MS Dhoni**, **Virat Kohli**, or someone **else**.  
# Model accuracy β‰ˆ 85%, so it may sometimes make mistakes.πŸ‘‡
#     """
# )

# uploaded_file = st.file_uploader("πŸ“Έ Choose an image...", type=["jpg", "jpeg", "png"])

# # ================================================================
# # STEP 3: Prediction Logic
# # ================================================================
# if uploaded_file is not None:
#     try:
#         # Show uploaded image
#         img = Image.open(uploaded_file).convert("RGB")
#         st.image(img, caption="Uploaded Image", use_container_width=True)
#         st.write("πŸ” Analyzing...")

#         # Preprocess
#         img = img.resize((150, 150))  # βœ… same size as training
#         x = image.img_to_array(img)
#         x = np.expand_dims(x, axis=0) / 255.0

#         # Predict
#         pred = model.predict(x)[0][0]

#         # Confidence calculation
#         dhoni_conf = 1 - pred
#         kohli_conf = pred

#         st.write(f"**Raw Prediction Value:** {pred:.4f}")

#         # Decision logic
#         if kohli_conf > 0.65:
#             st.success(f"🏏 Predicted: **Virat Kohli** (Confidence: {kohli_conf:.2f})")
#             st.progress(float(kohli_conf))
#         elif dhoni_conf > 0.65:
#             st.success(f"🏏 Predicted: **MS Dhoni** (Confidence: {dhoni_conf:.2f})")
#             st.progress(float(dhoni_conf))
#         else:
#             st.warning("πŸ€” The model is **not confident enough**. Might be someone **else**.")
#             st.progress(0.5)

#     except Exception as e:
#         st.error(f"❌ Error during prediction: {e}")
# else:
#     st.info("Please upload an image to get started.")



import streamlit as st 
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import numpy as np
from PIL import Image
import os

# ================================================================
# STEP 1: Load Model
# ================================================================
@st.cache_resource
def load_trained_model():
    MODEL_PATH = os.path.join(os.path.dirname(__file__), "dhoni_vs_kohli_final_model.h5")
    model = load_model(MODEL_PATH)
    return model

model = load_trained_model()

# ================================================================
# STEP 2: App UI
# ================================================================
st.set_page_config(page_title="Dhoni vs Kohli Classifier 🏏", layout="centered")
st.title("🏏 Dhoni vs Kohli Image Classifier")
st.markdown(
    """
Upload an image, and the model will tell whether it's **MS Dhoni**, **Virat Kohli**, or someone **else**.  
Model accuracy β‰ˆ 85%, so it may sometimes make mistakes.πŸ‘‡
    """
)

uploaded_file = st.file_uploader("πŸ“Έ Choose an image...", type=["jpg", "jpeg", "png"])

# ================================================================
# STEP 3: Prediction Logic with Unknown Handling
# ================================================================
if uploaded_file is not None:
    try:
        # Show uploaded image
        img = Image.open(uploaded_file).convert("RGB")
        st.image(img, caption="Uploaded Image", use_container_width=True)
        st.write("πŸ” Analyzing...")

        # Preprocess
        img = img.resize((150, 150))  # βœ… same size as training
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0) / 255.0

        # Predict
        pred = model.predict(x)[0][0]

        # Confidence distance from midpoint (0.5)
        confidence_distance = abs(pred - 0.5) * 2  # 0 β†’ uncertain, 1 β†’ confident

        # Show raw prediction for debugging
        st.write(f"**Raw Prediction Value:** {pred:.4f}")
        st.write(f"**Confidence Distance:** {confidence_distance:.2f}")

        # Decision logic
        if confidence_distance < 0.4:
            st.warning("πŸ€” Not confident β€” This might not be Dhoni or Kohli.")
            st.progress(0.4)
        elif pred > 0.5:
            kohli_conf = pred
            st.success(f"🏏 Predicted: **Virat Kohli** (Confidence: {kohli_conf:.2f})")
            st.progress(float(kohli_conf))
        else:
            dhoni_conf = 1 - pred
            st.success(f"🏏 Predicted: **MS Dhoni** (Confidence: {dhoni_conf:.2f})")
            st.progress(float(dhoni_conf))

    except Exception as e:
        st.error(f"❌ Error during prediction: {e}")
else:
    st.info("Please upload an image to get started.")