olivas/backend/app/services/aoi_analysis.py
DJP 3467dbcf03 Initial commit — OliVAS visual attention analysis platform
Full-stack application for predicting where humans look in images using
DeepGaze saliency models. Includes heatmap overlays, gaze sequence prediction,
hotspot detection, AOI analysis, rule-based insights, optional Claude AI
design analysis, and professional PDF report generation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 20:20:58 -05:00

47 lines
1.3 KiB
Python

import numpy as np
def compute_aoi_attention(
saliency: np.ndarray,
regions: list[dict],
) -> list[dict]:
"""
Compute attention metrics for user-defined regions.
Returns list of dicts with attention_pct, area_pct, attention_density.
"""
h, w = saliency.shape
total_saliency = saliency.sum()
total_area = h * w
if total_saliency == 0:
return [
{"attention_pct": 0.0, "area_pct": 0.0, "attention_density": 0.0}
for _ in regions
]
results = []
for region in regions:
rx, ry = region["x"], region["y"]
rw, rh = region["width"], region["height"]
# Clamp to image bounds
x1 = max(0, rx)
y1 = max(0, ry)
x2 = min(w, rx + rw)
y2 = min(h, ry + rh)
region_saliency = saliency[y1:y2, x1:x2].sum()
region_area = (x2 - x1) * (y2 - y1)
attention_pct = (region_saliency / total_saliency) * 100
area_pct = (region_area / total_area) * 100
density = attention_pct / max(area_pct, 0.01)
results.append({
"attention_pct": round(float(attention_pct), 2),
"area_pct": round(float(area_pct), 2),
"attention_density": round(float(density), 2),
})
return results