File size: 9,964 Bytes
b43c3a8
 
 
 
 
 
 
 
 
a59c3c4
 
 
b43c3a8
 
 
 
6f28f9e
 
 
 
 
ca6e756
 
a59c3c4
 
 
 
 
 
 
5b3cd1e
 
 
 
2a2a176
 
ca6e756
 
b43c3a8
 
 
 
 
 
 
 
 
 
 
 
2a2a176
5b3cd1e
2a2a176
 
 
b43c3a8
2a2a176
b43c3a8
 
 
 
6f28f9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b43c3a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f28f9e
a59c3c4
 
 
 
 
b43c3a8
 
 
 
 
 
 
 
 
 
 
 
 
 
a59c3c4
b43c3a8
 
6f28f9e
 
 
 
 
 
 
 
 
 
5b3cd1e
 
6f28f9e
ca6e756
b43c3a8
 
 
 
 
 
 
 
 
 
a59c3c4
 
 
b43c3a8
a59c3c4
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
"""Read one picture the way QR Scan Difficulty read every code, with the five of its eight scanners that run on Linux.

As in the measurement:
  * the picture is read in gray and shrunk, never enlarged, so its long side is at most each frame size (1920 down to
    224 pixels), with OpenCV's INTER_AREA; sizes that come out the same are read once;
  * each frame is staged as a gray PNG; zxing-cpp, WeChat's scanner and OpenCV's two detectors read it in a worker
    process (one OpenCV thread, library defaults, QR codes only), and ZXing reads it in a Java process the way
    Android's Settings app calls it;
  * a decode that runs past TIMEOUT_S counts as a miss: the worker is replaced and the rest is read without that call.
No measured picture had transparency; here a transparent part counts as white, as on a printed page. Nor did one hold
many codes: here, from the first frame where zxing-cpp reads MANY_CODES codes or more (reading_rules), OpenCV's newer
detector isn't run on that frame or any smaller one, and those decodes count as misses ("not_run").

A scanner reads a frame when one of the contents it returns is the picture's content. The measurement knew each code's
content; here it is the content the most scanners return (ties: the most frames, then text over raw bytes, then the
shortest). Raw bytes and the text a scanner makes of them (ISO-8859-1, QR's default, or Shift JIS) are one content.

The picture-quality checks (quality.py) are diagnostics and leave the reading as it is: the worker takes each read's
corners from the scanner's own result inside a guard and as plain data, so an error in them never costs a read; once a
reading has finished (not cut short), the worker measures its largest frame by the corners of the reads of its content.
Checks that fail or run past MEASURE_TIMEOUT_S are left out ("checks": None); a worker that ran past it is replaced as
after a stuck decode. In the same way, a finished reading also gives zxing-cpp's facts about the code
(reading_facts.chosen), on the largest frame where it read the content with a UEC ("uec_frame": that frame's largest
size): its unused error correction ("uec", from 1.0 when none was needed down to 0.0, with a second look where a copy
under SECOND_LOOK_BELOW, 3 pixels a square, reads under 1.0), the lowest over its reads of the content there; and,
from its last read of the content with a UEC there (or that read's second look, when it stands), the error correction
and mask ("ec_level", "mask"), the squares it sampled ("squares"), its raw bytes' SHA-256 and count ("raw_sha256",
"raw_bytes") and how many copies of that code it read there ("copies"); all None when it never read the content with
a UEC. It also gives the content's SHA-256 ("content": {"sha256"}), as one content's raw bytes, and the SHA-256s of
every set of raw bytes it stands for ("sha256s"; reading_content.py), which the page pairs pictures of one code by.
Where zxing-cpp read the content two or more times in the first frame it read it in, the checks and these facts leave
out its reads from its own smaller copies of that frame (reading_facts.own_reads): it reads those copies only while
the frame itself gave it fewer than 255 codes, and their reads have coarse corners and squares sampled at a third of
the frame's resolution or less.

The reader is here (Reader, and _checks for the picture-quality checks); the settings are in reading_rules.py, the
decoder worker in reading_worker.py, ZXing's Java process in reading_java.py, the content in reading_content.py and
zxing-cpp's facts in reading_facts.py.
"""

from __future__ import annotations

import hashlib
import json
import queue
import tempfile
import threading
import time
from multiprocessing import get_context

from .reading_content import _consensus, _same, describe
from .reading_facts import NO_FACTS, chosen, own_reads
from .reading_java import _Java
from .reading_rules import DEADLINE_S, FRAMES, POOL, SCANNERS, SELF_TEST, VENDOR, PictureError, frame_sizes
from .reading_worker import _decoders, _open, _TooBig, _Worker  # noqa: F401 - _decoders, _open and _TooBig: for tools

__all__ = ["FRAMES", "POOL", "SCANNERS", "VENDOR", "PictureError", "Reader", "frame_sizes"]


# ---- one picture ----------------------------------------------------------------------------------------------------

