Spaces:
Sleeping
Sleeping
Update app.py
Browse files48 unmodified lines4950515253542 unmodified lines575859606162636465666768192 unmodified lines26126226326426526626726826927027127248 unmodified linesreturn output_tensor.numpy().squeeze()def compute_saliency_map(pil_image):model = load_msi_net()input_image = np.array(pil_image.convert("RGB"), dtype=np.float32)2 unmodified linesinput_tensor, v_pad, h_pad = preprocess_input(input_image, target_shape)raw_output = model(input_tensor)output_tensor = list(raw_output.values())[0] if isinstance(raw_output, dict) else raw_outputsaliency = postprocess_output(output_tensor, v_pad, h_pad, original_shape)saliency = saliency.astype(np.float32)saliency -= saliency.min()if saliency.max() > 0:saliency /= saliency.max()return saliencydef render_heatmap_overlay(img_bgr, saliency_map, alpha=0.45):192 unmodified linessummary += ("\n---\n""**Methodology:** Attention prediction powered by MSI-Net, a peer-reviewed saliency model ""(Kroner et al., *Neural Networks*, 2020, ""[DOI: 10.1016/j.neunet.2020.05.004](https://doi.org/10.1016/j.neunet.2020.05.004)), ""evaluated on the MIT/Tübingen Saliency Benchmark - the same standard used by commercial ""tools like Attention Insight. This is a data-informed prediction, not a substitute for ""live user testing.")return Image.fromarray(overlay_rgb), summary48 unmodified lines495051525354555657585960616263646566676869707172737475767778792 unmodified lines828384858687888990919293949596979899192 unmodified lines29229329429529629729829930030130230330448 unmodified linesreturn output_tensor.numpy().squeeze()def compute_text_boost_map(pil_img):"""Large legible text reliably draws real human attention (confirmed byeye-tracking research), but MSI-Net (a bottom-up model) can underweightit relative to small high-contrast graphics. This adds a modest,size-proportional boost to detected text regions to correct that."""img_arr = np.array(pil_img.convert("RGB"))h, w = img_arr.shape[:2]data = pytesseract.image_to_data(img_arr, config="--psm 11", output_type=pytesseract.Output.DICT)text_map = np.zeros((h, w), dtype=np.float32)for i in range(len(data['text'])):word = data['text'][i].strip()tw_, th_ = data['width'][i], data['height'][i]if not word or th_ < 8 or tw_ < 5:continuex, y = data['left'][i], data['top'][i]weight = min(1.0, (th_ / h) * 6)pad = int(th_ * 0.15)y0, y1 = max(0, y - pad), min(h, y + th_ + pad)x0, x1 = max(0, x - pad), min(w, x + tw_ + pad)text_map[y0:y1, x0:x1] = np.maximum(text_map[y0:y1, x0:x1], weight)return text_mapdef compute_saliency_map(pil_image):model = load_msi_net()input_image = np.array(pil_image.convert("RGB"), dtype=np.float32)2 unmodified linesinput_tensor, v_pad, h_pad = preprocess_input(input_image, target_shape)raw_output = model(input_tensor)output_tensor = list(raw_output.values())[0] if isinstance(raw_output, dict) else raw_outputmsi_saliency = postprocess_output(output_tensor, v_pad, h_pad, original_shape)msi_saliency = msi_saliency.astype(np.float32)msi_saliency -= msi_saliency.min()if msi_saliency.max() > 0:msi_saliency /= msi_saliency.max()text_boost = compute_text_boost_map(pil_image)combined = 0.7 * msi_saliency + 0.3 * text_boostcombined -= combined.min()if combined.max() > 0:combined /= combined.max()return combineddef render_heatmap_overlay(img_bgr, saliency_map, alpha=0.45):192 unmodified linessummary += ("\n---\n""**Methodology:** Attention prediction combines MSI-Net, a peer-reviewed saliency model ""(Kroner et al., *Neural Networks*, 2020, ""[DOI: 10.1016/j.neunet.2020.05.004](https://doi.org/10.1016/j.neunet.2020.05.004)), ""with a size-proportional boost for legible text - since eye-tracking research confirms ""large headline text reliably draws attention, which pure bottom-up saliency models can ""underweight relative to small high-contrast graphics. This is a data-informed prediction, ""not a substitute for live user testing.")return Image.fromarray(overlay_rgb), summary
app.py+42-10