Passport photo reader: a camera view with a frame, read in the browser
CI / secret scan (push) Successful in 6s
CI / fmt, clippy, tests (push) Successful in 28s

On the new person and new passport forms, next to Read:

- "Take a photo" opens a camera view (getUserMedia, the back camera): a
  frame of the photo page's shape (125 by 88 mm), a band for the two lines,
  and a hint from four checks of the band four times a second: too dark,
  glare on the lines, no lines in the band, not sharp yet (variance of the
  Laplacian). Once the band has looked readable for a second it takes the
  photo itself; "Take the photo" and Cancel (or Escape) are there too.
  The band where the frame showed it is read first; when that holds no
  zone, the whole frame is searched.
- "Choose a picture" reads any image: EXIF orientation, the four quarter
  turns, a tilt of 7 degrees either way; a failed read says when there is
  glare in the picture.

Reading happens in the browser (static/mrz-photo.js): even light from a
local mean, the lines found with Tesseract's page layout, then the band cut
along their baselines, scaled and read as one block. Tesseract.js 7.0.0
and its core (SIMD and plain LSTM builds) with Doubango's MRZ model, all
served by the app from static/vendor/tesseract/ (README.txt: sources, npm
integrity, SHA-256, licenses Apache-2.0 and BSD-3), loaded at the first
photo. The photo never leaves the device: only the lines go into the zone's
field, and Read is pressed.

The server reads them like typed ones, through mrz::from_ocr when they are
no clean zone: the first line wherever it is, the second whose check digits
match best, a miscounted filler run cut or filled, K for < in the filler,
letters and digits where the zone has them, and look-alikes (O for 0, B for
8) swapped in the number fields only where a check digit asks for it,
preferring a digit. A photo's note asks to compare the names, which have no
check digit.

Only those two pages and the reader's files allow WebAssembly
('wasm-unsafe-eval', worker-src 'self', img-src blob:) and the camera
(Permissions-Policy camera=(self)); every other page keeps camera=().
Content types for .wasm and .gz.

Checks: fmt, clippy -D warnings, 99 unit and 50 integration tests (new:
from_ocr cases from real reads, tests/mrz_photo.rs for the markup, the two
policies, the files and a read from photo lines). In Edge (Playwright): 12
made-up phone photos and 16 specimen pages from Wikimedia Commons, every
zone that is readable read with all check digits matching (not: a hard
shadow across the lines; the specimens whose zone is blanked out), 1 to 5 s
each; the camera view with a fake camera: the glare and dark hints, and a
good picture taken by itself and the form filled in. Layout check 0
problems on both forms and a person, light and dark, desk and phone.

PLAN: task 7.3 done ahead of the client link, the decision recorded.
This commit is contained in:
grabowski committed 2026-11-20 16:47:40 +07:00
1 parent fc8c1b6a44
commit 1f54d93643
24 files changed
+2130 -52

No files matched your search

+11
View File
@@ -0,0 +1,11 @@
{# The passport photo reader (static/mrz-photo.js), shown only where the
browser can run it: a camera view with a frame for the photo page, or a
picture from the device. The photo stays on the device; the lines it
finds go into the zone's field and the form is read. #}
<span class="mrzphoto" data-mrz-photo-wrap hidden>
<button type="button" class="btn" data-mrz-camera>Take a photo</button>
<label class="btn filebtn">Choose a picture<input type="file" accept="image/*" data-mrz-photo data-vendor="/static/vendor/tesseract" data-langpath="/static/vendor/tesseract" data-lang="mrz" data-gzip="1" data-model="mrz-doubango-2019"></label>
</span>
<input type="hidden" name="mrz_source" value="">
<p class="hint mrz-status" data-mrz-status role="status" hidden></p>
<script src="/static/mrz-photo.js?v={{ v }}" defer></script>
+2 -1
View File
@@ -7,10 +7,11 @@
<button type="submit" name="action" value="save" class="default-submit" tabindex="-1" aria-hidden="true">Save</button>
<section class="card" id="mrz-card">
<header><h2>{{ heading }}</h2></header>
<p class="hint lead">Type or paste the two lines at the bottom of the photo page and press Read to fill in the fields below.</p>
<p class="hint lead">Type or paste the two lines at the bottom of the photo page and press Read, or take a photo of the page, to fill in the fields below.</p>
{{ fm.area("mrz", "Machine-readable zone, two lines of 44 characters", pf.mrz, err=errors.mrz, rows=2, cls="mrz") }}
<div class="formactions">
<button type="submit" name="action" value="read-mrz" class="btn" id="read-mrz" formnovalidate>Read</button>
{% include "_mrz_photo.html" %}
{% if mrz_note %}<p class="okline" role="status">{{ mrz_note }}</p>{% endif %}
</div>
{% if name_hint %}<p class="hintline" role="status">{{ name_hint }}</p>{% endif %}
+2 -1
View File
@@ -13,10 +13,11 @@
{% if mode == "new" %}
<section class="card" id="mrz-card">
<header><h2>Read the passport</h2></header>
<p class="hint lead">Type or paste the two lines at the bottom of the photo page, then press Read. The form below fills itself in, and the check digits in the lines catch typing errors.</p>
<p class="hint lead">Type or paste the two lines at the bottom of the photo page and press Read, or take a photo of the page. The form below fills itself in, and the check digits in the lines catch typing errors.</p>
{{ fm.area("mrz", "Machine-readable zone, two lines of 44 characters", pf.mrz, err=errors.mrz, rows=2, cls="mrz") }}
<div class="formactions">
<button type="submit" name="action" value="read-mrz" class="btn" id="read-mrz" formnovalidate>Read</button>
{% include "_mrz_photo.html" %}
{% if mrz_note %}<p class="okline" role="status">{{ mrz_note }}</p>{% endif %}
</div>
</section>