noracu 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +647 -3
- package/README.md +109 -188
- package/SECURITY.md +44 -1
- package/docs/README.zh-CN.md +143 -0
- package/docs/apps.md +38 -0
- package/docs/architecture.md +9 -9
- package/docs/ios-runner-protocol.md +20 -4
- package/docs/iphone.md +59 -0
- package/docs/known-issues.md +84 -0
- package/docs/tools.md +7 -7
- package/docs/troubleshooting.md +29 -0
- package/docs/usage.md +73 -11
- package/imctl/src/imctl/cli.py +116 -54
- package/imctl/src/imctl/gestures.py +96 -65
- package/imctl/src/imctl/input.py +104 -62
- package/imctl/src/imctl/screenrect.py +3 -1
- package/imctl/src/imctl/verify.py +72 -53
- package/imctl/src/imctl/vision.py +45 -24
- package/imctl/src/imctl/window.py +83 -37
- package/ios-runner/NoracuRunnerUITests/NoracuRunner.swift +114 -61
- package/ios-runner/NoracuRunnerUITests/RunnerCommands.swift +14 -4
- package/ios-runner/NoracuRunnerUITests/RunnerServer.swift +1 -2
- package/ios-runner/README.md +1 -1
- package/package.json +13 -6
- package/src/approval.js +66 -25
- package/src/bg-inject.js +9 -9
- package/src/doctor.js +156 -0
- package/src/ios-session.js +172 -40
- package/src/ios-setup.js +31 -14
- package/src/ios-xcuitest.js +71 -15
- package/src/mac-ax.js +535 -125
- package/src/server.mjs +928 -262
- package/README.zh-CN.md +0 -245
|
@@ -10,7 +10,6 @@ that OpenAI's Computer Use enforces with its revision mechanism.
|
|
|
10
10
|
from __future__ import annotations
|
|
11
11
|
|
|
12
12
|
import hashlib
|
|
13
|
-
import subprocess
|
|
14
13
|
import tempfile
|
|
15
14
|
import unicodedata
|
|
16
15
|
from dataclasses import dataclass
|
|
@@ -31,31 +30,13 @@ class ScreenState:
|
|
|
31
30
|
def capture_hash() -> ScreenState:
|
|
32
31
|
"""Capture the window and return a content fingerprint.
|
|
33
32
|
|
|
34
|
-
The
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
The window's own pixels (window_luma: by window id, so a window on top of it does not count), downsampled
|
|
34
|
+
to about half the window to suppress video-stream noise (compression flicker), hashed in memory. It used to
|
|
35
|
+
write a screencapture of the window's screen region and a downsampled copy to two tempfile.mktemp paths.
|
|
37
36
|
"""
|
|
38
37
|
b = get_bounds()
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
try:
|
|
42
|
-
subprocess.run(
|
|
43
|
-
["screencapture", "-x", f"-R{b.x},{b.y},{b.width},{b.height}", raw],
|
|
44
|
-
check=True,
|
|
45
|
-
timeout=10,
|
|
46
|
-
)
|
|
47
|
-
# Downsample to ~163x360 (half window) to suppress stream noise.
|
|
48
|
-
subprocess.run(
|
|
49
|
-
["sips", "-z", "360", "163", raw, "--out", small],
|
|
50
|
-
check=True,
|
|
51
|
-
capture_output=True,
|
|
52
|
-
timeout=10,
|
|
53
|
-
)
|
|
54
|
-
with open(small, "rb") as f:
|
|
55
|
-
digest = hashlib.md5(f.read()).hexdigest()
|
|
56
|
-
return ScreenState(md5=digest, width=b.width, height=b.height)
|
|
57
|
-
finally:
|
|
58
|
-
subprocess.run(["rm", "-f", raw, small], capture_output=True)
|
|
38
|
+
digest = hashlib.md5(window_luma((163, 360))).hexdigest()
|
|
39
|
+
return ScreenState(md5=digest, width=b.width, height=b.height)
|
|
59
40
|
|
|
60
41
|
|
|
61
42
|
def changed(before: ScreenState, after: ScreenState) -> bool:
|
|
@@ -103,14 +84,14 @@ def window_luma(size: tuple[int, int] = (36, 78)) -> bytes:
|
|
|
103
84
|
Reads the window's own pixels with CGWindowListCreateImage — valid
|
|
104
85
|
even when other windows cover it — and renders them into an 8-bit
|
|
105
86
|
gray bitmap whose backing store is a Python bytearray. No temp
|
|
106
|
-
files, no sips, no subprocess
|
|
87
|
+
files, no sips, no subprocess. Returns
|
|
107
88
|
``w*h`` bytes, one gray level (0-255) per pixel. Raises WindowError
|
|
108
89
|
when the window cannot be found, is not on screen (hidden,
|
|
109
90
|
minimized or on another Space), its image cannot be captured, or
|
|
110
91
|
the frame is a near-single gray level: during window transitions
|
|
111
92
|
CGWindowListCreateImage can hand back a blank/transparent frame,
|
|
112
93
|
and a frame diff against it "confirms" changes that never happened
|
|
113
|
-
on the phone
|
|
94
|
+
on the phone.
|
|
114
95
|
"""
|
|
115
96
|
import Quartz
|
|
116
97
|
|
|
@@ -143,8 +124,8 @@ def window_luma(size: tuple[int, int] = (36, 78)) -> bytes:
|
|
|
143
124
|
luma = bytes(buf)
|
|
144
125
|
if max(luma) - min(luma) >= 4:
|
|
145
126
|
return luma
|
|
146
|
-
#
|
|
147
|
-
# moment; look again shortly. Only a real frame is ever accepted
|
|
127
|
+
# A transition can hand back no image or a blank frame for a
|
|
128
|
+
# moment; look again shortly. Only a real frame is ever accepted.
|
|
148
129
|
if attempt < 2:
|
|
149
130
|
time.sleep(0.12)
|
|
150
131
|
if img is None:
|
|
@@ -211,7 +192,7 @@ def screen_rect(size: tuple[int, int] = SCREEN_ALPHA_SIZE):
|
|
|
211
192
|
def is_capture_error(exc: BaseException) -> bool:
|
|
212
193
|
"""True when ``exc`` means the pixels a check needed are not the phone's.
|
|
213
194
|
|
|
214
|
-
|
|
195
|
+
WindowError (window missing, offscreen, or a blank/transparent
|
|
215
196
|
frame) and CaptureError (screencapture by window id failed) both
|
|
216
197
|
mean a verification could not observe the screen — every check that
|
|
217
198
|
depends on a capture must count that as "unconfirmed", never as
|
|
@@ -243,19 +224,19 @@ def text_probe(text: str) -> str | None:
|
|
|
243
224
|
return probe if len(probe) >= 2 else None
|
|
244
225
|
|
|
245
226
|
|
|
246
|
-
# 0.2.5
|
|
227
|
+
# 0.2.5: a typed text is confirmed only by a sighting where a field can be —
|
|
247
228
|
# a spot where the text's probe was not seen before the paste, below the strip at the top where the
|
|
248
229
|
# status bar and notification banners are drawn. A rise in the whole-screen count alone (a banner that
|
|
249
230
|
# carries the same words, a label somewhere else) no longer confirms a paste.
|
|
250
231
|
BANNER_STRIP_NY = 0.15
|
|
251
|
-
#
|
|
232
|
+
# a text shorter than this (after the probe's compaction) is never
|
|
252
233
|
# confirmed by a sighting — "in" is part of "Sign in", "ok" of "Book" — so it never licenses a restore either
|
|
253
234
|
VERIFY_MIN_PROBE = 4
|
|
254
235
|
|
|
255
236
|
|
|
256
237
|
def probe_spots(text: str, hits) -> list[tuple[float, float]] | None:
|
|
257
238
|
"""Where ``text``'s probe shows in ``hits`` (one spot per OCR line that holds it), leaving out the
|
|
258
|
-
paste-progress box
|
|
239
|
+
paste-progress box and the banner strip at the top; None when the text is too short for a
|
|
259
240
|
probe. Hits without a position never count."""
|
|
260
241
|
probe = text_probe(text)
|
|
261
242
|
if probe is None:
|
|
@@ -263,9 +244,11 @@ def probe_spots(text: str, hits) -> list[tuple[float, float]] | None:
|
|
|
263
244
|
if len(probe) < VERIFY_MIN_PROBE:
|
|
264
245
|
return []
|
|
265
246
|
spots = []
|
|
266
|
-
|
|
247
|
+
hits = list(hits)
|
|
248
|
+
hud = _paste_hud_parts(hits) # the box's hits, also when it was read as two
|
|
249
|
+
for i, hit in enumerate(hits):
|
|
267
250
|
nx, ny = getattr(hit, "nx", None), getattr(hit, "ny", None)
|
|
268
|
-
if nx is None or ny is None or ny < BANNER_STRIP_NY or
|
|
251
|
+
if nx is None or ny is None or ny < BANNER_STRIP_NY or i in hud:
|
|
269
252
|
continue
|
|
270
253
|
if probe in _compact(getattr(hit, "text", hit)):
|
|
271
254
|
spots.append((nx, ny))
|
|
@@ -275,14 +258,16 @@ def probe_spots(text: str, hits) -> list[tuple[float, float]] | None:
|
|
|
275
258
|
def strip_spots(text: str, hits) -> list[tuple[float, float]]:
|
|
276
259
|
"""Where ``text``'s probe shows inside the banner strip at the top (the spots probe_spots leaves
|
|
277
260
|
out), the paste-progress box aside; [] when the text is too short to be confirmed at all. Only
|
|
278
|
-
explains an unconfirmed paste
|
|
261
|
+
explains an unconfirmed paste — never confirms one."""
|
|
279
262
|
probe = text_probe(text)
|
|
280
263
|
if probe is None or len(probe) < VERIFY_MIN_PROBE:
|
|
281
264
|
return []
|
|
282
265
|
spots = []
|
|
283
|
-
|
|
266
|
+
hits = list(hits)
|
|
267
|
+
hud = _paste_hud_parts(hits)
|
|
268
|
+
for i, hit in enumerate(hits):
|
|
284
269
|
nx, ny = getattr(hit, "nx", None), getattr(hit, "ny", None)
|
|
285
|
-
if nx is None or ny is None or ny >= BANNER_STRIP_NY or
|
|
270
|
+
if nx is None or ny is None or ny >= BANNER_STRIP_NY or i in hud:
|
|
286
271
|
continue
|
|
287
272
|
if probe in _compact(getattr(hit, "text", hit)):
|
|
288
273
|
spots.append((nx, ny))
|
|
@@ -297,16 +282,18 @@ def has_new_spot(before, after, eps: float = 0.03) -> bool:
|
|
|
297
282
|
def probe_count(text: str, hits) -> int | None:
|
|
298
283
|
"""How many times ``text``'s probe occurs in the joined OCR ``hits``;
|
|
299
284
|
None when the text is too short for a probe. The phone's "Pasting from …"
|
|
300
|
-
box is left out
|
|
285
|
+
box is left out: its words are never the typed text, even when the
|
|
301
286
|
probe is a piece of them ("from", "pro" in a Mac named "M5pro")."""
|
|
302
287
|
probe = text_probe(text)
|
|
303
288
|
if probe is None:
|
|
304
289
|
return None
|
|
305
|
-
|
|
306
|
-
|
|
290
|
+
hits = list(hits)
|
|
291
|
+
hud = _paste_hud_parts(hits) # the box's hits, also when it was read as two
|
|
292
|
+
return _compact("".join(getattr(hit, "text", hit) for i, hit in enumerate(hits)
|
|
293
|
+
if i not in hud)).count(probe)
|
|
307
294
|
|
|
308
295
|
|
|
309
|
-
#
|
|
296
|
+
# 2026-11-05 real device, load ~25: when the phone is slow to fetch a paste
|
|
310
297
|
# it shows a "Pasting from <Mac name>…" progress box mid-screen. That box alone
|
|
311
298
|
# moves the whole-window frame diff past TYPE_MIN_DIFF while the text has not
|
|
312
299
|
# arrived, and type_text() reported such pastes as typed. English UI only: an
|
|
@@ -318,6 +305,36 @@ def _is_paste_hud(hit) -> bool:
|
|
|
318
305
|
return PASTE_HUD_PROBE in _compact(getattr(hit, "text", hit))
|
|
319
306
|
|
|
320
307
|
|
|
308
|
+
# two hits whose centres are this close in height are words of one line…
|
|
309
|
+
SAME_LINE_NY = 0.012
|
|
310
|
+
# …and the two words of the box are next to each other: centres no further apart than this (the box is narrower
|
|
311
|
+
# than the screen). Two hits at the two ends of a line are not it.
|
|
312
|
+
HUD_PAIR_MAX_DX = 0.5
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def _paste_hud_parts(hits) -> set:
|
|
316
|
+
"""Indexes of the hits that make up the "Pasting from …" box: a hit that reads it alone; two
|
|
317
|
+
neighbours in reading order that read it only together (the box split over two lines); and two
|
|
318
|
+
hits of one line, taken left to right, that read it only together — wherever the reading order
|
|
319
|
+
put them. That order comes from row buckets (vision.read_screen), so two words of one line on
|
|
320
|
+
either side of a bucket edge come out swapped, or with other hits between them."""
|
|
321
|
+
hits = list(hits)
|
|
322
|
+
c = [_compact(str(getattr(h, "text", h))) for h in hits]
|
|
323
|
+
parts = {i for i, t in enumerate(c) if PASTE_HUD_PROBE in t}
|
|
324
|
+
together = lambda i, j: i not in parts and j not in parts and PASTE_HUD_PROBE in c[i] + c[j]
|
|
325
|
+
pairs = [(i, i + 1) for i in range(len(c) - 1) if together(i, i + 1)]
|
|
326
|
+
pos = [(getattr(h, "nx", None), getattr(h, "ny", None)) for h in hits]
|
|
327
|
+
for i, (xi, yi) in enumerate(pos):
|
|
328
|
+
if xi is None or yi is None:
|
|
329
|
+
continue
|
|
330
|
+
for j, (xj, yj) in enumerate(pos):
|
|
331
|
+
if j != i and xj is not None and yj is not None and abs(yi - yj) <= SAME_LINE_NY and 0 < xj - xi <= HUD_PAIR_MAX_DX and together(i, j):
|
|
332
|
+
pairs.append((i, j))
|
|
333
|
+
for i, j in pairs:
|
|
334
|
+
parts.update((i, j))
|
|
335
|
+
return parts
|
|
336
|
+
|
|
337
|
+
|
|
321
338
|
def shows_paste_hud(hits) -> bool:
|
|
322
339
|
"""True when the OCR read shows the phone's "Pasting from …" progress box
|
|
323
340
|
(compared like probes: NFKC, lowercase, whitespace removed). While it
|
|
@@ -327,10 +344,7 @@ def shows_paste_hud(hits) -> bool:
|
|
|
327
344
|
split "Pasting" and "from" into two hits. Only neighbours are joined
|
|
328
345
|
(not the whole screen), and a false match can only block a restore or a
|
|
329
346
|
confirmation (fail-closed), never license one."""
|
|
330
|
-
|
|
331
|
-
if any(PASTE_HUD_PROBE in _compact(t) for t in texts):
|
|
332
|
-
return True
|
|
333
|
-
return any(PASTE_HUD_PROBE in _compact(a + b) for a, b in zip(texts, texts[1:]))
|
|
347
|
+
return bool(_paste_hud_parts(hits))
|
|
334
348
|
|
|
335
349
|
|
|
336
350
|
def screen_shows_text(text: str, read_screen) -> bool:
|
|
@@ -363,7 +377,7 @@ def _compact(s: str) -> str:
|
|
|
363
377
|
|
|
364
378
|
|
|
365
379
|
def is_short_text(text: str, limit: int = 2) -> bool:
|
|
366
|
-
"""True when ``text`` normalizes to 1..``limit`` characters
|
|
380
|
+
"""True when ``text`` normalizes to 1..``limit`` characters.
|
|
367
381
|
|
|
368
382
|
Such texts are OCR-fragile: Vision's full-frame pass can skip an isolated
|
|
369
383
|
one- or two-character label entirely, so callers add a pass with
|
|
@@ -373,7 +387,7 @@ def is_short_text(text: str, limit: int = 2) -> bool:
|
|
|
373
387
|
|
|
374
388
|
|
|
375
389
|
def _fold_label(s: str) -> str:
|
|
376
|
-
"""Exact-reading key for a short label
|
|
390
|
+
"""Exact-reading key for a short label.
|
|
377
391
|
|
|
378
392
|
NFKC, lowercase, whitespace removed, trailing ".,:;" dropped, and "×"
|
|
379
393
|
folded to "x" — the readings Vision gives the one-letter app "X" — so
|
|
@@ -384,24 +398,29 @@ def _fold_label(s: str) -> str:
|
|
|
384
398
|
|
|
385
399
|
|
|
386
400
|
def new_text_positions(text: str, before, after, eps: float = 0.03) -> list:
|
|
387
|
-
"""Centres of hits in ``after`` reading exactly ``text`` where ``before`` had none
|
|
401
|
+
"""Centres of hits in ``after`` reading exactly ``text`` where ``before`` had none.
|
|
388
402
|
|
|
389
403
|
Same matching as new_text_occurrences(); returns the (nx, ny) of each
|
|
390
|
-
such hit instead of their count.
|
|
404
|
+
such hit instead of their count. As for a longer text (probe_spots), a
|
|
405
|
+
hit in the banner strip at the top (a notification can carry the
|
|
406
|
+
character) or in the "Pasting from …" box is no sighting.
|
|
391
407
|
"""
|
|
392
408
|
want = _fold_label(text)
|
|
393
409
|
if not want:
|
|
394
410
|
return []
|
|
411
|
+
after = list(after)
|
|
412
|
+
hud = _paste_hud_parts(after)
|
|
395
413
|
olds = [h for h in before if _fold_label(h.text) == want]
|
|
396
414
|
return [
|
|
397
|
-
(h.nx, h.ny) for h in after
|
|
398
|
-
if
|
|
415
|
+
(h.nx, h.ny) for i, h in enumerate(after)
|
|
416
|
+
if i not in hud and h.ny >= BANNER_STRIP_NY
|
|
417
|
+
and _fold_label(h.text) == want
|
|
399
418
|
and not any(abs(h.nx - o.nx) <= eps and abs(h.ny - o.ny) <= eps for o in olds)
|
|
400
419
|
]
|
|
401
420
|
|
|
402
421
|
|
|
403
422
|
class RepeatedSighting:
|
|
404
|
-
"""Evidence that a typed single character really arrived
|
|
423
|
+
"""Evidence that a typed single character really arrived.
|
|
405
424
|
|
|
406
425
|
One read showing the character at a new position is not enough: a
|
|
407
426
|
label the baseline read missed, a banner, a list still moving or a
|
|
@@ -448,7 +467,7 @@ def wait_still(luma=None, *, sleep=None, clock=None, timeout_s: float = 1.5,
|
|
|
448
467
|
|
|
449
468
|
|
|
450
469
|
def new_text_occurrences(text: str, before, after, eps: float = 0.03) -> int:
|
|
451
|
-
"""Hits in ``after`` reading exactly ``text`` where ``before`` had none
|
|
470
|
+
"""Hits in ``after`` reading exactly ``text`` where ``before`` had none.
|
|
452
471
|
|
|
453
472
|
A hit counts when no matching hit in ``before`` lay within ``eps`` (in
|
|
454
473
|
both axes) of it: evidence that a typed single character showed up even
|
|
@@ -29,7 +29,7 @@ class OCRError(RuntimeError):
|
|
|
29
29
|
|
|
30
30
|
|
|
31
31
|
class CaptureError(Exception):
|
|
32
|
-
"""Raised when the mirroring window cannot be captured by window id
|
|
32
|
+
"""Raised when the mirroring window cannot be captured by window id."""
|
|
33
33
|
|
|
34
34
|
|
|
35
35
|
# Injectable so tests can drive capture_window() without ever running a
|
|
@@ -48,16 +48,23 @@ class OCRHit:
|
|
|
48
48
|
return f"({self.nx:.2f},{self.ny:.2f}) {self.text!r} [{self.confidence:.2f}]"
|
|
49
49
|
|
|
50
50
|
|
|
51
|
-
def _capture_by_id(path: str) -> bool:
|
|
51
|
+
def _capture_by_id(path: str) -> bool | None:
|
|
52
52
|
"""One ``screencapture -l`` attempt; True when a non-empty PNG landed.
|
|
53
53
|
|
|
54
54
|
The window id is resolved fresh on every call, so the retry in
|
|
55
55
|
capture_window() can pick up a window that was mid-transition
|
|
56
56
|
(Space switch, un-minimize) on the first look.
|
|
57
|
+
|
|
58
|
+
None when the window is there but not on screen: a minimized window
|
|
59
|
+
still answers a capture by id — with the picture it had when it went
|
|
60
|
+
away (on a device: a PNG as large as a live one, no error) — and that
|
|
61
|
+
is not what the phone shows now. Nothing is captured then.
|
|
57
62
|
"""
|
|
58
63
|
info = get_window_info()
|
|
59
64
|
if info is None:
|
|
60
65
|
return False
|
|
66
|
+
if info.get("onscreen") is False:
|
|
67
|
+
return None
|
|
61
68
|
try:
|
|
62
69
|
# A normal capture takes 0.1-1.2 s; on a loaded Mac one hung for
|
|
63
70
|
# ~10 s, and with the retry that alone ate most of the server's
|
|
@@ -71,7 +78,7 @@ def _capture_by_id(path: str) -> bool:
|
|
|
71
78
|
# A missing or hung screencapture is a failed attempt, not a
|
|
72
79
|
# crash: it takes the same retry-then-CaptureError path as a
|
|
73
80
|
# nonzero return code, so callers report the capture as failed
|
|
74
|
-
# instead of dying with a raw traceback
|
|
81
|
+
# instead of dying with a raw traceback.
|
|
75
82
|
return False
|
|
76
83
|
return r.returncode == 0 and os.path.exists(path) and os.path.getsize(path) > 0
|
|
77
84
|
|
|
@@ -112,11 +119,12 @@ def capture_window(path: str | None = None) -> str:
|
|
|
112
119
|
other windows cover it or it is not frontmost. There is deliberately NO fallback to a screen-region
|
|
113
120
|
capture of the AX bounds: a region capture returns whatever windows happen to sit inside those
|
|
114
121
|
bounds — during a real session it once returned another app's sidebar, and every consumer (OCR,
|
|
115
|
-
expect guards, paste confirmation) would have trusted those pixels as the phone
|
|
122
|
+
expect guards, paste confirmation) would have trusted those pixels as the phone. When the
|
|
116
123
|
window id cannot be resolved, or screencapture fails (nonzero exit, missing or hung binary) or
|
|
117
124
|
writes nothing, one retry follows after 0.2 s (the window may be mid-transition); a second
|
|
118
125
|
failure raises CaptureError so callers report the capture as failed instead of acting on
|
|
119
|
-
another window's pixels.
|
|
126
|
+
another window's pixels. A window that is not on screen (hidden, minimized, on another Space)
|
|
127
|
+
is not captured either: the picture would be the one it had when it went away.
|
|
120
128
|
"""
|
|
121
129
|
owned = path is None
|
|
122
130
|
if owned:
|
|
@@ -124,14 +132,20 @@ def capture_window(path: str | None = None) -> str:
|
|
|
124
132
|
if _capture_by_id(path):
|
|
125
133
|
return path
|
|
126
134
|
time.sleep(0.2)
|
|
127
|
-
|
|
135
|
+
again = _capture_by_id(path)
|
|
136
|
+
if again:
|
|
128
137
|
return path
|
|
129
138
|
if owned:
|
|
130
139
|
discard_frame(path)
|
|
140
|
+
if again is None:
|
|
141
|
+
raise CaptureError(
|
|
142
|
+
"the iPhone Mirroring window is not on screen (hidden, minimized or on another Space): "
|
|
143
|
+
"a capture would show the picture it had when it went away"
|
|
144
|
+
)
|
|
131
145
|
raise CaptureError("could not capture the iPhone Mirroring window by id")
|
|
132
146
|
|
|
133
147
|
|
|
134
|
-
#
|
|
148
|
+
# 2026-11-05, macOS 27.2: Vision weights the languages in the order given. With
|
|
135
149
|
# English first it returned no Chinese at all on a Spotlight frame (0 CJK hits; the
|
|
136
150
|
# Top Hit label 拼多多 was missing, so phone_open could not find the app and every
|
|
137
151
|
# Chinese `expect` failed). Chinese first read the same frame's Chinese AND its English
|
|
@@ -262,7 +276,7 @@ def dedupe_hits(hits: list[OCRHit], eps: float = 0.02) -> list[OCRHit]:
|
|
|
262
276
|
return kept
|
|
263
277
|
|
|
264
278
|
|
|
265
|
-
#
|
|
279
|
+
# 2026-11-04: at Vision's default working scale the full-frame pass never
|
|
266
280
|
# reports an isolated single glyph (the app "X": its icon label, the Spotlight
|
|
267
281
|
# query echo). minimumTextHeight sets that working scale, and at these values
|
|
268
282
|
# the same full frame does report it. On 172 recorded mirror frames this sweep
|
|
@@ -276,7 +290,7 @@ SWEEP_MIN_TEXT_HEIGHTS = (0.018, 0.02, 0.022, 0.025, 0.03)
|
|
|
276
290
|
def ocr_sweep(image_path: str, languages: tuple[str, ...] = OCR_LANGUAGES,
|
|
277
291
|
language_correction: bool = False,
|
|
278
292
|
heights: tuple[float, ...] = SWEEP_MIN_TEXT_HEIGHTS) -> list[OCRHit]:
|
|
279
|
-
"""Full-frame OCR once per minimumTextHeight in ``heights``; merged hits
|
|
293
|
+
"""Full-frame OCR once per minimumTextHeight in ``heights``; merged hits.
|
|
280
294
|
|
|
281
295
|
Finds isolated one- or two-character labels that ocr() at Vision's default
|
|
282
296
|
scale skips. Hits are de-duplicated across heights (same text, centres
|
|
@@ -327,24 +341,31 @@ def ocr_sweep(image_path: str, languages: tuple[str, ...] = OCR_LANGUAGES,
|
|
|
327
341
|
|
|
328
342
|
def find(text: str, image_path: str | None = None,
|
|
329
343
|
exact: bool = False) -> OCRHit | None:
|
|
330
|
-
"""Find the
|
|
331
|
-
|
|
332
|
-
A
|
|
333
|
-
|
|
334
|
-
|
|
344
|
+
"""Find the OCR hit matching text; returns None if absent.
|
|
345
|
+
|
|
346
|
+
A hit that reads exactly ``text`` comes first; without ``exact``, the
|
|
347
|
+
first hit that contains it stands in only when no hit reads it exactly
|
|
348
|
+
("X" is the app X, not the "Xcode" listed above it). A one- or
|
|
349
|
+
two-character ``text`` without an exact hit is looked for again with
|
|
350
|
+
ocr_sweep(), which reads short labels the default pass skips. A frame
|
|
351
|
+
this captured itself is deleted after reading; a caller's ``image_path``
|
|
352
|
+
is left alone.
|
|
335
353
|
"""
|
|
336
|
-
def
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
354
|
+
def best(hits):
|
|
355
|
+
hits = list(hits)
|
|
356
|
+
same = next((h for h in hits if h.text == text), None)
|
|
357
|
+
if exact or same is not None:
|
|
358
|
+
return same
|
|
359
|
+
return next((h for h in hits if text in h.text), None)
|
|
341
360
|
|
|
342
361
|
owned = image_path is None
|
|
343
362
|
path = image_path or capture_window()
|
|
344
363
|
try:
|
|
345
|
-
hit =
|
|
346
|
-
if hit is None and 0 < len("".join(text.split())) <= 2:
|
|
347
|
-
|
|
364
|
+
hit = best(ocr(path))
|
|
365
|
+
if (hit is None or hit.text != text) and 0 < len("".join(text.split())) <= 2:
|
|
366
|
+
swept = best(ocr_sweep(path))
|
|
367
|
+
if swept is not None and (hit is None or swept.text == text):
|
|
368
|
+
hit = swept
|
|
348
369
|
return hit
|
|
349
370
|
finally:
|
|
350
371
|
if owned:
|
|
@@ -356,9 +377,9 @@ def read_screen(language_correction: bool = True, sweep: bool = False,
|
|
|
356
377
|
"""Convenience: capture + OCR the current mirroring window.
|
|
357
378
|
|
|
358
379
|
``language_correction`` is passed through to ocr(); see there.
|
|
359
|
-
``sweep=True`` reads with ocr_sweep() instead
|
|
380
|
+
``sweep=True`` reads with ocr_sweep() instead. ``also_sweep=True``
|
|
360
381
|
reads the same frame with both and merges the hits, so a short label
|
|
361
|
-
the default pass skips is still there
|
|
382
|
+
the default pass skips is still there. The frame is deleted once
|
|
362
383
|
it has been read.
|
|
363
384
|
"""
|
|
364
385
|
path = capture_window()
|