-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextRecognitionService.swift
58 lines (46 loc) · 2 KB
/
TextRecognitionService.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// TextRecognitionService.swift
// IntelliNote
//
// Created by Arminas Ruzgas on 2021-01-22.
//
import Vision
protocol TextRecognitionServiceDelegate: class {
func didRecognizeTextFromImage(_ text: String)
func didFailRecognizeTextFromImage()
}
class TextRecognitionService {
weak var delegate: TextRecognitionServiceDelegate?
func recognizeText(in cgImage: CGImage) {
// 1. Sending the request off the main thread
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
guard let self = self else { return }
//2. Creating object that processes image analysis requests
let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])
do {
//3. Performing the previously created request
try handler.perform([self.textRecognitionRequest])
} catch {
self.delegate?.didFailRecognizeTextFromImage()
}
}
}
}
extension TextRecognitionService {
private var textRecognitionRequest: VNRecognizeTextRequest {
// 1. Creating recognize request
return VNRecognizeTextRequest(completionHandler: { [weak self] (request, error) in
if let recognizedText = request.results as? [VNRecognizedTextObservation] {
// 2. If request provides a result - take the top candidate from the returned Strings.
let transcript = recognizedText.reduce("") { result, observation in
guard let candidate = observation.topCandidates(1).first?.string else { return "" }
return result.appending(candidate) + "\n"
}
DispatchQueue.main.async { [weak self] in
// 3. Inform the delegate about successfull text recognitioon
self?.delegate?.didRecognizeTextFromImage(transcript)
}
}
})
}
}