Image to Text

Image to Text 

Image to Text -> OCR

Apple -> Vision

Documentation

https://developer.apple.com/documentation/vision/detecting_objects_in_still_images

Closures (Lamda in Kotlin)

// Define a closure that takes no parameters and returns void
let simpleClosure: () -> Void = {
    print("This is a simple closure")
}

// Call the closure
simpleClosure()
// Define a closure that takes two Int parameters and returns their sum
let addClosure: (Int, Int) -> Int = { (a, b) in
    return a + b
}

// Call the closure
let sum = addClosure(3, 5)
print("The sum is \(sum)")

Closures in Swift are self-contained blocks of functionality that can be passed around

Closures

let numbers = [1, 2, 3, 4, 5]
let doubled = numbers.map { $0 * 2 }
print(doubled) // Output: [2, 4, 6, 8, 10]
let numbers = [1, 2, 3, 4, 5]
let doubled = numbers.map { number in
    return number * 2
}
print(doubled) // Output: [2, 4, 6, 8, 10]

Closures in Swift are self-contained blocks of functionality that can be passed aroun

CompletionHandler

// Define a function that simulates fetching data asynchronously
func fetchData(completion: @escaping ([String]) -> Void) {
    // Simulate asynchronous task, like fetching data from a server
    DispatchQueue.global().async {
        // Simulate some data
        let data = ["Apple", "Banana", "Orange", "Grapes"]
        
        // Call the completion handler with the fetched data
        completion(data)
    }
}

// Call the function and provide a closure as a completion handler
fetchData { fetchedData in
    // Process the fetched data
    print("Fetched data:", fetchedData)
}

UI

    var body: some View {
        VStack {
            if let image = image {
                Image(uiImage: image)
                    .resizable()
                    .scaledToFit()
            } else {
                Image(systemName: "globe")
                    .imageScale(.large)
                    .foregroundStyle(.tint)
            }
            Text(recognizedText.isEmpty ? "Recognized Text Will Appear Here" : recognizedText)
                .padding()
            
            Button("Select Image") {
                self.image = UIImage(named: "test")
                if let selectedImage = self.image {
                    self.processImage(image: selectedImage)
                }
            }
        }
        .padding()
    }
    

}

processImage

    private func processImage(image: UIImage) {
        guard let cgImage = image.cgImage else {
            print("Could not get cgimage")
            return
        }
        
        // Handle
        let imageRequestHandler = VNImageRequestHandler(cgImage: cgImage)
        
        // Request
        let request = VNRecognizeTextRequest { request, error in
            guard let observations = request.results as? [VNRecognizedTextObservation], error == nil else {
                print("Error recognizing text: \(error?.localizedDescription ?? "Unknown error")")
                return
            }
            
            let recognizedText = observations.compactMap {
                $0.topCandidates(1).first?.string
            }.joined(separator: "\n")
            
            DispatchQueue.main.async {
                self.recognizedText = recognizedText
            }
        }
        
        // Process request
        do {
            try imageRequestHandler.perform([request])
        } catch {
            print("Error performing image request: \(error)")
        }
    }

HW

https://github.com/iharnoor/iOSWeek5--ImageToText

https://developer.apple.com/tutorials/sample-apps/capturingphotos-captureandsave

Integrate camera and pass uiImage into processImage

Image to TextCode:

By Harnoor Singh

Image to TextCode:

  • 221