Skip to content

Instantly share code, notes, and snippets.

@anupamchugh
Created January 10, 2020 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anupamchugh/48a4ad7e2b7eada656b6584eba026355 to your computer and use it in GitHub Desktop.
Save anupamchugh/48a4ad7e2b7eada656b6584eba026355 to your computer and use it in GitHub Desktop.
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