File size: 6,716 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
"""Candidate bounding box detector runner (supports remote gpu3 and local execution)."""
from __future__ import annotations

import argparse
import hashlib
import json
import os
from pathlib import Path
import subprocess
import sys
import time
from typing import Dict, List, Optional
from PIL import Image, ImageDraw, ImageFont

# Add repo root to sys.path
REPO_ROOT = Path(__file__).resolve().parents[3]
if str(REPO_ROOT) not in sys.path:
    sys.path.append(str(REPO_ROOT))


def draw_proposal_overlay(
    image: Image.Image,
    proposals: List[Dict],
    out_path: Path,
    box_color: str = "#2563eb",
    label_bg_color: str = "#2563eb",
    text_color: str = "#ffffff",
):
    """Render a clean neutral overlay showing bounding boxes and stable proposal IDs."""
    overlay = image.copy()
    draw = ImageDraw.Draw(overlay)
    try:
        font = ImageFont.load_default()
    except Exception:
        font = None

    for p in proposals:
        l, t, r, b = p["bbox"]
        pid = p["id"]
        draw.rectangle([l, t, r, b], outline=box_color, width=2)
        # Background badge for label
        badge_w = 48
        badge_h = 16
        draw.rectangle([l, max(0, t - badge_h), l + badge_w, t], fill=label_bg_color)
        draw.text((l + 3, max(0, t - badge_h) + 2), pid, fill=text_color, font=font)

    overlay.save(out_path, format="PNG")


def run_detector_local(
    image_path: Path,
    weights_path: Path,
    device: str = "cpu",
    conf: float = 0.10,
    iou: float = 0.10,
) -> Dict:
    """Run local detector using YOLOv9Detector or Ultralytics."""
    omni_dir = REPO_ROOT / "vlm_testing/click_omniparser/OmniParser"
    if str(omni_dir) not in sys.path:
        sys.path.append(str(omni_dir))
    from util.yolov9 import YOLOv9Detector

    im = Image.open(image_path).convert("RGB")
    w, h = im.size
    weights_hash = hashlib.sha256(weights_path.read_bytes()).hexdigest()

    detector = YOLOv9Detector(str(weights_path), device=device)
    res = detector.predict(im, imgsz=1280, conf=conf, iou=iou)[0]

    proposals = []
    for idx, (box, score) in enumerate(zip(res.boxes.xyxy.cpu().tolist(), res.boxes.conf.cpu().tolist()), start=1):
        pid = f"d_{idx:04d}"
        proposals.append({
            "id": pid,
            "bbox": [round(float(v), 2) for v in box],
            "label": "icon",
            "confidence": round(float(score), 4),
        })

    return {
        "detector": "OmniParser-v2.0-icon_detect",
        "device": device,
        "weights_path": str(weights_path),
        "weights_sha256": weights_hash,
        "image_size": [w, h],
        "conf_threshold": conf,
        "iou_threshold": iou,
        "proposal_count": len(proposals),
        "proposals": proposals,
    }


def run_detector_remote_gpu3(
    image_path: Path,
    remote_host: str = "gpu3",
    remote_workdir: str = "/data/harshitj23/screen-understanding",
    weights_name: str = "omniparser-detector.pt",
    conf: float = 0.10,
    iou: float = 0.10,
) -> Dict:
    """Run detector on remote gpu3 via SSH."""
    im = Image.open(image_path).convert("RGB")
    w, h = im.size

    img_hash = hashlib.sha256(image_path.read_bytes()).hexdigest()[:16]
    remote_img = f"{remote_workdir}/runs/tmp_{img_hash}.png"
    remote_out = f"{remote_workdir}/runs/tmp_{img_hash}_out.json"

    # Copy image to remote
    subprocess.run(["scp", str(image_path), f"{remote_host}:{remote_img}"], check=True)

    remote_script = f"""
source /data/harshitj23/miniforge3/etc/profile.d/conda.sh
conda activate /data/harshitj23/envs/perception-py311
python -c '
import json, hashlib, time
from pathlib import Path
from ultralytics import YOLO
from PIL import Image

weights_file = Path("{remote_workdir}/models/{weights_name}")
weights_hash = hashlib.sha256(weights_file.read_bytes()).hexdigest()
model = YOLO(str(weights_file))

im = Image.open("{remote_img}").convert("RGB")
res = model.predict(im, imgsz=1280, conf={conf}, iou={iou}, device=0, verbose=False)[0]

proposals = []
for idx, (b, s, c) in enumerate(zip(res.boxes.xyxy.cpu().tolist(), res.boxes.conf.cpu().tolist(), res.boxes.cls.cpu().tolist()), start=1):
    proposals.append({{
        "id": f"d_{{idx:04d}}",
        "bbox": [round(float(v), 2) for v in b],
        "label": model.names[int(c)],
        "confidence": round(float(s), 4)
    }})

output = {{
    "detector": "{weights_name}",
    "device": "cuda:0",
    "weights_sha256": weights_hash,
    "image_size": list(im.size),
    "conf_threshold": {conf},
    "iou_threshold": {iou},
    "proposal_count": len(proposals),
    "proposals": proposals
}}
Path("{remote_out}").write_text(json.dumps(output, indent=2))
'
"""
    subprocess.run(["ssh", remote_host, remote_script], check=True)

    # Copy back output
    import tempfile
    with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as tmp:
        tmp_path = Path(tmp.name)
    try:
        subprocess.run(["scp", f"{remote_host}:{remote_out}", str(tmp_path)], check=True)
        data = json.loads(tmp_path.read_text())
    finally:
        if tmp_path.exists():
            tmp_path.unlink()
        subprocess.run(["ssh", remote_host, f"rm -f {remote_img} {remote_out}"])

    return data


def detect_proposals(
    image_path: Path,
    out_dir: Path,
    use_remote: bool = True,
    local_weights: Optional[Path] = None,
) -> Dict:
    """Main entry to run detector and save raw.json and overlay.png."""
    out_dir.mkdir(parents=True, exist_ok=True)
    raw_json_path = out_dir / "raw.json"
    overlay_path = out_dir / "overlay.png"

    result = None
    if use_remote:
        try:
            result = run_detector_remote_gpu3(image_path)
        except Exception as e:
            print(f"[Warning] Remote gpu3 detector failed: {e}. Falling back to local execution.")

    if result is None:
        if local_weights is None:
            local_weights = REPO_ROOT / "vlm_testing/click_omniparser/OmniParser/weights/icon_detect_v3/model.pt"
        result = run_detector_local(image_path, local_weights, device="cpu")

    raw_json_path.write_text(json.dumps(result, indent=2) + "\n")

    # Render overlay
    im = Image.open(image_path).convert("RGB")
    draw_proposal_overlay(im, result["proposals"], overlay_path)

    return result


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--image", type=Path, required=True)
    parser.add_argument("--output-dir", type=Path, required=True)
    parser.add_argument("--local", action="store_true")
    args = parser.parse_args()

    res = detect_proposals(args.image, args.output_dir, use_remote=not args.local)
    print(f"Detected {res['proposal_count']} proposals. Saved to {args.output_dir}")