Update app.py
Browse files
app.py
CHANGED
@@ -20,8 +20,7 @@ def count_and_label_red_patches(heatmap, threshold=200):
|
|
20 |
_, labels, stats, _ = cv2.connectedComponentsWithStats(red_mask.astype(np.uint8), connectivity=8)
|
21 |
|
22 |
num_red_patches = labels.max()
|
23 |
-
|
24 |
-
# Iterate through the labeled patches and put sequential numbers on top
|
25 |
for i in range(1, num_red_patches + 1):
|
26 |
patch_mask = (labels == i)
|
27 |
patch_centroid_x, patch_centroid_y = int(stats[i, cv2.CC_STAT_LEFT] + stats[i, cv2.CC_STAT_WIDTH] / 2), int(stats[i, cv2.CC_STAT_TOP] + stats[i, cv2.CC_STAT_HEIGHT] / 2)
|
@@ -46,33 +45,27 @@ if uploaded_image:
|
|
46 |
|
47 |
pred_saliency = model(img).squeeze().detach().numpy()
|
48 |
|
49 |
-
# Convert the saliency map to a heatmap with a blue color map
|
50 |
heatmap = (pred_saliency * 255).astype(np.uint8)
|
51 |
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET) # Use a blue colormap (JET)
|
52 |
|
53 |
-
# Resize the heatmap to match the image dimensions
|
54 |
heatmap = cv2.resize(heatmap, (image.width, image.height))
|
55 |
|
56 |
-
# Overlay red patch labels on the heatmap
|
57 |
heatmap, num_red_patches = count_and_label_red_patches(heatmap)
|
58 |
|
59 |
-
# Convert the image to a NumPy array and enhance the blue channel using CLAHE
|
60 |
enhanced_image = np.array(image)
|
61 |
b, g, r = cv2.split(enhanced_image)
|
62 |
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
|
63 |
b_enhanced = clahe.apply(b)
|
64 |
enhanced_image = cv2.merge((b_enhanced, g, r))
|
65 |
|
66 |
-
alpha = 0.7
|
67 |
blended_img = cv2.addWeighted(enhanced_image, 1 - alpha, heatmap, alpha, 0)
|
68 |
|
69 |
-
# Display the images in your Streamlit app
|
70 |
st.image(heatmap, caption='Enhanced Saliency Heatmap', use_column_width=True, channels='BGR')
|
71 |
st.image(enhanced_image, caption='Enhanced Blue Image', use_column_width=True, channels='BGR')
|
72 |
|
73 |
-
# Overlay the red patch count on the blended image
|
74 |
st.image(blended_img, caption=f'Blended Image with {num_red_patches} Red Patches', use_column_width=True, channels='BGR')
|
75 |
|
76 |
-
#
|
77 |
cv2.imwrite('example/result15.png', blended_img, [int(cv2.IMWRITE_JPEG_QUALITY), 200])
|
78 |
st.success('Saliency detection complete. Result saved as "example/result15.png".')
|
|
|
20 |
_, labels, stats, _ = cv2.connectedComponentsWithStats(red_mask.astype(np.uint8), connectivity=8)
|
21 |
|
22 |
num_red_patches = labels.max()
|
23 |
+
|
|
|
24 |
for i in range(1, num_red_patches + 1):
|
25 |
patch_mask = (labels == i)
|
26 |
patch_centroid_x, patch_centroid_y = int(stats[i, cv2.CC_STAT_LEFT] + stats[i, cv2.CC_STAT_WIDTH] / 2), int(stats[i, cv2.CC_STAT_TOP] + stats[i, cv2.CC_STAT_HEIGHT] / 2)
|
|
|
45 |
|
46 |
pred_saliency = model(img).squeeze().detach().numpy()
|
47 |
|
|
|
48 |
heatmap = (pred_saliency * 255).astype(np.uint8)
|
49 |
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET) # Use a blue colormap (JET)
|
50 |
|
|
|
51 |
heatmap = cv2.resize(heatmap, (image.width, image.height))
|
52 |
|
|
|
53 |
heatmap, num_red_patches = count_and_label_red_patches(heatmap)
|
54 |
|
|
|
55 |
enhanced_image = np.array(image)
|
56 |
b, g, r = cv2.split(enhanced_image)
|
57 |
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
|
58 |
b_enhanced = clahe.apply(b)
|
59 |
enhanced_image = cv2.merge((b_enhanced, g, r))
|
60 |
|
61 |
+
alpha = 0.7
|
62 |
blended_img = cv2.addWeighted(enhanced_image, 1 - alpha, heatmap, alpha, 0)
|
63 |
|
|
|
64 |
st.image(heatmap, caption='Enhanced Saliency Heatmap', use_column_width=True, channels='BGR')
|
65 |
st.image(enhanced_image, caption='Enhanced Blue Image', use_column_width=True, channels='BGR')
|
66 |
|
|
|
67 |
st.image(blended_img, caption=f'Blended Image with {num_red_patches} Red Patches', use_column_width=True, channels='BGR')
|
68 |
|
69 |
+
# Create a dir with name example to save
|
70 |
cv2.imwrite('example/result15.png', blended_img, [int(cv2.IMWRITE_JPEG_QUALITY), 200])
|
71 |
st.success('Saliency detection complete. Result saved as "example/result15.png".')
|