DasariHarshitha commited on
Commit
2c9c8b0
·
verified ·
1 Parent(s): 412bda6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -19
app.py CHANGED
@@ -6,7 +6,7 @@ from PIL import Image
6
 
7
  # 🔧 Configure Streamlit
8
  st.set_page_config(
9
- page_title="Animal Classifier",
10
  layout="centered",
11
  initial_sidebar_state="auto"
12
  )
@@ -24,42 +24,50 @@ model = load_model_once()
24
  class_indices = load_labels()
25
  class_labels = list(class_indices.keys())
26
 
27
- # 🏷 App Title
28
- st.title("🐾 Animal Image Classifier")
29
- st.markdown("Upload up to **3 images** and get predictions")
30
 
31
  # 📤 Upload Images
32
  uploaded_files = st.file_uploader(
33
- "Upload animal images",
34
  type=["jpg", "jpeg", "png"],
35
  accept_multiple_files=True
36
  )
37
 
38
  if uploaded_files:
39
- # Limit to 3 images for clarity
40
  uploaded_files = uploaded_files[:3]
41
- cols = st.columns(len(uploaded_files))
42
 
43
  for idx, uploaded_file in enumerate(uploaded_files):
44
  with cols[idx]:
45
- st.markdown(f"### Image {idx+1}")
46
 
47
- # 🖼️ Load & visually shrink image
48
  image = Image.open(uploaded_file).convert("RGB")
49
  preview = image.copy()
50
- preview.thumbnail((150, 150))
51
- st.image(preview, caption="Preview", use_container_width=True)
52
 
53
- # 🧪 Preprocess image for prediction
54
- resized = image.resize((128, 128))
55
  img_array = img_to_array(resized) / 255.0
56
  img_array = np.expand_dims(img_array, axis=0)
57
 
58
- # 🔮 Model Prediction
59
  preds = model.predict(img_array)[0]
60
- top_indices = preds.argsort()[-3:][::-1]
61
- top_labels = [class_labels[i] for i in top_indices]
62
- top_scores = [preds[i] for i in top_indices]
63
 
64
- # ✅ Display Top Prediction
65
- st.success(f"**🔮Prediction:** {top_labels[0].capitalize()}")
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  # 🔧 Configure Streamlit
8
  st.set_page_config(
9
+ page_title="WildVision 🐾 | Animal Identifier",
10
  layout="centered",
11
  initial_sidebar_state="auto"
12
  )
 
24
  class_indices = load_labels()
25
  class_labels = list(class_indices.keys())
26
 
27
+ # 🏷 App Header
28
+ st.title("🦁 WildVision - Smart Animal Identifier")
29
+ st.markdown("Upload up to **3 animal images** and discover what species they are!")
30
 
31
  # 📤 Upload Images
32
  uploaded_files = st.file_uploader(
33
+ "📸 Upload your animal photos",
34
  type=["jpg", "jpeg", "png"],
35
  accept_multiple_files=True
36
  )
37
 
38
  if uploaded_files:
39
+ # Limit to 3 images
40
  uploaded_files = uploaded_files[:3]
41
+ cols = st.columns(len(uploaded_files))
42
 
43
  for idx, uploaded_file in enumerate(uploaded_files):
44
  with cols[idx]:
45
+ st.markdown(f"#### 📷 Image {idx + 1}")
46
 
47
+ # 🖼️ Load and preview
48
  image = Image.open(uploaded_file).convert("RGB")
49
  preview = image.copy()
50
+ preview.thumbnail((150, 150))
51
+ st.image(preview, caption="Image Preview", use_container_width=True)
52
 
53
+ # 🧪 Preprocess for prediction
54
+ resized = image.resize((128, 128))
55
  img_array = img_to_array(resized) / 255.0
56
  img_array = np.expand_dims(img_array, axis=0)
57
 
58
+ # 🔮 Make prediction
59
  preds = model.predict(img_array)[0]
 
 
 
60
 
61
+ # ✅ Safe check
62
+ if len(preds) != len(class_labels):
63
+ st.error("⚠️ Mismatch between model outputs and label classes.")
64
+ else:
65
+ top_indices = preds.argsort()[-3:][::-1]
66
+ top_labels = [class_labels[i] for i in top_indices]
67
+ top_scores = [preds[i] for i in top_indices]
68
+
69
+ st.markdown("#### 🧠 Top Predictions:")
70
+ for label, score in zip(top_labels, top_scores):
71
+ st.write(f"➡️ **{label.capitalize()}**: {score:.2%}")
72
+
73
+ st.success(f"🎯 **Most Likely:** *{top_labels[0].capitalize()}* 🐾")