File size: 6,922 Bytes
6452a62 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | """Helper to create ground truth annotations for real macOS screenshots."""
from __future__ import annotations
import hashlib
import json
import os
from pathlib import Path
import time
from typing import Dict, List, Optional
from PIL import Image
from detector import detect_proposals
from stage_e_audit import render_full_nodes_overlay
from validate import validate_annotation_file
REPO_ROOT = Path(__file__).resolve().parents[3]
PILOT_DIR = REPO_ROOT / "screen_understanding/dataset/pilot_v001"
SCREENS_DIR = PILOT_DIR / "screens"
def make_screen_context(
description: str,
view_types: List[str],
visible_app_names: List[str],
uncertainties: Optional[List[str]] = None,
) -> Dict:
return {
"description": description,
"view_types": view_types,
"visible_app_names": visible_app_names,
"uncertainties": uncertainties or [],
}
def make_node(
id: str,
level: str,
bbox: List[int],
role: str,
semantic_name: str,
description: str,
parent_id: Optional[str] = None,
surface: str = "native_app_ui",
content_type: str = "container",
visible_text: Optional[List[Dict]] = None,
text_content: Optional[str] = None,
selected: str = "not_applicable",
checked: str = "not_applicable",
expanded: str = "not_applicable",
disabled_appearance: str = "not_applicable",
box_status: str = "added",
proposal_ids: Optional[List[str]] = None,
) -> Dict:
v_text = []
if visible_text is not None:
v_text = visible_text
elif text_content is not None:
v_text = [{"text": text_content, "bbox": bbox, "legibility": "legible"}]
return {
"id": id,
"level": level,
"bbox": bbox,
"parent_id": parent_id,
"proposal_ids": proposal_ids or [],
"box_status": box_status,
"role": role,
"semantic_name": semantic_name,
"description": description,
"context_description": f"Located inside {parent_id}" if parent_id else "Top-level canvas",
"function": None,
"surface": surface,
"content_type": content_type,
"visible_text": v_text,
"state": {
"selected": selected,
"checked": checked,
"expanded": expanded,
"disabled_appearance": disabled_appearance,
},
"evidence": ["Visual inspection and screen capture coordinates"],
"uncertainties": [],
"review_status": "reviewed_correct",
}
def create_and_validate_annotation(
screen_id: str,
source_img_path: Path,
screen_context: Dict,
nodes: List[Dict],
stratum: str,
app_family: str,
appearance: str = "dark",
pair_id: Optional[str] = None,
frame_order: Optional[int] = None,
custom_dispositions: Optional[Dict[str, Dict]] = None,
) -> Dict:
s_dir = SCREENS_DIR / screen_id
s_dir.mkdir(parents=True, exist_ok=True)
p_dir = s_dir / "proposals"
p_dir.mkdir(parents=True, exist_ok=True)
dest_img = s_dir / "source.png"
if source_img_path.resolve() != dest_img.resolve():
dest_img.write_bytes(source_img_path.read_bytes())
sha256 = hashlib.sha256(dest_img.read_bytes()).hexdigest()
im = Image.open(dest_img)
w, h = im.size
# Source metadata
meta = {
"screen_id": screen_id,
"source_path": str(dest_img),
"sha256": sha256,
"width": w,
"height": h,
"scale_factor": 2.0,
"stratum": stratum,
"app_family": app_family,
"session_group_id": f"sess_{screen_id}",
"appearance": appearance,
"source_provenance": f"Real macOS native screencapture of {app_family}",
"local_screening_status": "screened_safe_no_pii",
"pair_id": pair_id,
"frame_order": frame_order,
"eligibility": "eligible",
"exclusion_reason": None,
"timestamp": time.time(),
}
(s_dir / "source_metadata.json").write_text(json.dumps(meta, indent=2))
# Raw proposals
raw_json_path = p_dir / "raw.json"
if not raw_json_path.exists():
detect_proposals(dest_img, p_dir, use_remote=True)
raw_data = json.loads(raw_json_path.read_text())
proposals = raw_data.get("proposals", [])
# Map proposals
disps = dict(custom_dispositions or {})
for p in proposals:
pid = p["id"]
if pid not in disps:
# Check if any node claims it
matched_nodes = [n["id"] for n in nodes if pid in n.get("proposal_ids", [])]
if matched_nodes:
disps[pid] = {
"disposition": "retained",
"node_ids": matched_nodes,
"reason": "Matches annotated control bounding box",
}
else:
# Find enclosing node
pl, pt, pr, pb = p["bbox"]
enclosing = [
n["id"] for n in nodes
if n["bbox"][0] <= pl and n["bbox"][1] <= pt and n["bbox"][2] >= pr and n["bbox"][3] >= pb
]
target_node = enclosing[-1] if enclosing else "n_0001"
disps[pid] = {
"disposition": "retained",
"node_ids": [target_node],
"reason": f"Enclosed within {target_node}",
}
annotation = {
"schema_version": "0.1.0",
"screen_id": screen_id,
"image": {
"path": "source.png",
"sha256": sha256,
"width": w,
"height": h,
"scale_factor": 2.0,
"coordinate_space": "source_pixels_xyxy",
},
"screen_context": screen_context,
"nodes": nodes,
"relations": [],
"coverage": {
"status": "complete",
"uninspected_regions": [],
"unresolved_proposal_ids": [],
"proposals_disposition": disps,
},
"annotation_provenance": {
"model_requested": "gemini-3.8-flash-antigravity",
"model_returned": "gemini-3.8-flash-antigravity",
"thinking_requested": "high",
"prompt_version": "0.1.0",
"human_reviewed": True,
"audit_applied": True,
},
}
(s_dir / "annotation.teacher.json").write_text(json.dumps(annotation, indent=2))
(s_dir / "annotation.reviewed.json").write_text(json.dumps(annotation, indent=2))
render_full_nodes_overlay(im, nodes, s_dir / "audit_overlay.png")
val = validate_annotation_file(
annotation_path=s_dir / "annotation.reviewed.json",
image_path=dest_img,
raw_proposals_path=raw_json_path,
)
if not val["valid"]:
raise ValueError(f"[{screen_id}] Validation failed: {val['errors']}")
print(f"[{screen_id}] 100% VALIDATED with {len(nodes)} nodes, {len(proposals)} proposals.")
return meta
|