File size: 11,987 Bytes
5b3cd1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
"""The design test's worker process (see design.py), on the reading's pattern (reading_worker.py): one spawned process
at a low priority, with one OpenCV and one BLAS thread, that holds the scene renderer (scene.py), zxing-cpp and
WeChat's scanner, called through the reading's own decoder functions on frames in memory (none of OpenCV's detectors
is called). DesignWorker, in the server, starts it, asks it one thing at a time, and kills and replaces it when it goes
silent or dies.

Setup checks a picture in this order and answers ("cant", why) at the first that fails: its size (16 megapixels at
most), zxing-cpp's facts at full size (a valid QR read with its version, error correction and mask), one code (every
read a copy of the one nearest the centre, which the test takes, by the reading's own rules, its reads from zxing-cpp's
smaller copies of the picture left out first: design_rules._own and _copy), the version, the pixels per square, its
polarity (the three finders' outer rings darker than the light strips round them), the plain twin (made and read back
the same) and mirroring (zxing-cpp's sampled format squares match the twin's transposed).
Then each scene renders the twin's and the design's captures with their shared parts, makes both frames
(design_frames.py) and reads each with both scanners. A read counts when a payload's SHA-256 is one of the code's
truths: its raw bytes' and its text's in UTF-8. For a page whose browser reads the frames too, a scene also gives both
frames' hashes, the patch's box and the patch, and the design's capture JPEG, or, in a test with PNG frames, the
design's frame as PNG. The reads stay in the server; design.py keeps only their counts.

Messages, each a tuple whose first item names it; replies come back on the same pipe:
  ("setup", path, mode, rules)             -> ("set", facts) | ("cant", why) | ("unreadable",)
  ("scene", s, mode)                       -> ("scene", s, {"reads", "box", "sha256", "jpeg", "png", "patch"})
  ("selftest", png, jpeg, seeds, rules)    -> ("selftest", {"facts", "scenes", "check_jpeg_grey_sha256"})
  ("clear",)                               -> no reply; the test's arrays are dropped
mode is "jpeg", "png" or None (no pictures for the page). For acceptance runs only, QSD_DESIGN_SLOW_S makes each scene
start that many seconds late.
"""

from __future__ import annotations

import hashlib
import os
import threading
import time
from multiprocessing import get_context
from types import SimpleNamespace

from .design_rules import _copy, _inverted, _mirrored, _own
from .reading_rules import VENDOR
from .reading_worker import _decoders

SCANNERS = ("zxing-cpp", "wechat")
SPAWN_S = 180     # the worker loads its renderer and scanners within this, as the reading's does
SETUP_S = 30      # opening and checking one picture, within the test's own deadline too
SCENE_S = 20      # one scene, within the test's own deadline too
SELFTEST_S = 120  # the start-up self-test: a setup and two scenes
THREADS = ("OMP_NUM_THREADS", "OPENBLAS_NUM_THREADS", "MKL_NUM_THREADS")


# ---- the worker process: the setup's checks on zxing-cpp's reads are design_rules.py's -----------------------------

