pratik1000 commited on
Commit
51eabe8
Β·
verified Β·
1 Parent(s): bb9df79

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +83 -44
src/streamlit_app.py CHANGED
@@ -1,61 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  from tensorflow.keras.models import load_model
3
  from tensorflow.keras.preprocessing import image
4
  import numpy as np
5
  from PIL import Image
 
 
 
6
 
7
- # ================================================================
8
- # STEP 1: Load Model
9
- # ================================================================
10
  @st.cache_resource
11
  def load_trained_model():
12
- model = load_model('src/dhoni_vs_kohli_final_model.h5')
 
13
  return model
14
 
15
  model = load_trained_model()
 
16
 
17
- # ================================================================
18
- # STEP 2: App UI
19
- # ================================================================
20
- st.set_page_config(page_title="Dhoni vs Kohli Classifier 🏏", layout="centered")
21
- st.title("🏏 Dhoni vs Kohli Image Classifier")
22
- 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πŸ‘‡")
23
 
24
- uploaded_file = st.file_uploader("πŸ“Έ Choose an image...", type=["jpg", "jpeg", "png"])
25
-
26
- # ================================================================
27
- # STEP 3: Prediction Logic
28
- # ================================================================
29
  if uploaded_file is not None:
30
- # Show uploaded image
31
- img = Image.open(uploaded_file)
32
- st.image(img, caption="Uploaded Image", use_column_width=True)
33
- st.write("πŸ” Analyzing...")
34
-
35
- # Preprocess
36
- img = img.resize((150, 150))
37
  x = image.img_to_array(img)
38
- x = np.expand_dims(x, axis=0) / 255.0
39
-
40
- # Predict
41
- pred = model.predict(x)[0][0]
42
-
43
- # Define confidence thresholds
44
- dhoni_conf = 1 - pred
45
- kohli_conf = pred
46
-
47
- st.write(f"Raw Prediction Value: {pred:.4f}")
48
-
49
- # Decision Logic
50
- if kohli_conf > 0.75:
51
- st.success(f"🏏 Predicted: **Virat Kohli** (Confidence: {kohli_conf:.2f})")
52
- st.progress(float(kohli_conf))
53
- elif dhoni_conf > 0.75:
54
- st.success(f"🏏 Predicted: **MS Dhoni** (Confidence: {dhoni_conf:.2f})")
55
- st.progress(float(dhoni_conf))
56
- else:
57
- st.warning("πŸ€” The model is **not confident enough**. This might be **someone else (Unknown Person)**.")
58
- st.progress(0.5)
59
 
 
 
 
 
 
 
60
  else:
61
- st.info("Please upload an image to get started.")
 
 
1
+ # import streamlit as st
2
+ # from tensorflow.keras.models import load_model
3
+ # from tensorflow.keras.preprocessing import image
4
+ # import numpy as np
5
+ # from PIL import Image
6
+
7
+ # # ================================================================
8
+ # # STEP 1: Load Model
9
+ # # ================================================================
10
+ # @st.cache_resource
11
+ # def load_trained_model():
12
+ # model = load_model('src/dhoni_vs_kohli_final_model.h5')
13
+ # return model
14
+
15
+ # model = load_trained_model()
16
+
17
+ # # ================================================================
18
+ # # STEP 2: App UI
19
+ # # ================================================================
20
+ # st.set_page_config(page_title="Dhoni vs Kohli Classifier 🏏", layout="centered")
21
+ # st.title("🏏 Dhoni vs Kohli Image Classifier")
22
+ # 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πŸ‘‡")
23
+
24
+ # uploaded_file = st.file_uploader("πŸ“Έ Choose an image...", type=["jpg", "jpeg", "png"])
25
+
26
+ # # ================================================================
27
+ # # STEP 3: Prediction Logic
28
+ # # ================================================================
29
+ # if uploaded_file is not None:
30
+ # # Show uploaded image
31
+ # img = Image.open(uploaded_file)
32
+ # st.image(img, caption="Uploaded Image", use_column_width=True)
33
+ # st.write("πŸ” Analyzing...")
34
+
35
+ # # Preprocess
36
+ # img = img.resize((150, 150))
37
+ # x = image.img_to_array(img)
38
+ # x = np.expand_dims(x, axis=0) / 255.0
39
+
40
+ # # Predict
41
+ # pred = model.predict(x)[0][0]
42
+
43
+ # # Define confidence thresholds
44
+ # dhoni_conf = 1 - pred
45
+ # kohli_conf = pred
46
+
47
+ # st.write(f"Raw Prediction Value: {pred:.4f}")
48
+
49
+ # # Decision Logic
50
+ # if kohli_conf > 0.75:
51
+ # st.success(f"🏏 Predicted: **Virat Kohli** (Confidence: {kohli_conf:.2f})")
52
+ # st.progress(float(kohli_conf))
53
+ # elif dhoni_conf > 0.75:
54
+ # st.success(f"🏏 Predicted: **MS Dhoni** (Confidence: {dhoni_conf:.2f})")
55
+ # st.progress(float(dhoni_conf))
56
+ # else:
57
+ # st.warning("πŸ€” The model is **not confident enough**. This might be **someone else (Unknown Person)**.")
58
+ # st.progress(0.5)
59
+
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
+ st.set_page_config(page_title="Dhoni vs Kohli Classifier", layout="centered")
71
 
 
 
 
72
  @st.cache_resource
73
  def load_trained_model():
74
+ MODEL_PATH = os.path.join(os.path.dirname(__file__), "src/dhoni_vs_kohli_final_model.h5")
75
+ model = load_model(MODEL_PATH)
76
  return model
77
 
78
  model = load_trained_model()
79
+ labels = ["MS Dhoni", "Virat Kohli"]
80
 
81
+ st.title("🏏 Dhoni vs Kohli Classifier")
82
+ uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
 
 
 
 
83
 
 
 
 
 
 
84
  if uploaded_file is not None:
85
+ img = Image.open(uploaded_file).convert("RGB")
86
+ st.image(img, caption="Uploaded Image", use_container_width=True)
87
+ img = img.resize((224, 224))
 
 
 
 
88
  x = image.img_to_array(img)
89
+ x = np.expand_dims(x, axis=0)
90
+ x = x / 255.0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
+ try:
93
+ preds = model.predict(x)
94
+ pred_label = labels[np.argmax(preds)]
95
+ st.success(f"Prediction: {pred_label}")
96
+ except Exception as e:
97
+ st.error(f"Error during prediction: {e}")
98
  else:
99
+ st.info("Please upload an image to get prediction.")
100
+