SPB Git

Repository Public

SmartScanEngine.swift 4.7 KB 103 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP ID & Card Vault7// File             : SPVault/Engines/SmartScan/SmartScanEngine.swift8// Module           : SPVault9// Repository       : https://git.spboucher.ai/sp-id-card-vault10// Project Website  : Not specified11// Created          : 2026-12-0212// Last Modified    : 2026-12-0213//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import CoreGraphics42import Foundation4344/// A photograph as it came from the camera, upright, held in memory only.45struct CapturedStill: @unchecked Sendable {46    var image: CGImage47}4849/// One side of a card after correction and text recognition.50struct ProcessedSide: @unchecked Sendable {51    var image: CGImage52    var format: CardFormat53    var lines: [RecognizedLine]54}5556enum SmartScanError: Error, Equatable {57    case correctionFailed58}5960/// SP SmartScan Engine — the capture pipeline after the shutter:61/// detect → correct perspective → (optional restrained colour correction) →62/// recognise text → propose metadata → mask what must not be kept.63/// Every step works on in-memory images; nothing is written to disk or Photos.64enum SmartScanEngine {65    struct Detection: Sendable {66        var quad: CardQuad67        /// False when no card was found and a centred default frame is proposed.68        var wasDetected: Bool69    }7071    /// Card outline in a still, or a centred default the owner adjusts by hand.72    static func locateCard(in still: CGImage, expectedFormat: CardFormat = .id1) -> Detection {73        if let detected = CardDetector.detectRefined(in: still) { return Detection(quad: detected.quad, wasDetected: true) }74        let size = CGSize(width: still.width, height: still.height)75        return Detection(quad: .centred(aspect: expectedFormat.aspectRatio, imageSize: size), wasDetected: false)76    }7778    static func process(still: CGImage, quad: CardQuad, expectedFormat: CardFormat? = nil, enhance: Bool = false) throws -> ProcessedSide {79        guard let corrected = PerspectiveCorrector.correct(still, quad: quad, expectedFormat: expectedFormat) else { throw SmartScanError.correctionFailed }80        let image = enhance ? ImageAdjustments.enhanced(corrected.image) : corrected.image81        return ProcessedSide(image: image, format: corrected.format, lines: CardOCR.recognize(image))82    }8384    /// Proposed security-code masks for one side of a bank card.85    static func securityCodeRects(in side: ProcessedSide, cardSide: CardSide) -> [CGRect] {86        switch cardSide {87        case .back: return SecurityCodeDetector.candidates(in: side.lines)88        case .front:89            // Only American Express prints its code on the front.90            let number = CardTextParser.bankNumber(in: CardTextParser.usable(side.lines))91            let labelled = SecurityCodeDetector.candidates(in: side.lines.filter { $0.box.height < 0.2 && $0.tokens.count > 1 })92            guard number.flatMap(PaymentCard.network(for:)) == "American Express" else { return labelled }93            return SecurityCodeDetector.frontCandidates(in: side.lines) + labelled94        }95    }9697    static func makeDraft(kind: CardKind, front: ProcessedSide, back: ProcessedSide?, now: Date = Date()) -> ScanDraft {98        let suggestion = CardTextParser.suggestion(front: front.lines, back: back?.lines ?? [], kind: kind, now: now)99        return ScanDraft(kind: kind, front: front.image, back: back?.image, format: front.format,100                         dominantColor: CardImageCodec.dominantColor(of: front.image), suggestion: suggestion)101    }102}103
Files main
Menu

Theme