我想使用VNDetectTextRectanglesRequest
Vision 框架来检测图像中的区域,该图像仅包含一个字符,数字“9”,背景为白色。我正在使用以下代码来执行此操作:
private func performTextDetection() {
let textRequest = VNDetectTextRectanglesRequest(completionHandler: self.detectTextHandler)
textRequest.reportCharacterBoxes = true
textRequest.preferBackgroundProcessing = false
let handler = VNImageRequestHandler(cgImage: loadedImage.cgImage!, options: [:])
DispatchQueue.global(qos: .userInteractive).async {
do {
try handler.perform([textRequest])
} catch {
print ("Error")
}
}
}
func detectTextHandler(request: VNRequest, error: Error?) {
guard let observations = request.results, !observations.isEmpty else {
fatalError("no results")
}
print("there is result")
}
我得到的观察结果数为 0,但是如果我在黑色背景上提供带有文本“123”的图像,则“123”被检测为带有文本的区域。所描述的问题也会出现在 2 位数字上,白色背景上的“22”也不会被检测到。
在我的情况下,为什么 Vision API 只能检测到白色背景上的 3 位以上数字?