Spaces:
Sleeping
Sleeping
Update saliency_gradio.py
Browse files- saliency_gradio.py +10 -2
saliency_gradio.py
CHANGED
@@ -51,18 +51,26 @@ def postprocess_output(output_tensor, vertical_padding, horizontal_padding, orig
|
|
51 |
output_tensor = tf.image.resize(output_tensor, original_shape)
|
52 |
return output_tensor.numpy().squeeze()
|
53 |
|
54 |
-
def process_image(input_image):
|
55 |
input_image = np.array(input_image, dtype=np.float32)
|
56 |
original_shape = input_image.shape[:2]
|
57 |
target_shape = get_target_shape(original_shape)
|
|
|
58 |
input_tensor, vertical_padding, horizontal_padding = preprocess_input(input_image, target_shape)
|
59 |
output_tensor = model(input_tensor)["output"]
|
60 |
saliency_gray = postprocess_output(output_tensor, vertical_padding, horizontal_padding, original_shape)
|
|
|
61 |
total_saliency = np.sum(saliency_gray)
|
|
|
|
|
|
|
62 |
saliency_rgb = plt.cm.inferno(saliency_gray)[..., :3]
|
63 |
alpha = 0.9
|
64 |
blended_image = alpha * saliency_rgb + (1 - alpha) * input_image / 255
|
65 |
-
|
|
|
|
|
|
|
66 |
|
67 |
def predict_single(image):
|
68 |
return process_image(image)
|
|
|
51 |
output_tensor = tf.image.resize(output_tensor, original_shape)
|
52 |
return output_tensor.numpy().squeeze()
|
53 |
|
54 |
+
def process_image(input_image, threshold=0.5):
|
55 |
input_image = np.array(input_image, dtype=np.float32)
|
56 |
original_shape = input_image.shape[:2]
|
57 |
target_shape = get_target_shape(original_shape)
|
58 |
+
|
59 |
input_tensor, vertical_padding, horizontal_padding = preprocess_input(input_image, target_shape)
|
60 |
output_tensor = model(input_tensor)["output"]
|
61 |
saliency_gray = postprocess_output(output_tensor, vertical_padding, horizontal_padding, original_shape)
|
62 |
+
|
63 |
total_saliency = np.sum(saliency_gray)
|
64 |
+
above_threshold_saliency = np.sum(saliency_gray[saliency_gray > threshold])
|
65 |
+
ratio = above_threshold_saliency / total_saliency if total_saliency > 0 else 0.0
|
66 |
+
|
67 |
saliency_rgb = plt.cm.inferno(saliency_gray)[..., :3]
|
68 |
alpha = 0.9
|
69 |
blended_image = alpha * saliency_rgb + (1 - alpha) * input_image / 255
|
70 |
+
|
71 |
+
summary_text = f"{ratio:.4f}"
|
72 |
+
|
73 |
+
return blended_image, summary_text
|
74 |
|
75 |
def predict_single(image):
|
76 |
return process_image(image)
|