Apple Vision framework – Text extraction from image

后端 未结 3 1776
天命终不由人
天命终不由人 2021-02-02 11:13

I am using Vision framework for iOS 11 to detect text on image.

The texts are getting detected successfully, but how we can get the detected text?

3条回答
  •  闹比i
    闹比i (楼主)
    2021-02-02 11:46

    This will return a overlay image with rectangle box on detected text

    Here is the full xcode project https://github.com/cyruslok/iOS11-Vision-Framework-Demo

    Hope it is helpful

    // 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)
            }
        })
        request.reportCharacterBoxes = true
        do {
            try handler.perform([request])
            return result_img;
        } catch {
            return result_img;
        }
    }
    
    func drawRectangleForTextDectect(image: UIImage, results:Array) -> 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
    }
    

提交回复
热议问题