2017-09-07 79 views
1

中检测我试图在我的相机预览图层上的实时预览中获取检测区域。只在相机预览图层,iOS,SWIFT

有没有可能这样做,比如有一个活体饲料,并且你有面部检测,并且当你环顾它时,它只会在一个特定区域的脸部周围放置一个盒子,例如屏幕中央的一个矩形。预览中矩形外部的所有其他面不会被检测到?

即时通讯使用Vision,iOS,Swift。

+0

你提出的任何事情,我现在想做的一样? – djay

+0

我发现的最好办法是在检测区域放置警卫。因此,如果xCord yCord字母高亮功能中文本的宽度和高度创建一个cgrect,然后为您感兴趣的文本创建另一个cgrect,那么您只希望看到它们。然后使用guard secondcgrect.contains(1stcgrect.origin)else {return}在看到caLayer之前放置它,这只会在感兴趣的文本中围绕te物体展开轮廓。https://medium.com/@tonymerritt/hi-peng -27ff127cd659?source = linkShare-8fa3bb4b8b91-1511567697我在这里也发布了评论 –

+1

@djay我也发布了我的答案,以便您可以正确地看到它 –

回答

0

我想通了这一点通过的CALayer添加

之前添加保护之前查看所做负载

@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)

我希望这可以帮助别人。