Surendradjh commited on
Commit
44ddc90
·
verified ·
1 Parent(s): 7985f07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -19
app.py CHANGED
@@ -44,29 +44,32 @@ if choice == "Upload Image":
44
  image_= st.file_uploader("📤 Upload an image (JPG, JPEG, PNG)", type=["jpg", "jpeg", "png"])
45
  else:
46
  image_= Image.open(r"dog.jpg").convert('RGB')
47
-
48
  # Preprocess image
49
- image_resized = image_.resize((224, 224))
50
- x = image.img_to_array(image_resized)
51
- x = np.expand_dims(x, axis=0)
52
- x = preprocess_input(x)
53
 
54
  # Prediction
55
- preds = model.predict(x)
56
- pred_class = decode_predictions(preds, top=1)[0][0]
57
- st.markdown(f"**Predicted:** `{pred_class[1]}` with `{pred_class[2]*100:.2f}%` confidence")
58
 
59
  # Grad-CAM
60
- heatmap = make_gradcam_heatmap(x, model, last_conv_layer_name)
61
- img_cv = np.array(image_resized)
62
- heatmap = cv2.resize(heatmap, (img_cv.shape[1], img_cv.shape[0]))
63
- heatmap = np.uint8(255 * heatmap)
64
- heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
65
- superimposed_img = cv2.addWeighted(img_cv, 0.6, heatmap, 0.4, 0)
66
 
67
  # Display images
68
- col1, col2 = st.columns(2)
69
- with col1:
70
- st.image(image_resized, caption="Original Image", use_container_width=True)
71
- with col2:
72
- st.image(superimposed_img, caption="Grad-CAM", use_container_width=True)
 
 
 
 
44
  image_= st.file_uploader("📤 Upload an image (JPG, JPEG, PNG)", type=["jpg", "jpeg", "png"])
45
  else:
46
  image_= Image.open(r"dog.jpg").convert('RGB')
47
+ try:
48
  # Preprocess image
49
+ image_resized = image_.resize((224, 224))
50
+ x = image.img_to_array(image_resized)
51
+ x = np.expand_dims(x, axis=0)
52
+ x = preprocess_input(x)
53
 
54
  # Prediction
55
+ preds = model.predict(x)
56
+ pred_class = decode_predictions(preds, top=1)[0][0]
57
+ st.markdown(f"**Predicted:** `{pred_class[1]}` with `{pred_class[2]*100:.2f}%` confidence")
58
 
59
  # Grad-CAM
60
+ heatmap = make_gradcam_heatmap(x, model, last_conv_layer_name)
61
+ img_cv = np.array(image_resized)
62
+ heatmap = cv2.resize(heatmap, (img_cv.shape[1], img_cv.shape[0]))
63
+ heatmap = np.uint8(255 * heatmap)
64
+ heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
65
+ superimposed_img = cv2.addWeighted(img_cv, 0.6, heatmap, 0.4, 0)
66
 
67
  # Display images
68
+ col1, col2 = st.columns(2)
69
+ with col1:
70
+ st.image(image_resized, caption="Original Image", use_container_width=True)
71
+ with col2:
72
+ st.image(superimposed_img, caption="Grad-CAM", use_container_width=True)
73
+
74
+ except Exception as e:
75
+ st.write("Upload image")