在Swift中重塑脸部坐标

时间:2018-12-31 11:12:59

标签: swift image-processing face-detection vision ciimage

我想像视频中所示重塑脸部坐标:https://www.dropbox.com/s/vsttylwgt25szha/IMG_6590.TRIM.MOV?dl=0(很抱歉,视频的大小约为11 MB)。

我刚刚使用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()
}

如何从这里前进?谁能指导我?

0 个答案:

没有答案