gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:08:12 26 4
gpt4 key购买 nike

我使用 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)
}

最佳答案

我通过添加来解决这个问题。

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com