Spaces:
Runtime error
Runtime error
Update app.py
Browse files243 unmodified lines2442452462472482492472512522532482492502511 unmodified line253254255256257258259260261261262263264265243 unmodified lines w, h = image_pil.size size = max(w, h)
# Resize CAM to match image cam_resized = np.array(Image.fromarray(cam).resize((size, size), resample=Image.Resampling.BILINEAR))
# Normalize CAM to [0, 1] cam_norm = (cam_resized - cam_resized.min()) / (np.ptp(cam_resized) + 1e-8)
cam_norm = transforms.CenterCrop((h, w))(cam_norm) cam_norm = (cam - cam.min()) / (np.ptp(cam) + 1e-8)
# Create heatmap using matplotlib colormap colormap = cm.get_cmap('jet')1 unmodified line cam_alpha = (cam_norm >= vis_threshold).astype(np.float32) * alpha # Alpha mask
cam_rgba = np.dstack((cam_colored, cam_alpha)) # Shape: (H, W, 4) # Resize CAM to match image cam_resized = np.array(Image.fromarray(cam_rgba).resize((size, size), resample=Image.Resampling.BILINEAR))
cam_resized = transforms.CenterCrop((h, w))(torch.tensor(cam_resized)).numpy()
cam_image = Image.fromarray((cam_rgba * 255).astype(np.uint8), mode="RGBA") cam_image = Image.fromarray((cam_resized * 255).astype(np.uint8), mode="RGBA")
# Composite over original composite = Image.alpha_composite(image_pil, cam_image)app.py+8-7