Surendradjh commited on
Commit
2667bd1
·
verified ·
1 Parent(s): 91516cd

Update app (1).py

Browse files
Files changed (1) hide show
  1. 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 io
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(" Image Classifier + Grad-CAM Visualizer")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
- uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
41
- if uploaded_file:
42
- # Load and preprocess image
43
- img = Image.open(uploaded_file).convert('RGB')
44
- img_resized = img.resize((224, 224))
45
- x = image.img_to_array(img_resized)
46
- x = np.expand_dims(x, axis=0)
47
- x = preprocess_input(x)
48
 
49
- # Prediction
50
- preds = model.predict(x)
51
- pred_class = decode_predictions(preds, top=1)[0][0]
52
- st.markdown(f"**Predicted:** `{pred_class[1]}` with `{pred_class[2]*100:.2f}%` confidence")
53
 
54
- # Grad-CAM
55
- heatmap = make_gradcam_heatmap(x, model, last_conv_layer_name)
56
- img_cv = np.array(img_resized)
57
- heatmap = cv2.resize(heatmap, (img_cv.shape[1], img_cv.shape[0]))
58
- heatmap = np.uint8(255 * heatmap)
59
- heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
60
- superimposed_img = cv2.addWeighted(img_cv, 0.6, heatmap, 0.4, 0)
61
 
62
- # Display images
63
- col1, col2 = st.columns(2)
64
- with col1:
65
- st.image(img_resized, caption="Original Image", use_container_width=True)
66
- with col2:
67
- st.image(superimposed_img, caption="Grad-CAM", use_container_width=True)
 
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)