Download server/design_rules.py from devmandan/qr-scan-difficulty-check: direct link, hf CLI and curl.
- Browser
- Download file 11.2 kB
-
https://huggingface.co/spaces/devmandan/qr-scan-difficulty-check/resolve/main/server/design_rules.py
- Command line
-
hf download hf://spaces/devmandan/qr-scan-difficulty-check/server/design_rules.py
-
curl -L -o design_rules.py https://huggingface.co/spaces/devmandan/qr-scan-difficulty-check/resolve/main/server/design_rules.py
11.2 kB
| """The design test's rules (see design.py): its settings, read once at start-up from web/data/test.json (the page's own | |
| file, so the page and the server never disagree) and sizes.json's needs; each scanner's answer; the size check from a | |
| picture's header, before it is opened; the start-up self-test's judgement against its pinned values | |
| (design_pins.json); and, in the test worker, the setup's checks on zxing-cpp's reads of a picture | |
| (design_worker._setup): its reads at the picture's own size, copies of one code, polarity and mirroring. | |
| """ | |
| from __future__ import annotations | |
| import base64 | |
| import json | |
| import struct | |
| from math import comb, inf | |
| from pathlib import Path | |
| from types import SimpleNamespace | |
| from .reading_facts import _copy_of, own_reads | |
| from .reading_worker import _corners | |
| PINS = Path(__file__).with_name("design_pins.json") | |
| def answer(b: int, c: int, t: int, d: int, inv: int = 20, cost: int = 2, q: int = 16) -> str: | |
| """One scanner's answer over the paired scenes: b counts the scenes where it read the plain code and missed the | |
| design, c the reverse, t and d the scenes where it read the plain code and the design. "worse": a one-sided exact | |
| sign test on (b, c) under 1 / inv, in whole numbers (inv x the tail's count below 2 ** (b + c)); "like": a cost | |
| b - c of at most `cost`, with the plain code or the design read in at least q scenes; otherwise "close" (a cost | |
| over `cost`) or "few".""" | |
| n = b + c | |
| if inv * sum(comb(n, k) for k in range(b, n + 1)) < 2 ** n: | |
| return "worse" | |
| if b - c <= cost and (t >= q or d >= q): | |
| return "like" | |
| return "close" if b - c > cost else "few" | |
| def picture_size(data: bytes) -> tuple[int, int] | None: | |
| """(width, height) from a PNG's, a JPEG's or a WebP's header; None when the header doesn't say.""" | |
| try: | |
| if data[:8] == b"\x89PNG\r\n\x1a\n" and data[12:16] == b"IHDR": | |
| return struct.unpack(">II", data[16:24]) | |
| if data[:4] == b"RIFF" and data[8:12] == b"WEBP": | |
| if data[12:16] == b"VP8 ": # a key frame: its start code, then 14-bit width and height | |
| w, h = struct.unpack("<HH", data[26:30]) | |
| return w & 0x3FFF, h & 0x3FFF | |
| if data[12:16] == b"VP8L": | |
| bits = int.from_bytes(data[21:25], "little") | |
| return (bits & 0x3FFF) + 1, ((bits >> 14) & 0x3FFF) + 1 | |
| if data[12:16] == b"VP8X": | |
| return int.from_bytes(data[24:27], "little") + 1, int.from_bytes(data[27:30], "little") + 1 | |
| return None | |
| i = 2 if data[:2] == b"\xff\xd8" else len(data) | |
| while i + 9 <= len(data) and data[i] == 0xFF: | |
| m = data[i + 1] | |
| if m == 0xFF: # a fill byte | |
| i += 1 | |
| elif m == 0x01 or 0xD0 <= m <= 0xD7: # TEM and RST0-7 carry no length, as libjpeg reads them | |
| i += 2 | |
| elif 0xC0 <= m <= 0xCF and m not in (0xC4, 0xC8, 0xCC): # a frame header: height, then width | |
| h, w = struct.unpack(">HH", data[i + 5:i + 9]) | |
| return w, h | |
| elif m == 0xDA: # scan data before any frame header | |
| return None | |
| else: | |
| i += 2 + struct.unpack(">H", data[i + 2:i + 4])[0] | |
| except struct.error: | |
| return None | |
| return None | |
| def settings(test_json: Path, sizes_json: Path) -> dict: | |
| """The test's settings; raises on a missing or invalid file.""" | |
| tj = json.loads(test_json.read_text(encoding="utf-8")) | |
| needs = json.loads(sizes_json.read_text(encoding="utf-8"))["needs"] | |
| sv, lim, rule, check = tj["server"], tj["limits"], tj["rule"], tj["check"] | |
| n, seeds, inv = int(tj["scenes"]["s"]), [int(s) for s in tj["scenes"]["seeds"]], round(1 / rule["alpha"]) | |
| if len(seeds) != n or abs(1 / inv - rule["alpha"]) > 1e-12 or not needs: | |
| raise ValueError("test.json: its scenes, or its rule's alpha") | |
| if int(lim["max_version"]) > max(int(x["version"]) for x in needs): # p(v) is the next measured version up | |
| raise ValueError("test.json: max_version beyond sizes.json's needs") | |
| lo, hi = (int(x) for x in sv["retry_s"]) | |
| return { | |
| "n": n, "nice": int(sv["nice"]), "scene_wait_s": float(sv["scene_wait_s"]), | |
| "readings_switch": bool(sv["busy_while_readings_wait"]), | |
| "rule": {"inv": inv, "cost": int(rule["like_cost"]), "q": int(rule["q"])}, | |
| "worker": {"max_version": int(lim["max_version"]), "min_ppm": float(lim["min_ppm"]), | |
| "min_ppm_v1": float(lim["min_ppm_v1"]), "max_pixels": int(lim["max_pixels"]), "needs": needs, | |
| "seeds": seeds}, | |
| "queue": {"n": n, "deadline_s": float(sv["deadline_s"]), "hold_s": int(sv["slot_hold_s"]), | |
| "ticket_extra_s": float(sv["ticket_extra_s"]), "idle_s": float(sv["idle_s"]), | |
| "after_deadline_s": float(sv["after_deadline_s"]), "retry_s": (lo, hi), | |
| "max_tickets": int(sv["max_tickets"]), "held_max": int(sv["held_mb_max"]) * 1_000_000}, | |
| "check": {"png": base64.b64decode(check["png"]["b64"]), "jpeg": base64.b64decode(check["jpeg"]["b64"]), | |
| "jpeg_grey_sha256": check["jpeg"]["grey_sha256"]}, | |
| } | |
| def _near(a: str, b: str, tolerance: int) -> bool: | |
| """Two frames' block means (base64 of little-endian uint16, x16) within the tolerance everywhere.""" | |
| x, y = base64.b64decode(a), base64.b64decode(b) | |
| if len(x) != len(y) or not x or len(x) % 2: | |
| return False | |
| x, y = struct.unpack(f"<{len(x) // 2}H", x), struct.unpack(f"<{len(y) // 2}H", y) | |
| return max(abs(i - j) for i, j in zip(x, y, strict=True)) <= tolerance | |
| def judge(got: dict | None, pins: dict, jpeg_grey_sha256: str) -> tuple[bool, bool | None]: | |
| """(passed, same_frames) for the self-test's result: the code's facts and both scanners' reads as pinned, every | |
| frame's 32 x 32 block means within the tolerance, each patch rebuilding its frame to its hash, and the working | |
| check's JPEG decoding to the hash test.json pins. same_frames: whether the four frames also equal the bytes pinned | |
| from one machine, all four the Mac's or all four x86's (frame_sha256's "mac" and "x86"); None when no scene came | |
| back.""" | |
| got = got or {} | |
| facts, scenes, want = got.get("facts") or {}, got.get("scenes") or [], pins["scenes"] | |
| if not scenes or len(scenes) != len(want): | |
| return False, None | |
| ok = all(facts.get(k) == pins["facts"][k] for k in ("version", "ec_level", "mask", "raw_sha256")) | |
| ok = ok and got.get("check_jpeg_grey_sha256") == jpeg_grey_sha256 | |
| same = dict.fromkeys(("mac", "x86"), True) | |
| for g, p in zip(scenes, want, strict=True): | |
| ok = ok and g["seed"] == p["seed"] and g["reads"] == p["reads"] and g["paste_ok"] is True | |
| for kind in ("plain", "design"): | |
| ok = ok and _near(g["block_means_x16_b64"][kind], p["block_means_x16_b64"][kind], pins["tolerance_x16"]) | |
| for m in same: | |
| same[m] = same[m] and g["frame_sha256"][kind] == p["frame_sha256"].get(m, {}).get(kind) | |
| return bool(ok), any(same.values()) | |
| # ---- the setup's checks, in the test worker: T holds its OpenCV, numpy, scene renderer and quality.py ------------- | |
| def _inverted(T, gray, corners, n: int) -> bool: | |
| """Light on dark: the three finders' outer rings (24 squares each) sampled lighter than their separators, the light | |
| strips between each finder and the rest of the code (15 squares each), at the squares' centres through the read's | |
| corners.""" | |
| np, cv2 = T.np, T.cv2 | |
| ring = [(r0 + i, c0 + j) for r0, c0 in ((0, 0), (0, n - 7), (n - 7, 0)) for i in range(7) for j in range(7) | |
| if i in (0, 6) or j in (0, 6)] | |
| e = n - 8 | |
| sep = ([(7, j) for j in range(8)] + [(i, 7) for i in range(7)] + [(7, e + j) for j in range(8)] | |
| + [(i, e) for i in range(7)] + [(e, j) for j in range(8)] + [(e + 1 + i, 7) for i in range(7)]) | |
| hm = cv2.getPerspectiveTransform(np.float32([[0, 0], [n, 0], [n, n], [0, n]]), np.float32(corners)) | |
| def mean(cells): | |
| xy = cv2.perspectiveTransform(np.float32([[[c + 0.5, r + 0.5] for r, c in cells]]), hm)[0] | |
| x = np.clip(np.floor(xy[:, 0]).astype(int), 0, gray.shape[1] - 1) | |
| y = np.clip(np.floor(xy[:, 1]).astype(int), 0, gray.shape[0] - 1) | |
| return float(gray[y, x].mean()) | |
| return mean(ring) > mean(sep) | |
| def _mirrored(T, r, twin, n: int) -> bool: | |
| """zxing-cpp gives a mirrored code's squares transposed: its two copies of the format information, set by the error | |
| correction and mask alone (the data squares can differ from the twin's), match the twin's better transposed.""" | |
| np = T.np | |
| try: | |
| seen = np.asarray(r.to_image(scale=1, add_quiet_zones=False)) < 128 | |
| except Exception: # noqa: BLE001 - no squares to compare: not found mirrored | |
| return False | |
| k = twin.shape[0] // (n + 2 * T.scene.QUIET) | |
| at = (T.scene.QUIET + np.arange(n)) * k + k // 2 | |
| plain, f = twin[np.ix_(at, at)] < 128, np.zeros((n, n), bool) | |
| f[8, [*range(6), 7, 8, *range(n - 8, n)]] = f[[*range(6), 7, *range(n - 7, n)], 8] = True | |
| return seen.shape == (n, n) and int((seen.T != plain)[f].sum()) < int((seen != plain)[f].sum()) | |
| def _corners_of(T, r): | |
| """A zxing-cpp read's corners as the reading takes them (reading_worker._corners).""" | |
| return _corners(T.quality, "zxing-cpp", lambda y: (y.position, y.extra), r) | |
| def _copy(T, d, r) -> bool: | |
| """zxing-cpp's read d is a copy of r's code by the reading's own rule (reading_facts.chosen's copies): the same | |
| content, error correction and mask (with the version: scene.same_code), and, where both have corners as the reading | |
| takes them (reading_worker._corners), the same squares across and a mean side within COPY_SIDE of r's.""" | |
| return T.scene.same_code(d) == T.scene.same_code(r) and _copy_of(_corners_of(T, d), _corners_of(T, r)) | |
| def _own(T, gray, reads: list) -> list: | |
| """The reads (zxing-cpp's at library defaults) less those it made in its own smaller copies of the picture, by the | |
| reading's own-size rule (reading_facts.own_reads) for the content of the read nearest the centre: where zxing-cpp | |
| read that content two or more times, the picture is read again at its own size alone (try_downscale off), and only | |
| the reads that look repeats exactly (the same bytes and corners) are kept, all of them when none does. zxing-cpp | |
| reads copies a third the size only while the picture itself gave it fewer than 255 codes, as it can on another | |
| processor; a read there has coarse corners, and were it the one nearest the centre, the test would take it and the | |
| twin's scale would follow it.""" | |
| key, content = (0, "zxing-cpp"), bytes(T.scene.nearest(reads, gray.shape).bytes) | |
| look = SimpleNamespace(second_look=lambda g, how: [(bytes(x.bytes), _corners_of(T, x), None) | |
| for x in T.scene.qr_reads(g, **how)]) | |
| _, kept = own_reads(look, [(None, None, None, gray)], {key: [bytes(r.bytes) for r in reads]}, | |
| {key: [_corners_of(T, r) for r in reads]}, {key: list(reads)}, lambda ps: content in ps, inf) | |
| return [r for r in kept[key] if r is not None] | |