如何判断图片中是否没有文字--MLKit VisionCloudTextRecognizer
How to check if there is no text in an Image--MLKit VisionCloudTextRecognizer
我有一个名为 MLVision 的 class 来处理文本识别。
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)
}
}
}
}
这就是 ViewController 我称之为 Mlvision class。
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)
}
}
}
我有一个名为 MLVision 的 class 来处理文本识别。
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)
}
}
}
}
这就是 ViewController 我称之为 Mlvision class。
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)
}
}
}