def _setup(T, src, rules: dict):
    """(reply, the test's state or None) for a picture's path or bytes, checked in the order above."""
    S = T.scene
    try:
        lum, gray = S.open_design(src)
    except T.cv2.error as e:   # past OPENCV_IO_MAX_IMAGE_PIXELS OpenCV raises, rather than return None
        return (("cant", "big") if "MAX_IMAGE_PIXELS" in str(e) else ("unreadable",)), None
    except Exception:  # noqa: BLE001 - not a picture OpenCV opens
        return ("unreadable",), None
    if gray is None or gray.shape[0] * gray.shape[1] > rules["max_pixels"]:
        return (("cant", "big") if gray is not None else ("unreadable",)), None
    reads = S.qr_reads(gray)
    if not reads:
        return ("cant", "facts"), None
    reads = _own(T, gray, reads)
    r = S.nearest(reads, gray.shape)
    if not all(_copy(T, d, r) for d in reads):
        return ("cant", "several"), None
    f = S.facts_of(r)
    v = f["version"]
    n = 17 + 4 * v
    if v > rules["max_version"]:
        return ("cant", "version"), None
    if S.px_per_square(f["corners"], v) < rules["min_ppm_v1" if v == 1 else "min_ppm"]:
        return ("cant", "small"), None
    if _inverted(T, gray, f["corners"], n):
        return ("cant", "inverted"), None
    design = S.Design(lum, gray, f=f)
    try:
        twin = design.twin()
    except Exception:  # noqa: BLE001 - the content doesn't fit such a code, or it reads back differently
        return ("cant", "twin"), None
    if _mirrored(T, r, twin.gray, n):
        return ("cant", "mirrored"), None
    ppm = next(x["ppm"] for x in rules["needs"] if x["version"] >= max(v, 2))
    text = hashlib.sha256((f["text"] or "").encode("utf-8")).hexdigest()
    facts = {"version": v, "ec_level": f["ec"], "mask": f["mask"], "raw_sha256": f["sha256"], "text_sha256": text,
             "ppm": ppm, "n": n}
    return ("set", facts), {"objs": (twin, design), "truths": {f["sha256"], text}, "ppm": ppm,
                            "seeds": rules["seeds"]}


def _frames(T, test: dict, seed: int):
    """The twin's and the design's capture JPEGs at a seed, and their frames."""
    got = T.scene.seed_captures(test["objs"], seed, (test["ppm"],))
    jpegs = got[(0, 0)], got[(0, 1)]
    return jpegs, tuple(T.frames.frame_of(j) for j in jpegs)


def _reads(T, frames, truths) -> dict:
    """{scanner: [plain code's frame read, design's frame read]}, each 0 or 1."""
    out = {name: [0, 0] for name in SCANNERS}
    for i, frame in enumerate(frames):
        bgr = T.cv2.cvtColor(frame, T.cv2.COLOR_GRAY2BGR)
        for name in SCANNERS:
            try:
                payloads, _ = T.decoders[name](bgr)
            except Exception:  # noqa: BLE001 - a decoder that fails reads nothing, as in the reading
                payloads = []
            out[name][i] = int(any(hashlib.sha256(p).hexdigest() in truths for p in payloads))
    return out


def _scene(T, test: dict, s: int, mode: str | None) -> dict:
    (_, design_jpeg), (plain, design) = _frames(T, test, test["seeds"][s])
    out = {"reads": _reads(T, (plain, design), test["truths"]), "box": None, "sha256": None, "jpeg": None,
           "png": None, "patch": None}
    if mode:
        F = T.frames
        box = F.box_of(plain, design)
        out.update(box=box, sha256={"plain": F.sha256(plain), "design": F.sha256(design)}, patch=F.patch_of(plain, box))
        out["jpeg" if mode == "jpeg" else "png"] = design_jpeg if mode == "jpeg" else F.png_of(design)
    return out


def _selftest(T, png: bytes, jpeg: bytes, seeds, rules: dict) -> dict:
    """The working check's code as a design beside its own twin, set up and rendered as any test is, at fixed seeds."""
    F = T.frames
    reply, test = _setup(T, png, rules)
    out = {"facts": reply[1] if reply[0] == "set" else {"cant": reply[-1]}, "scenes": [],
           "check_jpeg_grey_sha256": None}
    for seed in seeds if test is not None else ():
        _, (plain, design) = _frames(T, test, seed)
        box = F.box_of(plain, design)
        rebuilt = F.paste(design, F.patch_of(plain, box), box)
        out["scenes"].append({
            "seed": seed, "box": box, "reads": _reads(T, (plain, design), test["truths"]),
            "frame_sha256": {"plain": F.sha256(plain), "design": F.sha256(design)},
            "block_means_x16_b64": {"plain": F.block_means(plain), "design": F.block_means(design)},
            "paste_ok": rebuilt is not None and F.sha256(rebuilt) == F.sha256(plain)})
    grey = F.decode_grey(jpeg)
    out["check_jpeg_grey_sha256"] = None if grey is None else hashlib.sha256(grey.tobytes()).hexdigest()
    return out


