Datasets:
File size: 1,587 Bytes
f16895b | 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 | """Does a control show text? Used by prep.py when OCR read nothing in the box (it misses short and small words like "API").
The page says what text the element has, but that can be hidden text, an icon-font name or a checkbox value, so we also
require that the box is wide enough for that text and that the pixels look like a row of separate letters."""
import re
import numpy as np
NO_TEXT_INPUTS = {"checkbox", "radio", "range", "color", "file", "image"}
def letter_runs(im, r):
"""Number of separate ink runs along x inside the box, ignoring border lines."""
a = np.asarray(im.crop((max(r[0], 0), max(r[1], 0), min(r[2], im.width), min(r[3], im.height))).convert("RGB"), dtype=np.int16)
if a.shape[0] < 4 or a.shape[1] < 4:
return 0
edge = np.concatenate([a[0], a[-1], a[:, 0], a[:, -1]])
bg = np.median(edge, axis=0)
ink = (np.abs(a - bg).max(axis=2) > 48)
ink[ink.mean(axis=1) > 0.7, :] = False # horizontal border lines and filled bars
ink[:, ink.mean(axis=0) > 0.85] = False # vertical border lines
cols = ink.any(axis=0)
return int(np.count_nonzero(cols[1:] & ~cols[:-1]) + cols[0])
def shows_text(im, e):
t = re.sub(r"[^A-Za-z0-9]", "", e["text"])
if len(t) < 2 or (e["tag"] == "input" and e["type"] in NO_TEXT_INPUTS):
return False
r = e["rect"]; w, h = r[2] - r[0], r[3] - r[1]
if w < 0.3 * min(h, 24) * min(len(e["text"]), 40): # box too narrow to hold that text: hidden label or icon-font name
return False
return letter_runs(im, r) >= max(2, min(len(t), 12) * 0.6)
|