|
| 1 | +"""Detect the display scale a template renders at (visual DPI). |
| 2 | +
|
| 3 | +A template cropped at 100% display scale will not match pixel-for-pixel on a |
| 4 | +machine running at 150% DPI — everything is 1.5x bigger. ``visual_match. |
| 5 | +match_template`` *can* sweep scales, but it returns only the single best match's |
| 6 | +location and throws the per-scale scores away. ``scale_detect`` keeps the whole |
| 7 | +profile: it scores the template against the haystack at a range of scales and |
| 8 | +reports *which scale wins, by how much*, so an automation can infer the effective |
| 9 | +UI scale / DPI and how confident that inference is. |
| 10 | +
|
| 11 | +It reuses ``visual_match._score_map`` (the full ``matchTemplate`` surface, |
| 12 | +oriented higher = better) for each scale, so the source is any ndarray / path / |
| 13 | +PIL image (or the live screen). cv2 / numpy are lazily imported. Imports no |
| 14 | +``PySide6``. |
| 15 | +""" |
| 16 | +from typing import Any, Dict, List, Optional, Sequence |
| 17 | + |
| 18 | +ImageSource = Any |
| 19 | +# Common Windows display scales (100% / 125% / 150% / 175% / 200%). |
| 20 | +_DEFAULT_SCALES = (1.0, 1.25, 1.5, 1.75, 2.0) |
| 21 | + |
| 22 | + |
| 23 | +def _score_at(template: ImageSource, haystack: Optional[ImageSource], |
| 24 | + region: Optional[Sequence[int]], method: str, |
| 25 | + scale: float) -> Optional[Dict[str, Any]]: |
| 26 | + import cv2 |
| 27 | + from je_auto_control.utils.visual_match.visual_match import _score_map |
| 28 | + score_map, tmpl = _score_map(template, haystack, region=region, |
| 29 | + method=method, scale=scale) |
| 30 | + if score_map is None: |
| 31 | + return None # template larger than haystack at this scale |
| 32 | + _min_v, max_v, _min_loc, max_loc = cv2.minMaxLoc(score_map) |
| 33 | + height, width = int(tmpl.shape[0]), int(tmpl.shape[1]) |
| 34 | + x, y = int(max_loc[0]), int(max_loc[1]) |
| 35 | + return {"scale": float(scale), "score": float(max_v), "x": x, "y": y, |
| 36 | + "width": width, "height": height, |
| 37 | + "center": [x + width // 2, y + height // 2]} |
| 38 | + |
| 39 | + |
| 40 | +def scale_sweep(template: ImageSource, haystack: Optional[ImageSource] = None, *, |
| 41 | + region: Optional[Sequence[int]] = None, |
| 42 | + scales: Optional[Sequence[float]] = None, |
| 43 | + method: str = "ccoeff_normed") -> List[Dict[str, Any]]: |
| 44 | + """Score ``template`` against the haystack at each scale. |
| 45 | +
|
| 46 | + Returns ``[{scale, score, x, y, width, height, center}]`` (best match per |
| 47 | + scale), skipping scales at which the template is larger than the haystack. |
| 48 | + """ |
| 49 | + chosen = tuple(scales) if scales else _DEFAULT_SCALES |
| 50 | + results = [] |
| 51 | + for scale in chosen: |
| 52 | + entry = _score_at(template, haystack, region, method, float(scale)) |
| 53 | + if entry is not None: |
| 54 | + results.append(entry) |
| 55 | + return results |
| 56 | + |
| 57 | + |
| 58 | +def detect_scale(template: ImageSource, haystack: Optional[ImageSource] = None, *, |
| 59 | + region: Optional[Sequence[int]] = None, |
| 60 | + scales: Optional[Sequence[float]] = None, |
| 61 | + method: str = "ccoeff_normed") -> Optional[Dict[str, Any]]: |
| 62 | + """Infer the display scale ``template`` renders at (its visual DPI). |
| 63 | +
|
| 64 | + Returns ``{scale, scale_percent, score, center, margin, candidates}`` — the |
| 65 | + winning scale, its percentage, the match score, and ``margin`` (how far it |
| 66 | + beats the runner-up: a confidence in the inference). ``None`` if no scale |
| 67 | + matched (template never fit the haystack). |
| 68 | + """ |
| 69 | + sweep = scale_sweep(template, haystack, region=region, scales=scales, |
| 70 | + method=method) |
| 71 | + if not sweep: |
| 72 | + return None |
| 73 | + ranked = sorted(sweep, key=lambda entry: entry["score"], reverse=True) |
| 74 | + best = ranked[0] |
| 75 | + margin = best["score"] - ranked[1]["score"] if len(ranked) > 1 else best["score"] |
| 76 | + return {"scale": best["scale"], "scale_percent": round(best["scale"] * 100), |
| 77 | + "score": best["score"], "center": best["center"], |
| 78 | + "margin": float(margin), "candidates": sweep} |
0 commit comments