我试图在我的相机预览层的实时预览中获得一个检测区域。
是否有可能,假设有一个实时提要并且您启用了面部检测,并且当您环顾四周时,它只会在特定区域的面部周围放置一个框,例如屏幕中央的一个矩形。预览中矩形之外的所有其他人脸都没有被检测到?
我正在使用 Vision、iOS、Swift。
我试图在我的相机预览层的实时预览中获得一个检测区域。
是否有可能,假设有一个实时提要并且您启用了面部检测,并且当您环顾四周时,它只会在特定区域的面部周围放置一个框,例如屏幕中央的一个矩形。预览中矩形之外的所有其他人脸都没有被检测到?
我正在使用 Vision、iOS、Swift。
我通过在 CALayer 添加之前添加一个守卫来解决这个问题
在 View 加载之前
@IBOutlet weak var scanAreaImage: UIImageView!
var regionOfInterest: CGRect!
在 View 中确实加载了 scanAreaImage.frame 是我通过情节提要放入的图像视图,这将代表我只想检测的区域,
let someRect: CGRect = scanAreaImage.frame
regionOfInterest = someRect
然后在视觉文本检测部分。
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)!
// This is the section I Added for the rec of interest detection zone.
//////////////////////////////////////////////
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
// regionOfInterest being the cgRect you created earlier
//////////////////////////////////////////////
let outline = CALayer()
outline.frame = CGRect(x: xCord, y: yCord, width: width, height: height)
outline.borderWidth = 1.0
if textColour == 1 {
outline.borderColor = UIColor.blue.cgColor
}else {
outline.borderColor = UIColor.clear.cgColor
}
cameraPreviewlayer?.addSublayer(outline)
这只会显示您在情节提要中创建的矩形内的事物的轮廓。(我的是scanAreaImage)
我希望这可以帮助别人。