def _child(conn, wechat_dir: str, nice: int) -> None:
    try:
        os.nice(nice)
        for name in THREADS:   # before numpy and OpenCV load, which read them once
            os.environ[name] = "1"
        import cv2
        import numpy as np

        cv2.setNumThreads(1)
        from . import design_frames, quality, scene

        _, decoders = _decoders(wechat_dir, quality)
        T = SimpleNamespace(cv2=cv2, np=np, scene=scene, frames=design_frames, decoders=decoders, quality=quality)
        slow = float(os.environ.get("QSD_DESIGN_SLOW_S") or 0)
    except Exception as e:  # noqa: BLE001 - reported to the parent, which leaves the test off
        conn.send(("broken", repr(e)))
        return
    conn.send(("ready",))
    test = None
    while True:
        try:
            msg = conn.recv()
        except EOFError:
            return
        try:
            if msg[0] == "setup":
                reply, test = _setup(T, msg[1], msg[3])
                conn.send(reply)
            elif msg[0] == "scene":
                time.sleep(slow)
                conn.send(("scene", msg[1], None if test is None else _scene(T, test, msg[1], msg[2])))
            elif msg[0] == "selftest":
                conn.send(("selftest", _selftest(T, *msg[1:])))
            elif msg[0] == "clear":
                test = None
        except Exception as e:  # noqa: BLE001 - the parent counts it as the worker failing
            test = None
            conn.send(("failed", repr(e)))


# ---- in the server --------------------------------------------------------------------------------------------------

class DesignWorker:
    """The test worker process. A silent or dead one is killed; replace() starts another."""

    def __init__(self, nice: int = 10):
        self._ctx, self._nice, self._lock = get_context("spawn"), nice, threading.Lock()
        self._spawn()

    def _spawn(self, wait: float = SPAWN_S) -> None:
        self.conn, child = self._ctx.Pipe()
        self.proc = self._ctx.Process(target=_child, args=(child, str(VENDOR / "wechat"), self._nice), daemon=True)
        self.proc.start()
        child.close()
        msg = self._recv(wait)
        if msg is None or msg[0] != "ready":
            self._close()
            raise RuntimeError(f"design worker did not start: {msg}")

    def _close(self) -> None:
        self.proc.kill()
        self.proc.join(5)
        self.conn.close()

    def _recv(self, timeout: float):
        try:
            if not self.conn.poll(max(0.0, timeout)):
                return None
            return self.conn.recv()
        except (EOFError, OSError):
            return None

    def alive(self) -> bool:
        return not self.conn.closed and self.proc.is_alive()

    def close(self) -> None:
        self.proc.kill()   # first, without the lock, so that a call waiting on the worker returns at once
        with self._lock:
            self._close()

    def replace(self) -> None:
        """Kill this worker (if it still runs) and start another; raises RuntimeError when that fails."""
        with self._lock:
            self._close()
            self._spawn()

    def ask(self, msg: tuple, timeout: float):
        """The worker's reply to msg, or None when it stays silent for timeout seconds or dies: it is then killed, and
        the caller replaces it. One that died while idle is replaced first, within the same timeout."""
        end = time.monotonic() + timeout
        with self._lock:
            if not self.alive():
                self._close()
                self._spawn(min(SPAWN_S, timeout))
            try:
                self.conn.send(msg)
            except OSError:
                self._close()
                return None
            got = self._recv(end - time.monotonic())
            if got is None:
                self._close()
            return got

    def tell(self, msg: tuple) -> None:
        """Send msg without waiting for a reply (it has none); a dead worker is left for the next ask."""
        with self._lock:
            try:
                if self.alive():
                    self.conn.send(msg)
            except OSError:
                pass