Passport photo reader: a camera view with a frame, read in the browser
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:
1 parent
fc8c1b6a44
commit
1f54d93643
24 files changed
+2130
-52
No files matched your search
+63
-27
@@ -10,7 +10,7 @@ use chrono::NaiveDate;
|
||||
use minijinja::context;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::{AppState, Res, not_found_page, page};
|
||||
use super::{AppState, Res, not_found_page, page, with_reader};
|
||||
use crate::audit::{self, Action, Entity, Entry};
|
||||
use crate::countries;
|
||||
use crate::dates::{self, Status};
|
||||
@@ -198,13 +198,27 @@ fn log<'a>(
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads the zone typed into a form; a text that is no zone becomes a
|
||||
/// field error.
|
||||
fn read_zone(text: &str, today: NaiveDate, errors: &mut Errors) -> Option<Mrz> {
|
||||
/// Reads the zone typed into a form or read from a photo. When the text is
|
||||
/// no zone whose check digits all match, the zone in it as OCR read it
|
||||
/// (`mrz::from_ocr`: lines in any order, look-alike characters) replaces
|
||||
/// it, if that one matches or the text was no zone at all. A text that
|
||||
/// holds no zone becomes a field error.
|
||||
fn read_zone(text: &mut String, today: NaiveDate, errors: &mut Errors) -> Option<Mrz> {
|
||||
if text.is_empty() {
|
||||
return None;
|
||||
}
|
||||
match mrz::parse(text, today) {
|
||||
let typed = mrz::parse(text, today);
|
||||
if typed.as_ref().is_ok_and(Mrz::all_ok) {
|
||||
return typed.ok();
|
||||
}
|
||||
if let Some(found) = mrz::from_ocr(text)
|
||||
&& let Ok(m) = mrz::parse(&found, today)
|
||||
&& (m.all_ok() || typed.is_err())
|
||||
{
|
||||
*text = found;
|
||||
return Some(m);
|
||||
}
|
||||
match typed {
|
||||
Ok(m) => Some(m),
|
||||
Err(msg) => {
|
||||
err(errors, "mrz", msg);
|
||||
@@ -213,20 +227,40 @@ fn read_zone(text: &str, today: NaiveDate, errors: &mut Errors) -> Option<Mrz> {
|
||||
}
|
||||
}
|
||||
|
||||
fn zone_note(m: &Mrz, errors: &mut Errors) -> Option<String> {
|
||||
if m.all_ok() {
|
||||
Some("Filled in from the machine-readable zone; every check digit matches.".into())
|
||||
} else {
|
||||
err(
|
||||
errors,
|
||||
"mrz",
|
||||
format!(
|
||||
"Filled in, but the check digits do not match for: {}. Compare the lines with \
|
||||
the passport.",
|
||||
m.failed().join(", ")
|
||||
),
|
||||
);
|
||||
None
|
||||
/// `photo`: the lines came from the photo reader (the form's
|
||||
/// `mrz_source`), whose names have no check digit to catch a misread.
|
||||
fn zone_note(m: &Mrz, photo: bool, errors: &mut Errors) -> Option<String> {
|
||||
match (m.all_ok(), photo) {
|
||||
(true, false) => {
|
||||
Some("Filled in from the machine-readable zone; every check digit matches.".into())
|
||||
}
|
||||
(true, true) => Some(
|
||||
"Read from the photo; every check digit matches. The names have no check digit: \
|
||||
compare them with the passport."
|
||||
.into(),
|
||||
),
|
||||
(false, _) => {
|
||||
let what = if photo {
|
||||
"Read from the photo"
|
||||
} else {
|
||||
"Filled in"
|
||||
};
|
||||
err(
|
||||
errors,
|
||||
"mrz",
|
||||
format!(
|
||||
"{what}, but the check digits do not match for: {}. Compare the lines \
|
||||
with the passport and correct them{}.",
|
||||
m.failed().join(", "),
|
||||
if photo {
|
||||
", or take the photo again"
|
||||
} else {
|
||||
""
|
||||
}
|
||||
),
|
||||
);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -365,7 +399,7 @@ fn person_form_page(
|
||||
),
|
||||
None => ("New person".to_string(), "new", "/people".to_string()),
|
||||
};
|
||||
page(
|
||||
with_reader(page(
|
||||
st,
|
||||
user,
|
||||
"person_form.html",
|
||||
@@ -376,7 +410,7 @@ fn person_form_page(
|
||||
p, tab => "overview", f, pf, errors, mrz_note, mode, action, companies,
|
||||
..select_lists()
|
||||
},
|
||||
)
|
||||
))
|
||||
}
|
||||
|
||||
/// The companies the employer is chosen from (the active ones and the one
|
||||
@@ -415,7 +449,8 @@ pub async fn create(
|
||||
let mut pf = PersonForm::from_fields(&f);
|
||||
let mut ppf = PassportForm::from_fields(&f);
|
||||
let mut errors = Errors::new();
|
||||
let zone = read_zone(&ppf.mrz, today, &mut errors);
|
||||
let zone = read_zone(&mut ppf.mrz, today, &mut errors);
|
||||
let photo = get(&f, "mrz_source") == "photo";
|
||||
|
||||
if get(&f, "action") == "read-mrz" {
|
||||
let mut note = None;
|
||||
@@ -423,7 +458,7 @@ pub async fn create(
|
||||
Some(m) => {
|
||||
pf.take_mrz(m, true);
|
||||
ppf.take_mrz(m, true);
|
||||
note = zone_note(m, &mut errors);
|
||||
note = zone_note(m, photo, &mut errors);
|
||||
}
|
||||
None if ppf.mrz.is_empty() => {
|
||||
err(&mut errors, "mrz", "Paste the two lines first.");
|
||||
@@ -640,7 +675,7 @@ fn passport_form_page(
|
||||
),
|
||||
None => ("New passport", "new", format!("/people/{}/passports", p.id)),
|
||||
};
|
||||
page(
|
||||
with_reader(page(
|
||||
st,
|
||||
user,
|
||||
"passport_form.html",
|
||||
@@ -648,7 +683,7 @@ fn passport_form_page(
|
||||
"people",
|
||||
status,
|
||||
context! { p, tab => "overview", pf, errors, mrz_note, name_hint, mode, action, heading => title, ..select_lists() },
|
||||
)
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn passport_new(
|
||||
@@ -710,14 +745,15 @@ async fn passport_save(
|
||||
let today = dates::today();
|
||||
let mut pf = PassportForm::from_fields(f);
|
||||
let mut errors = Errors::new();
|
||||
let zone = read_zone(&pf.mrz, today, &mut errors);
|
||||
let zone = read_zone(&mut pf.mrz, today, &mut errors);
|
||||
let photo = get(f, "mrz_source") == "photo";
|
||||
let mut notes = Notes::default();
|
||||
|
||||
if get(f, "action") == "read-mrz" {
|
||||
match &zone {
|
||||
Some(m) => {
|
||||
pf.take_mrz(m, true);
|
||||
notes.mrz_note = zone_note(m, &mut errors);
|
||||
notes.mrz_note = zone_note(m, photo, &mut errors);
|
||||
notes.name_hint = name_hint(m, &person);
|
||||
}
|
||||
None if pf.mrz.is_empty() => err(&mut errors, "mrz", "Paste the two lines first."),
|
||||
|
||||
Reference in new issue
Block a user