Created
January 10, 2020 13:39
-
-
Save anupamchugh/48a4ad7e2b7eada656b6584eba026355 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ContentView : View { | |
let pkCanvas = PKCanvasRepresentation() | |
@State var digitPredicted = "NA" | |
private let textRecognitionWorkQueue = DispatchQueue(label: "VisionRequest", qos: .userInitiated, attributes: [], autoreleaseFrequency: .workItem) | |
var body: some View { | |
VStack{ | |
ARViewContainer(overlayText: $digitPredicted).edgesIgnoringSafeArea(.all) | |
pkCanvas | |
HStack{ | |
Button(action: { | |
let image = self.pkCanvas.canvasView.drawing.image(from: self.pkCanvas.canvasView.drawing.bounds, scale: 1.0) | |
self.recognizeTextInImage(image) | |
self.pkCanvas.canvasView.drawing = PKDrawing() | |
}){ | |
Text("Extract Digit") | |
}.buttonStyle(MyButtonStyle(color: .blue)) | |
Text(digitPredicted) | |
} | |
} | |
} | |
private func recognizeTextInImage(_ image: UIImage) { | |
guard let cgImage = image.cgImage else { return } | |
let model = try! VNCoreMLModel(for: MNISTClassifier().model) | |
let request = VNCoreMLRequest(model: model) | |
request.imageCropAndScaleOption = .scaleFit | |
textRecognitionWorkQueue.async { | |
let requestHandler = VNImageRequestHandler(cgImage: cgImage, options: [:]) | |
do { | |
try requestHandler.perform([request]) | |
if let observations = request.results as? [VNClassificationObservation]{ | |
self.digitPredicted = observations.first?.identifier ?? "" | |
} | |
} catch { | |
print(error) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment