vision
Role: stateless, CPU-only image analysis for borrowing sheets: extract first name, last name and birth date from a French CNI (2021+ model) through its TD1 MRZ or from an EPITA student card through OCR heuristics, and detect QR codes (and Data Matrix) in checkout / return photos.
Limits: the only Python service. No database, no Kafka, no authz check, no audit records: images and results are never stored nor logged (uploads stay in memory, Tesseract reads from stdin). Internal only: the gateway has no route to it. Results are suggestions the user must review.
Stack
FastAPI + Uvicorn (Python 3.14, uv), Pillow + pillow-heif (decoding), Tesseract CLI (OCR, subprocess with a
timeout), zxing-cpp (codes), PyJWT (principal token), prometheus-client, optional OpenTelemetry
(otel extra, installed in the Docker image). Sources in services/vision/src/vision/.
| Module | Responsibility |
|---|---|
app.py, __main__.py | application factory, error handlers, Uvicorn entry point (python -m vision) |
routes.py | /v1 endpoints and probes |
auth.py | X-Atl-Principal verification |
images.py | decoding, EXIF orientation, OCR preprocessing |
identity.py, mrz.py, mrz_detect.py, epita.py | extraction pipelines |
ocr.py | Tesseract runner (TSV output parsed into lines with confidence) |
qr.py | code detection |
middleware.py, metrics.py, logs.py, telemetry.py, problems.py | body limit, metrics, JSON logs, tracing, RFC 9457 errors |
Authentication
Every /v1 call needs the X-Atl-Principal token signed by the gateway (or by a service holding
PRINCIPAL_SIGNING_KEY): EdDSA, iss=atl-gateway, aud=atl-internal, exp required (5 s leeway), kind in
user, api_key, service and a non-empty sub. Otherwise 401 unauthenticated. See
identity. Any authenticated principal is accepted: there is no permission check.
Identity extraction
Images are decoded in memory (jpeg, png, webp, heic only; pixel limit), EXIF orientation applied, converted to RGB.
- CNI (
kind=autoorfr_id_card): grayscale, contrast-stretched copies are OCRed with the MRZ language and a MRZ character whitelist, trying in order the bottom half, the full image, the bottom half at a smaller width and the bottom half rotated 180°, stopping when every check digit passes. The best TD1 candidate (check digits first, then OCR confidence) is kept if at least half of its check digits pass.document_typeisfr_id_cardonly for issuerFRAand codeID. A pre-2021 two-line MRZ is recognised and rejected. - EPITA card (
kind=epita_card, orautowhen no CNI was found and the text contains a card keyword): full-page OCR (TESSERACT_TEXT_LANG) then labelled fields (NOM,PRENOM,NE LE...), name line patterns and plausible dates (age 14 to 80). All assumptions live inepita.Layout; the real card layout has not been sampled, so these heuristics are unverified.
Confidence: CNI = 0.6 × check digit ratio + 0.4 × mean OCR confidence; EPITA = 0.9 × mean name confidence.
Names are never covered by TD1 check digits (names_not_checksummed is always set for a CNI).
QR detection
Each image is converted to grayscale, downscaled to a 2400 px longest side, and scanned for QR, Micro QR, rMQR and Data Matrix codes. Duplicates (same text and format) are removed per image.
Dependencies
- Called by: borrowing (planned: identity extraction and checkout photo detection, see borrowing). No caller is wired in the code yet.
- Infrastructure: none (Tesseract binary and languages in the image).
- No shared Go library; it reimplements principal verification, problem+json and JSON logs.
Inputs and outputs
| Kind | Name | Notes |
|---|---|---|
| HTTP | POST /v1/extract/identity, POST /v1/detect/qr | API reference; internal only |
| HTTP | GET /healthz, GET /readyz, GET /metrics | probes and Prometheus, on the HTTP port, no auth |
| Kafka in / out | none |
/readyz answers 503 when tesseract --version fails. If Tesseract is unavailable at startup the service
still starts (warning) with the configured languages.
Observability
- Metrics (
/metricson the HTTP port, no separate metrics port):vision_http_requests_total(route, method, status),vision_http_request_duration_seconds(route),vision_identity_extractions_total(document type),vision_qr_codes_detected_total. - JSON logs on stdout: method, route, status, duration (no query string, no body); extraction logs carry only the document type, confidence and duration. Probes are not logged.
- Tracing when
OTEL_EXPORTER_OTLP_ENDPOINTis set (OTLP/HTTP, service namevision, probes excluded); see observability.
Data
None. Uploads are read in memory (the multipart parser never spools to disk) and closed after processing.
Docker image
services/vision/Dockerfile (build context services/vision): python:3.14-slim-trixie, tesseract-ocr +
tesseract-ocr-fra, the BSD-3 mrz OCR-B model (DoubangoTelecom/tesseractMRZ, pinned by checksum), non-root
user 10001, OMP_THREAD_LIMIT=1, port 8090, healthcheck on /healthz. In docker-compose.yml it runs in the
apps profile on 127.0.0.1:8090 with only PRINCIPAL_PUBLIC_KEY set.
Local run: uv sync --all-extras, uv run pytest --cov (coverage gate 85 %), then
PRINCIPAL_PUBLIC_KEY=... uv run python -m vision.
Configuration
| Variable | Default | Description |
|---|---|---|
PRINCIPAL_PUBLIC_KEY | required | Base64 raw 32-byte Ed25519 public key; startup fails without it |
HTTP_HOST, HTTP_PORT | 0.0.0.0, 8090 | Listen address |
MAX_UPLOAD_BYTES | 10485760 | Per image (10 MiB) |
MAX_QR_IMAGES | 10 | Images per QR request |
MAX_IMAGE_PIXELS | 40000000 | Decompression bomb guard |
MAX_CONCURRENCY | 2 | Concurrent OCR / decode jobs |
TESSERACT_CMD | tesseract | Executable |
TESSERACT_TIMEOUT_SECONDS | 20 | Per Tesseract run |
TESSERACT_MRZ_LANG | auto | auto = first installed of mrz, ocrb, eng, fra |
TESSERACT_TEXT_LANG | fra+eng | Languages for EPITA cards (uninstalled ones dropped) |
LOG_LEVEL | INFO | Python logging level |
OTEL_EXPORTER_OTLP_ENDPOINT | empty | Enables tracing (other OTEL_* variables read by the exporter) |
Integer variables must be positive. The request body limit is MAX_UPLOAD_BYTES × MAX_QR_IMAGES + 64 KiB.
vision does not read APP_ENV, HTTP_ADDR or METRICS_ADDR.