我使用 Vision 进行文本检测,但这会在整个屏幕上显示检测,有没有办法让它只检测特定区域,例如我在屏幕中间绘制的矩形。
所以当整个屏幕是一个摄像头时,如果文本在中心进入一个矩形,它会在其周围绘制框。
下面是我的文本检测代码和 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-推荐答案 strong>
我通过添加发现了这一点。
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/
|