Reshape Face Coordinate in Swift
Asked Answered
C

0

7

I want to reshape the face coordinate like showing in the video: https://www.dropbox.com/s/vsttylwgt25szha/IMG_6590.TRIM.MOV?dl=0 (Sorry, unfortunetly the video is about 11 MB in size).

I've just capture the face coordinate using iOS Vision API:

// Facial landmarks are GREEN.
fileprivate func drawFeatures(onFaces faces: [VNFaceObservation], onImageWithBounds bounds: CGRect) {
    CATransaction.begin()
    for faceObservation in faces {

        let faceBounds = boundingBox(forRegionOfInterest: faceObservation.boundingBox, withinImageBounds: bounds)

        guard let landmarks = faceObservation.landmarks else {
            continue
        }

        // Iterate through landmarks detected on the current face.
        let landmarkLayer = CAShapeLayer()
        let landmarkPath = CGMutablePath()
        let affineTransform = CGAffineTransform(scaleX: faceBounds.size.width, y: faceBounds.size.height)

        // Treat eyebrows and lines as open-ended regions when drawing paths.
        let openLandmarkRegions: [VNFaceLandmarkRegion2D?] = [
            //landmarks.leftEyebrow,
            //landmarks.rightEyebrow,
            landmarks.faceContour,
            landmarks.noseCrest,
          //  landmarks.medianLine
        ]

        // Draw eyes, lips, and nose as closed regions.
        let closedLandmarkRegions = [
            landmarks.nose
            ].compactMap { $0 } // Filter out missing regions.

        // Draw paths for the open regions.
        for openLandmarkRegion in openLandmarkRegions where openLandmarkRegion != nil {
            landmarkPath.addPoints(in: openLandmarkRegion!,
                                   applying: affineTransform,
                                   closingWhenComplete: false)
        }

        // Draw paths for the closed regions.
        for closedLandmarkRegion in closedLandmarkRegions {
            landmarkPath.addPoints(in: closedLandmarkRegion ,
                                   applying: affineTransform,
                                   closingWhenComplete: true)
        }

        // Format the path's appearance: color, thickness, shadow.
        landmarkLayer.path = landmarkPath
        landmarkLayer.lineWidth = 1
        landmarkLayer.strokeColor = UIColor.green.cgColor

        landmarkLayer.fillColor = nil

        landmarkLayer.shadowOpacity = 1.0
        landmarkLayer.shadowRadius = 1

        // Locate the path in the parent coordinate system.
        landmarkLayer.anchorPoint = .zero
        landmarkLayer.frame = faceBounds
        landmarkLayer.transform = CATransform3DMakeScale(1, -1, 1)

        pathLayer?.addSublayer(landmarkLayer)
    }

    CATransaction.commit()
}

How to step forward from here? Can anyone guide me please?

Conference answered 31/12, 2018 at 11:12 Comment(4)
Check with : #45299139Strongarm
I'm at work and don't have access to DropBox. Can you describe the video in words?Westleigh
Yes sure. What I need to achieve is to detect all the points of the face (Which I already doing) and then reshape the face to thin or fate as per the user scroll the bar. Any help is appreciate.Conference
@Sandeep-Systematix did you figure out how to do it?Easting

© 2022 - 2024 — McMap. All rights reserved.