Offlin33er commited on
Commit
5a6b200
·
verified ·
1 Parent(s): b61332d

Smoke test: validator self-checks, ICAO 9303 specimen, TrOCR single-image inference

Browse files
Files changed (1) hide show
  1. smoke.py +70 -0
smoke.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Smoke test: validator self-checks + ICAO 9303 worked example + one TrOCR inference."""
2
+ import random
3
+
4
+ from PIL import Image, ImageDraw, ImageFont
5
+ from validators import validate_ssn, validate_mrz_line2, mrz_check_digit, validate_document
6
+
7
+ checks = [
8
+ (validate_ssn("000-12-3456"), False),
9
+ (validate_ssn("666-12-3456"), False),
10
+ (validate_ssn("900-12-3456"), False),
11
+ (validate_ssn("123-00-3456"), False),
12
+ (validate_ssn("123-45-0000"), False),
13
+ (validate_ssn("123-45-6789"), True),
14
+ (validate_ssn("123456789"), False),
15
+ ]
16
+ for (ok, why), expected in checks:
17
+ assert ok == expected, (ok, why, expected)
18
+ print("validator spot checks: OK")
19
+
20
+ cd = mrz_check_digit("L898902C3")
21
+ assert cd == "6", cd
22
+ print("ICAO check-digit example (L898902C3 -> " + cd + "): OK")
23
+
24
+ # ICAO Doc 9303 Part 4 specimen line 2 (all check digits per the spec document)
25
+ SPECIMEN = "L898902C36UTO7408122F1204159ZE184226B<<<<<10"
26
+ ok, why = validate_mrz_line2(SPECIMEN)
27
+ assert ok and why == "ok", (ok, why)
28
+ print("ICAO specimen line validates: OK")
29
+
30
+ from evaluate_validators import gen_valid_mrz, gen_invalid_mrz
31
+ rng = random.Random(0)
32
+ for _ in range(200):
33
+ ok, why = validate_mrz_line2(gen_valid_mrz(rng))
34
+ assert ok, why
35
+ for _ in range(200):
36
+ bad, _m = gen_invalid_mrz(rng)
37
+ ok, why = validate_mrz_line2(bad)
38
+ assert not ok, (bad, why)
39
+ print("MRZ round-trip + corruption: OK")
40
+
41
+ f = validate_document({
42
+ "document_number": "123-45-6789",
43
+ "name": "ANNA MARIA ERIKSSON",
44
+ "date_of_birth": "1974-08-12",
45
+ "issue_date": "2023-04-15",
46
+ "expiry_date": "2022-04-14",
47
+ })
48
+ assert f == ["expiry_before_issue"], f
49
+ print("document-level findings: OK ->", f)
50
+
51
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
52
+ from trocr_compat import ensure_trocr_tokenizer
53
+ import torch
54
+ model_id = "microsoft/trocr-base-printed"
55
+ ensure_trocr_tokenizer(model_id)
56
+ processor = TrOCRProcessor.from_pretrained(model_id)
57
+ model = VisionEncoderDecoderModel.from_pretrained(model_id)
58
+ model.eval()
59
+ img = Image.new("RGB", (400, 80), "white")
60
+ d = ImageDraw.Draw(img)
61
+ try:
62
+ font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf", 32)
63
+ except Exception:
64
+ font = ImageFont.load_default()
65
+ d.text((10, 20), "TOTAL 49.00 USD", fill="black", font=font)
66
+ pv = processor(images=img, return_tensors="pt").pixel_values
67
+ with torch.no_grad():
68
+ ids = model.generate(pv, max_new_tokens=32)
69
+ print("TrOCR output:", processor.batch_decode(ids, skip_special_tokens=True)[0])
70
+ print("SMOKE OK")