作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个名为 MLVision 的类来处理文本识别。
class MLVision {
lazy var vision = Vision.vision()
var textRecognizer: VisionTextRecognizer!
var textArray = [] as Array<String>
var lineText :String = ""
func startRecognising(image : UIImage,completion: @escaping (_ result: [String]) -> ()){
let visionImage = VisionImage(image: image)
let options = VisionCloudTextRecognizerOptions()
options.languageHints = ["en","hi"]
textRecognizer = vision.cloudTextRecognizer(options: options)
textRecognizer.process(visionImage, completion: { (features, error) in
guard error == nil, let features = features else {
print("No text Found!!")
return
}
for block in features.blocks {
for line in block.lines{
self.lineText = line.text
self.separate(lineText: self.lineText)
}
}
completion(self.textArray)
})
}
func separate(lineText : String){
let separators = CharacterSet(charactersIn: (":)(,•/·]["))
let ofWordsArray = lineText.components(separatedBy: separators)
for word in ofWordsArray{
let low = word.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
if low != ""{
textArray.append(low)
}
}
}
}
这就是我称之为 Mlvision 类的 ViewController。
let mlVision = MLVision()
override func viewDidLoad() {
super.viewDidLoad()
mlVision.startRecognising(image: croppedImage!){ result in
self.textArray = result
self.searchIngredients(textArray: self.textArray)
}
}
所以基本上我的应用程序识别文本然后将其分开并将其传递到数组中。
如果我检查没有文本的图像以显示警报,我想处理。我知道guard error == nil, let features = features else {return}
处理它,但我想从我的 ViewController 中检查它。有办法吗?
最佳答案
如果我答对了你的问题 - 你只想处理你开始它的范围内的识别错误,如果是这样你可以使用 Result 修改你的代码到下面.
// add your own Errors
enum RecognitionError: Error {
case textNotFound
}
//modify `completion` closure by passing `Result` object instead of array
func startRecognising(image : UIImage,
completion: @escaping (_ result: Result<[String], RecognitionError>) -> ()){
let visionImage = VisionImage(image: image)
let options = VisionCloudTextRecognizerOptions()
options.languageHints = ["en","hi"]
textRecognizer = vision.cloudTextRecognizer(options: options)
textRecognizer.process(visionImage, completion: { (features, error) in
guard error == nil, let features = features else {
print("No text Found!!")
completion(.failure(.textNotFound))
return
}
for block in features.blocks {
for line in block.lines{
self.lineText = line.text
self.separate(lineText: self.lineText)
}
}
completion(.success(self.textArray))
})
}
并在您的 ViewController 中处理它:
override func viewDidLoad() {
super.viewDidLoad()
mlVision.startRecognising(image: croppedImage!){ result in
switch result {
case .success(let array):
self.textArray = result
self.searchIngredients(textArray: self.textArray)
case .failure(let error):
print(error)
}
}
}
关于ios - 如何判断图片中是否没有文字--MLKit VisionCloudTextRecognizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56735886/
我有一个名为 MLVision 的类来处理文本识别。 class MLVision { lazy var vision = Vision.vision() var textRecogn
我是一名优秀的程序员,十分优秀!