gpt4 book ai didi

ios - 如何判断图片中是否没有文字--MLKit VisionCloudTextRecognizer

转载 作者:搜寻专家 更新时间:2023-11-01 06:51:47 28 4
gpt4 key购买 nike

我有一个名为 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com