Spaces:
Sleeping
Sleeping
Update app (1).py
Browse files- app (1).py +39 -29
app (1).py
CHANGED
|
@@ -6,8 +6,7 @@ from tensorflow.keras.applications.resnet50 import preprocess_input, decode_pred
|
|
| 6 |
from tensorflow.keras.preprocessing import image
|
| 7 |
import cv2
|
| 8 |
from PIL import Image
|
| 9 |
-
import
|
| 10 |
-
import matplotlib.pyplot as plt
|
| 11 |
|
| 12 |
# Load pre-trained model
|
| 13 |
model = ResNet50(weights='imagenet')
|
|
@@ -30,38 +29,49 @@ def make_gradcam_heatmap(img_array, model, last_conv_layer_name, pred_index=None
|
|
| 30 |
conv_outputs = conv_outputs[0]
|
| 31 |
heatmap = conv_outputs @ pooled_grads[..., tf.newaxis]
|
| 32 |
heatmap = tf.squeeze(heatmap)
|
| 33 |
-
|
| 34 |
heatmap = tf.maximum(heatmap, 0) / tf.math.reduce_max(heatmap)
|
| 35 |
return heatmap.numpy()
|
| 36 |
|
| 37 |
# Streamlit UI
|
| 38 |
-
st.title("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
x = image.img_to_array(img_resized)
|
| 46 |
-
x = np.expand_dims(x, axis=0)
|
| 47 |
-
x = preprocess_input(x)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
| 6 |
from tensorflow.keras.preprocessing import image
|
| 7 |
import cv2
|
| 8 |
from PIL import Image
|
| 9 |
+
import os
|
|
|
|
| 10 |
|
| 11 |
# Load pre-trained model
|
| 12 |
model = ResNet50(weights='imagenet')
|
|
|
|
| 29 |
conv_outputs = conv_outputs[0]
|
| 30 |
heatmap = conv_outputs @ pooled_grads[..., tf.newaxis]
|
| 31 |
heatmap = tf.squeeze(heatmap)
|
|
|
|
| 32 |
heatmap = tf.maximum(heatmap, 0) / tf.math.reduce_max(heatmap)
|
| 33 |
return heatmap.numpy()
|
| 34 |
|
| 35 |
# Streamlit UI
|
| 36 |
+
st.title("🧠 Image Classifier + Grad-CAM Visualizer")
|
| 37 |
+
|
| 38 |
+
# File uploader
|
| 39 |
+
uploaded_file = st.file_uploader("📤 Upload an image (JPG, JPEG, PNG)", type=["jpg", "jpeg", "png"])
|
| 40 |
+
|
| 41 |
+
# Use default image if no upload
|
| 42 |
+
if uploaded_file is not None:
|
| 43 |
+
image_ = Image.open(uploaded_file).convert('RGB')
|
| 44 |
+
else:
|
| 45 |
+
st.info("No image uploaded. Using default image.")
|
| 46 |
+
default_path = "image_default.jpg" # Make sure this file exists in your project folder
|
| 47 |
+
if os.path.exists(default_path):
|
| 48 |
+
image_ = Image.open(default_path).convert('RGB')
|
| 49 |
+
else:
|
| 50 |
+
st.error("Default image not found. Please upload an image.")
|
| 51 |
+
st.stop()
|
| 52 |
|
| 53 |
+
# Preprocess image
|
| 54 |
+
image_resized = image_.resize((224, 224))
|
| 55 |
+
x = image.img_to_array(image_resized)
|
| 56 |
+
x = np.expand_dims(x, axis=0)
|
| 57 |
+
x = preprocess_input(x)
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
# Prediction
|
| 60 |
+
preds = model.predict(x)
|
| 61 |
+
pred_class = decode_predictions(preds, top=1)[0][0]
|
| 62 |
+
st.markdown(f"**Predicted:** `{pred_class[1]}` with `{pred_class[2]*100:.2f}%` confidence")
|
| 63 |
|
| 64 |
+
# Grad-CAM
|
| 65 |
+
heatmap = make_gradcam_heatmap(x, model, last_conv_layer_name)
|
| 66 |
+
img_cv = np.array(image_resized)
|
| 67 |
+
heatmap = cv2.resize(heatmap, (img_cv.shape[1], img_cv.shape[0]))
|
| 68 |
+
heatmap = np.uint8(255 * heatmap)
|
| 69 |
+
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
|
| 70 |
+
superimposed_img = cv2.addWeighted(img_cv, 0.6, heatmap, 0.4, 0)
|
| 71 |
|
| 72 |
+
# Display images
|
| 73 |
+
col1, col2 = st.columns(2)
|
| 74 |
+
with col1:
|
| 75 |
+
st.image(image_resized, caption="Original Image", use_container_width=True)
|
| 76 |
+
with col2:
|
| 77 |
+
st.image(superimposed_img, caption="Grad-CAM", use_container_width=True)
|