Priyan Rai
Priyan Rai

Reputation: 19

Text Recognition in Images in Swift

Im new to Swift and I was trying to make an app that can parse the from on a screenshot. I have the following code so far, and I wasn't able to figure out a proper way to call the recognize function in my ContentView:

struct ContentView: View {
    @State var selectedItems: PhotosPickerItem?
    @State var selectedPhotoData: Data?
    
    func recogText(selData: Data?)
    {
        if let selData = selData, let image = UIImage(data: selData){
            guard let cgImage = image.cgImage else {return}
            let handler = VNImageRequestHandler(cgImage: cgImage)
            let request = VNDetectTextRectanglesRequest { request, error in
                guard let observations = request.results as? [VNRecognizedTextObservation],
                      error == nil else {return}
                let text = observations.compactMap({
                    $0.topCandidates(1).first?.string
                    }).joined(separator: ", ")
                print(text.count)
            }
            
            do {
                try handler.perform([request])
            }
            catch {
                print("Unable to perform the requests: \(error).")
            }
        }
    }
    
    var body: some View {
        VStack{
            
            //Icon
            mainImage()
            
            //Button
            PhotosPicker(selection: $selectedItems, matching: .images) {
                Label("Select a photo", systemImage: "photo")
            }
            .tint(.blue)
            .controlSize(.large)
            .buttonStyle(.borderedProminent)
            .onChange(of: selectedItems) { newItem in
                Task {
                    if let data = try? await newItem?.loadTransferable(type: Data.self) {
                        selectedPhotoData = data
                        let _ = recogText(selData: data)
                    }
                }
            }
        }
    }
}

Expected a print of the parsed text but no output was found.

Upvotes: 0

Views: 996

Answers (1)

Engeland
Engeland

Reputation: 41

Hello here is an example function that might help you. Of course you have to replace the TestImage with yours. This might work for you and you need to import Vision

func recogText() {
    let textRecognitionRequest = VNRecognizeTextRequest { (request, error) in
        // Insert code to process the text recognition results here
        guard let observations = request.results as? [VNRecognizedTextObservation] else { return }

        for observation in observations {
            let topCandidate = observation.topCandidates(1).first
            if let recognizedText = topCandidate?.string {
                print(recognizedText)
            }
        }
    }
    
    textRecognitionRequest.recognitionLevel = .accurate
    
    let image = UIImage(named: "TestImage")
    let imageRequestHandler = VNImageRequestHandler(cgImage: (image?.cgImage!)!, options: [:])

    do {
        try imageRequestHandler.perform([textRecognitionRequest])
    } catch {
        print(error)
    }
}

Upvotes: 1

Related Questions