Skip to content

Commit

Permalink
mod testing image protocol/ add squared image (#102)
Browse files Browse the repository at this point in the history
* mod testing image protocol/ add squared image

* adapt to iOS 9

* mod testing image protocol/ add squared image

* [RFC] added extension

* nextSquareAndFullCardImage protocol

* add objc back to protocol

* hopefully the last commit

* rid of all whitespace i hope

* missed one whitespace
  • Loading branch information
jaimejiyepark authored Dec 4, 2019
1 parent 19a8200 commit f1815dc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 46 deletions.
34 changes: 6 additions & 28 deletions CardScan/Classes/ScanBaseViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ 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 @@ -18,7 +18,7 @@ import Vision
}


@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 +273,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 @@ -424,6 +401,7 @@ import Vision
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 +416,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 (squareImage, fullImage) = 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: squareImage, fullCardImage: fullImage)
}
}

Expand Down
37 changes: 20 additions & 17 deletions Example/CardScan/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -166,25 +181,13 @@ class ViewController: UIViewController, ScanEvents, ScanDelegate, FullScanString
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.

0 comments on commit f1815dc

Please sign in to comment.