简体   繁体   中英

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 wasnt able to figure out proper way to call the recognize function in my ContentView, any help is appreciated: `

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

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)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM