Files
werk/scripts/android_labels.py
waoziandClaude Opus 5.5 aa6cc16955 Run Android input and document tests on a private emulator
scripts/private-emulator.py owns a disposable headless API 31 emulator.
android_startup.py launches Werkzeug and waits for visible Home; it waits
out only the System UI "isn't responding" dialog, so an app ANR still
fails. Its regression tests run in make test. The input, profile and
Workbook document-picker tests use it, find controls by text or content
description, and allow a cold system picker more time.

Local tests (on the final tree of this series, published pins, no local
overrides, through build-queue): make test including ziran fmt --check,
gui-smoke, gui-test, gallery-gui-test and web-smoke on private displays.
Not run on these pins: the private-emulator Android gate. On an earlier
pin the x86_64 APK built and validated and android-input-test passed
until Entity's add-node page, which needs Kryon's density damage fix
(78af4090); it runs with the next Kryon pin update.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-17 01:10:41 -03:00

45 lines
2.1 KiB
Python

"""Locate visible labels in screenshots from the private Android emulator."""
import csv
import io
import subprocess
from PIL import Image
def label_position(path, word, psm=11):
def normalized(value):
return "".join(character for character in value.casefold() if character.isalnum())
for scale in (1, 3):
screenshot = path
if scale == 3:
# Tesseract misses white labels on small, dark theme buttons.
# Separate those glyphs from the background before enlarging.
with Image.open(path) as source:
contrast = source.convert("L").point(lambda value: 0 if value > 160 else 255)
contrast = contrast.resize((source.width * scale, source.height * scale),
Image.Resampling.BICUBIC)
screenshot = path.with_name(path.stem + "-contrast.png")
contrast.save(screenshot)
data = subprocess.check_output(
["tesseract", str(screenshot), "stdout", "--psm", str(psm), "-c", "tessedit_create_tsv=1"],
stderr=subprocess.DEVNULL, text=True)
rows = [item for item in csv.DictReader(io.StringIO(data), delimiter="\t")
if item["text"].strip()]
count = len(word.split())
for index in range(len(rows) - count + 1):
group = rows[index:index + count]
if normalized(" ".join(item["text"] for item in group)) != normalized(word):
continue
# Sparse-text OCR may split a button's words into separate blocks.
# Require one visual line and left-to-right order instead.
top = max(int(item["top"]) for item in group)
bottom = min(int(item["top"]) + int(item["height"]) for item in group)
if bottom <= top or any(int(left["left"]) >= int(right["left"])
for left, right in zip(group, group[1:])):
continue
return ((int(group[0]["left"]) + int(group[-1]["left"]) +
int(group[-1]["width"])) // 2 // scale,
(top + bottom) // 2 // scale)
return None