• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 检测带有矩形的文本而不是整个屏幕的视觉,iOS,Swift

[复制链接]
菜鸟教程小白 发表于 2022-12-11 18:37:27 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我使用 Vision 进行文本检测,但这会在整个屏幕上显示检测,有没有办法让它只检测特定区域,例如我在屏幕中间绘制的矩形。

所以当整个屏幕是一个摄像头时,如果文本在中心进入一个矩形,它会在其周围绘制框。

Photo of how I want it

下面是我的文本检测代码和 cameraPreviewLayer

如果可能的话,我是否需要 2 个相机预览层?

func setupPreviewLayer() {
    cameraPreviewlayer = AVCaptureVideoPreviewLayer(session: captureSession)
    cameraPreviewlayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
    cameraPreviewlayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
    cameraPreviewlayer?.frame = self.view.frame
    self.view.layer.insertSublayer(cameraPreviewlayer!, at: 0)
}

func startTextDetection() {
    let textRequest = VNDetectTextRectanglesRequest(completionHandler: self.detectTextHandler)
    textRequest.reportCharacterBoxes = true
    self.requests = [textRequest]
}

func detectTextHandler(request: VNRequest, error: Error?) {
    guard let observations = request.results else {
        print("no result")
        return
    }

    let result = observations.map({$0 as? VNTextObservation})

    DispatchQueue.main.async() {
        self.cameraPreviewlayer?.sublayers?.removeSubrange(1...)
        for region in result {
            guard let rg = region else {
                continue
            }

            self.highlightWord(box: rg)

            if let boxes = region?.characterBoxes {
                for characterBox in boxes {
                    self.highlightLetters(box: characterBox)
                }
            }
        }
    }
}

func highlightWord(box: VNTextObservation) {
    guard let boxes = box.characterBoxes else {
        return
    }

    var maxX: CGFloat = 9999.0
    var minX: CGFloat = 0.0
    var maxY: CGFloat = 9999.0
    var minY: CGFloat = 0.0

    for char in boxes {
        if char.bottomLeft.x < maxX {
            maxX = char.bottomLeft.x
        }
        if char.bottomRight.x > minX {
            minX = char.bottomRight.x
        }
        if char.bottomRight.y < maxY {
            maxY = char.bottomRight.y
        }
        if char.topRight.y > minY {
            minY = char.topRight.y
        }
    }

    let xCord = maxX * (cameraPreviewlayer?.frame.size.width)!
    let yCord = (1 - minY) * (cameraPreviewlayer?.frame.size.height)!
    let width = (minX - maxX) * (cameraPreviewlayer?.frame.size.width)!
    let height = (minY - maxY) * (cameraPreviewlayer?.frame.size.height)!

    let outline = CALayer()
    outline.frame = CGRect(x: xCord, y: yCord, width: width, height: height)
    outline.borderWidth = 2.0
    outline.borderColor = UIColor.red.cgColor

    cameraPreviewlayer?.addSublayer(outline)
}

func highlightLetters(box: VNRectangleObservation) {
    let xCord = box.topLeft.x * (cameraPreviewlayer?.frame.size.width)!
    let yCord = (1 - box.topLeft.y) * (cameraPreviewlayer?.frame.size.height)!
    let width = (box.topRight.x - box.bottomLeft.x) * (cameraPreviewlayer?.frame.size.width)!
    let height = (box.topLeft.y - box.bottomLeft.y) * (cameraPreviewlayer?.frame.size.height)!

    let outline = CALayer()
    outline.frame = CGRect(x: xCord, y: yCord, width: width, height: height)
    outline.borderWidth = 1.0
    outline.borderColor = UIColor.blue.cgColor

    cameraPreviewlayer?.addSublayer(outline)
}



Best Answer-推荐答案


我通过添加发现了这一点。

let wordRect = CGRect(x: xCord, y: yCord, width: width, height: height)
    guard regionOfInterest.contains(wordRect.origin) else { return } // only draw a box if the orgin of the word box is within the regionOfInterest

之前

 let outline = CALayer()
outline.frame = CGRect(x: xCord, y: yCord, width: width, height: height)
outline.borderWidth = 1.0
outline.borderColor = UIColor.blue.cgColor

cameraPreviewlayer?.addSublayer(outline)

这样它只会运行矩形中单词的代码 我将单词 rect 设置为我只想在其中显示文本的 uiview 大小的框架。

关于ios - 检测带有矩形的文本而不是整个屏幕的视觉,iOS,Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46044381/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap