Aller au contenu principal

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/.

ModuleResponsibility
app.py, __main__.pyapplication factory, error handlers, Uvicorn entry point (python -m vision)
routes.py/v1 endpoints and probes
auth.pyX-Atl-Principal verification
images.pydecoding, EXIF orientation, OCR preprocessing
identity.py, mrz.py, mrz_detect.py, epita.pyextraction pipelines
ocr.pyTesseract runner (TSV output parsed into lines with confidence)
qr.pycode detection
middleware.py, metrics.py, logs.py, telemetry.py, problems.pybody 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.

  1. CNI (kind = auto or fr_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_type is fr_id_card only for issuer FRA and code ID. A pre-2021 two-line MRZ is recognised and rejected.
  2. EPITA card (kind = epita_card, or auto when 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 in epita.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​

KindNameNotes
HTTPPOST /v1/extract/identity, POST /v1/detect/qrAPI reference; internal only
HTTPGET /healthz, GET /readyz, GET /metricsprobes and Prometheus, on the HTTP port, no auth
Kafka in / outnone

/readyz answers 503 when tesseract --version fails. If Tesseract is unavailable at startup the service still starts (warning) with the configured languages.

Observability​

  • Metrics (/metrics on 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_ENDPOINT is set (OTLP/HTTP, service name vision, 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​

VariableDefaultDescription
PRINCIPAL_PUBLIC_KEYrequiredBase64 raw 32-byte Ed25519 public key; startup fails without it
HTTP_HOST, HTTP_PORT0.0.0.0, 8090Listen address
MAX_UPLOAD_BYTES10485760Per image (10 MiB)
MAX_QR_IMAGES10Images per QR request
MAX_IMAGE_PIXELS40000000Decompression bomb guard
MAX_CONCURRENCY2Concurrent OCR / decode jobs
TESSERACT_CMDtesseractExecutable
TESSERACT_TIMEOUT_SECONDS20Per Tesseract run
TESSERACT_MRZ_LANGautoauto = first installed of mrz, ocrb, eng, fra
TESSERACT_TEXT_LANGfra+engLanguages for EPITA cards (uninstalled ones dropped)
LOG_LEVELINFOPython logging level
OTEL_EXPORTER_OTLP_ENDPOINTemptyEnables 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.