Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -49,6 +49,31 @@ def postprocess_output(output_tensor, vp, hp, original_shape):
|
|
| 49 |
return output_tensor.numpy().squeeze()
|
| 50 |
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
def compute_saliency_map(pil_image):
|
| 53 |
model = load_msi_net()
|
| 54 |
input_image = np.array(pil_image.convert("RGB"), dtype=np.float32)
|
|
@@ -57,12 +82,18 @@ def compute_saliency_map(pil_image):
|
|
| 57 |
input_tensor, v_pad, h_pad = preprocess_input(input_image, target_shape)
|
| 58 |
raw_output = model(input_tensor)
|
| 59 |
output_tensor = list(raw_output.values())[0] if isinstance(raw_output, dict) else raw_output
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
def render_heatmap_overlay(img_bgr, saliency_map, alpha=0.45):
|
|
@@ -261,12 +292,13 @@ def analyze(pil_image, overlay_strength):
|
|
| 261 |
|
| 262 |
summary += (
|
| 263 |
"\n---\n"
|
| 264 |
-
"**Methodology:** Attention prediction
|
| 265 |
"(Kroner et al., *Neural Networks*, 2020, "
|
| 266 |
"[DOI: 10.1016/j.neunet.2020.05.004](https://doi.org/10.1016/j.neunet.2020.05.004)), "
|
| 267 |
-
"
|
| 268 |
-
"
|
| 269 |
-
"
|
|
|
|
| 270 |
)
|
| 271 |
|
| 272 |
return Image.fromarray(overlay_rgb), summary
|
|
|
|
| 49 |
return output_tensor.numpy().squeeze()
|
| 50 |
|
| 51 |
|
| 52 |
+
def compute_text_boost_map(pil_img):
|
| 53 |
+
"""
|
| 54 |
+
Large legible text reliably draws real human attention (confirmed by
|
| 55 |
+
eye-tracking research), but MSI-Net (a bottom-up model) can underweight
|
| 56 |
+
it relative to small high-contrast graphics. This adds a modest,
|
| 57 |
+
size-proportional boost to detected text regions to correct that.
|
| 58 |
+
"""
|
| 59 |
+
img_arr = np.array(pil_img.convert("RGB"))
|
| 60 |
+
h, w = img_arr.shape[:2]
|
| 61 |
+
data = pytesseract.image_to_data(img_arr, config="--psm 11", output_type=pytesseract.Output.DICT)
|
| 62 |
+
text_map = np.zeros((h, w), dtype=np.float32)
|
| 63 |
+
for i in range(len(data['text'])):
|
| 64 |
+
word = data['text'][i].strip()
|
| 65 |
+
tw_, th_ = data['width'][i], data['height'][i]
|
| 66 |
+
if not word or th_ < 8 or tw_ < 5:
|
| 67 |
+
continue
|
| 68 |
+
x, y = data['left'][i], data['top'][i]
|
| 69 |
+
weight = min(1.0, (th_ / h) * 6)
|
| 70 |
+
pad = int(th_ * 0.15)
|
| 71 |
+
y0, y1 = max(0, y - pad), min(h, y + th_ + pad)
|
| 72 |
+
x0, x1 = max(0, x - pad), min(w, x + tw_ + pad)
|
| 73 |
+
text_map[y0:y1, x0:x1] = np.maximum(text_map[y0:y1, x0:x1], weight)
|
| 74 |
+
return text_map
|
| 75 |
+
|
| 76 |
+
|
| 77 |
def compute_saliency_map(pil_image):
|
| 78 |
model = load_msi_net()
|
| 79 |
input_image = np.array(pil_image.convert("RGB"), dtype=np.float32)
|
|
|
|
| 82 |
input_tensor, v_pad, h_pad = preprocess_input(input_image, target_shape)
|
| 83 |
raw_output = model(input_tensor)
|
| 84 |
output_tensor = list(raw_output.values())[0] if isinstance(raw_output, dict) else raw_output
|
| 85 |
+
msi_saliency = postprocess_output(output_tensor, v_pad, h_pad, original_shape)
|
| 86 |
+
msi_saliency = msi_saliency.astype(np.float32)
|
| 87 |
+
msi_saliency -= msi_saliency.min()
|
| 88 |
+
if msi_saliency.max() > 0:
|
| 89 |
+
msi_saliency /= msi_saliency.max()
|
| 90 |
+
|
| 91 |
+
text_boost = compute_text_boost_map(pil_image)
|
| 92 |
+
combined = 0.7 * msi_saliency + 0.3 * text_boost
|
| 93 |
+
combined -= combined.min()
|
| 94 |
+
if combined.max() > 0:
|
| 95 |
+
combined /= combined.max()
|
| 96 |
+
return combined
|
| 97 |
|
| 98 |
|
| 99 |
def render_heatmap_overlay(img_bgr, saliency_map, alpha=0.45):
|
|
|
|
| 292 |
|
| 293 |
summary += (
|
| 294 |
"\n---\n"
|
| 295 |
+
"**Methodology:** Attention prediction combines MSI-Net, a peer-reviewed saliency model "
|
| 296 |
"(Kroner et al., *Neural Networks*, 2020, "
|
| 297 |
"[DOI: 10.1016/j.neunet.2020.05.004](https://doi.org/10.1016/j.neunet.2020.05.004)), "
|
| 298 |
+
"with a size-proportional boost for legible text - since eye-tracking research confirms "
|
| 299 |
+
"large headline text reliably draws attention, which pure bottom-up saliency models can "
|
| 300 |
+
"underweight relative to small high-contrast graphics. This is a data-informed prediction, "
|
| 301 |
+
"not a substitute for live user testing."
|
| 302 |
)
|
| 303 |
|
| 304 |
return Image.fromarray(overlay_rgb), summary
|