Download server/design_worker.py from devmandan/qr-scan-difficulty-check: direct link, hf CLI and curl.
- Browser
- Download file 12 kB
-
https://huggingface.co/spaces/devmandan/qr-scan-difficulty-check/resolve/main/server/design_worker.py
- Command line
-
hf download hf://spaces/devmandan/qr-scan-difficulty-check/server/design_worker.py
-
curl -L -o design_worker.py https://huggingface.co/spaces/devmandan/qr-scan-difficulty-check/resolve/main/server/design_worker.py
12 kB
| """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 | |