Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collect flash frames #231

Merged
merged 2 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public enum MainLoopState {
case ocrAndCard
case ocrIncorrect
case ocrDelayForCard
case ocrForceFlash
case finished
}

Expand Down
7 changes: 5 additions & 2 deletions CardScan/Classes/CreditCardOcr/OcrMainLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ import UIKit

public protocol OcrMainLoopDelegate: class {
func complete(creditCardOcrResult: CreditCardOcrResult)
func prediction(prediction: CreditCardOcrPrediction, squareCardImage: CGImage, fullCardImage: CGImage)
func prediction(prediction: CreditCardOcrPrediction, squareCardImage: CGImage, fullCardImage: CGImage, state: MainLoopState)
func showCardDetails(number: String?, expiry: String?, name: String?)
func showCardDetailsWithFlash(number: String?, expiry: String?, name: String?)
func showWrongCard(number: String?, expiry: String?, name: String?)
func showNoCard()
func shouldUsePrediction(errorCorrectedNumber: String?, prediction: CreditCardOcrPrediction) -> Bool
Expand Down Expand Up @@ -208,7 +209,7 @@ open class OcrMainLoop : MachineLearningLoop {
guard let self = self else { return }
guard !self.userDidCancel else { return }
guard let squareCardImage = image.squareCardImage(roiRectangle: roi) else { return }
delegate?.prediction(prediction: prediction, squareCardImage: squareCardImage, fullCardImage: image)
delegate?.prediction(prediction: prediction, squareCardImage: squareCardImage, fullCardImage: image, state: self.errorCorrection.stateMachine.loopState())
}
guard let result = self.combine(prediction: prediction), result.state == .finished else {
self.postAnalyzerToQueueAndRun(ocr: ocr)
Expand Down Expand Up @@ -237,6 +238,8 @@ open class OcrMainLoop : MachineLearningLoop {
delegate?.showWrongCard(number: result.number, expiry: result.expiry, name: result.name)
case MainLoopState.ocrOnly, MainLoopState.ocrAndCard, MainLoopState.ocrDelayForCard:
delegate?.showCardDetails(number: result.number, expiry: result.expiry, name: result.name)
case .ocrForceFlash:
delegate?.showCardDetailsWithFlash(number: result.number, expiry: result.expiry, name: result.name)
case MainLoopState.finished:
delegate?.complete(creditCardOcrResult: result)
}
Expand Down
18 changes: 15 additions & 3 deletions CardScan/Classes/ScanBaseViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public protocol TestingImageDataSource: AnyObject {
self.onScannedCard(number: creditCardOcrResult.number, expiryYear: creditCardOcrResult.expiryYear, expiryMonth: creditCardOcrResult.expiryMonth, scannedImage: scannedCardImage)
}

open func prediction(prediction: CreditCardOcrPrediction, squareCardImage: CGImage, fullCardImage: CGImage) {
open func prediction(prediction: CreditCardOcrPrediction, squareCardImage: CGImage, fullCardImage: CGImage, state: MainLoopState) {
if self.showDebugImageView {
let numberBoxes = prediction.numberBoxes?.map { (UIColor.blue, $0) } ?? []
let expiryBoxes = prediction.expiryBoxes?.map { (UIColor.red, $0) } ?? []
Expand All @@ -384,17 +384,23 @@ public protocol TestingImageDataSource: AnyObject {
self.scannedCardImage = UIImage(cgImage: prediction.image)
}

let isFlashForcedOn: Bool
switch (state) {
case .ocrForceFlash: isFlashForcedOn = true
default: isFlashForcedOn = false
}

let cardSize = CGSize(width: prediction.image.width, height: prediction.image.height)
if let number = prediction.number, let numberBox = prediction.numberBox {
let expiry = prediction.expiryObject()
let expiryBox = prediction.expiryBox

ScanBaseViewController.machineLearningQueue.async {
self.scanEventsDelegate?.onNumberRecognized(number: number, expiry: expiry, numberBoundingBox: numberBox, expiryBoundingBox: expiryBox, croppedCardSize: cardSize, squareCardImage: squareCardImage, fullCardImage: fullCardImage, centeredCardState: prediction.centeredCardState, uxFrameConfidenceValues: prediction.uxFrameConfidenceValues)
self.scanEventsDelegate?.onNumberRecognized(number: number, expiry: expiry, numberBoundingBox: numberBox, expiryBoundingBox: expiryBox, croppedCardSize: cardSize, squareCardImage: squareCardImage, fullCardImage: fullCardImage, centeredCardState: prediction.centeredCardState, uxFrameConfidenceValues: prediction.uxFrameConfidenceValues, flashForcedOn: isFlashForcedOn)
}
} else {
ScanBaseViewController.machineLearningQueue.async {
self.scanEventsDelegate?.onFrameDetected(croppedCardSize: cardSize, squareCardImage: squareCardImage, fullCardImage: fullCardImage, centeredCardState: prediction.centeredCardState, uxFrameConfidenceValues: prediction.uxFrameConfidenceValues)
self.scanEventsDelegate?.onFrameDetected(croppedCardSize: cardSize, squareCardImage: squareCardImage, fullCardImage: fullCardImage, centeredCardState: prediction.centeredCardState, uxFrameConfidenceValues: prediction.uxFrameConfidenceValues, flashForcedOn: isFlashForcedOn)
}
}
}
Expand All @@ -404,6 +410,12 @@ public protocol TestingImageDataSource: AnyObject {
showCardNumber(number, expiry: expiry)
}

public func showCardDetailsWithFlash(number: String?, expiry: String?, name: String?) {
if !isTorchOn() { toggleTorch() }
guard let number = number else { return }
showCardNumber(number, expiry: expiry)
}

public func shouldUsePrediction(errorCorrectedNumber: String?, prediction: CreditCardOcrPrediction) -> Bool {
guard let predictedNumber = prediction.number else { return true }
return useCurrentFrameNumber(errorCorrectedNumber: errorCorrectedNumber, currentFrameNumber: predictedNumber)
Expand Down
4 changes: 2 additions & 2 deletions CardScan/Classes/ScanEventsProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// serial dispatch queue.
//
public protocol ScanEvents {
mutating func onNumberRecognized(number: String, expiry: Expiry?, numberBoundingBox: CGRect, expiryBoundingBox: CGRect?, croppedCardSize: CGSize, squareCardImage: CGImage, fullCardImage: CGImage, centeredCardState: CenteredCardState?, uxFrameConfidenceValues: UxFrameConfidenceValues?)
mutating func onNumberRecognized(number: String, expiry: Expiry?, numberBoundingBox: CGRect, expiryBoundingBox: CGRect?, croppedCardSize: CGSize, squareCardImage: CGImage, fullCardImage: CGImage, centeredCardState: CenteredCardState?, uxFrameConfidenceValues: UxFrameConfidenceValues?, flashForcedOn: Bool)
mutating func onScanComplete(scanStats: ScanStats)
mutating func onFrameDetected(croppedCardSize: CGSize, squareCardImage: CGImage, fullCardImage: CGImage, centeredCardState: CenteredCardState?, uxFrameConfidenceValues: UxFrameConfidenceValues?)
mutating func onFrameDetected(croppedCardSize: CGSize, squareCardImage: CGImage, fullCardImage: CGImage, centeredCardState: CenteredCardState?, uxFrameConfidenceValues: UxFrameConfidenceValues?, flashForcedOn: Bool)
}
4 changes: 2 additions & 2 deletions CardScan/Classes/SimpleScanViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ open class SimpleScanViewController: ScanBaseViewController {
}
}

override open func prediction(prediction: CreditCardOcrPrediction, squareCardImage: CGImage, fullCardImage: CGImage) {
super.prediction(prediction: prediction, squareCardImage: squareCardImage, fullCardImage: fullCardImage)
override open func prediction(prediction: CreditCardOcrPrediction, squareCardImage: CGImage, fullCardImage: CGImage, state: MainLoopState) {
super.prediction(prediction: prediction, squareCardImage: squareCardImage, fullCardImage: fullCardImage, state: state)

showScannedCardDetails(prediction: prediction)
}
Expand Down
4 changes: 2 additions & 2 deletions Example/CardScan/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ extension ViewController: SimpleScanDelegate {
extension ViewController: ScanEvents {
func onNumberRecognized(number: String, expiry: Expiry?, numberBoundingBox: CGRect,
expiryBoundingBox: CGRect?, croppedCardSize: CGSize, squareCardImage:
CGImage, fullCardImage: CGImage, centeredCardState: CenteredCardState?, uxFrameConfidenceValues: UxFrameConfidenceValues?) {}
CGImage, fullCardImage: CGImage, centeredCardState: CenteredCardState?, uxFrameConfidenceValues: UxFrameConfidenceValues?, flashForcedOn: Bool) {}
func onScanComplete(scanStats: ScanStats) {}
func onFrameDetected(croppedCardSize: CGSize, squareCardImage: CGImage, fullCardImage: CGImage,
centeredCardState: CenteredCardState?, uxFrameConfidenceValues: UxFrameConfidenceValues?) {}
centeredCardState: CenteredCardState?, uxFrameConfidenceValues: UxFrameConfidenceValues?, flashForcedOn: Bool) {}
}

extension ViewController: FullScanStringsDataSource {
Expand Down