我正在使用iOS 11的视觉框架来检测图像上的文本。
文本正在被成功检测,但是我们如何获得检测到的文本呢?
发布于 2020-03-15 20:01:10
在Apple Vision中,您可以使用VNRecognizeTextRequest类轻松地从图像中提取文本,允许您进行图像分析请求,以查找和识别图像中的文本。
VNRecognizeTextRequest 从iOS 13.0和macOS 10.15开始工作。
下面是一个代码片段,向你展示了如何做到这一点:
let requestHandler = VNImageRequestHandler(url: imageURL, options: [:])
let request = VNRecognizeTextRequest { (request, error) in
guard let observations = request.results as? [VNRecognizedTextObservation]
else { return }
for observation in observations {
let topCandidate: [VNRecognizedText] = observation.topCandidates(1)
if let recognizedText: VNRecognizedText = topCandidate.first {
label.text = recognizedText.string
}
}
}然后,您必须为recognitionLevel实例属性赋值:
// non-realtime asynchronous but accurate text recognition
request.recognitionLevel = VNRequestTextRecognitionLevel.accurate
// nearly realtime but not-accurate text recognition
request.recognitionLevel = VNRequestTextRecognitionLevel.fast
try? requestHandler.perform([request])发布于 2017-06-19 12:29:26
不完全是一个副本,但类似于:Converting a Vision VNTextObservation to a String
您需要使用CoreML或其他库来执行OCR (SwiftOCR等)。
发布于 2017-06-16 10:50:26
这将在检测到的文本上返回带有矩形框的叠加图像
下面是完整的xcode项目https://github.com/cyruslok/iOS11-Vision-Framework-Demo
希望能对大家有所帮助
// Text Detect
func textDetect(dectect_image:UIImage, display_image_view:UIImageView)->UIImage{
let handler:VNImageRequestHandler = VNImageRequestHandler.init(cgImage: (dectect_image.cgImage)!)
var result_img:UIImage = UIImage.init();
let request:VNDetectTextRectanglesRequest = VNDetectTextRectanglesRequest.init(completionHandler: { (request, error) in
if( (error) != nil){
print("Got Error In Run Text Dectect Request");
}else{
result_img = self.drawRectangleForTextDectect(image: dectect_image,results: request.results as! Array<VNTextObservation>)
}
})
request.reportCharacterBoxes = true
do {
try handler.perform([request])
return result_img;
} catch {
return result_img;
}
}
func drawRectangleForTextDectect(image: UIImage, results:Array<VNTextObservation>) -> UIImage {
let renderer = UIGraphicsImageRenderer(size: image.size)
var t:CGAffineTransform = CGAffineTransform.identity;
t = t.scaledBy( x: image.size.width, y: -image.size.height);
t = t.translatedBy(x: 0, y: -1 );
let img = renderer.image { ctx in
for item in results {
let TextObservation:VNTextObservation = item
ctx.cgContext.setFillColor(UIColor.clear.cgColor)
ctx.cgContext.setStrokeColor(UIColor.green.cgColor)
ctx.cgContext.setLineWidth(1)
ctx.cgContext.addRect(item.boundingBox.applying(t))
ctx.cgContext.drawPath(using: .fillStroke)
for item_2 in TextObservation.characterBoxes!{
let RectangleObservation:VNRectangleObservation = item_2
ctx.cgContext.setFillColor(UIColor.clear.cgColor)
ctx.cgContext.setStrokeColor(UIColor.red.cgColor)
ctx.cgContext.setLineWidth(1)
ctx.cgContext.addRect(RectangleObservation.boundingBox.applying(t))
ctx.cgContext.drawPath(using: .fillStroke)
}
}
}
return img
}https://stackoverflow.com/questions/44566291
复制相似问题