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

modifying testing image protocol to return square and full-screen images #102

Merged
merged 12 commits into from
Dec 4, 2019
38 changes: 6 additions & 32 deletions CardScan/Classes/ScanBaseViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import AVKit
import VideoToolbox
import Vision


@objc public protocol TestingImageDataSource {
@objc func nextImage() -> CGImage?
public protocol TestingImageDataSource: AnyObject {
func nextSquareAndFullImage() -> (CGImage, CGImage)?
}

@objc open class ScanBaseViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate, ScanEvents, AfterPermissions {
Expand All @@ -17,8 +16,7 @@ import Vision
// this shouldn't get called
}


@objc public weak var testingImageDataSource: TestingImageDataSource?
public weak var testingImageDataSource: TestingImageDataSource?
@objc public var errorCorrectionDuration = 1.5
@objc public var includeCardImage = false
@objc public var showDebugImageView = false
Expand Down Expand Up @@ -273,29 +271,6 @@ import Vision
}


/* We're not ready for bardcodes again yet
@available(iOS 11.0, *)
func handleBarcodeResults(_ results: [Any]) {
for result in results {
// Cast the result to a barcode-observation

if let barcode = result as? VNBarcodeObservation, barcode.symbology == .QR {

if let payload = barcode.payloadStringValue {
DispatchQueue.main.async {
// XXX FIXME get QR Codes working again
if self.calledDelegate {
return
}
self.calledDelegate = true
self.scanDelegate?.userDidScanQrCode.map { $0(self, payload) }

}
}
}
}
}*/

@available(iOS 11.2, *)
func blockingQrModel(pixelBuffer: CVPixelBuffer) {
let semaphore = DispatchSemaphore(value: 0)
Expand Down Expand Up @@ -423,7 +398,7 @@ import Vision
self.machineLearningSemaphore.signal()
return
}

guard let fullCardImage = self.toCGImage(pixelBuffer: pixelBuffer) else {
print("could not get the cgImage from the pixel buffer")
self.machineLearningSemaphore.signal()
Expand All @@ -438,13 +413,13 @@ import Vision

// we allow apps that integrate to supply their own sequence of images
// for use in testing
//let image = self.testingImageDataSource?.nextImage() ?? squareCardImage
let squareAndFullCardImage = self.testingImageDataSource?.nextSquareAndFullImage() ?? (squareCardImage, fullCardImage)

if #available(iOS 11.2, *) {
if self.scanQrCode {
self.blockingQrModel(pixelBuffer: pixelBuffer)
} else {
self.blockingOcrModel(squareCardImage: squareCardImage, fullCardImage: fullCardImage)
self.blockingOcrModel(squareCardImage: squareAndFullCardImage.0, fullCardImage: squareAndFullCardImage.1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

naming individual tuple elements here doesn't seem great people would need to know that .0 is square and .1 is full. For squareAndFullCardImage why not have variables directly, something like let (square, full) = self.testingImageDataSource...

}
}

Expand Down Expand Up @@ -521,7 +496,6 @@ import Vision
let rect = CGRect(x: cx - width / 2.0, y: cy - height / 2.0, width: width, height: height)

self.currentImageRect = rect
jaimejiyepark marked this conversation as resolved.
Show resolved Hide resolved

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove whitespace changes from here and the few other places that they show up?

return image.cropping(to: rect)
}
}
40 changes: 21 additions & 19 deletions Example/CardScan/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import UIKit
import CardScan

class ViewController: UIViewController, ScanEvents, ScanDelegate, FullScanStringsDataSource, TestingImageDataSource {

let testImages = [UIImage(imageLiteralResourceName: "frame0"),
UIImage(imageLiteralResourceName: "frame19"),
UIImage(imageLiteralResourceName: "frame38"),
Expand All @@ -24,10 +24,25 @@ class ViewController: UIViewController, ScanEvents, ScanDelegate, FullScanString

var currentTestImages: [CGImage]?

func nextImage() -> CGImage? {
let nextImage = self.currentTestImages?.first
func nextSquareAndFullImage() -> (CGImage, CGImage)? {
guard let fullCardImage = self.currentTestImages?.first else {
print("could not get full size test image")
return nil
}

let squareCropImage = fullCardImage
let width = CGFloat(squareCropImage.width)
let height = width
let x = CGFloat(0)
let y = CGFloat(squareCropImage.height) * 0.5 - height * 0.5

guard let squareCardImage = squareCropImage.cropping(to: CGRect(x: x, y: y, width: width, height: height)) else {
print("could not crop test image")
return nil
}

self.currentTestImages = self.currentTestImages?.dropFirst().map { $0 }
return nextImage
return (squareCardImage, fullCardImage)
}

func scanCard() -> String { return "New Scan Card" }
Expand Down Expand Up @@ -165,26 +180,13 @@ class ViewController: UIViewController, ScanEvents, ScanDelegate, FullScanString
print("scan view controller not supported on this hardware")
return
}

let cgImages = self.testImages.compactMap { $0.cgImage }

// pull the images from the center, use the full width but keep the
// aspect ratio consistent with what the model is expecting
self.currentTestImages = cgImages.compactMap { image in
let width = CGFloat(image.width)
let height = 302.0 * width / 480.0
let x = CGFloat(0)
let y = CGFloat(image.height) * 0.5 - height * 0.5

return image.cropping(to: CGRect(x: x, y: y, width: width, height: height))
}

self.currentTestImages = self.testImages.compactMap { $0.cgImage }
vc.testingImageDataSource = self
vc.showDebugImageView = true
self.present(vc, animated: true)
}

func onNumberRecognized(number: String, expiry: Expiry?, numberBoundingBox: CGRect, expiryBoundingBox: CGRect?, squareCardImage: CGImage, fullCardImage: CGImage) {
func onNumberRecognized(number: String, expiry: Expiry?, numberBoundingBox: CGRect, expiryBoundingBox: CGRect?, croppedCardSize: CGSize, squareCardImage: CGImage, fullCardImage: CGImage) {
print("number recognized")
}

Expand Down
1 change: 0 additions & 1 deletion Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.