Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +25 -25
streamlit_app.py
CHANGED
@@ -9,6 +9,7 @@ import numpy as np
|
|
9 |
from streamlit_webrtc import webrtc_streamer, WebRtcMode, RTCConfiguration
|
10 |
import av
|
11 |
import cv2
|
|
|
12 |
|
13 |
# Configure Streamlit
|
14 |
st.set_page_config(
|
@@ -36,12 +37,13 @@ def load_model():
|
|
36 |
st.error("Model file not found. Please upload butterfly_classifier.pth to your space.")
|
37 |
return None
|
38 |
|
39 |
-
|
40 |
-
model
|
41 |
model.load_state_dict(torch.load(MODEL_PATH, map_location="cpu"))
|
42 |
model.eval()
|
43 |
return model
|
44 |
|
|
|
45 |
model = load_model()
|
46 |
|
47 |
if model is None:
|
@@ -73,24 +75,6 @@ def predict_butterfly(image):
|
|
73 |
|
74 |
return predicted_class, confidence.item()
|
75 |
|
76 |
-
def display_prediction(predicted_class, confidence):
|
77 |
-
"""Display prediction with confidence check"""
|
78 |
-
if confidence < 0.70: # Below 70% confidence
|
79 |
-
st.warning("⚠️ **Image not clear - Unable to identify butterfly**")
|
80 |
-
st.info(f"Confidence too low: {confidence:.1%}")
|
81 |
-
st.markdown("**Tips for better results:**")
|
82 |
-
st.markdown("- Use better lighting")
|
83 |
-
st.markdown("- Get closer to the butterfly")
|
84 |
-
st.markdown("- Ensure the butterfly is clearly visible")
|
85 |
-
st.markdown("- Avoid blurry or dark images")
|
86 |
-
else:
|
87 |
-
st.success(f"**🦋 Identified: {predicted_class}**")
|
88 |
-
st.info(f"Confidence: {confidence:.1%}")
|
89 |
-
|
90 |
-
if predicted_class in butterfly_info:
|
91 |
-
st.write("**Species Information:**")
|
92 |
-
st.write(butterfly_info[predicted_class]["description"])
|
93 |
-
|
94 |
# Video frame callback for live camera
|
95 |
class VideoProcessor:
|
96 |
def __init__(self):
|
@@ -107,7 +91,7 @@ class VideoProcessor:
|
|
107 |
rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
108 |
predicted_class, confidence = predict_butterfly(rgb_img)
|
109 |
|
110 |
-
if predicted_class and confidence > 0.
|
111 |
self.prediction_text = f"{predicted_class} ({confidence:.2f})"
|
112 |
|
113 |
# Draw prediction on frame
|
@@ -144,14 +128,22 @@ with tab1:
|
|
144 |
with col2:
|
145 |
predicted_class, confidence = predict_butterfly(image)
|
146 |
|
147 |
-
if predicted_class:
|
148 |
st.success(f"**Prediction: {predicted_class}**")
|
149 |
st.info(f"Confidence: {confidence:.2%}")
|
150 |
|
151 |
if predicted_class in butterfly_info:
|
152 |
st.write("**Species Information:**")
|
153 |
st.write(butterfly_info[predicted_class]["description"])
|
154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
except Exception as e:
|
156 |
st.error(f"Error processing image: {str(e)}")
|
157 |
|
@@ -179,13 +171,21 @@ with tab2:
|
|
179 |
with col2:
|
180 |
predicted_class, confidence = predict_butterfly(image)
|
181 |
|
182 |
-
if predicted_class:
|
183 |
st.success(f"**Prediction: {predicted_class}**")
|
184 |
st.info(f"Confidence: {confidence:.2%}")
|
185 |
|
186 |
if predicted_class in butterfly_info:
|
187 |
st.write("**Species Information:**")
|
188 |
st.write(butterfly_info[predicted_class]["description"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
|
190 |
except Exception as e:
|
191 |
st.error(f"Error processing image: {str(e)}")
|
@@ -193,6 +193,6 @@ with tab2:
|
|
193 |
# Add footer with instructions
|
194 |
st.markdown("---")
|
195 |
st.markdown("### How to use:")
|
196 |
-
st.markdown("1. **
|
197 |
st.markdown("2. **Upload Image**: Choose a butterfly photo from your device")
|
198 |
st.markdown("3. **Best Results**: Use clear, well-lit photos with the butterfly clearly visible")
|
|
|
9 |
from streamlit_webrtc import webrtc_streamer, WebRtcMode, RTCConfiguration
|
10 |
import av
|
11 |
import cv2
|
12 |
+
import timm
|
13 |
|
14 |
# Configure Streamlit
|
15 |
st.set_page_config(
|
|
|
37 |
st.error("Model file not found. Please upload butterfly_classifier.pth to your space.")
|
38 |
return None
|
39 |
|
40 |
+
# Use EfficientNet-B0 (same as training)
|
41 |
+
model = timm.create_model('efficientnet_b0', pretrained=False, num_classes=len(class_names))
|
42 |
model.load_state_dict(torch.load(MODEL_PATH, map_location="cpu"))
|
43 |
model.eval()
|
44 |
return model
|
45 |
|
46 |
+
# Load the model
|
47 |
model = load_model()
|
48 |
|
49 |
if model is None:
|
|
|
75 |
|
76 |
return predicted_class, confidence.item()
|
77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
# Video frame callback for live camera
|
79 |
class VideoProcessor:
|
80 |
def __init__(self):
|
|
|
91 |
rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
92 |
predicted_class, confidence = predict_butterfly(rgb_img)
|
93 |
|
94 |
+
if predicted_class and confidence > 0.8: # Only show if confidence > 80%
|
95 |
self.prediction_text = f"{predicted_class} ({confidence:.2f})"
|
96 |
|
97 |
# Draw prediction on frame
|
|
|
128 |
with col2:
|
129 |
predicted_class, confidence = predict_butterfly(image)
|
130 |
|
131 |
+
if predicted_class and confidence >= 0.80: # Only show if confidence >= 80%
|
132 |
st.success(f"**Prediction: {predicted_class}**")
|
133 |
st.info(f"Confidence: {confidence:.2%}")
|
134 |
|
135 |
if predicted_class in butterfly_info:
|
136 |
st.write("**Species Information:**")
|
137 |
st.write(butterfly_info[predicted_class]["description"])
|
138 |
+
else:
|
139 |
+
st.warning("⚠️ **Image not clear - Unable to identify butterfly**")
|
140 |
+
st.info(f"Confidence too low: {confidence:.1%}")
|
141 |
+
st.markdown("**Tips for better results:**")
|
142 |
+
st.markdown("- Use better lighting")
|
143 |
+
st.markdown("- Get closer to the butterfly")
|
144 |
+
st.markdown("- Ensure the butterfly is clearly visible")
|
145 |
+
st.markdown("- Avoid blurry or dark images")
|
146 |
+
|
147 |
except Exception as e:
|
148 |
st.error(f"Error processing image: {str(e)}")
|
149 |
|
|
|
171 |
with col2:
|
172 |
predicted_class, confidence = predict_butterfly(image)
|
173 |
|
174 |
+
if predicted_class and confidence >= 0.80: # Only show if confidence >= 80%
|
175 |
st.success(f"**Prediction: {predicted_class}**")
|
176 |
st.info(f"Confidence: {confidence:.2%}")
|
177 |
|
178 |
if predicted_class in butterfly_info:
|
179 |
st.write("**Species Information:**")
|
180 |
st.write(butterfly_info[predicted_class]["description"])
|
181 |
+
else:
|
182 |
+
st.warning("⚠️ **Image not clear - Unable to identify butterfly**")
|
183 |
+
st.info(f"Confidence too low: {confidence:.1%}")
|
184 |
+
st.markdown("**Tips for better results:**")
|
185 |
+
st.markdown("- Use better lighting")
|
186 |
+
st.markdown("- Get closer to the butterfly")
|
187 |
+
st.markdown("- Ensure the butterfly is clearly visible")
|
188 |
+
st.markdown("- Avoid blurry or dark images")
|
189 |
|
190 |
except Exception as e:
|
191 |
st.error(f"Error processing image: {str(e)}")
|
|
|
193 |
# Add footer with instructions
|
194 |
st.markdown("---")
|
195 |
st.markdown("### How to use:")
|
196 |
+
st.markdown("1. **Camera Capture**: Take a photo using your device camera")
|
197 |
st.markdown("2. **Upload Image**: Choose a butterfly photo from your device")
|
198 |
st.markdown("3. **Best Results**: Use clear, well-lit photos with the butterfly clearly visible")
|