def _checks(worker: _Worker, frames: list, w: int, pool: dict, geo: dict, hit, deadline: float) -> dict | None:
    """The picture-quality checks of a reading that finished, on its largest frame, by the corners of the reads hit
    takes for its content; None when none of those reads has corners, when the worker isn't running or the deadline
    has passed, or when the checks fail. Diagnostics only: nothing here can cost the reading."""
    try:
        if not worker.alive() or time.monotonic() >= deadline:
            return None
        found = {(n, k): c for (k, n), ps in pool.items()
                 for p, c in zip(ps, geo.get((k, n), []), strict=False) if c is not None and hit([p])}
        if not found:
            return None
        w1280 = next(f[0] for f in frames if 1280 in f[2])
        checks = worker.measure(frames[0][3], found, [(f[0], f[1]) for f in frames],
                                {"to1280": w1280 / frames[0][0], "to_native": w / frames[0][0]})
        # encoded as Starlette's JSONResponse encodes the response: a value it would refuse leaves the checks out
        json.dumps(checks, ensure_ascii=False, allow_nan=False).encode("utf-8")
        return checks if isinstance(checks, dict) else None
    except Exception:  # noqa: BLE001 - diagnostics never cost the reading
        return None


class Reader:
    """Reads pictures, `slots` at a time (one decoder worker each); the ZXing process is shared."""

    def __init__(self, slots: int = 2):
        ctx = get_context("spawn")
        self.java = _Java()
        self.workers = [_Worker(ctx) for _ in range(slots)]
        self.free: queue.Queue = queue.Queue()
        for w in self.workers:
            self.free.put(w)
        self._self_test()

    def _self_test(self) -> None:
        """Refuse to start, rather than place every picture wrongly, unless all five read a clean code at every size."""
        r = self.read(str(SELF_TEST))
        missed = [n for n in SCANNERS if not all(r["reads"][n])]
        if missed or r["partial"]:
            self.close()
            raise RuntimeError(f"self-test failed: {', '.join(missed) or 'the reading ran out of time'}")

    def close(self) -> None:
        for w in self.workers:
            w.close()
        self.java.close()

    def read(self, path: str, truth: set[str] | None = None) -> dict:
        """Read one picture file. `truth` (sha256 hex digests of the content) replaces the consensus, as the
        measurement scored known codes; the server never passes it. "checks": the picture-quality checks of a
        reading that finished (see _checks), else None; "uec", "uec_frame", "copies", "ec_level", "mask",
        "squares", "raw_sha256" and "raw_bytes": zxing-cpp's facts (reading_facts.chosen: "uec" the lowest over its
        reads of the content in their frame, "copies" how many copies of one code it read there, the others from
        that read), all None when zxing-cpp never read the content with a UEC and for a reading cut short;
        "not_run": the decodes the many-codes rule left out, {scanner: [frame sizes]}, {} when none."""
        t0 = time.monotonic()
        deadline = t0 + DEADLINE_S
        worker = self.free.get()
        try:
            with tempfile.TemporaryDirectory(prefix="qsd-") as tmp:
                w, h, frames = worker.stage(path, tmp)
                java: dict = {"r": [None] * len(frames)}

                def zxing_java():
                    java["r"] = self.java.read([f[3] for f in frames], deadline)

                t = threading.Thread(target=zxing_java, daemon=True)
                t.start()
                try:
                    pool, geo, facts, partial, off = worker.decode(frames, deadline)
                finally:
                    t.join()
                contents = {(n, k): p for (k, n), p in pool.items()}
                for k, p in enumerate(java["r"]):
                    contents[("zxing-java", k)] = p or []
                    partial = partial or p is None
                if truth is None:
                    content = _consensus(contents)
                    def hit(ps): return content is not None and any(_same(p, content) for p in ps)  # noqa: E306
                else:
                    content = None
                    def hit(ps): return any(hashlib.sha256(p).hexdigest() in truth for p in ps)  # noqa: E306
                if not partial:   # the checks and the facts leave out zxing-cpp's reads from its smaller copies
                    geo, facts = own_reads(worker, frames, pool, geo, facts, hit, deadline)
                checks = None if partial else _checks(worker, frames, w, pool, geo, hit, deadline)
                code = dict(NO_FACTS) if partial else chosen(worker, frames, pool, geo, facts, hit, deadline)
        finally:
            self.free.put(worker)
        reads = {n: [0] * len(FRAMES) for n in SCANNERS}
        sizes = [None] * len(FRAMES)
        for k, (fw, fh, labels, _) in enumerate(frames):
            for f in labels:
                fi = FRAMES.index(f)
                sizes[fi] = [fw, fh]
                for n in SCANNERS:
                    reads[n][fi] = int(hit(contents.get((n, k), [])))
        not_run: dict[str, list[int]] = {}   # the many-codes rule's decodes, by scanner: frame sizes, largest first
        for k, n in sorted(off):
            not_run.setdefault(n, []).extend(frames[k][2])
        return {"size": [w, h], "frames": sizes, "reads": reads, "content": describe(content), "partial": partial,
                "checks": checks, **code, "not_run": not_run, "ms": round(1000 * (time.monotonic() - t0))}