Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Face landmark in smaller AVCaptureVideoPreviewLayer #3

Closed
davnag opened this issue Jul 10, 2018 · 2 comments
Closed

Face landmark in smaller AVCaptureVideoPreviewLayer #3

davnag opened this issue Jul 10, 2018 · 2 comments

Comments

@davnag
Copy link

davnag commented Jul 10, 2018

Hi, thank you for a great example!

I'm trying to do this in a smaller view but can´t figure out how to calculate the new Face landmark frames.

Say that I have a smaller AVCaptureVideoPreviewLayer say in a CGRect(x: 0, y: 44, width: 320, height: 300). I get face landmarks like this.

Do you have any idé how I can accomplish this?

img_6078e6f6106f-1

@Willjay90
Copy link
Owner

The VNFaceObservation returns a normalized bounding box. You have to do the math for translation according to size of your layer. For example, I just use the frame.width and frame.height of my Preview.

    func drawFaceWithLandmarks(face: VNFaceObservation) {
    
        let transform = CGAffineTransform(scaleX: 1, y: -1).translatedBy(x: 0, y: -frame.height)
    
        let translate = CGAffineTransform.identity.scaledBy(x: frame.width, y: frame.height)
    
        // The coordinates are normalized to the dimensions of the processed image, with the origin at the image's lower-left corner.
        let facebounds = face.boundingBox.applying(translate).applying(transform)
       ...

@davnag
Copy link
Author

davnag commented Jul 14, 2018

I'm still having problem with this and I think its because the videoGravity property I have to change on AVCaptureVideoPreviewLayer.

I made a smaller preview view and changed .videoGravity on videoPreviewLayer
image

var videoPreviewLayer: AVCaptureVideoPreviewLayer {
   let videoLayer = layer as! AVCaptureVideoPreviewLayer
   videoLayer.videoGravity = .resizeAspectFill
   return videoLayer
}

If you have the time could you please check the changes I made on this fork.
https://github.com/davnag/AppleFaceDetection

@davnag davnag closed this as completed Jul 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants