我想要使用Vision框架中的VNDetectTextRectanglesRequest
,来检测只包含一个字符、数字“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”会被检测为一个具有文本的区域。对于两位数的数字,“22”在白色背景上也无法被检测到。
为什么视觉API在我的情况下只能检测到白色背景上的3位以上数字?